diff --git a/.gitignore b/.gitignore index 5e6d3c2d9..d12b4d991 100644 --- a/.gitignore +++ b/.gitignore @@ -286,3 +286,5 @@ install.sh # Confidential temporary files — must not reach public repository tmp/ test/Generated/GeneratedCode/*.generated.cs +test/MSBuild/Generated/**/*.generated.cs +test/MSBuild/GeneratedOutput/**/*.cs \ No newline at end of file diff --git a/src/Refitter.Tests/GenerateCommandTests.cs b/src/Refitter.Tests/GenerateCommandTests.cs index 19c463fa7..ec57bf469 100644 --- a/src/Refitter.Tests/GenerateCommandTests.cs +++ b/src/Refitter.Tests/GenerateCommandTests.cs @@ -151,7 +151,7 @@ public void Command_Should_Have_Protected_ExecuteAsync_Method() [Test] public void GetOutputPath_Should_Root_Relative_To_SettingsFile_When_OutputFolder_Is_Default() { - var settingsFilePath = @"C:\Projects\MyApi\petstore.refitter"; + var settingsFilePath = Path.Combine(Path.GetTempPath(), "Projects", "MyApi", "petstore.refitter"); var settings = new Settings { SettingsFilePath = settingsFilePath, @@ -172,13 +172,14 @@ public void GetOutputPath_Should_Root_Relative_To_SettingsFile_When_OutputFolder method.Should().NotBeNull(); var result = method!.Invoke(null, [settings, refitSettings]) as string; - result.Should().Be(@"C:\Projects\MyApi\./Generated\Output.cs"); + var expectedPath = Path.Combine(Path.GetDirectoryName(settingsFilePath)!, "./Generated", "Output.cs"); + result.Should().Be(expectedPath); } [Test] public void GetOutputPath_Should_Root_Relative_To_SettingsFile_When_OutputFolder_Is_Custom() { - var settingsFilePath = @"C:\Projects\MyApi\petstore.refitter"; + var settingsFilePath = Path.Combine(Path.GetTempPath(), "Projects", "MyApi", "petstore.refitter"); var settings = new Settings { SettingsFilePath = settingsFilePath, @@ -198,14 +199,15 @@ public void GetOutputPath_Should_Root_Relative_To_SettingsFile_When_OutputFolder var result = method!.Invoke(null, [settings, refitSettings]) as string; - result.Should().Be(@"C:\Projects\MyApi\./CustomOutput\PetStore.cs"); + var expectedPath = Path.Combine(Path.GetDirectoryName(settingsFilePath)!, "./CustomOutput", "PetStore.cs"); + result.Should().Be(expectedPath); } [Test] public void GetOutputPath_Should_Use_SettingsFileName_When_OutputFilename_Is_Missing() { // OutputFilename will be defaulted by ApplySettingsFileDefaults - var settingsFilePath = @"C:\Projects\MyApi\petstore.refitter"; + var settingsFilePath = Path.Combine(Path.GetTempPath(), "Projects", "MyApi", "petstore.refitter"); var settings = new Settings { SettingsFilePath = settingsFilePath, @@ -232,13 +234,15 @@ public void GetOutputPath_Should_Use_SettingsFileName_When_OutputFilename_Is_Mis var result = method!.Invoke(null, [settings, refitSettings]) as string; // Should use settings file base name - result.Should().Be(@"C:\Projects\MyApi\./Generated\petstore.cs"); + var expectedFilename = Path.GetFileNameWithoutExtension(settingsFilePath) + ".cs"; + var expectedPath = Path.Combine(Path.GetDirectoryName(settingsFilePath)!, "./Generated", expectedFilename); + result.Should().Be(expectedPath); } [Test] public void ApplySettingsFileDefaults_Should_Set_Default_OutputFolder() { - var settingsFilePath = @"C:\Projects\MyApi\petstore.refitter"; + var settingsFilePath = Path.Combine(Path.GetTempPath(), "Projects", "MyApi", "petstore.refitter"); var refitSettings = new RefitGeneratorSettings { OutputFolder = null @@ -257,7 +261,7 @@ public void ApplySettingsFileDefaults_Should_Set_Default_OutputFolder() [Test] public void ApplySettingsFileDefaults_Should_Preserve_Explicit_OutputFolder() { - var settingsFilePath = @"C:\Projects\MyApi\petstore.refitter"; + var settingsFilePath = Path.Combine(Path.GetTempPath(), "Projects", "MyApi", "petstore.refitter"); var refitSettings = new RefitGeneratorSettings { OutputFolder = "./CustomFolder" @@ -275,7 +279,7 @@ public void ApplySettingsFileDefaults_Should_Preserve_Explicit_OutputFolder() [Test] public void ApplySettingsFileDefaults_Should_Set_Default_When_Empty_OutputFolder() { - var settingsFilePath = @"C:\Projects\MyApi\petstore.refitter"; + var settingsFilePath = Path.Combine(Path.GetTempPath(), "Projects", "MyApi", "petstore.refitter"); var refitSettings = new RefitGeneratorSettings { OutputFolder = string.Empty @@ -289,4 +293,4 @@ public void ApplySettingsFileDefaults_Should_Set_Default_When_Empty_OutputFolder refitSettings.OutputFolder.Should().Be("./Generated"); } -} +} \ No newline at end of file diff --git a/src/Refitter/GenerateCommand.cs b/src/Refitter/GenerateCommand.cs index 7ca9a85f5..f2e0c2203 100644 --- a/src/Refitter/GenerateCommand.cs +++ b/src/Refitter/GenerateCommand.cs @@ -39,26 +39,25 @@ protected override async Task ExecuteAsync(CommandContext context, Settings { RefitGeneratorSettings refitGeneratorSettings; - // When settings file is provided, deserialize it first and use as source of truth - if (!string.IsNullOrWhiteSpace(settings.SettingsFilePath)) + try { - var json = await File.ReadAllTextAsync(settings.SettingsFilePath); - refitGeneratorSettings = Serializer.Deserialize(json); - - // Allow CLI to override OpenApiPath if explicitly provided - if (!string.IsNullOrWhiteSpace(settings.OpenApiPath)) - refitGeneratorSettings.OpenApiPath = settings.OpenApiPath; + // When settings file is provided, deserialize it first and use as source of truth + if (!string.IsNullOrWhiteSpace(settings.SettingsFilePath)) + { + var json = await File.ReadAllTextAsync(settings.SettingsFilePath); + refitGeneratorSettings = Serializer.Deserialize(json); - ApplySettingsFileDefaults(settings.SettingsFilePath, refitGeneratorSettings); - } - else - { - // No settings file - build from CLI arguments - refitGeneratorSettings = CreateRefitGeneratorSettings(settings); - } + // Allow CLI to override OpenApiPath if explicitly provided + if (!string.IsNullOrWhiteSpace(settings.OpenApiPath)) + refitGeneratorSettings.OpenApiPath = settings.OpenApiPath; - try - { + ApplySettingsFileDefaults(settings.SettingsFilePath, refitGeneratorSettings); + } + else + { + // No settings file - build from CLI arguments + refitGeneratorSettings = CreateRefitGeneratorSettings(settings); + } var stopwatch = Stopwatch.StartNew(); var version = GetType().Assembly.GetName().Version!.ToString(); if (version == "1.0.0.0") @@ -936,4 +935,4 @@ internal static string DetermineSettingsFilePath(Settings settings) // Default: put .refitter file in current directory return FileExtensionConstants.Refitter; } -} +} \ No newline at end of file diff --git a/test/MSBuild/build.ps1 b/test/MSBuild/build.ps1 index 169c64ce7..9ddb792e8 100644 --- a/test/MSBuild/build.ps1 +++ b/test/MSBuild/build.ps1 @@ -57,8 +57,9 @@ Write-Host "PASS: No stray Output.cs in root directory" -ForegroundColor Green # Check interface naming (smoke test for naming.interfaceName) $petstoreContent = Get-Content "Generated\Petstore.cs" -Raw if ($petstoreContent -notmatch "interface ISwaggerPetstore") { - Write-Host "WARNING: Expected interface ISwaggerPetstore not found in Petstore.cs" -ForegroundColor Yellow - Write-Host " naming.interfaceName setting may have been ignored" -ForegroundColor Yellow + Write-Host "ERROR: Expected interface ISwaggerPetstore not found in Petstore.cs" -ForegroundColor Red + Write-Host " naming.interfaceName setting may have been ignored" -ForegroundColor Red + exit 1 } else { Write-Host "PASS: Interface ISwaggerPetstore found (respects naming.interfaceName)" -ForegroundColor Green } @@ -72,7 +73,9 @@ Write-Host "PASS: GeneratedOutput\PetstoreWithFolder.cs exists (respects explici $petstoreWithFolderContent = Get-Content "GeneratedOutput\PetstoreWithFolder.cs" -Raw if ($petstoreWithFolderContent -notmatch "interface ISwaggerPetstoreWithFolder") { - Write-Host "WARNING: Expected interface ISwaggerPetstoreWithFolder not found in GeneratedOutput\PetstoreWithFolder.cs" -ForegroundColor Yellow + Write-Host "ERROR: Expected interface ISwaggerPetstoreWithFolder not found in GeneratedOutput\PetstoreWithFolder.cs" -ForegroundColor Red + Write-Host " naming.interfaceName setting may have been ignored" -ForegroundColor Red + exit 1 } else { Write-Host "PASS: Interface ISwaggerPetstoreWithFolder found (respects naming.interfaceName with outputFolder)" -ForegroundColor Green } @@ -81,4 +84,4 @@ Write-Host "=== All Issue #998 Checks Complete ===" -ForegroundColor Cyan Write-Host "" dotnet remove package Refitter.MSBuild -Remove-Item Refitter.MSBuild.*.nupkg -Force +Remove-Item Refitter.MSBuild.*.nupkg -Force \ No newline at end of file