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
181 changes: 181 additions & 0 deletions src/Refitter.Tests/GenerationOrchestratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using FluentAssertions;
using Refitter.Core;
using TUnit.Core;

namespace Refitter.Tests;

public class GenerationOrchestratorTests
{
[Test]
public async Task RunAsync_Should_Generate_Code_And_Return_Zero()
{
var workspace = Path.Combine(
AppContext.BaseDirectory,
"GenerationOrchestratorTests",
Guid.NewGuid().ToString("N"));

try
{
var openApiPath = Path.Combine(workspace, "spec.json");
var outputPath = Path.Combine(workspace, "Output.cs");
Directory.CreateDirectory(workspace);

File.WriteAllText(
openApiPath,
"""
{
"openapi": "3.0.0",
"info": { "title": "Test API", "version": "1.0.0" },
"paths": {
"/pets": {
"get": {
"operationId": "GetPets",
"responses": { "200": { "description": "ok" } }
}
}
}
}
""");

var settings = new RefitGeneratorSettings
{
OpenApiPath = openApiPath,
Namespace = "TestNamespace",
GenerateContracts = false,
GenerateClients = true,
};

var cliSettings = new Settings
{
OpenApiPath = openApiPath,
OutputPath = outputPath,
NoLogging = true,
NoBanner = true,
SkipValidation = true,
};

var reporter = new SimpleGenerationReporter();
var orchestrator = new GenerationOrchestrator();
var result = await orchestrator.RunAsync(settings, cliSettings, reporter, default);

result.Should().Be(0);
File.Exists(outputPath).Should().BeTrue();
var generatedCode = await File.ReadAllTextAsync(outputPath);
generatedCode.Should().Contain("TestNamespace");
generatedCode.Should().Contain("GetPets");
Comment on lines +61 to +65

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add generated-code compilation checks in the success-path tests.

The tests validate text content but do not verify the generated C# compiles; this misses regressions that produce syntactically invalid output.

As per coding guidelines, "Follow the unit testing pattern with OpenAPI specification constants, [Fact] decorated test methods, FluentAssertions for validation, and BuildHelper.BuildCSharp() for compilation verification in test classes".

Also applies to: 129-134

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Refitter.Tests/GenerationOrchestratorTests.cs` around lines 61 - 65,
Tests in GenerationOrchestratorTests.cs only assert generatedCode contains
strings but do not verify it compiles; update the success-path tests so that
after reading generatedCode you call BuildHelper.BuildCSharp(generatedCode) (or
the overload used across tests) and assert the build succeeded (e.g., no
diagnostics/error and result is true) using FluentAssertions. Modify the test
cases that set and read the generatedCode variable in the
GenerationOrchestratorTests class (apply the same change to the other
success-path test mentioned) to include constants/OpenAPI spec usage as needed
and a final BuildHelper.BuildCSharp(...) call with an assertion to ensure the
emitted C# compiles.

Source: Coding guidelines

}
finally
{
if (Directory.Exists(workspace))
Directory.Delete(workspace, recursive: true);
}
}

[Test]
public async Task RunAsync_Should_Generate_Multiple_Files_And_Return_Zero()
{
var workspace = Path.Combine(
AppContext.BaseDirectory,
"GenerationOrchestratorTests",
Guid.NewGuid().ToString("N"));

try
{
var openApiPath = Path.Combine(workspace, "spec.json");
var outputDir = Path.Combine(workspace, "Generated");
Directory.CreateDirectory(workspace);

File.WriteAllText(
openApiPath,
"""
{
"openapi": "3.0.0",
"info": { "title": "Test API", "version": "1.0.0" },
"paths": {
"/pets": {
"get": {
"operationId": "GetPets",
"tags": ["pets"],
"responses": { "200": { "description": "ok" } }
}
}
}
}
""");

var settings = new RefitGeneratorSettings
{
OpenApiPath = openApiPath,
Namespace = "TestNamespace",
GenerateMultipleFiles = true,
OutputFolder = outputDir,
GenerateContracts = false,
};

var cliSettings = new Settings
{
OpenApiPath = openApiPath,
OutputPath = outputDir,
GenerateMultipleFiles = true,
NoLogging = true,
NoBanner = true,
SkipValidation = true,
};

var reporter = new SimpleGenerationReporter();
var orchestrator = new GenerationOrchestrator();
var result = await orchestrator.RunAsync(settings, cliSettings, reporter, default);

result.Should().Be(0);
var generatedFiles = Directory.GetFiles(outputDir, "*.cs");
generatedFiles.Should().NotBeEmpty();
var generatedCode = await File.ReadAllTextAsync(generatedFiles[0]);
generatedCode.Should().Contain("TestNamespace");
}
finally
{
if (Directory.Exists(workspace))
Directory.Delete(workspace, recursive: true);
}
}

[Test]
public async Task RunAsync_Should_Return_NonZero_On_Exception()
{
var workspace = Path.Combine(
AppContext.BaseDirectory,
"GenerationOrchestratorTests",
Guid.NewGuid().ToString("N"));

try
{
var openApiPath = Path.Combine(workspace, "nonexistent.json");
Directory.CreateDirectory(workspace);

var settings = new RefitGeneratorSettings
{
OpenApiPath = openApiPath,
Namespace = "TestNamespace",
};

var cliSettings = new Settings
{
OpenApiPath = openApiPath,
NoLogging = true,
NoBanner = true,
SkipValidation = true,
};

var reporter = new SimpleGenerationReporter();
var orchestrator = new GenerationOrchestrator();
var result = await orchestrator.RunAsync(settings, cliSettings, reporter, default);

result.Should().NotBe(0);
}
finally
{
if (Directory.Exists(workspace))
Directory.Delete(workspace, recursive: true);
}
}
}
Loading
Loading