1
1
namespace Microsoft . ComponentDetection . Detectors . Tests ;
2
2
3
+ using System ;
3
4
using System . Collections . Generic ;
4
5
using System . IO ;
5
6
using System . Linq ;
@@ -43,6 +44,8 @@ public class DotNetComponentDetectorTests : BaseDetectorTest<DotNetComponentDete
43
44
// uses GetParentDirectory, NormalizePath
44
45
private readonly Mock < IPathUtilityService > mockPathUtilityService = new ( ) ;
45
46
47
+ private Func < string , DirectoryInfo , CommandLineExecutionResult > commandLineCallback ;
48
+
46
49
/// <summary>
47
50
/// Initializes a new instance of the <see cref="DotNetComponentDetectorTests"/> class.
48
51
/// </summary>
@@ -64,7 +67,8 @@ public DotNetComponentDetectorTests()
64
67
this . mockPathUtilityService . Setup ( x => x . NormalizePath ( It . IsAny < string > ( ) ) ) . Returns ( ( string p ) => p ) ; // don't do normalization
65
68
this . mockPathUtilityService . Setup ( x => x . GetParentDirectory ( It . IsAny < string > ( ) ) ) . Returns ( ( string p ) => Path . GetDirectoryName ( p ) ) ;
66
69
67
- this . mockCommandLineInvocationService . Setup ( x => x . ExecuteCommandAsync ( It . IsAny < string > ( ) , It . IsAny < IEnumerable < string > > ( ) , It . IsAny < DirectoryInfo > ( ) , It . IsAny < CancellationToken > ( ) , It . IsAny < string [ ] > ( ) ) ) . Returns ( Task . FromResult ( this . commandLineExecutionResult ) ) ;
70
+ this . mockCommandLineInvocationService . Setup ( x => x . ExecuteCommandAsync ( It . IsAny < string > ( ) , It . IsAny < IEnumerable < string > > ( ) , It . IsAny < DirectoryInfo > ( ) , It . IsAny < CancellationToken > ( ) , It . IsAny < string [ ] > ( ) ) )
71
+ . Returns ( ( string c , IEnumerable < string > ac , DirectoryInfo d , CancellationToken ct , string [ ] args ) => Task . FromResult ( this . CommandResult ( c , d ) ) ) ;
68
72
}
69
73
70
74
private bool FileExists ( string path )
@@ -147,11 +151,20 @@ private void AddDirectory(string path, string subDirectory = null)
147
151
148
152
private void SetCommandResult ( int exitCode , string stdOut = null , string stdErr = null )
149
153
{
154
+ this . commandLineCallback = null ;
150
155
this . commandLineExecutionResult . ExitCode = exitCode ;
151
156
this . commandLineExecutionResult . StdOut = stdOut ;
152
157
this . commandLineExecutionResult . StdErr = stdErr ;
153
158
}
154
159
160
+ private void SetCommandResult ( Func < string , DirectoryInfo , CommandLineExecutionResult > callback )
161
+ {
162
+ this . commandLineCallback = callback ;
163
+ }
164
+
165
+ private CommandLineExecutionResult CommandResult ( string command , DirectoryInfo directory ) =>
166
+ ( this . commandLineCallback != null ) ? this . commandLineCallback ( command , directory ) : this . commandLineExecutionResult ;
167
+
155
168
[ TestCleanup ]
156
169
public void ClearMocks ( )
157
170
{
@@ -214,6 +227,31 @@ public async Task TestDotNetDetectorGlobalJson_ReturnsSDKVersion()
214
227
var globalJson = GlobalJson ( "42.0.800" ) ;
215
228
this . AddFile ( projectPath , null ) ;
216
229
this . AddFile ( Path . Combine ( RootDir , "path" , "global.json" ) , globalJson ) ;
230
+ this . SetCommandResult ( - 1 ) ;
231
+
232
+ var ( scanResult , componentRecorder ) = await this . DetectorTestUtility
233
+ . WithFile ( "project.assets.json" , projectAssets )
234
+ . ExecuteDetectorAsync ( ) ;
235
+
236
+ scanResult . ResultCode . Should ( ) . Be ( ProcessingResultCode . Success ) ;
237
+
238
+ var detectedComponents = componentRecorder . GetDetectedComponents ( ) ;
239
+ detectedComponents . Should ( ) . HaveCount ( 2 ) ;
240
+
241
+ var discoveredComponents = detectedComponents . ToArray ( ) ;
242
+ discoveredComponents . Where ( component => component . Component . Id == "42.0.800 unknown unknown - DotNet" ) . Should ( ) . ContainSingle ( ) ;
243
+ discoveredComponents . Where ( component => component . Component . Id == "42.0.800 net8.0 unknown - DotNet" ) . Should ( ) . ContainSingle ( ) ;
244
+ }
245
+
246
+ [ TestMethod ]
247
+ public async Task TestDotNetDetectorGlobalJsonRollForward_ReturnsSDKVersion ( )
248
+ {
249
+ var projectPath = Path . Combine ( RootDir , "path" , "to" , "project" ) ;
250
+ var projectAssets = ProjectAssets ( "projectName" , "does-not-exist" , projectPath , "net8.0" ) ;
251
+ var globalJson = GlobalJson ( "8.0.100" ) ;
252
+ this . AddFile ( projectPath , null ) ;
253
+ this . AddFile ( Path . Combine ( RootDir , "path" , "global.json" ) , globalJson ) ;
254
+ this . SetCommandResult ( 0 , "8.0.808" ) ;
217
255
218
256
var ( scanResult , componentRecorder ) = await this . DetectorTestUtility
219
257
. WithFile ( "project.assets.json" , projectAssets )
@@ -225,8 +263,8 @@ public async Task TestDotNetDetectorGlobalJson_ReturnsSDKVersion()
225
263
detectedComponents . Should ( ) . HaveCount ( 2 ) ;
226
264
227
265
var discoveredComponents = detectedComponents . ToArray ( ) ;
228
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 42 .0.800 " ) . Should ( ) . ContainSingle ( ) ;
229
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 42 .0.800 - net8.0" ) . Should ( ) . ContainSingle ( ) ;
266
+ discoveredComponents . Where ( component => component . Component . Id == "8 .0.808 unknown unknown - DotNet " ) . Should ( ) . ContainSingle ( ) ;
267
+ discoveredComponents . Where ( component => component . Component . Id == "8 .0.808 net8.0 unknown - DotNet " ) . Should ( ) . ContainSingle ( ) ;
230
268
}
231
269
232
270
[ TestMethod ]
@@ -247,7 +285,7 @@ public async Task TestDotNetDetectorNoGlobalJson_ReturnsDotNetVersion()
247
285
detectedComponents . Should ( ) . ContainSingle ( ) ;
248
286
249
287
var discoveredComponents = detectedComponents . ToArray ( ) ;
250
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 86.75.309 - net8.0" ) . Should ( ) . ContainSingle ( ) ;
288
+ discoveredComponents . Where ( component => component . Component . Id == "86.75.309 net8.0 unknown - DotNet " ) . Should ( ) . ContainSingle ( ) ;
251
289
}
252
290
253
291
[ TestMethod ]
@@ -268,7 +306,7 @@ public async Task TestDotNetDetectorNoGlobalJsonNoDotnet_ReturnsUnknownVersion()
268
306
detectedComponents . Should ( ) . ContainSingle ( ) ;
269
307
270
308
var discoveredComponents = detectedComponents . ToArray ( ) ;
271
- discoveredComponents . Where ( component => component . Component . Id == "dotnet unknown - net8.0" ) . Should ( ) . ContainSingle ( ) ;
309
+ discoveredComponents . Where ( component => component . Component . Id == "unknown net8.0 unknown - DotNet " ) . Should ( ) . ContainSingle ( ) ;
272
310
}
273
311
274
312
[ TestMethod ]
@@ -289,20 +327,24 @@ public async Task TestDotNetDetectorMultipleTargetFrameworks()
289
327
detectedComponents . Should ( ) . HaveCount ( 3 ) ;
290
328
291
329
var discoveredComponents = detectedComponents . ToArray ( ) ;
292
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 1.2.3 - net8.0" ) . Should ( ) . ContainSingle ( ) ;
293
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 1.2.3 - net6.0" ) . Should ( ) . ContainSingle ( ) ;
294
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 1.2.3 - netstandard2.0" ) . Should ( ) . ContainSingle ( ) ;
330
+ discoveredComponents . Where ( component => component . Component . Id == "1.2.3 net8.0 unknown - DotNet " ) . Should ( ) . ContainSingle ( ) ;
331
+ discoveredComponents . Where ( component => component . Component . Id == "1.2.3 net6.0 unknown - DotNet " ) . Should ( ) . ContainSingle ( ) ;
332
+ discoveredComponents . Where ( component => component . Component . Id == "1.2.3 netstandard2.0 unknown - DotNet " ) . Should ( ) . ContainSingle ( ) ;
295
333
}
296
334
297
335
[ TestMethod ]
298
336
public async Task TestDotNetDetectorMultipleProjectsWithDifferentOutputTypeAndSdkVersion ( )
299
337
{
300
- // dotnet on the path with be version 1.2.3
301
- this . SetCommandResult ( 0 , "1.2.3" ) ;
302
-
303
338
// dotnet from global.json will be 4.5.6
304
339
var globalJson = GlobalJson ( "4.5.6" ) ;
305
- this . AddFile ( Path . Combine ( RootDir , "path" , "global.json" ) , globalJson ) ;
340
+ var globalJsonDir = Path . Combine ( RootDir , "path" ) ;
341
+ this . AddFile ( Path . Combine ( globalJsonDir , "global.json" ) , globalJson ) ;
342
+
343
+ this . SetCommandResult ( ( c , d ) => new CommandLineExecutionResult ( )
344
+ {
345
+ ExitCode = 0 ,
346
+ StdOut = d . FullName == globalJsonDir ? "4.5.6" : "1.2.3" ,
347
+ } ) ;
306
348
307
349
// set up a library project - under global.json
308
350
var libraryProjectName = "library" ;
@@ -338,11 +380,11 @@ public async Task TestDotNetDetectorMultipleProjectsWithDifferentOutputTypeAndSd
338
380
detectedComponents . Should ( ) . HaveCount ( 6 ) ;
339
381
340
382
var discoveredComponents = detectedComponents . ToArray ( ) ;
341
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 4.5.6" ) . Should ( ) . ContainSingle ( ) ;
342
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 4.5.6 - net8.0 - library " ) . Should ( ) . ContainSingle ( ) ;
343
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 4.5.6 - net6.0 - library " ) . Should ( ) . ContainSingle ( ) ;
344
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 4.5.6 - netstandard2.0 - library " ) . Should ( ) . ContainSingle ( ) ;
345
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 1.2.3 - net8.0 - application " ) . Should ( ) . ContainSingle ( ) ;
346
- discoveredComponents . Where ( component => component . Component . Id == "dotnet 1.2.3 - net48 - application " ) . Should ( ) . ContainSingle ( ) ;
383
+ discoveredComponents . Where ( component => component . Component . Id == "4.5.6 unknown unknown - DotNet " ) . Should ( ) . ContainSingle ( ) ;
384
+ discoveredComponents . Where ( component => component . Component . Id == "4.5.6 net8.0 library - DotNet " ) . Should ( ) . ContainSingle ( ) ;
385
+ discoveredComponents . Where ( component => component . Component . Id == "4.5.6 net6.0 library - DotNet " ) . Should ( ) . ContainSingle ( ) ;
386
+ discoveredComponents . Where ( component => component . Component . Id == "4.5.6 netstandard2.0 library - DotNet " ) . Should ( ) . ContainSingle ( ) ;
387
+ discoveredComponents . Where ( component => component . Component . Id == "1.2.3 net8.0 application - DotNet " ) . Should ( ) . ContainSingle ( ) ;
388
+ discoveredComponents . Where ( component => component . Component . Id == "1.2.3 net48 application - DotNet " ) . Should ( ) . ContainSingle ( ) ;
347
389
}
348
390
}
0 commit comments