From 80ca4e330d77a82d09f514bf2527665299ecfb0e Mon Sep 17 00:00:00 2001 From: Oleksandr Liakhevych Date: Thu, 1 May 2025 14:04:46 +0300 Subject: [PATCH] Fix double using directive. Use Verify for snapshots. --- .gitignore | 4 +- .../GenerateAspNetCoreClient.Command.csproj | 2 +- .../GenerateClientCommand.cs | 3 - .../IWeatherForecastApi.verified.cs} | 2 +- .../ITestWebApiMinimalApiApi.verified.cs | 15 ++++ .../IWeatherForecastApi.verified.cs | 18 ++++ .../IAnotherTestApi.verified.cs} | 2 +- .../IWeatherForecastApi.verified.cs} | 2 +- .../ITestWebApiMinimalApiApi.verified.cs} | 2 +- .../IWeatherForecastApi.verified.cs} | 2 +- .../IVersionApi.verified.cs} | 2 +- .../ClientGenerationTests.cs | 89 +++++-------------- .../GenerateAspNetCoreClient.Tests.csproj | 18 ++-- Tests/OutputTest/OutputProject.csproj | 4 +- .../TestWebApi.Controllers.csproj | 2 +- .../TestWebApi.MinimalApi.csproj | 4 +- .../TestWebApi.Versioning.csproj | 2 +- 17 files changed, 86 insertions(+), 87 deletions(-) rename Tests/GenerateAspNetCoreClient.Tests/{__snapshots__/TestWebApi.Controllers.UseApiResponses/IWeatherForecastApi.cs.snap => ClientGenerationTests.GenerationTest_UseApiResponses/IWeatherForecastApi.verified.cs} (94%) create mode 100644 Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses_UseCancellationTokens/ITestWebApiMinimalApiApi.verified.cs create mode 100644 Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses_UseCancellationTokens/IWeatherForecastApi.verified.cs rename Tests/GenerateAspNetCoreClient.Tests/{__snapshots__/TestWebApi.Controllers/IAnotherTestApi.cs.snap => ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Controllers/IAnotherTestApi.verified.cs} (96%) rename Tests/GenerateAspNetCoreClient.Tests/{__snapshots__/TestWebApi.Controllers/IWeatherForecastApi.cs.snap => ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Controllers/IWeatherForecastApi.verified.cs} (98%) rename Tests/GenerateAspNetCoreClient.Tests/{__snapshots__/TestWebApi.MinimalApi/ITestWebApiMinimalApiApi.cs.snap => ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.MinimalApi/ITestWebApiMinimalApiApi.verified.cs} (91%) rename Tests/GenerateAspNetCoreClient.Tests/{__snapshots__/TestWebApi.MinimalApi/IWeatherForecastApi.cs.snap => ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.MinimalApi/IWeatherForecastApi.verified.cs} (93%) rename Tests/GenerateAspNetCoreClient.Tests/{__snapshots__/TestWebApi.Versioning/IVersionApi.cs.snap => ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Versioning/IVersionApi.verified.cs} (94%) diff --git a/.gitignore b/.gitignore index 41adefd..e4e01dd 100644 --- a/.gitignore +++ b/.gitignore @@ -339,4 +339,6 @@ ASALocalRun/ # BeatPulse healthcheck temp database healthchecksdb -/GenerateAspNetCoreClient/Properties/debug-paths.txt \ No newline at end of file +/GenerateAspNetCoreClient/Properties/debug-paths.txt + +*.received.* \ No newline at end of file diff --git a/GenerateAspNetCoreClient.Command/GenerateAspNetCoreClient.Command.csproj b/GenerateAspNetCoreClient.Command/GenerateAspNetCoreClient.Command.csproj index 7a23a19..b45926b 100644 --- a/GenerateAspNetCoreClient.Command/GenerateAspNetCoreClient.Command.csproj +++ b/GenerateAspNetCoreClient.Command/GenerateAspNetCoreClient.Command.csproj @@ -11,7 +11,7 @@ - + diff --git a/GenerateAspNetCoreClient.Command/GenerateClientCommand.cs b/GenerateAspNetCoreClient.Command/GenerateClientCommand.cs index 790c15e..bf98e64 100644 --- a/GenerateAspNetCoreClient.Command/GenerateClientCommand.cs +++ b/GenerateAspNetCoreClient.Command/GenerateClientCommand.cs @@ -126,9 +126,6 @@ private static IEnumerable GetImportedNamespaces(Client clientModel, Has var namespaces = GetNamespaces(clientModel.EndpointMethods, ambiguousTypes) .Concat(options.AdditionalNamespaces); - if (options.AddCancellationTokenParameters) - namespaces = namespaces.Append("System.Threading"); - namespaces = namespaces .OrderByDescending(ns => ns.StartsWith("System")) .ThenBy(ns => ns); diff --git a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Controllers.UseApiResponses/IWeatherForecastApi.cs.snap b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses/IWeatherForecastApi.verified.cs similarity index 94% rename from Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Controllers.UseApiResponses/IWeatherForecastApi.cs.snap rename to Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses/IWeatherForecastApi.verified.cs index 217e123..4c35494 100644 --- a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Controllers.UseApiResponses/IWeatherForecastApi.cs.snap +++ b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses/IWeatherForecastApi.verified.cs @@ -1,4 +1,4 @@ -// +// using System.Collections.Generic; using System.Threading.Tasks; diff --git a/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses_UseCancellationTokens/ITestWebApiMinimalApiApi.verified.cs b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses_UseCancellationTokens/ITestWebApiMinimalApiApi.verified.cs new file mode 100644 index 0000000..3c2270a --- /dev/null +++ b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses_UseCancellationTokens/ITestWebApiMinimalApiApi.verified.cs @@ -0,0 +1,15 @@ +// + +using System.Threading; +using System.Threading.Tasks; +using Refit; +using TestWebApi.Models; + +namespace Test.Name.Space +{ + public partial interface ITestWebApiMinimalApiApi + { + [Post("/weather-forecast")] + Task PostWeatherForecast([Body] WeatherForecast forecast, CancellationToken cancellationToken = default); + } +} \ No newline at end of file diff --git a/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses_UseCancellationTokens/IWeatherForecastApi.verified.cs b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses_UseCancellationTokens/IWeatherForecastApi.verified.cs new file mode 100644 index 0000000..f4428f8 --- /dev/null +++ b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_UseApiResponses_UseCancellationTokens/IWeatherForecastApi.verified.cs @@ -0,0 +1,18 @@ +// + +using System.Threading; +using System.Threading.Tasks; +using Refit; +using TestWebApi.Models; + +namespace Test.Name.Space +{ + public partial interface IWeatherForecastApi + { + [Get("/weather-forecast")] + Task> GetWeatherForecast(CancellationToken cancellationToken = default); + + [Get("/weather-forecast/with-name")] + Task> GetSomeWeather(int days, CancellationToken cancellationToken = default); + } +} \ No newline at end of file diff --git a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Controllers/IAnotherTestApi.cs.snap b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Controllers/IAnotherTestApi.verified.cs similarity index 96% rename from Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Controllers/IAnotherTestApi.cs.snap rename to Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Controllers/IAnotherTestApi.verified.cs index 4ab9dca..60b2b8e 100644 --- a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Controllers/IAnotherTestApi.cs.snap +++ b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Controllers/IAnotherTestApi.verified.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Threading.Tasks; diff --git a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Controllers/IWeatherForecastApi.cs.snap b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Controllers/IWeatherForecastApi.verified.cs similarity index 98% rename from Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Controllers/IWeatherForecastApi.cs.snap rename to Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Controllers/IWeatherForecastApi.verified.cs index b677193..2cb906e 100644 --- a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Controllers/IWeatherForecastApi.cs.snap +++ b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Controllers/IWeatherForecastApi.verified.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; diff --git a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.MinimalApi/ITestWebApiMinimalApiApi.cs.snap b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.MinimalApi/ITestWebApiMinimalApiApi.verified.cs similarity index 91% rename from Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.MinimalApi/ITestWebApiMinimalApiApi.cs.snap rename to Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.MinimalApi/ITestWebApiMinimalApiApi.verified.cs index 57e838b..bf39b58 100644 --- a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.MinimalApi/ITestWebApiMinimalApiApi.cs.snap +++ b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.MinimalApi/ITestWebApiMinimalApiApi.verified.cs @@ -1,4 +1,4 @@ -// +// using System.Threading.Tasks; using Refit; diff --git a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.MinimalApi/IWeatherForecastApi.cs.snap b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.MinimalApi/IWeatherForecastApi.verified.cs similarity index 93% rename from Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.MinimalApi/IWeatherForecastApi.cs.snap rename to Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.MinimalApi/IWeatherForecastApi.verified.cs index 0cfdafc..80eea54 100644 --- a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.MinimalApi/IWeatherForecastApi.cs.snap +++ b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.MinimalApi/IWeatherForecastApi.verified.cs @@ -1,4 +1,4 @@ -// +// using System.Threading.Tasks; using Refit; diff --git a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Versioning/IVersionApi.cs.snap b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Versioning/IVersionApi.verified.cs similarity index 94% rename from Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Versioning/IVersionApi.cs.snap rename to Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Versioning/IVersionApi.verified.cs index 210131e..ff4837e 100644 --- a/Tests/GenerateAspNetCoreClient.Tests/__snapshots__/TestWebApi.Versioning/IVersionApi.cs.snap +++ b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.GenerationTest_testProjectName=TestWebApi.Versioning/IVersionApi.verified.cs @@ -1,4 +1,4 @@ -// +// using System.Threading.Tasks; using Refit; diff --git a/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.cs b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.cs index c8028ce..b266b57 100644 --- a/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.cs +++ b/Tests/GenerateAspNetCoreClient.Tests/ClientGenerationTests.cs @@ -1,23 +1,19 @@ -using System; -using System.IO; -using System.Text.RegularExpressions; +using System.IO; +using System.Threading.Tasks; using DotNet.Cli.Build; using GenerateAspNetCoreClient.Options; using NUnit.Framework; +using VerifyNUnit; namespace GenerateAspNetCoreClient.Tests { [NonParallelizable] public class ClientGenerationTests { - private static readonly string _snapshotsPath = Path.Combine("..", "..", "..", "__snapshots__", "{0}"); - private static readonly string _inputPath = Path.Combine("..", "..", "..", "..", "{0}", "{0}.csproj"); private static readonly string _outPath = Path.Combine("..", "..", "..", "..", "OutputTest", "Client"); private static readonly string _outProjectPath = Path.Combine("..", "..", "..", "..", "OutputTest", "OutputProject.csproj"); - private static readonly bool _regenerateSnapshots = false; - [TearDown] public void CleanOutput() { @@ -27,7 +23,7 @@ public void CleanOutput() [TestCase("TestWebApi.Controllers")] [TestCase("TestWebApi.Versioning")] [TestCase("TestWebApi.MinimalApi")] - public void GenerationTest(string testProjectName) + public async Task GenerationTest(string testProjectName) { var options = new GenerateClientOptions { @@ -35,16 +31,15 @@ public void GenerationTest(string testProjectName) OutPath = _outPath, Namespace = "Test.Name.Space", }; - + Program.CreateClient(options); - + Assert.That(() => Project.FromPath(_outProjectPath).Build(), Throws.Nothing); - AssertSnapshotMatch(testProjectName); + await Verifier.VerifyDirectory(_outPath); } - [Test] - public void GenerationTest_UseApiResponses() + public async Task GenerationTest_UseApiResponses() { var options = new GenerateClientOptions { @@ -57,61 +52,25 @@ public void GenerationTest_UseApiResponses() Program.CreateClient(options); Assert.That(() => Project.FromPath(_outProjectPath).Build(), Throws.Nothing); - AssertSnapshotMatch("TestWebApi.Controllers.UseApiResponses"); - } - - private static void RegenerateSnapshots(string testProjectName) - { - var snapshotsPath = string.Format(_snapshotsPath, testProjectName); - - if (Directory.Exists(snapshotsPath)) - { - Directory.Delete(snapshotsPath, recursive: true); - } - - Directory.CreateDirectory(snapshotsPath); - - var generatedFiles = Directory.EnumerateFiles(_outPath, "*", new EnumerationOptions { RecurseSubdirectories = true }); - - foreach (var generatedFile in generatedFiles) - { - var relativePath = Path.GetRelativePath(_outPath, generatedFile); - File.Move(generatedFile, Path.Combine(snapshotsPath, relativePath + ".snap")); - } - - throw new Exception("Don't forget to disable snapshot regeneration."); + await Verifier.VerifyDirectory(_outPath); } - - private static void AssertSnapshotMatch(string testProjectName) + + [Test] + public async Task GenerationTest_UseApiResponses_UseCancellationTokens() { - if (_regenerateSnapshots) - { - RegenerateSnapshots(testProjectName); - } - - var snapshotsPath = string.Format(_snapshotsPath, testProjectName); - var generatedFiles = Directory.EnumerateFiles(_outPath, "*", new EnumerationOptions { RecurseSubdirectories = true }); - - foreach (var generatedFile in generatedFiles) - { - var relativePath = Path.GetRelativePath(_outPath, generatedFile); - var snapshotPath = Path.Combine(snapshotsPath, relativePath + ".snap"); - - Assert.That(snapshotPath, Does.Exist, $"Unexpected file generated ({relativePath})"); - - var actualContent = File.ReadAllText(generatedFile); - var expectedContent = File.ReadAllText(snapshotPath); - - Assert.That(actualContent, Is.EqualTo(expectedContent).Using(IgnoreLineEndings), - $"File mismatch (${relativePath})"); - } - - static bool IgnoreLineEndings(string s1, string s2) + var options = new GenerateClientOptions { - s1 = Regex.Replace(s1, "(\r|\n|\r\n)", "\r"); - s2 = Regex.Replace(s2, "(\r|\n|\r\n)", "\r"); - return s1 == s2; - } + InputPath = string.Format(_inputPath,"TestWebApi.MinimalApi"), + UseApiResponses = true, + AddCancellationTokenParameters = true, + OutPath = _outPath, + Namespace = "Test.Name.Space", + }; + + Program.CreateClient(options); + + Assert.That(() => Project.FromPath(_outProjectPath).Build(), Throws.Nothing); + await Verifier.VerifyDirectory(_outPath); } } } diff --git a/Tests/GenerateAspNetCoreClient.Tests/GenerateAspNetCoreClient.Tests.csproj b/Tests/GenerateAspNetCoreClient.Tests/GenerateAspNetCoreClient.Tests.csproj index 048fdc6..fce226d 100644 --- a/Tests/GenerateAspNetCoreClient.Tests/GenerateAspNetCoreClient.Tests.csproj +++ b/Tests/GenerateAspNetCoreClient.Tests/GenerateAspNetCoreClient.Tests.csproj @@ -5,20 +5,21 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -27,4 +28,11 @@ + + + + + + + diff --git a/Tests/OutputTest/OutputProject.csproj b/Tests/OutputTest/OutputProject.csproj index 44b9835..c905fcd 100644 --- a/Tests/OutputTest/OutputProject.csproj +++ b/Tests/OutputTest/OutputProject.csproj @@ -6,8 +6,8 @@ - - + + diff --git a/Tests/TestWebApi.Controllers/TestWebApi.Controllers.csproj b/Tests/TestWebApi.Controllers/TestWebApi.Controllers.csproj index 912caa8..43f2332 100644 --- a/Tests/TestWebApi.Controllers/TestWebApi.Controllers.csproj +++ b/Tests/TestWebApi.Controllers/TestWebApi.Controllers.csproj @@ -5,7 +5,7 @@ - + diff --git a/Tests/TestWebApi.MinimalApi/TestWebApi.MinimalApi.csproj b/Tests/TestWebApi.MinimalApi/TestWebApi.MinimalApi.csproj index 6a23ee1..3875176 100644 --- a/Tests/TestWebApi.MinimalApi/TestWebApi.MinimalApi.csproj +++ b/Tests/TestWebApi.MinimalApi/TestWebApi.MinimalApi.csproj @@ -7,11 +7,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/Tests/TestWebApi.Versioning/TestWebApi.Versioning.csproj b/Tests/TestWebApi.Versioning/TestWebApi.Versioning.csproj index f13be16..d99a990 100644 --- a/Tests/TestWebApi.Versioning/TestWebApi.Versioning.csproj +++ b/Tests/TestWebApi.Versioning/TestWebApi.Versioning.csproj @@ -7,7 +7,7 @@ - +