1111using Microsoft . VisualStudio . TestTools . UnitTesting ;
1212using Moq ;
1313using Microsoft . ComponentDetection . TestsUtilities ;
14+ using Microsoft . ComponentDetection . Common ;
1415
1516namespace Microsoft . ComponentDetection . Detectors . Tests
1617{
@@ -21,18 +22,25 @@ public class GoComponentDetectorTests
2122 {
2223 private DetectorTestUtility < GoComponentDetector > detectorTestUtility ;
2324 private Mock < ICommandLineInvocationService > commandLineMock ;
25+
26+ private Mock < IEnvironmentVariableService > envVarService ;
2427 private ScanRequest scanRequest ;
2528
2629 [ TestInitialize ]
2730 public void TestInitialize ( )
2831 {
2932 commandLineMock = new Mock < ICommandLineInvocationService > ( ) ;
33+ envVarService = new Mock < IEnvironmentVariableService > ( ) ;
34+
3035 var loggerMock = new Mock < ILogger > ( ) ;
3136
37+ envVarService . Setup ( x => x . DoesEnvironmentVariableExist ( "EnableGoCliScan" ) ) . Returns ( false ) ;
38+
3239 var detector = new GoComponentDetector
3340 {
3441 CommandLineInvocationService = commandLineMock . Object ,
3542 Logger = loggerMock . Object ,
43+ EnvVarService = envVarService . Object ,
3644 } ;
3745
3846 var tempPath = Path . GetTempPath ( ) ;
@@ -254,6 +262,8 @@ public async Task TestGoDetector_GoCommandNotFound()
254262 commandLineMock . Setup ( x => x . CanCommandBeLocated ( "go" , null , It . IsAny < DirectoryInfo > ( ) , It . IsAny < string [ ] > ( ) ) )
255263 . ReturnsAsync ( false ) ;
256264
265+ envVarService . Setup ( x => x . DoesEnvironmentVariableExist ( "EnableGoCliScan" ) ) . Returns ( true ) ;
266+
257267 await TestGoSumDetectorWithValidFile_ReturnsSuccessfully ( ) ;
258268 }
259269
@@ -263,6 +273,8 @@ public async Task TestGoDetector_GoCommandThrows()
263273 commandLineMock . Setup ( x => x . CanCommandBeLocated ( "go" , null , It . IsAny < DirectoryInfo > ( ) , It . IsAny < string [ ] > ( ) ) )
264274 . ReturnsAsync ( ( ) => throw new Exception ( "Some horrible error occured" ) ) ;
265275
276+ envVarService . Setup ( x => x . DoesEnvironmentVariableExist ( "EnableGoCliScan" ) ) . Returns ( true ) ;
277+
266278 await TestGoSumDetectorWithValidFile_ReturnsSuccessfully ( ) ;
267279 }
268280
@@ -278,6 +290,8 @@ public async Task TestGoDetector_GoGraphCommandFails()
278290 ExitCode = 1 ,
279291 } ) ;
280292
293+ envVarService . Setup ( x => x . DoesEnvironmentVariableExist ( "EnableGoCliScan" ) ) . Returns ( true ) ;
294+
281295 await TestGoSumDetectorWithValidFile_ReturnsSuccessfully ( ) ;
282296 }
283297
@@ -290,6 +304,8 @@ public async Task TestGoDetector_GoGraphCommandThrows()
290304 commandLineMock . Setup ( x => x . ExecuteCommand ( "go mod graph" , null , It . IsAny < DirectoryInfo > ( ) , It . IsAny < string > ( ) ) )
291305 . ReturnsAsync ( ( ) => throw new Exception ( "Some horrible error occured" ) ) ;
292306
307+ envVarService . Setup ( x => x . DoesEnvironmentVariableExist ( "EnableGoCliScan" ) ) . Returns ( true ) ;
308+
293309 await TestGoSumDetectorWithValidFile_ReturnsSuccessfully ( ) ;
294310 }
295311
@@ -308,14 +324,24 @@ public async Task TestGoDetector_GoGraphHappyPath()
308324 StdOut = goGraph ,
309325 } ) ;
310326
327+ envVarService . Setup ( x => x . DoesEnvironmentVariableExist ( "EnableGoCliScan" ) ) . Returns ( true ) ;
328+
311329 var ( scanResult , componentRecorder ) = await detectorTestUtility
312330 . WithFile ( "go.mod" , string . Empty )
313331 . ExecuteDetector ( ) ;
314332
315333 Assert . AreEqual ( ProcessingResultCode . Success , scanResult . ResultCode ) ;
316334
317335 var detectedComponents = componentRecorder . GetDetectedComponents ( ) ;
318- Assert . AreEqual ( 0 , detectedComponents . Count ( ) ) ;
336+ Assert . AreEqual ( 4 , detectedComponents . Count ( ) ) ;
337+ }
338+
339+ [ TestMethod ]
340+ public async Task TestGoDetector_GoCliRequiresEnvVarToRun ( )
341+ {
342+ await TestGoSumDetectorWithValidFile_ReturnsSuccessfully ( ) ;
343+
344+ commandLineMock . Verify ( x => x . CanCommandBeLocated ( "go" , null , It . IsAny < DirectoryInfo > ( ) , It . IsAny < string [ ] > ( ) ) , Times . Never ) ;
319345 }
320346 }
321347}
0 commit comments