Skip to content

Commit ed22500

Browse files
authored
Fixed Go117 detector (#1361)
1 parent f763e96 commit ed22500

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

src/Microsoft.ComponentDetection.Detectors/go/Go117ComponentDetector.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,35 @@ private async Task<bool> ShouldRunGoGraphAsync()
157157

158158
private async Task<Version> GetGoVersionAsync()
159159
{
160-
var processExecution = await this.commandLineInvocationService.ExecuteCommandAsync("go", null, null, cancellationToken: default, new List<string> { "version" }.ToArray());
161-
if (processExecution.ExitCode != 0)
160+
try
162161
{
163-
return null;
164-
}
162+
var isGoAvailable = await this.commandLineInvocationService.CanCommandBeLocatedAsync("go", null, null, new List<string> { "version" }.ToArray());
163+
if (!isGoAvailable)
164+
{
165+
this.Logger.LogInformation("Go CLI was not found in the system");
166+
return null;
167+
}
165168

166-
// Define the regular expression pattern to match the version number
167-
var versionPattern = @"go version go(\d+\.\d+\.\d+)";
168-
var match = Regex.Match(processExecution.StdOut, versionPattern);
169+
var processExecution = await this.commandLineInvocationService.ExecuteCommandAsync("go", null, null, cancellationToken: default, new List<string> { "version" }.ToArray());
170+
if (processExecution.ExitCode != 0)
171+
{
172+
return null;
173+
}
169174

170-
if (match.Success)
175+
// Define the regular expression pattern to match the version number
176+
var versionPattern = @"go version go(\d+\.\d+\.\d+)";
177+
var match = Regex.Match(processExecution.StdOut, versionPattern);
178+
179+
if (match.Success)
180+
{
181+
// Extract the version number from the match
182+
var versionStr = match.Groups[1].Value;
183+
return new Version(versionStr);
184+
}
185+
}
186+
catch (Exception e)
171187
{
172-
// Extract the version number from the match
173-
var versionStr = match.Groups[1].Value;
174-
return new Version(versionStr);
188+
this.Logger.LogWarning("Failed to get go version: {Exception}", e);
175189
}
176190

177191
return null;

test/Microsoft.ComponentDetection.Detectors.Tests/Go117ComponentDetectorTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public async Task Go117ModDetector_GoCliIs1_11OrGreater_GoGraphIsExecutedAsync()
5151
{
5252
var goMod = string.Empty;
5353

54+
this.commandLineMock.Setup(x => x.CanCommandBeLocatedAsync("go", null, null, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
55+
.ReturnsAsync(true);
56+
5457
this.commandLineMock.Setup(x => x.ExecuteCommandAsync("go", null, null, default, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
5558
.ReturnsAsync(new CommandLineExecutionResult
5659
{
@@ -85,6 +88,9 @@ public async Task Go117ModDetector_GoCliIs111OrLessThan1_11_GoGraphIsNotExecuted
8588
{
8689
var goMod = string.Empty;
8790

91+
this.commandLineMock.Setup(x => x.CanCommandBeLocatedAsync("go", null, null, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
92+
.ReturnsAsync(true);
93+
8894
this.commandLineMock.Setup(x => x.ExecuteCommandAsync("go", null, null, default, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
8995
.ReturnsAsync(new CommandLineExecutionResult
9096
{
@@ -114,6 +120,9 @@ public async Task Go117ModDetector_GoModFileFound_GoModParserIsExecuted()
114120
var goModParserMock = new Mock<IGoParser>();
115121
this.mockParserFactory.Setup(x => x.CreateParser(GoParserType.GoMod, It.IsAny<ILogger>())).Returns(goModParserMock.Object);
116122

123+
this.commandLineMock.Setup(x => x.CanCommandBeLocatedAsync("go", null, null, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
124+
.ReturnsAsync(true);
125+
117126
this.commandLineMock.Setup(x => x.ExecuteCommandAsync("go", null, null, default, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
118127
.ReturnsAsync(new CommandLineExecutionResult
119128
{
@@ -136,6 +145,9 @@ public async Task Go117ModDetector_GoSumFileFound_GoSumParserIsExecuted()
136145
var goSumParserMock = new Mock<IGoParser>();
137146
this.mockParserFactory.Setup(x => x.CreateParser(GoParserType.GoSum, It.IsAny<ILogger>())).Returns(goSumParserMock.Object);
138147

148+
this.commandLineMock.Setup(x => x.CanCommandBeLocatedAsync("go", null, null, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
149+
.ReturnsAsync(true);
150+
139151
this.commandLineMock.Setup(x => x.ExecuteCommandAsync("go", null, null, default, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
140152
.ReturnsAsync(new CommandLineExecutionResult
141153
{
@@ -151,4 +163,25 @@ public async Task Go117ModDetector_GoSumFileFound_GoSumParserIsExecuted()
151163

152164
goSumParserMock.Verify(parser => parser.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()), Times.Once);
153165
}
166+
167+
[TestMethod]
168+
public async Task Go117ModDetector_ExecutingGoVersionFails_DetectorDoesNotFail()
169+
{
170+
var goModParserMock = new Mock<IGoParser>();
171+
this.mockParserFactory.Setup(x => x.CreateParser(GoParserType.GoMod, It.IsAny<ILogger>())).Returns(goModParserMock.Object);
172+
173+
this.commandLineMock.Setup(x => x.CanCommandBeLocatedAsync("go", null, null, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
174+
.ReturnsAsync(true);
175+
176+
this.commandLineMock.Setup(x => x.ExecuteCommandAsync("go", null, null, default, It.Is<string[]>(p => p.SequenceEqual(new List<string> { "version" }.ToArray()))))
177+
.Throws(new InvalidOperationException("Failed to execute go version"));
178+
179+
var (scanResult, componentRecorder) = await this.DetectorTestUtility
180+
.WithFile("go.mod", string.Empty)
181+
.ExecuteDetectorAsync();
182+
183+
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
184+
185+
goModParserMock.Verify(parser => parser.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()), Times.Once);
186+
}
154187
}

0 commit comments

Comments
 (0)