Repository navigation
fix: report generic class constraint violations as failed tests in both modes #7112
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
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
687 changes: 487 additions & 200 deletions
687
src/TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs
Large diffs are not rendered by default.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
src/TUnit.Core/Attributes/TestData/GenericConstraintViolationDataSource.cs
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,95 @@ | ||
| using System.ComponentModel; | ||
| using TUnit.Core.Exceptions; | ||
|
|
||
| namespace TUnit.Core; | ||
|
|
||
| /// <summary> | ||
| /// Stands in for a data source whose inferred generic type arguments break the test's generic | ||
| /// constraints. The source generator cannot emit a call to the test with those type arguments, so it | ||
| /// emits this data source instead. It wraps the original data source and yields one row for each of | ||
| /// its rows, and each row throws <see cref="GenericTypeResolutionException"/>, so the engine reports one | ||
| /// failed test per row with the same message that reflection mode produces, rather than the rows | ||
| /// silently disappearing. Without a wrapped data source it yields a single failing row. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This type is used by generated code and by the engine. It is not meant to be applied by users. It | ||
| /// derives from <see cref="Attribute"/> because the engine adds a test's data source to the test's attributes. | ||
| /// </remarks> | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public class GenericConstraintViolationDataSource : Attribute, IDataSourceAttribute | ||
| { | ||
| private GenericConstraintViolationDataSource(string message, IDataSourceAttribute? innerDataSource) | ||
| { | ||
| Message = message; | ||
| InnerDataSource = innerDataSource; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a data source with a single row that fails test building with <paramref name="message"/>. | ||
| /// </summary> | ||
| /// <param name="message">Describes the constraint that the inferred type argument breaks.</param> | ||
| public static GenericConstraintViolationDataSource Create(string message) | ||
| { | ||
| return new GenericConstraintViolationDataSource(message, null); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a data source that fails each row of <paramref name="innerDataSource"/> with <paramref name="message"/>. | ||
| /// </summary> | ||
| /// <param name="message">Describes the constraint that the inferred type argument breaks.</param> | ||
| /// <param name="innerDataSource">The data source whose rows inferred the constraint-breaking type arguments.</param> | ||
| /// <returns> | ||
| /// A wrapper that implements <see cref="IAccessesInstanceData"/> when <paramref name="innerDataSource"/> does, | ||
| /// so the engine still supplies the test class instance that the inner source needs to yield its rows. | ||
| /// </returns> | ||
| public static GenericConstraintViolationDataSource Create(string message, IDataSourceAttribute innerDataSource) | ||
| { | ||
| return innerDataSource is IAccessesInstanceData | ||
| ? new InstanceDataAccessing(message, innerDataSource) | ||
| : new GenericConstraintViolationDataSource(message, innerDataSource); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the message of the <see cref="GenericTypeResolutionException"/> raised for each row. | ||
| /// </summary> | ||
| public string Message { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the data source whose rows inferred the constraint-breaking type arguments, if any. | ||
| /// </summary> | ||
| public IDataSourceAttribute? InnerDataSource { get; } | ||
|
|
||
| /// <inheritdoc /> | ||
| /// <remarks>Mirrors the inner data source, so an empty source behaves as it would without the wrapper.</remarks> | ||
| public bool SkipIfEmpty { get => InnerDataSource?.SkipIfEmpty ?? false; set { } } | ||
|
|
||
| /// <inheritdoc /> | ||
| /// <remarks>Mirrors the inner data source.</remarks> | ||
| public bool DeferEnumeration { get => InnerDataSource?.DeferEnumeration ?? false; set { } } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async IAsyncEnumerable<Func<Task<object?[]?>>> GetDataRowsAsync(DataGeneratorMetadata dataGeneratorMetadata) | ||
| { | ||
| var message = Message; | ||
|
|
||
| if (InnerDataSource is null) | ||
| { | ||
| yield return () => Task.FromException<object?[]?>(new GenericTypeResolutionException(message)); | ||
| yield break; | ||
| } | ||
|
|
||
| await foreach (var rowFactory in InnerDataSource.GetDataRowsAsync(dataGeneratorMetadata)) | ||
| { | ||
| // Run the row's own factory first, as reflection mode does before it resolves the row's type | ||
| // arguments, so the factory's side effects happen and its own exceptions take precedence. | ||
| yield return async () => | ||
| { | ||
| await rowFactory(); | ||
| throw new GenericTypeResolutionException(message); | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| private sealed class InstanceDataAccessing(string message, IDataSourceAttribute innerDataSource) | ||
| : GenericConstraintViolationDataSource(message, innerDataSource), IAccessesInstanceData; | ||
| } | ||
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,178 @@ | ||
| using System.ComponentModel; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace TUnit.Core; | ||
|
|
||
| /// <summary> | ||
| /// A test method on a generic class whose class type arguments, inferred from class-level rows, | ||
| /// <see cref="GenerateGenericTestAttribute"/> or method-level rows, break the class's generic | ||
| /// constraints. A <see cref="TestEntry{T}"/> cannot be emitted for a closed type that breaks its own | ||
| /// constraints, so the source generator emits this entry against the open generic type instead. The | ||
| /// engine reports one failed test per message, with the same name and message as reflection mode. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This type is used by generated code. It is not meant to be used directly. | ||
| /// </remarks> | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public sealed class GenericConstraintViolationEntry | ||
| { | ||
| /// <summary>Test method name.</summary> | ||
| public required string MethodName { get; init; } | ||
|
|
||
| /// <summary>Source file path.</summary> | ||
| public required string FilePath { get; init; } | ||
|
|
||
| /// <summary>Source start line number.</summary> | ||
| public required int LineNumber { get; init; } | ||
|
|
||
| /// <summary>Method metadata, declared on the open generic type.</summary> | ||
| public required MethodMetadata MethodMetadata { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// One message per invalid row or type, in the order reflection mode discovers them: | ||
| /// class-level <see cref="GenerateGenericTestAttribute"/> types, then class rows, then method rows. | ||
| /// </summary> | ||
| public required string[] Messages { get; init; } | ||
|
|
||
| internal TestMetadata ToTestMetadata([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)] Type genericTypeDefinition, string testSessionId) | ||
| { | ||
| var metadata = GenericConstraintViolationTestMetadata.Create( | ||
| genericTypeDefinition, | ||
| MethodName, | ||
| MethodMetadata, | ||
| FilePath, | ||
| LineNumber, | ||
| Messages); | ||
| metadata.TestSessionId = testSessionId; | ||
| return metadata; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test metadata that only reports generic constraint violations. Its method data sources are | ||
| /// <see cref="GenericConstraintViolationDataSource"/> instances, so each of their rows becomes one | ||
| /// failed test through the engine's data-generation-error path. Both engine modes build failures | ||
| /// for constraint-violating class type arguments through this type, so the results match. | ||
| /// </summary> | ||
| internal sealed class GenericConstraintViolationTestMetadata : TestMetadata | ||
| { | ||
| public static GenericConstraintViolationTestMetadata Create( | ||
| [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)] Type testClassType, | ||
| string methodName, | ||
| MethodMetadata methodMetadata, | ||
| string filePath, | ||
| int lineNumber, | ||
| IReadOnlyList<string> messages) | ||
| { | ||
| var dataSources = new IDataSourceAttribute[messages.Count]; | ||
| for (var i = 0; i < dataSources.Length; i++) | ||
| { | ||
| dataSources[i] = GenericConstraintViolationDataSource.Create(messages[i]); | ||
| } | ||
|
|
||
| return new GenericConstraintViolationTestMetadata | ||
| { | ||
| TestName = methodName, | ||
| TestClassType = testClassType, | ||
| TestMethodName = methodName, | ||
| FilePath = filePath, | ||
| LineNumber = lineNumber, | ||
| MethodMetadata = methodMetadata, | ||
| AttributeFactory = static () => [], | ||
| DataSources = dataSources, | ||
| ClassDataSources = [], | ||
| PropertyDataSources = [], | ||
| }; | ||
| } | ||
|
|
||
| // Every row of the data sources fails test building, so an executable test is never created. | ||
| public override Func<ExecutableTestCreationContext, TestMetadata, AbstractExecutableTest> CreateExecutableTestFactory | ||
| => static (_, metadata) => throw new InvalidOperationException( | ||
| $"Test '{metadata.TestName}' on '{metadata.TestClassType.Name}' only reports generic constraint violations and cannot be executed."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Exposes the <see cref="GenericConstraintViolationEntry"/> instances registered for one open | ||
| /// generic type to the engine, alongside the regular <see cref="TestEntrySource{T}"/> sources. | ||
| /// </summary> | ||
| internal sealed class GenericConstraintViolationEntrySource : ITestEntrySource | ||
| { | ||
| [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)] | ||
| private readonly Type _genericTypeDefinition; | ||
| private readonly object _lock = new(); | ||
| private List<Func<GenericConstraintViolationEntry[]>>? _factories; | ||
| private volatile GenericConstraintViolationEntry[]? _entries; | ||
| private string? _className; | ||
|
|
||
| public GenericConstraintViolationEntrySource([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicMethods)] Type genericTypeDefinition, Func<GenericConstraintViolationEntry[]> factory) | ||
| { | ||
| _genericTypeDefinition = genericTypeDefinition; | ||
| _factories = [factory]; | ||
| } | ||
|
|
||
| internal void AddFactory(Func<GenericConstraintViolationEntry[]> factory) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| if (_entries is not null) | ||
| { | ||
| _entries = [.. _entries, .. factory()]; | ||
| return; | ||
| } | ||
|
|
||
| _factories!.Add(factory); | ||
| } | ||
| } | ||
|
|
||
| private GenericConstraintViolationEntry[] Resolve() | ||
| { | ||
| if (_entries is not null) | ||
| { | ||
| return _entries; | ||
| } | ||
|
|
||
| lock (_lock) | ||
| { | ||
| if (_entries is not null) | ||
| { | ||
| return _entries; | ||
| } | ||
|
|
||
| var entries = new List<GenericConstraintViolationEntry>(); | ||
| foreach (var factory in _factories!) | ||
| { | ||
| entries.AddRange(factory()); | ||
| } | ||
|
|
||
| _entries = entries.ToArray(); | ||
| _factories = null; | ||
| return _entries; | ||
| } | ||
| } | ||
|
|
||
| public int Count => Resolve().Length; | ||
|
|
||
| public Type ClassType => _genericTypeDefinition; | ||
|
|
||
| public string ClassName => _className ??= Extensions.TestContextExtensions.GetNestedTypeName(_genericTypeDefinition); | ||
|
|
||
| public TestEntryFilterData GetFilterData(int index) | ||
| { | ||
| var entry = Resolve()[index]; | ||
| return new TestEntryFilterData | ||
| { | ||
| MethodName = entry.MethodName, | ||
| ClassName = ClassName, | ||
| Categories = [], | ||
| Properties = [], | ||
| DependsOn = [], | ||
| HasDataSource = true, | ||
| RepeatCount = 0, | ||
|
thomhurst marked this conversation as resolved.
Resolved
|
||
| }; | ||
| } | ||
|
|
||
| public IReadOnlyList<TestMetadata> Materialize(int index, string testSessionId) | ||
| { | ||
| return [Resolve()[index].ToTestMetadata(_genericTypeDefinition, testSessionId)]; | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.