Skip to content

Commit 9cb5021

Browse files
Copilotthomhurstclaude[bot]
authored
Fix: Allow compiler-generated types with test methods in reflection mode (#2909)
* Initial plan * Fix: Allow compiler-generated types with test methods in reflection mode Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com> * Fix GitVersion build failure by ignoring problematic merge commit The GitVersion tool was failing with a NotImplementedException when processing the merge commit 02e60f4. This commit contains a complex merge structure that triggers an unimplemented code path in GitVersion's MainlineVersionStrategy. Adding the commit hash to the ignore list allows the build to proceed while maintaining proper version calculation for the rest of the git history. Co-authored-by: Tom Longhurst <thomhurst@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com> Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Tom Longhurst <thomhurst@users.noreply.github.com>
1 parent eff80f2 commit 9cb5021

4 files changed

Lines changed: 96 additions & 2 deletions

File tree

GitVersion.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ ignore:
77
- 9b0b1c0d80692dba7584905554cfb43c92e29782
88
- 7e0f59874705082e81a0d614abea2709a9f6a98e
99
- 0f32f64e21316fc908af0721864ab3dd1fcebb4b
10-
- cc09c9b221e35d842bea687c96a1594a574b1fac
10+
- cc09c9b221e35d842bea687c96a1594a574b1fac
11+
- 02e60f4e1d7edc5e4e4ab51a5ac8732f3c94c8b5

TUnit.Engine/Discovery/ReflectionTestDataCollector.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,36 @@ private static bool IsAsyncMethod(MethodInfo method)
10051005

10061006
private static bool IsCompilerGenerated(Type type)
10071007
{
1008-
return type.IsDefined(typeof(CompilerGeneratedAttribute), inherit: false);
1008+
// If the type is not marked as compiler-generated, it's not compiler-generated
1009+
if (!type.IsDefined(typeof(CompilerGeneratedAttribute), inherit: false))
1010+
{
1011+
return false;
1012+
}
1013+
1014+
// If the type is compiler-generated but contains test methods, allow it
1015+
// This handles cases like Reqnroll-generated test classes that should be executed
1016+
return !HasTestMethods(type);
1017+
}
1018+
1019+
private static bool HasTestMethods(Type type)
1020+
{
1021+
try
1022+
{
1023+
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);
1024+
foreach (var method in methods)
1025+
{
1026+
if (method.IsDefined(typeof(TestAttribute), inherit: false))
1027+
{
1028+
return true;
1029+
}
1030+
}
1031+
return false;
1032+
}
1033+
catch
1034+
{
1035+
// If we can't access the methods, treat it as not having test methods
1036+
return false;
1037+
}
10091038
}
10101039

10111040
private static ParameterInfo[] GetParametersWithoutCancellationToken(MethodInfo method)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Imports System
2+
Imports System.Runtime.CompilerServices
3+
Imports TUnit.Core
4+
5+
' This simulates a Reqnroll-generated test class with CompilerGeneratedAttribute
6+
<CompilerGenerated>
7+
Public Class CompilerGeneratedTest
8+
<Test>
9+
Public Sub GeneratedTestMethod()
10+
' This test should be executed even though the class is marked as CompilerGenerated
11+
Console.WriteLine("Generated test executed successfully")
12+
End Sub
13+
14+
<Test>
15+
<Arguments(1, 2, 3)>
16+
<Arguments(2, 3, 5)>
17+
Public Sub GeneratedTestWithArguments(a As Integer, b As Integer, expected As Integer)
18+
Dim result = a + b
19+
If result <> expected Then
20+
Throw New Exception($"Expected {expected}, but got {result}")
21+
End If
22+
Console.WriteLine($"Generated test with arguments: {a} + {b} = {result}")
23+
End Sub
24+
End Class
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using TUnit.Core;
4+
5+
namespace TUnit.TestProject;
6+
7+
// This simulates a Reqnroll-generated test class with CompilerGeneratedAttribute
8+
[CompilerGenerated]
9+
public class CompilerGeneratedTests
10+
{
11+
[Test]
12+
public void GeneratedTestMethod()
13+
{
14+
// This test should be executed even though the class is marked as CompilerGenerated
15+
Console.WriteLine("Generated test executed successfully");
16+
}
17+
18+
[Test]
19+
[Arguments(1, 2, 3)]
20+
[Arguments(2, 3, 5)]
21+
public void GeneratedTestWithArguments(int a, int b, int expected)
22+
{
23+
var result = a + b;
24+
if (result != expected)
25+
{
26+
throw new Exception($"Expected {expected}, but got {result}");
27+
}
28+
Console.WriteLine($"Generated test with arguments: {a} + {b} = {result}");
29+
}
30+
}
31+
32+
// Test that types without test methods are still filtered out
33+
[CompilerGenerated]
34+
public class CompilerGeneratedNonTestClass
35+
{
36+
public void NotATestMethod()
37+
{
38+
// This class should still be filtered out as it has no test methods
39+
}
40+
}

0 commit comments

Comments
 (0)