Fix the problem that the test projects are not being enriched by VsTest.#2751
Open
MischaVreeburg wants to merge 1 commit intostryker-mutator:masterfrom
Open
Conversation
For the enrichment, an alternative datasource has been chosen along with some clever parsing. It now falls back for C# files to the previously scanned test source files and tries to map the test full name with the name in the file and uses that info to get the test file path, along with the method declaration.
Member
|
Thanks for this PR. This is not a review but a bunch of general comments:
Overall, I think this is a good move, but I would like feedback from the core team regarding if this is an approach they support (i.e. this simply improving and supporting it until a fix from VsTest teams is made available) |
Member
|
@dupdob sounds good to me, I have no objection and I agree with your review feedback as well. |
dupdob
requested changes
Jul 24, 2024
Member
dupdob
left a comment
There was a problem hiding this comment.
The test detection logic should be moved to a specific class, or methods.
You need to add unit tests for this
Comment on lines
+72
to
115
| //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."); | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
Could be please move this logic to a dedicated class ? at least dedicated methods?
This code needs unit tests.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
For the enrichment, an alternative datasource has been chosen along with some clever parsing. It now falls back for C# files to the previously scanned test source files and tries to map the test full name with the name in the file and uses that info to get the test file path, along with the method declaration.
This PR fixes issue: #2750