Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- lock file now contains the relative path to the description in case of local path #3582

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed missing imports for method parameters that are query parameters.
- Replaces the use of "new" by "override" and "virtual" in CSharp models.
- Fixed query parameters type mapping for arrays. [#3354](https://github.com/microsoft/kiota/issues/3354)
- The lock file now contains the relative path to the description in case of local path. [#3151](https://github.com/microsoft/kiota/issues/3151)
- Fixed bug where base64url and decimal types would not be generated properly in Java.
- Fixed bug where symbol name cleanup would not work on forward single quotes characters [#3426](https://github.com/microsoft/kiota/issues/3426).
- Fixed a bug where a "models" API path segment in the description would derail generation. [#3400](https://github.com/microsoft/kiota/issues/3400)
Expand Down
22 changes: 18 additions & 4 deletions src/Kiota.Builder/Lock/LockManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ public IEnumerable<string> GetDirectoriesContainingLockFile(string searchDirecto
}
private static async Task<KiotaLock?> GetLockFromDirectoryInternalAsync(string directoryPath, CancellationToken cancellationToken)
{
var lockFile = Path.Combine(directoryPath, LockFileName);
if (File.Exists(lockFile))
var lockFilePath = Path.Combine(directoryPath, LockFileName);
if (File.Exists(lockFilePath))
{
#pragma warning disable CA2007
await using var fileStream = File.OpenRead(lockFile);
await using var fileStream = File.OpenRead(lockFilePath);
#pragma warning restore CA2007
return await GetLockFromStreamInternalAsync(fileStream, cancellationToken).ConfigureAwait(false);
var result = await GetLockFromStreamInternalAsync(fileStream, cancellationToken).ConfigureAwait(false);
if (result is not null && IsDescriptionLocal(result.DescriptionLocation) && !Path.IsPathRooted(result.DescriptionLocation))
{
result.DescriptionLocation = Path.GetFullPath(Path.Combine(directoryPath, result.DescriptionLocation));
}
return result;
}
return null;
}
Expand Down Expand Up @@ -70,8 +75,17 @@ private static async Task WriteLockFileInternalAsync(string directoryPath, Kiota
#pragma warning disable CA2007
await using var fileStream = File.Open(lockFilePath, FileMode.Create);
#pragma warning restore CA2007
lockInfo.DescriptionLocation = GetRelativeDescriptionPath(lockInfo.DescriptionLocation, lockFilePath);
await JsonSerializer.SerializeAsync(fileStream, lockInfo, context.KiotaLock, cancellationToken).ConfigureAwait(false);
}
private static bool IsDescriptionLocal(string descriptionPath) => !descriptionPath.StartsWith("http", StringComparison.OrdinalIgnoreCase);
private static string GetRelativeDescriptionPath(string descriptionPath, string lockFilePath)
{
if (IsDescriptionLocal(descriptionPath) &&
Path.GetDirectoryName(lockFilePath) is string lockFileDirectoryPath)
return Path.GetRelativePath(lockFileDirectoryPath, descriptionPath);
return descriptionPath;
}
/// <inheritdoc/>
public Task BackupLockFileAsync(string directoryPath, CancellationToken cancellationToken = default)
{
Expand Down
25 changes: 23 additions & 2 deletions tests/Kiota.Builder.Tests/Lock/LockManagementServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Kiota.Builder.Tests.Lock;
public class LockManagementServiceTests
{
[Fact]
public async Task DefensivePrograming()
public async Task DefensiveProgramming()
{
var lockManagementService = new LockManagementService();
Assert.Throws<ArgumentNullException>(() => lockManagementService.GetDirectoriesContainingLockFile(null));
Expand All @@ -22,13 +22,34 @@ public async Task DefensivePrograming()
public async Task Identity()
{
var lockManagementService = new LockManagementService();
var descriptionPath = Path.Combine(Path.GetTempPath(), "description.yml");
var lockFile = new KiotaLock
{
DescriptionLocation = "description",
ClientClassName = "foo",
ClientNamespaceName = "bar",
DescriptionLocation = descriptionPath,
};
var path = Path.GetTempPath();
await lockManagementService.WriteLockFileAsync(path, lockFile);
lockFile.DescriptionLocation = Path.GetFullPath(descriptionPath); // expected since we write the relative path but read to the full path
var result = await lockManagementService.GetLockFromDirectoryAsync(path);
Assert.Equal(lockFile, result, new KiotaLockComparer());
}
[Fact]
public async Task UsesRelativePaths()
{
var tmpPath = Path.Combine(Path.GetTempPath(), "tests", "kiota");
var lockManagementService = new LockManagementService();
var descriptionPath = Path.Combine(tmpPath, "information", "description.yml");
var descriptionDirectory = Path.GetDirectoryName(descriptionPath);
Directory.CreateDirectory(descriptionDirectory);
var lockFile = new KiotaLock
{
DescriptionLocation = descriptionPath,
};
var outputDirectory = Path.Combine(tmpPath, "output");
Directory.CreateDirectory(outputDirectory);
await lockManagementService.WriteLockFileAsync(outputDirectory, lockFile);
Assert.Equal($"..{Path.DirectorySeparatorChar}information{Path.DirectorySeparatorChar}description.yml", lockFile.DescriptionLocation, StringComparer.OrdinalIgnoreCase);
}
}
Loading