Skip to content

Commit 8360853

Browse files
authored
Add additional removal indicator (#1245)
* add additional removal indicator * remove param * pr feedback
1 parent 7528c8f commit 8360853

File tree

8 files changed

+22
-15
lines changed

8 files changed

+22
-15
lines changed

src/Microsoft.ComponentDetection.Common/FileUtilityService.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Microsoft.ComponentDetection.Common;
22

33
using System.Collections.Generic;
44
using System.IO;
5+
using System.Linq;
56
using System.Threading.Tasks;
67
using Microsoft.ComponentDetection.Contracts;
78

@@ -39,14 +40,14 @@ public Stream MakeFileStream(string fileName)
3940
}
4041

4142
/// <inheritdoc />
42-
public (string DuplicateFilePath, bool CreatedDuplicate) DuplicateFileWithoutLines(string fileName, string removalIndicator)
43+
public (string DuplicateFilePath, bool CreatedDuplicate) DuplicateFileWithoutLines(string fileName, params string[] removalIndicators)
4344
{
4445
// Read all lines from the file and filter out the lines that start with the removal indicator.
4546
var removedAnyLines = false;
4647
var linesToKeep = new List<string>();
4748
foreach (var line in File.ReadLines(fileName))
4849
{
49-
if (line == null || line.Trim().StartsWith(removalIndicator, System.StringComparison.OrdinalIgnoreCase))
50+
if (string.IsNullOrEmpty(line) || removalIndicators.Any(removalIndicator => line.Trim().StartsWith(removalIndicator, System.StringComparison.OrdinalIgnoreCase)))
5051
{
5152
removedAnyLines = true;
5253
}

src/Microsoft.ComponentDetection.Contracts/IFileUtilityService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public interface IFileUtilityService
4747
/// Duplicates a file, removing any lines that starts with the given string.
4848
/// </summary>
4949
/// <param name="fileName">Path to the file.</param>
50-
/// <param name="removalIndicator">The string that indicates a line should be removed.</param>
50+
/// <param name="removalIndicators">The strings that indicates a line should be removed.</param>
5151
/// <returns>Returns the path of the new file, and whether or not one was created.</returns>
52-
(string DuplicateFilePath, bool CreatedDuplicate) DuplicateFileWithoutLines(string fileName, string removalIndicator);
52+
(string DuplicateFilePath, bool CreatedDuplicate) DuplicateFileWithoutLines(string fileName, params string[] removalIndicators);
5353
}

src/Microsoft.ComponentDetection.Detectors/Microsoft.ComponentDetection.Detectors.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
33
<ItemGroup>
44
<PackageReference Include="DotNet.Glob" />
55
<PackageReference Include="Microsoft.Extensions.Logging" />
@@ -14,7 +14,7 @@
1414
<PackageReference Include="System.Reactive" />
1515
<PackageReference Include="System.Threading.Tasks.Dataflow" />
1616
<PackageReference Include="Tomlyn.Signed" />
17-
<PackageReference Include="Valleysoft.DockerfileModel " />
17+
<PackageReference Include="Valleysoft.DockerfileModel" />
1818

1919
<!-- Fix alerts -->
2020
<PackageReference Include="System.Formats.Asn1" />

src/Microsoft.ComponentDetection.Detectors/pip/PipCommandService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private async Task<CommandLineExecutionResult> ExecuteCommandAsync(
149149
if (this.environmentService.IsEnvironmentVariableValueTrue(PipReportIgnoreFileLevelIndexUrlEnvVar))
150150
{
151151
// check for --index-url in requirements.txt and remove it from the file, since we want to use PIP_INDEX_URL from the environment.
152-
var (duplicateFilePath, createdDuplicate) = this.fileUtilityService.DuplicateFileWithoutLines(formattedPath, "--index-url");
152+
var (duplicateFilePath, createdDuplicate) = this.fileUtilityService.DuplicateFileWithoutLines(formattedPath, "--index-url", "-i");
153153
if (createdDuplicate)
154154
{
155155
var duplicateFileName = Path.GetFileName(duplicateFilePath);

test/Microsoft.ComponentDetection.Common.Tests/FileUtilityServiceTests.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@ public void DuplicateFileWithoutLines_WithLinesToRemove_ShouldCreateDuplicateFil
2323

2424
// Arrange
2525
var fileName = $"{directory}/Resources/test-file-duplicate.txt";
26-
var removalIndicator = "//REMOVE";
2726
var expectedDuplicateFilePath = Path.Combine(directory, "Resources", "temp.test-file-duplicate.txt");
2827

2928
// Act
30-
var (duplicateFilePath, createdDuplicate) = this.fileUtilityService.DuplicateFileWithoutLines(fileName, removalIndicator);
29+
var (duplicateFilePath, createdDuplicate) = this.fileUtilityService.DuplicateFileWithoutLines(fileName, "//REMOVE", "//ME");
3130

3231
// Assert
3332
createdDuplicate.Should().BeTrue();
3433
duplicateFilePath.Should().Be(expectedDuplicateFilePath);
3534
File.Exists(expectedDuplicateFilePath).Should().BeTrue();
35+
36+
var contents = File.ReadAllText(expectedDuplicateFilePath);
37+
contents.Should().NotContain("//REMOVE");
38+
contents.Should().NotContain("//ME");
39+
contents.Should().Contain("hello");
40+
contents.Should().Contain("world");
3641
}
3742

3843
[TestMethod]

test/Microsoft.ComponentDetection.Common.Tests/Microsoft.ComponentDetection.Common.Tests.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<ItemGroup Label="Project References">
44
<ProjectReference Include="..\Microsoft.ComponentDetection.TestsUtilities\Microsoft.ComponentDetection.TestsUtilities.csproj" />
@@ -9,8 +9,8 @@
99
<PackageReference Include="FluentAssertions.Analyzers" PrivateAssets="all" />
1010
<PackageReference Include="Microsoft.Extensions.Logging" />
1111
<PackageReference Include="System.Threading.Tasks.Dataflow" />
12-
<PackageReference Include="MSTest.TestAdapter "/>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk "/>
12+
<PackageReference Include="MSTest.TestAdapter" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1414
</ItemGroup>
1515

1616
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
hello
22
//REMOVE
33
world
4+
//ME

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public async Task PipCommandService_GeneratesReport_WithDuplicate_Async()
500500
this.envVarService.Setup(x => x.IsEnvironmentVariableValueTrue("PipReportIgnoreFileLevelIndexUrl")).Returns(true);
501501
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("pip", It.IsAny<IEnumerable<string>>(), "--version")).ReturnsAsync(true);
502502

503-
this.fileUtilityService.Setup(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url"))
503+
this.fileUtilityService.Setup(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url", "-i"))
504504
.Returns(("C:/asdf/temp.requirements.txt", true))
505505
.Verifiable();
506506
this.fileUtilityService.Setup(x => x.ReadAllTextAsync(It.IsAny<FileInfo>()))
@@ -535,7 +535,7 @@ public async Task PipCommandService_GeneratesReport_WithoutDuplicate_Async()
535535
this.envVarService.Setup(x => x.IsEnvironmentVariableValueTrue("PipReportIgnoreFileLevelIndexUrl")).Returns(true);
536536
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("pip", It.IsAny<IEnumerable<string>>(), "--version")).ReturnsAsync(true);
537537

538-
this.fileUtilityService.Setup(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url"))
538+
this.fileUtilityService.Setup(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url", "-i"))
539539
.Returns((null, false))
540540
.Verifiable();
541541
this.fileUtilityService.Setup(x => x.ReadAllTextAsync(It.IsAny<FileInfo>()))
@@ -590,7 +590,7 @@ public async Task PipCommandService_GeneratesReport_UseFileIndex_Async()
590590
this.logger.Object);
591591

592592
var (report, reportFile) = await service.GenerateInstallationReportAsync(testPath);
593-
this.fileUtilityService.Verify(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url"), Times.Never);
593+
this.fileUtilityService.Verify(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url", "-i"), Times.Never);
594594
this.commandLineInvokationService.Verify();
595595
}
596596

0 commit comments

Comments
 (0)