Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 14 additions & 10 deletions src/Refitter.Tests/GenerateCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
[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,
Expand All @@ -172,13 +172,14 @@
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,
Expand All @@ -198,14 +199,15 @@

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,
Expand All @@ -232,16 +234,18 @@
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

Check warning on line 248 in src/Refitter.Tests/GenerateCommandTests.cs

View workflow job for this annotation

GitHub Actions / 👌 Verify build

Cannot convert null literal to non-nullable reference type.

Check warning on line 248 in src/Refitter.Tests/GenerateCommandTests.cs

View workflow job for this annotation

GitHub Actions / 👌 Verify build

Cannot convert null literal to non-nullable reference type.
};

var method = typeof(GenerateCommand).GetMethod(
Expand All @@ -257,7 +261,7 @@
[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"
Expand All @@ -275,7 +279,7 @@
[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
Expand All @@ -289,4 +293,4 @@

refitSettings.OutputFolder.Should().Be("./Generated");
}
}
}
35 changes: 17 additions & 18 deletions src/Refitter/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,25 @@ protected override async Task<int> 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<RefitGeneratorSettings>(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<RefitGeneratorSettings>(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")
Expand Down Expand Up @@ -936,4 +935,4 @@ internal static string DetermineSettingsFilePath(Settings settings)
// Default: put .refitter file in current directory
return FileExtensionConstants.Refitter;
}
}
}
11 changes: 7 additions & 4 deletions test/MSBuild/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Loading