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: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dotnet build DependencyModules.sln --configuration Release

The `restore` step needs the `test-tools` feed in `NuGet.config`, which has the Microsoft.Testing.Platform 3 preview that VerificationGlue.Tests uses. No shipping package references VerificationGlue.

The unit tests are in two projects while they move to VerificationGlue.Tests. `tests/DependencyModules.Specs` has the `RuntimeTests` and `ApiTests` folders and a part of the `GeneratorTests` folder. `tests/DependencyModules.Tests` has the other folders and the remaining `GeneratorTests` files. Both projects compile the test harness in `tests/DependencyModules.Specs/Infrastructure`. Each project reads the snapshots in its own `Snapshots` folder.
The unit tests are in two projects while they move to VerificationGlue.Tests. `tests/DependencyModules.Specs` has the `RuntimeTests`, `ApiTests` and `GeneratorTests` folders. `tests/DependencyModules.Tests` has the other folders. Both projects compile the test harness in `tests/DependencyModules.Specs/Infrastructure`. Each project reads the snapshots in its own `Snapshots` folder.

To run all tests with code coverage, use the coverage script. The script writes the report to `artifacts/coverage`. If you give a percentage, the script fails when the line coverage is less than that value.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
using System.Linq;
using DependencyModules.Tests.Infrastructure;
using Microsoft.CodeAnalysis;
using Xunit;

namespace DependencyModules.Tests.GeneratorTests;
namespace DependencyModules.Specs.GeneratorTests;

