Skip to content

Commit 7547296

Browse files
committed
Refactor mergeTestResultsToExplorer for smoother use with LSP test results
The dotnet cli approach fundamentally expected tests to be run and merged by project, but VSTest doesn't. MergeTestResultsToExplorer really only needed the project and target framework for creating new items. By adding project and target framework to the each test result, we can merge test items without expecting them to be batched by project
1 parent 9ec3d11 commit 7547296

File tree

1 file changed

+48
-59
lines changed

1 file changed

+48
-59
lines changed

src/Components/TestExplorer.fs

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ type TestResult =
304304
Expected: string option
305305
Actual: string option
306306
Timing: float
307-
TestFramework: TestFrameworkId option }
307+
TestFramework: TestFrameworkId option
308+
ProjectFilePath: ProjectFilePath
309+
TargetFramework: TargetFramework }
308310

309311
module TestResult =
310312
let tryExtractExpectedAndActual (message: string option) =
@@ -324,7 +326,7 @@ module TestResult =
324326

325327
expected, actual
326328

327-
let ofTestResultDTO (testResultDto: TestResultDTO) =
329+
let ofTestResultDTO (testResultDto: TestResultDTO) : TestResult =
328330
let expected, actual = tryExtractExpectedAndActual testResultDto.ErrorMessage
329331

330332
{ FullTestName = testResultDto.TestItem |> TestItemDTO.getNormalizedFullName
@@ -335,7 +337,9 @@ module TestResult =
335337
Timing = testResultDto.Duration.Milliseconds
336338
TestFramework = testResultDto.TestItem.ExecutorUri |> TestFrameworkId.tryFromExecutorUri
337339
Expected = expected
338-
Actual = actual }
340+
Actual = actual
341+
ProjectFilePath = testResultDto.TestItem.ProjectFilePath
342+
TargetFramework = testResultDto.TestItem.TargetFramework }
339343

340344
module Path =
341345

@@ -1487,8 +1491,6 @@ module Interactions =
14871491
(testItemFactory: TestItem.TestItemFactory)
14881492
(tryGetLocation: TestId -> LocationRecord option)
14891493
(testRun: TestRun)
1490-
(projectPath: ProjectPath)
1491-
(targetFramework: TargetFramework)
14921494
(shouldDeleteMissing: bool)
14931495
(expectedToRun: TestItem array)
14941496
(testResults: TestResult array)
@@ -1503,18 +1505,19 @@ module Interactions =
15031505
parentCollection.delete testWithoutResult.id
15041506

15051507

1506-
let getOrMakeHierarchyPath testFramework =
1508+
let getOrMakeHierarchyPath (testResult: TestResult) =
15071509
let testItemFactory (ti: TestItem.TestItemBuilder) =
15081510
testItemFactory
15091511
{ ti with
1510-
testFramework = testFramework }
1512+
testFramework = testResult.TestFramework }
15111513

15121514
TestItem.getOrMakeHierarchyPath
15131515
rootTestCollection
15141516
testItemFactory
15151517
tryGetLocation
1516-
projectPath
1517-
targetFramework
1518+
testResult.ProjectFilePath
1519+
testResult.TargetFramework
1520+
testResult.FullTestName
15181521

15191522
let treeItemComparable (t: TestItem) = TestItem.getFullName t.id
15201523
let resultComparable (r: TestResult) = r.FullTestName
@@ -1529,12 +1532,15 @@ module Interactions =
15291532

15301533
added
15311534
|> Array.iter (fun additionalResult ->
1532-
let treeItem =
1533-
getOrMakeHierarchyPath additionalResult.TestFramework additionalResult.FullTestName
1535+
let treeItem = getOrMakeHierarchyPath additionalResult
15341536

15351537
displayTestResultInExplorer testRun (treeItem, additionalResult))
15361538

1537-
let private trxResultToTestResult (trxResult: TrxParser.TestWithResult) =
1539+
let private trxResultToTestResult
1540+
(projectFilePath: ProjectFilePath)
1541+
(targetFramework: TargetFramework)
1542+
(trxResult: TrxParser.TestWithResult)
1543+
=
15381544

15391545
let expected, actual =
15401546
TestResult.tryExtractExpectedAndActual trxResult.UnitTestResult.Output.ErrorInfo.Message
@@ -1547,16 +1553,17 @@ module Interactions =
15471553
Expected = expected
15481554
Actual = actual
15491555
Timing = trxResult.UnitTestResult.Duration.Milliseconds
1550-
TestFramework = TestFrameworkId.tryFromExecutorUri trxResult.UnitTest.TestMethod.AdapterTypeName }
1556+
TestFramework = TestFrameworkId.tryFromExecutorUri trxResult.UnitTest.TestMethod.AdapterTypeName
1557+
ProjectFilePath = projectFilePath
1558+
TargetFramework = targetFramework }
15511559

15521560
type TrimMissing = bool
15531561

15541562
module TrimMissing =
15551563
let Trim = true
15561564
let NoTrim = false
15571565

1558-
type MergeTestResultsToExplorer =
1559-
TestRun -> ProjectPath -> TargetFramework -> TrimMissing -> TestItem array -> TestResult array -> unit
1566+
type MergeTestResultsToExplorer = TestRun -> TrimMissing -> TestItem array -> TestResult array -> unit
15601567

15611568
let private runTestProject_withoutExceptionHandling
15621569
(mergeResultsToExplorer: MergeTestResultsToExplorer)
@@ -1597,7 +1604,8 @@ module Interactions =
15971604
TestRun.Output.appendLine testRun output
15981605

