Skip to content

Add ModuleTestComposition, the runner-neutral entry point, to DependencyModules.Testing - #141

Merged
ipjohnson merged 1 commit into
mainfrom
agent/stp-vgdm-entry-point
Nov 27, 2026
Merged

ipjohnson merged 1 commit into
mainfrom
agent/stp-vgdm-entry-point

Conversation

@ipjohnson

Copy link
Copy Markdown
Owner

What changed

This is step 1 of "Changes, in the order they land" in docs/design/verificationglue.md.

DependencyModules.Testing gets four public types in DependencyModules.Testing.Impl, with the signatures of Part 3:

  • ModuleTestComposition.BuildAsync(services, modules, testMethod, overrides, currentTest, parent) builds a test's container in the order of Part 3. The steps are: register an ITestContainerSource, then currentTest, then the narrowest environment. Then it loads the modules reversed, runs the setup attributes with mock support first, and registers the parent's shared instances. Then it runs the parameter providers for each parameter that no override names, registers the overrides as instances, and builds with the narrowest IServiceProviderBuilderAttribute. Last, it runs the startup attributes and initializes the TestContainerSource. When a step throws, it disposes every container it built and rethrows. Overrides come after the setup attributes, so an override beats a [TestExport] for the same service.
  • ModuleTestComposition.BuildScopeAsync(services, modules, scopeAttributes) builds the parent for a class or an assembly. It loads the modules reversed with the default environment, registers each [TestExport(Shared = true)], and builds with BuildServiceProvider().
  • ModuleTestContainer has Services, ResolveArgumentsAsync, GetParameterValueAsync, the two Resolve overloads and DisposeAsync. It tracks every container that its TestContainerSource builds. DisposeAsync disposes them, the last one built first.
  • TestServiceOverride (record) and TestMethodContext (plain ITestMethodContext).

Package references: DependencyModules.Testing now references DependencyModules.Runtime and Microsoft.Extensions.DependencyInjection (8.0.1 for net8.0, 10.0.0 for net10.0). These are the same versions as DependencyModules.xUnit. That package replaces the direct Microsoft.Extensions.DependencyInjection.Abstractions reference, which it brings in transitively. This follows the recommendation of open question 1. I removed the sentence in IModuleEnvironmentProvider's remarks that said Testing does not reference Runtime. No package references VerificationGlue.

The xUnit, xUnit4 and NUnit runners are not changed. Step 2 moves them onto this entry point.

Other files: PublicApiTests.TestingApi.verified.txt gains the four types, and TestParameterResolver's public surface is unchanged. There are CHANGELOG entries under Added and Changed. The Testing package description mentions ModuleTestComposition.

Deviations from the design, and decisions it left open

  1. TestParameterResolver "stays public and unchanged". Its public API is unchanged. It gained an internal constructor that takes a skip predicate, which is how step 7 skips the overridden parameters in both phases. It also gained internal members for GetParameterValueAsync and for a resolve that does not construct interfaces. ModuleTestContainer uses these, so it resolves exactly as xUnit and NUnit do today.
  2. TestExportAttribute gained an internal Register(IServiceCollection). BuildScopeAsync has no ITestMethodContext to pass to SetupServiceCollection, so it calls Register directly.
  3. Step 6, "each method parameter marked [Shared]", means a parameter with SharedAttribute or a subclass of it. Any ISharedTestRegistration would not work here, because [Mock] and [InjectValues] also implement it with Shared = true.
  4. Step 6 reads IsolatedServices from the test's attributes and from the parent's scope attributes. The design does not say where to read them.
  5. Step 6 skips a shared service that the parent cannot produce (GetService returns null), and the child then resolves its own. A [Shared] parameter with [FromKeyedServices] is shared under its key. The design does not cover either case.
  6. The parent resolves each shared type once and caches it. A null result is cached too.
  7. Resolve(ParameterInfo) returns null for an unregistered interface, abstract class or value type. It builds only a non-abstract class, as the doc comment in Part 3 says. When such a build fails, the build's exception propagates. ResolveArgumentsAsync keeps today's behaviour, which tries to build any type.
  8. BuildAsync throws ArgumentException before it registers anything when an override's Instance is not assignable to its ServiceType. The design has no such check.
  9. DisposeAsync disposes every container even when one throws. It rethrows a single exception as it is, and throws more than one as an AggregateException. When BuildAsync fails, an exception from the cleanup disposal is dropped, so the caller gets the exception of the step that failed.
  10. ResolveArgumentsAsync and GetParameterValueAsync on a container from BuildScopeAsync throw InvalidOperationException, because there is no test method. GetParameterValueAsync throws ArgumentException for a parameter of another method.
  11. BuildAsync does not collect IDependencyModuleProvider attributes from testMethod.Attributes. The caller passes every module in modules, as Part 3 describes. Step 2 must collect them in the runners, together with the [ModuleTest] types.