/// <summary>
/// Declaring <c>[DependencyModule] partial class ApplicationModule</c> in a project that already
Expand All @@ -26,29 +25,30 @@ namespace DependencyModules.Tests.GeneratorTests;
/// The variants below are kept as a set on purpose: every one of them was reported as the trigger
/// at some point, and only the last is.
/// </summary>
public class ApplicationModuleCollisionTests
public partial class ApplicationModuleCollisionTests
{
[Fact]
[VerifyTest]
public void DeclaringApplicationModuleInTheRootNamespace_DoesNotCollide()
{
var result = Run(DeclaredModule("ConfiguredRoot"));

Assert.Empty(result.DuplicateHintNames);
Verify(result.DuplicateHintNames.Count == 0);
result.AssertNoErrors();
}

/// <summary>
/// CS8785 is how a duplicate hint name reaches the build, and it is only a warning — which is
/// the reason this shipped. Asserted separately from AssertNoErrors for that reason.
/// </summary>
[Fact]
[VerifyTest]
public void DeclaringApplicationModuleInTheRootNamespace_DoesNotWarn()
{
var result = Run(DeclaredModule("ConfiguredRoot"));

Assert.DoesNotContain(
result.CompilationDiagnostics.Concat(result.GeneratorDiagnostics),
diagnostic => diagnostic.Id == "CS8785"
Verify(
!result
.CompilationDiagnostics.Concat(result.GeneratorDiagnostics)
.Any(diagnostic => diagnostic.Id == "CS8785")
);
}

Expand All @@ -57,35 +57,35 @@ public void DeclaringApplicationModuleInTheRootNamespace_DoesNotWarn()
/// add to. Consolidation already preferred a declared module over a generated one wherever it
/// recognised the two as the same — this only ever failed to recognise them.
/// </summary>
[Fact]
[VerifyTest]
public void TheDeclaredModuleIsTheOneGenerated()
{
var result = Run(DeclaredModule("ConfiguredRoot"));

var module = result.SourceContaining("ApplicationModule.Module");

Assert.Contains("namespace ConfiguredRoot", module);
Verify(module.Contains("namespace ConfiguredRoot"));
}

/// <summary>
/// The registrations still have to arrive. Merging the two models is only correct if the
/// surviving one carries what the project asked for.
/// </summary>
[Fact]
[VerifyTest]
public void TheSurvivingModuleStillRegistersTheProjectsServices()
{
var result = Run(DeclaredModule("ConfiguredRoot"));

Assert.Contains("Thing", result.SourceContaining("ApplicationModule.Dependencies"));
Verify(result.SourceContaining("ApplicationModule.Dependencies").Contains("Thing"));
}

/// <summary>
/// The four shapes that always built cleanly, kept so that a fix cannot quietly change them.
/// Each differs from the failing case in exactly one way.
/// </summary>
[Theory]
[VerifyTest]
// A different name never collides with the generated ApplicationModule.
[InlineData(
[Row(
"different name",
"""
namespace ConfiguredRoot;
Expand All @@ -94,7 +94,7 @@ public partial class CompositionModule;
"""
)]
// A namespace other than RootNamespace produces a different hint name.
[InlineData(
[Row(
"different namespace",
"""
namespace SomewhereElse;
Expand All @@ -103,7 +103,7 @@ public partial class ApplicationModule;
"""
)]
// The global namespace, likewise.
[InlineData(
[Row(
"global namespace",
"""
[DependencyModules.Runtime.Attributes.DependencyModule]
Expand All @@ -114,9 +114,9 @@ public void AShapeThatNeverCollided_StillBuildsCleanly(string variant, string de
{
var result = Run(declaration);

Assert.Empty(result.DuplicateHintNames);
Verify(result.DuplicateHintNames.Count == 0);
result.AssertNoErrors();
Assert.NotEmpty(variant);
Verify(variant.Length != 0);
}

/// <summary>
Expand All @@ -129,7 +129,7 @@ public void AShapeThatNeverCollided_StillBuildsCleanly(string variant, string de
/// left as it is rather than narrowed: a project relying on the generated module today would
/// lose it, and this fix makes the shape work either way.
/// </summary>
[Fact]
[VerifyTest]
public void AnExplicitMainWithADeclaredApplicationModule_BuildsCleanly()
{
var result = GeneratorTestHarness.Run(
Expand All @@ -149,7 +149,7 @@ public static class Program {
OutputKind.ConsoleApplication
);

Assert.Empty(result.DuplicateHintNames);
Verify(result.DuplicateHintNames.Count == 0);
result.AssertNoErrors();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using DependencyModules.Tests.Infrastructure;
using Xunit;

namespace DependencyModules.Tests.GeneratorTests;
namespace DependencyModules.Specs.GeneratorTests;

/// <summary>
/// DM0016. A module generates its attribute in the module's own namespace, and an assembly-level
Expand All @@ -12,7 +11,7 @@ namespace DependencyModules.Tests.GeneratorTests;
/// running, so it does not exist in the compilation being examined and nothing about it resolves.
/// That makes the false positives the interesting cases, and most of these tests are one.
/// </summary>
public class AssemblyModuleAttributeDiagnosticsTests
public partial class AssemblyModuleAttributeDiagnosticsTests
{
private const string ModuleInNamespace = """
namespace MyApp.Composition;
Expand All @@ -21,27 +20,27 @@ namespace MyApp.Composition;
public partial class ApplicationModule;
""";

[Fact]
[VerifyTest]
public void MissingUsing_IsReported()
{
var result = Run("[assembly: ApplicationModule]");

var diagnostic = Assert.Single(result.GeneratorDiagnostics, d => d.Id == "DM0016");
var diagnostic = Single(result.GeneratorDiagnostics, d => d.Id == "DM0016");

Assert.Contains("MyApp.Composition", diagnostic.GetMessage());
Assert.Contains("using MyApp.Composition;", diagnostic.GetMessage());
Verify(diagnostic.GetMessage().Contains("MyApp.Composition"));
Verify(diagnostic.GetMessage().Contains("using MyApp.Composition;"));
}

/// <summary>The suffixed spelling names the same module.</summary>
[Fact]
[VerifyTest]
public void MissingUsing_IsReported_ForTheAttributeSuffixedSpelling()
{
var result = Run("[assembly: ApplicationModuleAttribute]");

Assert.Single(result.GeneratorDiagnostics, d => d.Id == "DM0016");
Single(result.GeneratorDiagnostics, d => d.Id == "DM0016");
}

[Fact]
[VerifyTest]
public void TheUsingBeingPresent_IsSilent()
{
var result = Run(
Expand All @@ -52,14 +51,14 @@ public void TheUsingBeingPresent_IsSilent()
"""
);

Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0016");
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0016"));
}

/// <summary>
/// A global using in any file supplies the namespace everywhere, so reading only the file the
/// attribute sits in would report a build that is already correct.
/// </summary>
[Fact]
[VerifyTest]
public void AGlobalUsingInAnotherFile_IsSilent()
{
var result = GeneratorTestHarness.Run(
Expand All @@ -71,28 +70,28 @@ public void AGlobalUsingInAnotherFile_IsSilent()
}
);

Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0016");
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0016"));
}

[Fact]
[VerifyTest]
public void AQualifiedUsage_IsSilent()
{
var result = Run("[assembly: MyApp.Composition.ApplicationModule]");

Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0016");
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0016"));
}

/// <summary>An attribute this compilation declares no module for belongs to somebody else.</summary>
[Fact]
[VerifyTest]
public void AnUnrelatedAssemblyAttribute_IsSilent()
{
var result = Run("[assembly: System.Reflection.AssemblyMetadata(\"key\", \"value\")]");

Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0016");
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0016"));
}

/// <summary>A module in the global namespace has no namespace to import.</summary>
[Fact]
[VerifyTest]
public void AModuleInTheGlobalNamespace_IsSilent()
{
var result = GeneratorTestHarness.Run(
Expand All @@ -106,14 +105,14 @@ public partial class ApplicationModule;
}
);

Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0016");
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0016"));
}

/// <summary>
/// A using alias imports one name rather than a namespace, so it does not bring the attribute
/// into scope under the name written here and the report still stands.
/// </summary>
[Fact]
[VerifyTest]
public void AUsingAlias_DoesNotCountAsTheImport()
{
var result = Run(
Expand All @@ -124,7 +123,7 @@ public void AUsingAlias_DoesNotCountAsTheImport()
"""
);

Assert.Single(result.GeneratorDiagnostics, d => d.Id == "DM0016");
Single(result.GeneratorDiagnostics, d => d.Id == "DM0016");
}

/// <summary>
Expand All @@ -133,7 +132,7 @@ public void AUsingAlias_DoesNotCountAsTheImport()
/// they were read by nobody: a clean build, and an <c>InvalidOperationException</c> at the first
/// resolve.
/// </summary>
[Fact]
[VerifyTest]
public void AnAssemblyAttributeOutsideTheEntryPointFile_IsReported()
{
var result = RunWithEntryPoint(
Expand All @@ -145,34 +144,32 @@ public void AnAssemblyAttributeOutsideTheEntryPointFile_IsReported()
program: "System.Console.WriteLine();"
);

var diagnostic = Assert.Single(result.GeneratorDiagnostics, d => d.Id == "DM0019");
var diagnostic = Single(result.GeneratorDiagnostics, d => d.Id == "DM0019");

Assert.Contains("ApplicationModule", diagnostic.GetMessage());
Assert.Contains("Program.cs", diagnostic.GetMessage());
Verify(diagnostic.GetMessage().Contains("ApplicationModule"));
Verify(diagnostic.GetMessage().Contains("Program.cs"));
}

/// <summary>
/// A qualified name needs no import, so DM0016 cannot apply. The attribute is still read by
/// nobody outside the entry point file.
/// </summary>
[Theory]
[InlineData("[assembly: MyApp.Composition.ApplicationModule]")]
[InlineData("[assembly: MyApp.Composition.ApplicationModuleAttribute]")]
[InlineData("[assembly: global::MyApp.Composition.ApplicationModule]")]
[InlineData(
"using Composition = MyApp.Composition;\n[assembly: Composition.ApplicationModule]"
)]
[VerifyTest]
[Row("[assembly: MyApp.Composition.ApplicationModule]")]
[Row("[assembly: MyApp.Composition.ApplicationModuleAttribute]")]
[Row("[assembly: global::MyApp.Composition.ApplicationModule]")]
[Row("using Composition = MyApp.Composition;\n[assembly: Composition.ApplicationModule]")]
public void AQualifiedAssemblyAttributeOutsideTheEntryPointFile_IsReported(string bootstrap)
{
var result = RunWithEntryPoint(bootstrap, program: "System.Console.WriteLine();");

var diagnostic = Assert.Single(result.GeneratorDiagnostics, d => d.Id == "DM0019");
var diagnostic = Single(result.GeneratorDiagnostics, d => d.Id == "DM0019");

Assert.Contains("ApplicationModule", diagnostic.GetMessage());
Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0016");
Verify(diagnostic.GetMessage().Contains("ApplicationModule"));
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0016"));
}

[Fact]
[VerifyTest]
public void AQualifiedAssemblyAttributeInTheEntryPointFile_IsSilent()
{
var result = RunWithEntryPoint(
Expand All @@ -184,22 +181,22 @@ public void AQualifiedAssemblyAttributeInTheEntryPointFile_IsSilent()
"""
);

Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0019");
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0019"));
}

/// <summary>A qualified attribute that names no module is somebody else's.</summary>
[Fact]
[VerifyTest]
public void AQualifiedAttributeThatNamesNoModule_IsSilent()
{
var result = RunWithEntryPoint(
bootstrap: "[assembly: System.Reflection.AssemblyMetadata(\"key\", \"value\")]",
program: "System.Console.WriteLine();"
);

Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0019");
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0019"));
}

[Fact]
[VerifyTest]
public void AnAssemblyAttributeInTheEntryPointFile_IsSilent()
{
var result = RunWithEntryPoint(
Expand All @@ -213,7 +210,7 @@ public void AnAssemblyAttributeInTheEntryPointFile_IsSilent()
"""
);

Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0019");
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0019"));
}

/// <summary>
Expand All @@ -222,7 +219,7 @@ public void AnAssemblyAttributeInTheEntryPointFile_IsSilent()
/// relative to — and the test integration reads assembly attributes at run time anyway, which is
/// what the testing guide shows.
/// </summary>
[Fact]
[VerifyTest]
public void WithNoGeneratedApplicationModule_IsSilent()
{
var result = Run(
Expand All @@ -233,7 +230,7 @@ public void WithNoGeneratedApplicationModule_IsSilent()
"""
);

Assert.DoesNotContain(result.GeneratorDiagnostics, d => d.Id == "DM0019");
Verify(!result.GeneratorDiagnostics.Any(d => d.Id == "DM0019"));
}

private static GeneratorResult Run(string bootstrap) =>
Expand Down
Loading
Loading