-
-
Notifications
You must be signed in to change notification settings - Fork 63
Extract GenerationOrchestrator from GenerateCommand #1128
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, andBuildHelper.BuildCSharp()for compilation verification in test classes".Also applies to: 129-134
🤖 Prompt for AI Agents
Source: Coding guidelines