Skip to content
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

Fix the problem that the test projects are not being enriched by VsTest. #2751

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions src/Stryker.Core/Stryker.Core/Initialisation/ProjectMutator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -30,7 +31,7 @@ public IMutationTestProcess MutateProject(StrykerOptions options, MutationTestIn
{
var process = _injectedMutationTestProcess ?? new MutationTestProcess(input, options, reporters,
new MutationTestExecutor(input.TestRunner));

// Enrich test projects info with unit tests
EnrichTestProjectsWithTestInfo(input.InitialTestRun, input.TestProjectsInfo);

Expand All @@ -46,7 +47,15 @@ private void EnrichTestProjectsWithTestInfo(InitialTestRun initialTestRun, TestP
initialTestRun.Result.VsTestDescriptions
.Select(desc => desc.Case)
// F# has a different syntax tree and would throw further down the line
.Where(unitTest => Path.GetExtension(unitTest.CodeFilePath) == ".cs");
.Where(unitTest => Path.GetExtension(unitTest.CodeFilePath) == ".cs").ToList();

if (!unitTests.Any())
{
unitTests = initialTestRun.Result.VsTestDescriptions
.Select(desc => desc.Case)
// F# has a different syntax tree and would throw further down the line
.Where(unitTest => Path.GetExtension(unitTest.CodeFilePath) != ".fs").ToList();
}

foreach (var unitTest in unitTests)
{
Expand All @@ -60,7 +69,49 @@ private void EnrichTestProjectsWithTestInfo(InitialTestRun initialTestRun, TestP
}
else
{
_logger.LogDebug("Could not locate unit test in any testfile. This should not happen and results in incorrect test reporting.");
//Test if you can find the file by scanning the sources for testcase name
var qualifiedNameArray = unitTest.FullyQualifiedName.Split('.');
var methodName = qualifiedNameArray[^1];
var className = qualifiedNameArray[^2];
var nameSpace1 = new ArraySegment<string>(qualifiedNameArray, 0, qualifiedNameArray.Length - 2);
var nameSpace = $"namespace {string.Join('.', nameSpace1)}";

testFile = testProjectsInfo.TestFiles.Where(tFile => !tFile.FilePath.EndsWith("GlobalSuppressions.cs")).SingleOrDefault(tFile =>
tFile.Source.Contains(className) && tFile.Source.Contains(methodName) && tFile.Source.Contains(nameSpace));
if (testFile is not null)
{
var testDescriptions =
initialTestRun.Result.VsTestDescriptions.Where(td => td.Description.Name == unitTest.FullyQualifiedName);
foreach (var testDescription in testDescriptions)
{
testDescription.Description.TestFilePath = testFile.FilePath;
}

var lineNumber = unitTest.LineNumber;
if (lineNumber < 1)
{
var lines = testFile.Source.Split("\r\n");
foreach (var line in lines)
{
if (line.Contains(methodName) && !line.Contains("class"))
{
lineNumber = Array.IndexOf(testFile.Source.Split("\r\n"),
testFile.Source.Split("\r\n").First(sourceLine => sourceLine.Contains(methodName) && !sourceLine.Contains("class")));
break;
}
}
}

var lineSpan = testFile.SyntaxTree.GetText().Lines[lineNumber].Span;
var nodesInSpan = testFile.SyntaxTree.GetRoot().DescendantNodes(lineSpan);
var node = nodesInSpan.First(n => n is MethodDeclarationSyntax);
testFile.AddTest(unitTest.Id, unitTest.FullyQualifiedName, node);
}
else
{
_logger.LogDebug(
"Could not locate unit test in any testfile. This should not happen and results in incorrect test reporting.");
}
}
Comment on lines +72 to 115
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be please move this logic to a dedicated class ? at least dedicated methods?

This code needs unit tests.

}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Stryker.Core/Stryker.Core/Mutants/TestDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public TestDescription(Guid id, string name, string testFilePath)

public string Name { get; }

public string TestFilePath { get; }
public string TestFilePath { get; set; }

private bool Equals(TestDescription other)
{
Expand Down
Loading