From 8c2550fdf60e6e1f73482aa49309ba7a0e9c0935 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:03:42 +0000 Subject: [PATCH] Fix core generator test warnings (#7428) --- .../AssemblyNamesWithDashesTests.cs | 47 ++++++++++++------- .../NuGetDownloader.cs | 3 +- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/tests/TUnit.Core.SourceGenerator.Tests/AssemblyNamesWithDashesTests.cs b/tests/TUnit.Core.SourceGenerator.Tests/AssemblyNamesWithDashesTests.cs index c61234cb191..a1837c2f568 100644 --- a/tests/TUnit.Core.SourceGenerator.Tests/AssemblyNamesWithDashesTests.cs +++ b/tests/TUnit.Core.SourceGenerator.Tests/AssemblyNamesWithDashesTests.cs @@ -1,25 +1,38 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using TUnit.Core.SourceGenerator.Generators; +using TUnit.Tests.Shared; + namespace TUnit.Core.SourceGenerator.Tests; -/// -/// Test to verify that assembly names with dashes are properly handled in Before(Assembly) hooks. -/// This addresses the issue where assembly names like "test-assembly" would generate invalid C# variable names. -/// -internal class AssemblyNamesWithDashesTests : TestsBase +internal class AssemblyNamesWithDashesTests { [Test] public async Task AssemblyNameWithDashes_ShouldGenerateValidVariableNames() { - // This test verifies that the fix for issue #2919 works correctly. - // Previously, assembly names with dashes would generate invalid C# code like: - // var tunit50-test_assembly = typeof(SomeType).Assembly; - // - // After the fix, it should generate valid C# code like: - // var tunit50_test_assembly = typeof(SomeType).Assembly; + const string source = """ + using TUnit.Core; + + public class AssemblyHooks + { + [Before(HookType.Assembly)] + public static void BeforeAssembly() { } + } + """; + + var compilation = CSharpCompilation.Create( + "test-assembly", + [GeneratorTestInput.Parse(source)], + ReferencesHelper.References, + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); + + GeneratorDriver driver = CSharpGeneratorDriver.Create( + [new HookMetadataGenerator().AsSourceGenerator()], + parseOptions: GeneratorTestInput.ParseOptions); + driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out var output, out var diagnostics); - // Since this is a minimal test to document the fix, we just verify that - // a test project with dashes in the assembly name can be built successfully. - // The actual integration testing is done via manual verification with test projects. - - await Assert.That(true).IsTrue(); + await Assert.That(driver.GetRunResult().GeneratedTrees).IsNotEmpty(); + await Assert.That(diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error)).IsEmpty(); + await Assert.That(output.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error)).IsEmpty(); } -} \ No newline at end of file +} diff --git a/tests/TUnit.Core.SourceGenerator.Tests/NuGetDownloader.cs b/tests/TUnit.Core.SourceGenerator.Tests/NuGetDownloader.cs index 33551dee840..a1970381361 100644 --- a/tests/TUnit.Core.SourceGenerator.Tests/NuGetDownloader.cs +++ b/tests/TUnit.Core.SourceGenerator.Tests/NuGetDownloader.cs @@ -24,7 +24,8 @@ public static async Task> DownloadPackageAsync(st var sourceRepositoryProvider = new SourceRepositoryProvider(new PackageSourceProvider(settings), Repository.Provider.GetCoreV3()); var repository = sourceRepositoryProvider.CreateRepository(new PackageSource("https://api.nuget.org/v3/index.json")); - var resource = await repository.GetResourceAsync(); + var resource = await repository.GetResourceAsync() + ?? throw new InvalidOperationException("The NuGet source does not provide a package download resource."); Directory.CreateDirectory(OutputPath);