Skip to content

Commit 225d19b

Browse files
authored
Allow overriding test source location (#3331)
1 parent 1528016 commit 225d19b

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,18 @@ private static (string filePath, int lineNumber) GetTestMethodSourceLocation(
23002300
MethodDeclarationSyntax methodSyntax,
23012301
AttributeData testAttribute)
23022302
{
2303+
// Prioritize TestAttribute's File/Line from [CallerFilePath]/[CallerLineNumber] first
2304+
var attrFilePath = testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString();
2305+
if (!string.IsNullOrEmpty(attrFilePath))
2306+
{
2307+
var attrLineNumber = (int?)testAttribute.ConstructorArguments.ElementAtOrDefault(1).Value ?? 0;
2308+
if (attrLineNumber > 0)
2309+
{
2310+
return (attrFilePath!, attrLineNumber);
2311+
}
2312+
}
2313+
2314+
// Fall back to method syntax location
23032315
var methodLocation = methodSyntax.GetLocation();
23042316
var filePath = methodLocation.SourceTree?.FilePath;
23052317
if (!string.IsNullOrEmpty(filePath))
@@ -2308,14 +2320,7 @@ private static (string filePath, int lineNumber) GetTestMethodSourceLocation(
23082320
return (filePath!, lineNumber);
23092321
}
23102322

2311-
var attrFilePath = testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString();
2312-
if (!string.IsNullOrEmpty(attrFilePath))
2313-
{
2314-
var attrLineNumber = (int?)testAttribute.ConstructorArguments.ElementAtOrDefault(1).Value ??
2315-
methodLocation.GetLineSpan().StartLinePosition.Line + 1;
2316-
return (attrFilePath!, attrLineNumber);
2317-
}
2318-
2323+
// Final fallback
23192324
filePath = methodSyntax.SyntaxTree.FilePath ?? "";
23202325
var fallbackLineNumber = methodLocation.GetLineSpan().StartLinePosition.Line + 1;
23212326
return (filePath, fallbackLineNumber);
@@ -2326,6 +2331,18 @@ private static (string filePath, int lineNumber) GetTestMethodSourceLocation(
23262331
AttributeData testAttribute,
23272332
InheritsTestsClassMetadata classInfo)
23282333
{
2334+
// Prioritize TestAttribute's File/Line from [CallerFilePath]/[CallerLineNumber] first
2335+
var attrFilePath = testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString();
2336+
if (!string.IsNullOrEmpty(attrFilePath))
2337+
{
2338+
var attrLineNumber = (int?)testAttribute.ConstructorArguments.ElementAtOrDefault(1).Value ?? 0;
2339+
if (attrLineNumber > 0)
2340+
{
2341+
return (attrFilePath!, attrLineNumber);
2342+
}
2343+
}
2344+
2345+
// Fall back to method symbol location
23292346
var methodLocation = method.Locations.FirstOrDefault();
23302347
if (methodLocation != null && methodLocation.IsInSource)
23312348
{
@@ -2337,14 +2354,7 @@ private static (string filePath, int lineNumber) GetTestMethodSourceLocation(
23372354
}
23382355
}
23392356

2340-
var attrFilePath = testAttribute.ConstructorArguments.ElementAtOrDefault(0).Value?.ToString();
2341-
if (!string.IsNullOrEmpty(attrFilePath))
2342-
{
2343-
var attrLineNumber = (int?)testAttribute.ConstructorArguments.ElementAtOrDefault(1).Value ??
2344-
classInfo.ClassSyntax.GetLocation().GetLineSpan().StartLinePosition.Line + 1;
2345-
return (attrFilePath!, attrLineNumber);
2346-
}
2347-
2357+
// Final fallback to class location
23482358
var classLocation = classInfo.ClassSyntax.GetLocation();
23492359
var derivedFilePath = classLocation.SourceTree?.FilePath ?? classInfo.ClassSyntax.SyntaxTree.FilePath ?? "";
23502360
var derivedLineNumber = classLocation.GetLineSpan().StartLinePosition.Line + 1;

0 commit comments

Comments
 (0)