Skip to content

Commit bd4dcaa

Browse files
committed
Pass CancellationToken to ParseAsync
1 parent e2e6ff6 commit bd4dcaa

File tree

6 files changed

+39
-33
lines changed

6 files changed

+39
-33
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected override async Task OnFileFoundAsync(ProcessRequest processRequest, ID
115115
case ".MOD":
116116
{
117117
this.Logger.LogDebug("Found Go.mod: {Location}", file.Location);
118-
var wasModParsedSuccessfully = await this.goParserFactory.CreateParser(GoParserType.GoMod, this.Logger).ParseAsync(singleFileComponentRecorder, file, record);
118+
var wasModParsedSuccessfully = await this.goParserFactory.CreateParser(GoParserType.GoMod, this.Logger).ParseAsync(singleFileComponentRecorder, file, record, cancellationToken);
119119

120120
// Check if go.mod was parsed successfully and Go version is >= 1.17 in go.mod
121121
if (wasModParsedSuccessfully &&
@@ -155,7 +155,7 @@ await GoDependencyGraphUtility.GenerateAndPopulateDependencyGraphAsync(
155155
{
156156
if (!wasGoCliDisabled)
157157
{
158-
wasGoCliScanSuccessful = await this.goParserFactory.CreateParser(GoParserType.GoCLI, this.Logger).ParseAsync(singleFileComponentRecorder, file, record);
158+
wasGoCliScanSuccessful = await this.goParserFactory.CreateParser(GoParserType.GoCLI, this.Logger).ParseAsync(singleFileComponentRecorder, file, record, cancellationToken);
159159
}
160160
else
161161
{
@@ -176,7 +176,7 @@ await GoDependencyGraphUtility.GenerateAndPopulateDependencyGraphAsync(
176176
{
177177
record.WasGoFallbackStrategyUsed = true;
178178
this.Logger.LogDebug("Go CLI scan when considering {GoSumLocation} was not successful. Falling back to scanning go.sum", file.Location);
179-
await this.goParserFactory.CreateParser(GoParserType.GoSum, this.Logger).ParseAsync(singleFileComponentRecorder, file, record);
179+
await this.goParserFactory.CreateParser(GoParserType.GoSum, this.Logger).ParseAsync(singleFileComponentRecorder, file, record, cancellationToken);
180180
}
181181
else
182182
{

src/Microsoft.ComponentDetection.Detectors/go/Parsers/GoCLIParser.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ namespace Microsoft.ComponentDetection.Detectors.Go;
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Diagnostics.CodeAnalysis;
76
using System.IO;
87
using System.Text;
98
using System.Text.Json;
9+
using System.Threading;
1010
using System.Threading.Tasks;
1111
using Microsoft.ComponentDetection.Common.Telemetry.Records;
1212
using Microsoft.ComponentDetection.Contracts;
@@ -26,11 +26,11 @@ public GoCLIParser(ILogger logger, IFileUtilityService fileUtilityService, IComm
2626
this.commandLineInvocationService = commandLineInvocationService;
2727
}
2828

29-
[SuppressMessage("Maintainability", "CA1508:Avoid dead conditional code", Justification = "False positive")]
3029
public async Task<bool> ParseAsync(
3130
ISingleFileComponentRecorder singleFileComponentRecorder,
3231
IComponentStream file,
33-
GoGraphTelemetryRecord record)
32+
GoGraphTelemetryRecord record,
33+
CancellationToken cancellationToken = default)
3434
{
3535
record.WasGraphSuccessful = false;
3636
record.DidGoCliCommandFail = false;
@@ -50,7 +50,7 @@ public async Task<bool> ParseAsync(
5050
"Detection time may be improved by activating fallback strategy (https://github.com/microsoft/component-detection/blob/main/docs/detectors/go.md#fallback-detection-strategy). " +
5151
"But, it will introduce noise into the detected components.");
5252

53-
var goDependenciesProcess = await this.commandLineInvocationService.ExecuteCommandAsync("go", null, workingDirectory: projectRootDirectory, ["list", "-mod=readonly", "-m", "-json", "all"]);
53+
var goDependenciesProcess = await this.commandLineInvocationService.ExecuteCommandAsync("go", null, projectRootDirectory, cancellationToken, "list", "-mod=readonly", "-m", "-json", "all");
5454
if (goDependenciesProcess.ExitCode != 0)
5555
{
5656
this.logger.LogError("Go CLI command \"go list -m -json all\" failed with error: {GoDependenciesProcessStdErr}", goDependenciesProcess.StdErr);
@@ -67,7 +67,8 @@ public async Task<bool> ParseAsync(
6767
this.logger,
6868
singleFileComponentRecorder,
6969
projectRootDirectory.FullName,
70-
record);
70+
record,
71+
cancellationToken);
7172

7273
return true;
7374
}

src/Microsoft.ComponentDetection.Detectors/go/Parsers/GoModParser.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Microsoft.ComponentDetection.Detectors.Go;
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Text.RegularExpressions;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using Microsoft.ComponentDetection.Common.Telemetry.Records;
1011
using Microsoft.ComponentDetection.Contracts;
@@ -71,7 +72,8 @@ private static void HandleReplaceDirective(
7172
public async Task<bool> ParseAsync(
7273
ISingleFileComponentRecorder singleFileComponentRecorder,
7374
IComponentStream file,
74-
GoGraphTelemetryRecord record)
75+
GoGraphTelemetryRecord record,
76+
CancellationToken cancellationToken = default)
7577
{
7678
// Collect replace directives
7779
var (replacePathDirectives, moduleReplacements) = await this.GetAllReplaceDirectivesAsync(file);
@@ -84,7 +86,7 @@ public async Task<bool> ParseAsync(
8486
// There can be multiple require( ) sections in go 1.17+. loop over all of them.
8587
while (!reader.EndOfStream)
8688
{
87-
var line = await reader.ReadLineAsync();
89+
var line = await reader.ReadLineAsync(cancellationToken);
8890

8991
while (line != null && !line.StartsWith("require ("))
9092
{
@@ -100,11 +102,11 @@ public async Task<bool> ParseAsync(
100102
this.TryRegisterDependencyFromModLine(file, line[StartString.Length..], singleFileComponentRecorder, replacePathDirectives, moduleReplacements);
101103
}
102104

103-
line = await reader.ReadLineAsync();
105+
line = await reader.ReadLineAsync(cancellationToken);
104106
}
105107

106108
// Stopping at the first ) restrict the detection to only the require section.
107-
while ((line = await reader.ReadLineAsync()) != null && !line.EndsWith(')'))
109+
while ((line = await reader.ReadLineAsync(cancellationToken)) != null && !line.EndsWith(')'))
108110
{
109111
this.TryRegisterDependencyFromModLine(file, line, singleFileComponentRecorder, replacePathDirectives, moduleReplacements);
110112
}

src/Microsoft.ComponentDetection.Detectors/go/Parsers/GoSumParser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Microsoft.ComponentDetection.Detectors.Go;
33

44
using System.IO;
55
using System.Text.RegularExpressions;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.ComponentDetection.Common.Telemetry.Records;
89
using Microsoft.ComponentDetection.Contracts;
@@ -24,12 +25,13 @@ public class GoSumParser : IGoParser
2425
public async Task<bool> ParseAsync(
2526
ISingleFileComponentRecorder singleFileComponentRecorder,
2627
IComponentStream file,
27-
GoGraphTelemetryRecord record)
28+
GoGraphTelemetryRecord record,
29+
CancellationToken cancellationToken = default)
2830
{
2931
using var reader = new StreamReader(file.Stream);
3032

3133
string line;
32-
while ((line = await reader.ReadLineAsync()) != null)
34+
while ((line = await reader.ReadLineAsync(cancellationToken)) != null)
3335
{
3436
if (this.TryToCreateGoComponentFromSumLine(line, out var goComponent))
3537
{
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#nullable disable
22
namespace Microsoft.ComponentDetection.Detectors.Go;
33

4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Microsoft.ComponentDetection.Common.Telemetry.Records;
67
using Microsoft.ComponentDetection.Contracts;
78

89
public interface IGoParser
910
{
10-
Task<bool> ParseAsync(ISingleFileComponentRecorder singleFileComponentRecorder, IComponentStream file, GoGraphTelemetryRecord record);
11+
Task<bool> ParseAsync(ISingleFileComponentRecorder singleFileComponentRecorder, IComponentStream file, GoGraphTelemetryRecord record, CancellationToken cancellationToken = default);
1112
}

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ public async Task GoModDetector_GoModFileFound_GoModParserIsExecuted()
869869

870870
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
871871

872-
goModParserMock.Verify(parser => parser.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()), Times.Once);
872+
goModParserMock.Verify(parser => parser.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>()), Times.Once);
873873
}
874874

875875
/// <summary>
@@ -888,10 +888,10 @@ public async Task GoDetector_GoSum_GoSumParserExecuted(bool goCliSucceeds)
888888
this.envVarService.Setup(x => x.IsEnvironmentVariableValueTrue("DisableGoCliScan")).Returns(false);
889889

890890
// Setup go cli parser to succeed/fail
891-
this.mockGoCliParser.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>())).ReturnsAsync(goCliSucceeds);
891+
this.mockGoCliParser.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>())).ReturnsAsync(goCliSucceeds);
892892

893893
// Setup go sum parser to succeed
894-
this.mockGoSumParser.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>())).ReturnsAsync(true);
894+
this.mockGoSumParser.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);
895895
var (scanResult, componentRecorder) = await this.DetectorTestUtility
896896
.WithFile("go.sum", string.Empty)
897897
.ExecuteDetectorAsync();
@@ -913,8 +913,8 @@ public async Task GoDetector_GoSum_GoSumParserExecutedIfCliDisabled()
913913
// Setup environment variable to disable CLI scan
914914
this.envVarService.Setup(s => s.IsEnvironmentVariableValueTrue("DisableGoCliScan")).Returns(true);
915915

916-
// Setup go sum parser to succed
917-
goSumParserMock.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>())).ReturnsAsync(true);
916+
// Setup go sum parser to succeed
917+
goSumParserMock.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);
918918