15991606
let testResults =
1600-
TrxParser.extractTrxResults trxPath |> Array.map trxResultToTestResult
1607+
TrxParser.extractTrxResults trxPath
1608+
|> Array.map (trxResultToTestResult projectPath projectRunRequest.TargetFramework)
16011609

16021610
if Array.isEmpty testResults then
16031611
let message =
@@ -1606,13 +1614,7 @@ module Interactions =
16061614
window.showWarningMessage (message) |> ignore
16071615
TestRun.Output.appendWarningLine testRun message
16081616
else
1609-
mergeResultsToExplorer
1610-
testRun
1611-
projectPath
1612-
projectRunRequest.TargetFramework
1613-
TrimMissing.Trim
1614-
runnableTests
1615-
testResults
1617+
mergeResultsToExplorer testRun TrimMissing.Trim runnableTests testResults
16161618
}
16171619

16181620
let runTestProject
@@ -1776,44 +1778,25 @@ module Interactions =
17761778

17771779
let private runTests_WithLanguageServer
17781780
mergeTestResultsToExplorer
1781+
(rootTestCollection: TestItemCollection)
17791782
(req: TestRunRequest)
17801783
testRun
1781-
projectRunRequests
17821784
=
17831785
promise {
17841786
try
1785-
let runnableTestsByProject =
1786-
projectRunRequests
1787-
|> Array.map (fun rr -> rr.ProjectPath, rr.Tests |> Array.collect TestItem.runnableChildren)
1788-
|> Map
1787+
let expectedToRun =
1788+
req.``include``
1789+
|> Option.map Array.ofSeq
1790+
|> Option.defaultValue (rootTestCollection.TestItems())
1791+
|> Array.collect TestItem.runnableChildren
17891792

1790-
let expectedTestsById =
1791-
runnableTestsByProject
1792-
|> Map.values
1793-
|> Seq.collect (Seq.map (fun t -> t.id, t))
1794-
|> Map
1793+
let expectedTestsById = expectedToRun |> Array.map (fun t -> t.id, t) |> Map
17951794

17961795
let mergeResults (shouldTrim: TrimMissing) (resultDtos: TestResultDTO array) =
1797-
let groups =
1798-
resultDtos
1799-
|> Array.groupBy (fun tr -> tr.TestItem.ProjectFilePath, tr.TestItem.TargetFramework)
1800-
1801-
groups
1802-
|> Array.iter (fun ((projPath, targetFramework), results) ->
1803-
let expectedToRun =
1804-
runnableTestsByProject
1805-
|> Map.tryFind projPath
1806-
|> Option.defaultValue Array.empty
1807-
1808-
let actuallyRan: TestResult array = results |> Array.map TestResult.ofTestResultDTO
1809-
1810-
mergeTestResultsToExplorer
1811-
testRun
1812-
projPath
1813-
targetFramework
1814-
shouldTrim
1815-
expectedToRun
1816-
actuallyRan)
1796+
let actuallyRan: TestResult array =
1797+
resultDtos |> Array.map TestResult.ofTestResultDTO
1798+
1799+
mergeTestResultsToExplorer testRun shouldTrim expectedToRun actuallyRan
18171800

18181801
let showStarted (testItems: TestItemDTO array) =
18191802
try
@@ -1875,12 +1858,19 @@ module Interactions =
18751858
| Some selectedCases when Seq.isEmpty selectedCases -> None, None
18761859
| Some selectedCases ->
18771860
let filter =
1878-
projectRunRequests
1879-
|> Array.collect (fun rr -> rr.Tests)
1861+
selectedCases
1862+
|> Array.ofSeq
1863+
|> Array.filter (fun t -> t.id |> TestItem.getFullName <> String.Empty)
18801864
|> buildFilterExpression
18811865
|> Some
18821866

1883-
let projectSubset = projectRunRequests |> Array.map (fun p -> p.ProjectPath) |> Some
1867+
let projectSubset =
1868+
selectedCases
1869+
|> Seq.map (TestItem.getId >> TestItem.getProjectPath)
1870+
|> Seq.distinct
1871+
|> Array.ofSeq
1872+
|> Some
1873+
18841874
filter, projectSubset
18851875

18861876
logger.Debug($"Test Filter Expression: {filterExpression}")
@@ -1923,7 +1913,6 @@ module Interactions =
19231913
if testController.items.size < 1. then
19241914
!! testRun.``end`` ()
19251915
else
1926-
let projectRunRequests = filtersToProjectRunRequests testController.items req
19271916

19281917
let testItemFactory = TestItem.itemFactoryForController testController
19291918

@@ -1933,7 +1922,6 @@ module Interactions =
19331922
let runTestProject =
19341923
runTestProject mergeTestResultsToExplorer makeTrxPath testRun _ct
19351924

1936-
19371925
let buildProject testRun projectRunRequest =
19381926
promise {
19391927

@@ -1952,6 +1940,7 @@ module Interactions =
19521940
}
19531941

19541942
promise {
1943+
let projectRunRequests = filtersToProjectRunRequests testController.items req
19551944

19561945
projectRunRequests
19571946
|> Array.collect (fun rr -> rr.Tests |> TestItem.runnableFromArray)
@@ -1971,7 +1960,7 @@ module Interactions =
19711960

19721961
()
19731962
else
1974-
do! runTests_WithLanguageServer mergeTestResultsToExplorer req testRun projectRunRequests
1963+
do! runTests_WithLanguageServer mergeTestResultsToExplorer testController.items req testRun
19751964

19761965
testRun.``end`` ()
19771966
}

0 commit comments

Comments
 (0)