Why

VerificationGlue's Testing mode (steps 5 to 7 of the design) needs one entry point that builds a DependencyModules test container from attribute instances, a MethodInfo and mock overrides, with no VerificationGlue type in it. Step 2 moves xUnit and NUnit onto the same entry point, so the three runners share one build order.

How it was tested

New tests are in tests/DependencyModules.Specs/TestingTests/ModuleTestCompositionTests.cs (VerificationGlue.Tests, per CONTRIBUTING.md). They drive ModuleTestComposition against a plain ServiceCollection and cover:

  • Order of steps. A log and the descriptor order at build time check every step: environment, module, mock support, setup, parameter, build, startup. They also check where the source, current test, environment and override registrations sit in the collection.
  • Modules. The module list is reversed before LoadModules.
  • Environment. The narrowest provider that answers decides, and modules see it. With no provider, no environment is registered.
  • Setup attributes. A [TestExport] beats a module, and mock support runs first.
  • Overrides. An override beats a [TestExport]. A keyed override registers under its key. A [Mock] parameter with an override runs no provider: without the skip, its missing mock-support attribute would throw. A keyed parameter is skipped only by an override with the same key. An override whose instance has the wrong type is rejected.
  • Current test. It is registered, CurrentTest.Provider is not changed, and nothing is registered for null.
  • Build and startup. The narrowest builder builds the container, and startup receives the built container. The ITestContainerSource builds and starts like the first container and keeps the parameters, and its containers are disposed.
  • Failures. A failed startup disposes the container. A failed disposal does not hide the startup failure. A setup failure is rethrown. DisposeAsync disposes every container when they throw (AggregateException), and one failure is rethrown as it is.
  • Resolution. Data rows fill the leading parameters. The Resolve overloads give the provider, registered, keyed and built services, and return null for the missing cases.
  • Scopes and parents. BuildScopeAsync registers only the shared exports, loads modules reversed, and has no parameters. Children share the parent's instance of a [Shared] parameter (one instance from a transient registration, not disposed by the children) and of shared exports. Keyed sharing works. Isolated services are not taken from the parent. An override beats the parent's instance. A child resolves its own copy of a service the parent does not have. TestMethodContext has argument checks.

Commands run before the commit, all passing:

  • dotnet csharpier check .
  • dotnet restore and dotnet build DependencyModules.sln --configuration Release: 0 errors.
  • ./scripts/coverage.sh 85: DependencyModules.Tests 896/896, SutProject.Tests 174/174, SutProject.NUnitTests 46/46 and WebApiApp.Tests 2/2, each on net8.0 and net10.0. DependencyModules.Specs passed 326/326. Line coverage is 92.9%. ModuleTestComposition, ModuleTestContainer and TestMethodContext are at 100%.
  • ./scripts/test-xunit.sh 4: passed.
  • ./scripts/verify-packages.sh: all package checks passed for net8.0 and net10.0.
  • ! grep -rqi verificationglue src --include='*.csproj': passed.

Not done here: the xUnit and NUnit runners are not moved onto the entry point (step 2), and DM0021 is not changed (step 3). Nothing in VerificationGlue calls the entry point yet.

🤖 Generated with Claude Code


Opened by Agent Driver for step stp-vgdm-entry-point of Task tsk-verificationglue.

ModuleTestComposition.BuildAsync builds a test's container in the steps that
ModuleTestCase and ModuleTestCommand take, plus a current-test registration,
the services shared from a parent, and overrides registered after the setup
attributes. BuildScopeAsync builds the parent of a class or an assembly.
DependencyModules.Testing now references DependencyModules.Runtime and
Microsoft.Extensions.DependencyInjection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant