Skip to content

Commit

Permalink
#2845: Inherited [Collection] attribute not considered for xUnit1041
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Dec 9, 2023
1 parent 488306e commit 14788b8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ [Fact] public void TestMethod() {{ }}
await Verify.VerifyAnalyzer(source);
}

[Fact]
public async void WithInheritedFixture_DoesNotTrigger()
{
var source = @"
using Xunit;
public class Fixture { }
[CollectionDefinition(""test"")]
public class TestCollection : ICollectionFixture<Fixture> { }
[Collection(""test"")]
public abstract class TestContext {
protected TestContext(Fixture fixture) { }
}
public class TestClass : TestContext {
public TestClass(Fixture fixture) : base(fixture) { }
[Fact]
public void TestMethod() { }
}";

await Verify.VerifyAnalyzer(source);
}

[Theory]
[InlineData("[Collection(nameof(TestCollection))]", "")]
[InlineData("", "[Collection(nameof(TestCollection))]")]
Expand Down
13 changes: 8 additions & 5 deletions src/xunit.analyzers/X1000/EnsureFixturesHaveASource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ public override void AnalyzeCompilation(

// Get the collection name from [Collection], if present
var collectionAttributeType = xunitContext.Core.CollectionAttributeType;
var collectionDefinitionName =
namedType
.GetAttributes()
.FirstOrDefault(a => a.AttributeClass.IsAssignableFrom(collectionAttributeType))
?.ConstructorArguments.FirstOrDefault().Value?.ToString();
string? collectionDefinitionName = null;

for (var type = namedType; type is not null && collectionDefinitionName is null; type = type.BaseType)
collectionDefinitionName =
type
.GetAttributes()
.FirstOrDefault(a => a.AttributeClass.IsAssignableFrom(collectionAttributeType))
?.ConstructorArguments.FirstOrDefault().Value?.ToString();

// Need to construct a full set of types we know can be resolved. Start with things
// like ITestOutputHelper and ITestContextAccessor (since they're injected by the framework)
Expand Down

0 comments on commit 14788b8

Please sign in to comment.