1
1
using BaseProtocol ;
2
2
using bsp4csharp . Protocol ;
3
- using Microsoft . Build . Construction ;
4
3
using Microsoft . Build . Evaluation ;
5
4
using dotnet_bsp . Logging ;
6
5
using Microsoft . TestPlatform . VsTestConsole . TranslationLayer ;
7
6
using Microsoft . TestPlatform . VsTestConsole . TranslationLayer . Interfaces ;
8
7
using TestResult = bsp4csharp . Protocol . TestResult ;
9
- using Microsoft . VisualStudio . TestPlatform . ObjectModel . Client ;
10
8
using Newtonsoft . Json . Linq ;
11
9
using dotnet_bsp . EventHandlers ;
12
10
using System . Text . RegularExpressions ;
11
+ using Microsoft . Build . Graph ;
12
+ using Microsoft . Build . Execution ;
13
13
14
14
namespace dotnet_bsp . Handlers ;
15
15
@@ -39,20 +39,13 @@ public Task<TestResult> HandleRequestAsync(TestParams testParams, RequestContext
39
39
40
40
if ( initParams . RootUri . IsFile )
41
41
{
42
- var projectTargets = new Dictionary < string , JObject ? > ( ) ;
43
-
44
- var testTargetFiles = BuildHelper
45
- . FilterProjectsOutIfPartOfAnSolutionTarget ( testParams . Targets )
46
- . Select ( x => x . ToString ( ) ) ;
42
+ var workspacePath = initParams . RootUri . LocalPath ;
43
+ context . Logger . LogInformation ( "Get loaded test projects from {}" , workspacePath ) ;
47
44
45
+ var testTargetFiles = BuildHelper . ExtractProjectsFromSolutions ( testParams . Targets ) ;
48
46
var projects = new ProjectCollection ( ) ;
49
- foreach ( var target in testTargetFiles )
50
- {
51
- HandleProject ( testParams , projects , target , projectTargets ) ;
52
- }
53
47
54
- var workspacePath = initParams . RootUri . LocalPath ;
55
- context . Logger . LogInformation ( "Get loaded test projects from {}" , workspacePath ) ;
48
+ context . Logger . LogInformation ( "Projects in use {}" , string . Join ( "," , testTargetFiles ) ) ;
56
49
57
50
_baseProtocolClientManager . SendClearDiagnosticsMessage ( ) ;
58
51
@@ -75,16 +68,26 @@ public Task<TestResult> HandleRequestAsync(TestParams testParams, RequestContext
75
68
76
69
if ( testResult )
77
70
{
78
- foreach ( var projectTarget in projectTargets )
71
+ JObject ? testParamsData = null ;
72
+ if ( testParams . DataKind == TestParamsDataKinds . DotnetTest &&
73
+ testParams . Data is JObject data )
74
+ {
75
+ testParamsData = data ;
76
+ }
77
+
78
+ var graph = new ProjectGraph ( testTargetFiles , projects ) ;
79
+ var testProjects = graph . ProjectNodesTopologicallySorted
80
+ . Where ( x => x . ProjectInstance . IsTestProject ( ) ) ;
81
+ foreach ( var testProject in testProjects )
79
82
{
80
- var proj = projects . LoadProject ( projectTarget . Key ) ;
81
83
msBuildLogger = new MSBuildLogger ( _baseProtocolClientManager , testParams . OriginId , workspacePath ) ;
82
84
85
+ var proj = testProject . ProjectInstance ;
83
86
context . Logger . LogInformation ( "Start test target: {}" , proj . ProjectFileLocation ) ;
84
87
var targetPath = proj . Properties . First ( x => x . Name == "TargetPath" ) . EvaluatedValue ;
85
88
context . Logger . LogInformation ( "targetPath: {}" , targetPath ) ;
86
89
87
- var dotnetTestParamsData = projectTarget . Value ? . ToObject < DotnetTestParamsData > ( ) ;
90
+ var dotnetTestParamsData = testParamsData ? . ToObject < DotnetTestParamsData > ( ) ;
88
91
if ( dotnetTestParamsData is not null )
89
92
{
90
93
RunAllTests ( proj , [ targetPath ] , testParams . OriginId , dotnetTestParamsData . RunSettings , dotnetTestParamsData . Filter , context , msBuildLogger ) ;
@@ -104,10 +107,9 @@ public Task<TestResult> HandleRequestAsync(TestParams testParams, RequestContext
104
107
} ) ;
105
108
}
106
109
107
- private void HandleProject ( TestParams testParams , ProjectCollection projects , string projectFile , Dictionary < string , JObject ? > projectTargets )
110
+ private void HandleTestProject ( TestParams testParams , ProjectInstance project , Dictionary < string , JObject ? > projectTargets )
108
111
{
109
- var proj = projects . LoadProject ( projectFile ) ;
110
- if ( ! projectTargets . ContainsKey ( proj . FullPath ) )
112
+ if ( project . IsTestProject ( ) && ! projectTargets . ContainsKey ( project . FullPath ) )
111
113
{
112
114
JObject ? testData = null ;
113
115
if ( testParams . DataKind == TestParamsDataKinds . DotnetTest &&
@@ -116,11 +118,11 @@ private void HandleProject(TestParams testParams, ProjectCollection projects, st
116
118
testData = data ;
117
119
}
118
120
119
- projectTargets [ proj . FullPath ] = testData ;
121
+ projectTargets [ project . FullPath ] = testData ;
120
122
}
121
123
}
122
124
123
- private void RunAllTests ( Project proj , IEnumerable < string > targets , string ? originId , string ? testRunSettings , string testCaseFilter , RequestContext context , MSBuildLogger msBuildLogger )
125
+ private void RunAllTests ( ProjectInstance proj , IEnumerable < string > targets , string ? originId , string ? testRunSettings , string testCaseFilter , RequestContext context , MSBuildLogger msBuildLogger )
124
126
{
125
127
var outputPath = proj . Properties . First ( x => x . Name == "OutputPath" ) . EvaluatedValue ;
126
128
context . Logger . LogInformation ( "outputPath: {}" , outputPath ) ;
0 commit comments