919919
var (scanResult, componentRecorder) = await this.DetectorTestUtility
920920
.WithFile("go.sum", string.Empty)
@@ -941,7 +941,7 @@ public async Task GoModDetector_ExecutingGoVersionFails_DetectorDoesNotFail()
941941

942942
scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
943943

944-
this.mockGoModParser.Verify(parser => parser.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()), Times.Once);
944+
this.mockGoModParser.Verify(parser => parser.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>()), Times.Once);
945945
}
946946

947947
[TestMethod]
@@ -996,9 +996,9 @@ public async Task GoDetector_GoMod_VerifyNestedRootsUnderGTE117_AreSkipped()
996996
var processedFiles = new List<string>();
997997
this.SetupMockGoModParser();
998998
this.mockGoModParser
999-
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()))
999+
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>()))
10001000
.ReturnsAsync(true)
1001-
.Callback<ISingleFileComponentRecorder, IComponentStream, GoGraphTelemetryRecord>((_, file, record) =>
1001+
.Callback<ISingleFileComponentRecorder, IComponentStream, GoGraphTelemetryRecord, CancellationToken>((_, file, record, _) =>
10021002
{
10031003
processedFiles.Add(file.Location);
10041004
record.GoModVersion = "1.18";
@@ -1031,9 +1031,9 @@ public async Task GoDetector_GoMod_VerifyNestedRootsUnderLT117AreNotSkipped()
10311031
var processedFiles = new List<string>();
10321032
this.SetupMockGoModParser();
10331033
this.mockGoModParser
1034-
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()))
1034+
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>()))
10351035
.ReturnsAsync(true)
1036-
.Callback<ISingleFileComponentRecorder, IComponentStream, GoGraphTelemetryRecord>((_, file, record) =>
1036+
.Callback<ISingleFileComponentRecorder, IComponentStream, GoGraphTelemetryRecord, CancellationToken>((_, file, record, _) =>
10371037
{
10381038
processedFiles.Add(file.Location);
10391039
var rootMod = Path.Combine(root, "go.mod");
@@ -1080,8 +1080,8 @@ public async Task GoDetector_GoMod_VerifyNestedRootsAreNotSkippedIfParentParseFa
10801080
this.SetupMockGoModParser();
10811081

10821082
this.mockGoModParser
1083-
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()))
1084-
.ReturnsAsync((ISingleFileComponentRecorder recorder, IComponentStream file, GoGraphTelemetryRecord record) =>
1083+
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>()))
1084+
.ReturnsAsync((ISingleFileComponentRecorder recorder, IComponentStream file, GoGraphTelemetryRecord record, CancellationToken cancellationToken) =>
10851085
{
10861086
processedFiles.Add(file.Location);
10871087
var aMod = Path.Combine(root, "a", "go.mod");
@@ -1134,9 +1134,9 @@ public async Task GoDetector_GoSum_VerifyNestedRootsUnderGoSum_AreSkipped()
11341134
this.envVarService.Setup(x => x.IsEnvironmentVariableValueTrue("DisableGoCliScan")).Returns(false);
11351135
this.SetupMockGoCLIParser();
11361136
this.mockGoCliParser
1137-
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()))
1137+
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>()))
11381138
.ReturnsAsync(true)
1139-
.Callback<ISingleFileComponentRecorder, IComponentStream, GoGraphTelemetryRecord>((_, file, record) =>
1139+
.Callback<ISingleFileComponentRecorder, IComponentStream, GoGraphTelemetryRecord, CancellationToken>((_, file, record, _) =>
11401140
{
11411141
processedFiles.Add(file.Location);
11421142
});
@@ -1167,8 +1167,8 @@ public async Task GoDetector_GoSum_VerifyNestedRootsAreNotSkippedIfParentParseFa
11671167
this.SetupMockGoSumParser();
11681168

11691169
this.mockGoModParser
1170-
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()))
1171-
.ReturnsAsync((ISingleFileComponentRecorder recorder, IComponentStream file, GoGraphTelemetryRecord record) =>
1170+
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>()))
1171+
.ReturnsAsync((ISingleFileComponentRecorder recorder, IComponentStream file, GoGraphTelemetryRecord record, CancellationToken cancellationToken) =>
11721172
{
11731173
processedFiles.Add(file.Location);
11741174
var bMod = Path.Combine(root, "b", "go.mod");
@@ -1182,8 +1182,8 @@ public async Task GoDetector_GoSum_VerifyNestedRootsAreNotSkippedIfParentParseFa
11821182
});
11831183

11841184
this.mockGoCliParser
1185-
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>()))
1186-
.ReturnsAsync((ISingleFileComponentRecorder recorder, IComponentStream file, GoGraphTelemetryRecord record) =>
1185+
.Setup(p => p.ParseAsync(It.IsAny<ISingleFileComponentRecorder>(), It.IsAny<IComponentStream>(), It.IsAny<GoGraphTelemetryRecord>(), It.IsAny<CancellationToken>()))
1186+
.ReturnsAsync((ISingleFileComponentRecorder recorder, IComponentStream file, GoGraphTelemetryRecord record, CancellationToken cancellationToken) =>
11871187
{
11881188
processedFiles.Add(file.Location);
11891189
return file.Location != Path.Combine(root, "a", "go.sum");

0 commit comments

Comments
 (0)