add additional removal indicator
This commit is contained in:
Родитель
1d1e13bfa7
Коммит
541b6468a2
|
@ -2,6 +2,7 @@ namespace Microsoft.ComponentDetection.Common;
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.ComponentDetection.Contracts;
|
using Microsoft.ComponentDetection.Contracts;
|
||||||
|
|
||||||
|
@ -39,14 +40,14 @@ public class FileUtilityService : IFileUtilityService
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public (string DuplicateFilePath, bool CreatedDuplicate) DuplicateFileWithoutLines(string fileName, string removalIndicator)
|
public (string DuplicateFilePath, bool CreatedDuplicate) DuplicateFileWithoutLines(string fileName, params string[] removalIndicators)
|
||||||
{
|
{
|
||||||
// Read all lines from the file and filter out the lines that start with the removal indicator.
|
// Read all lines from the file and filter out the lines that start with the removal indicator.
|
||||||
var removedAnyLines = false;
|
var removedAnyLines = false;
|
||||||
var linesToKeep = new List<string>();
|
var linesToKeep = new List<string>();
|
||||||
foreach (var line in File.ReadLines(fileName))
|
foreach (var line in File.ReadLines(fileName))
|
||||||
{
|
{
|
||||||
if (line == null || line.Trim().StartsWith(removalIndicator, System.StringComparison.OrdinalIgnoreCase))
|
if (line == null || removalIndicators.Any(removalIndicator => line.Trim().StartsWith(removalIndicator, System.StringComparison.OrdinalIgnoreCase)))
|
||||||
{
|
{
|
||||||
removedAnyLines = true;
|
removedAnyLines = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ public interface IFileUtilityService
|
||||||
/// Duplicates a file, removing any lines that starts with the given string.
|
/// Duplicates a file, removing any lines that starts with the given string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fileName">Path to the file.</param>
|
/// <param name="fileName">Path to the file.</param>
|
||||||
/// <param name="removalIndicator">The string that indicates a line should be removed.</param>
|
/// <param name="removalIndicators">The strings that indicates a line should be removed.</param>
|
||||||
/// <returns>Returns the path of the new file, and whether or not one was created.</returns>
|
/// <returns>Returns the path of the new file, and whether or not one was created.</returns>
|
||||||
(string DuplicateFilePath, bool CreatedDuplicate) DuplicateFileWithoutLines(string fileName, string removalIndicator);
|
(string DuplicateFilePath, bool CreatedDuplicate) DuplicateFileWithoutLines(string fileName, params string[] removalIndicators);
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,7 +149,7 @@ public class PipCommandService : IPipCommandService
|
||||||
if (this.environmentService.IsEnvironmentVariableValueTrue(PipReportIgnoreFileLevelIndexUrlEnvVar))
|
if (this.environmentService.IsEnvironmentVariableValueTrue(PipReportIgnoreFileLevelIndexUrlEnvVar))
|
||||||
{
|
{
|
||||||
// check for --index-url in requirements.txt and remove it from the file, since we want to use PIP_INDEX_URL from the environment.
|
// check for --index-url in requirements.txt and remove it from the file, since we want to use PIP_INDEX_URL from the environment.
|
||||||
var (duplicateFilePath, createdDuplicate) = this.fileUtilityService.DuplicateFileWithoutLines(formattedPath, "--index-url");
|
var (duplicateFilePath, createdDuplicate) = this.fileUtilityService.DuplicateFileWithoutLines(formattedPath, "--index-url", "-i");
|
||||||
if (createdDuplicate)
|
if (createdDuplicate)
|
||||||
{
|
{
|
||||||
var duplicateFileName = Path.GetFileName(duplicateFilePath);
|
var duplicateFileName = Path.GetFileName(duplicateFilePath);
|
||||||
|
@ -169,7 +169,7 @@ public class PipCommandService : IPipCommandService
|
||||||
// When PIP_INDEX_URL is set, we need to pass it as a parameter to pip install command.
|
// When PIP_INDEX_URL is set, we need to pass it as a parameter to pip install command.
|
||||||
// This should be done before running detection by the build system, otherwise the detection
|
// This should be done before running detection by the build system, otherwise the detection
|
||||||
// will default to the public PyPI index if not configured in pip defaults. Note this index URL may have credentials, we need to remove it when logging.
|
// will default to the public PyPI index if not configured in pip defaults. Note this index URL may have credentials, we need to remove it when logging.
|
||||||
pipReportCommand += $" --dry-run --ignore-installed --quiet --no-input --report {reportName}";
|
pipReportCommand += $" --dry-run --ignore-installed --quiet --no-cache-dir --no-input --report {reportName}";
|
||||||
if (this.environmentService.DoesEnvironmentVariableExist("PIP_INDEX_URL"))
|
if (this.environmentService.DoesEnvironmentVariableExist("PIP_INDEX_URL"))
|
||||||
{
|
{
|
||||||
pipReportCommand += $" --index-url {this.environmentService.GetEnvironmentVariable("PIP_INDEX_URL")}";
|
pipReportCommand += $" --index-url {this.environmentService.GetEnvironmentVariable("PIP_INDEX_URL")}";
|
||||||
|
|
|
@ -23,16 +23,21 @@ public class FileUtilityServiceTests
|
||||||
|
|
||||||
// Arrange
|
// Arrange
|
||||||
var fileName = $"{directory}/Resources/test-file-duplicate.txt";
|
var fileName = $"{directory}/Resources/test-file-duplicate.txt";
|
||||||
var removalIndicator = "//REMOVE";
|
|
||||||
var expectedDuplicateFilePath = Path.Combine(directory, "Resources", "temp.test-file-duplicate.txt");
|
var expectedDuplicateFilePath = Path.Combine(directory, "Resources", "temp.test-file-duplicate.txt");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var (duplicateFilePath, createdDuplicate) = this.fileUtilityService.DuplicateFileWithoutLines(fileName, removalIndicator);
|
var (duplicateFilePath, createdDuplicate) = this.fileUtilityService.DuplicateFileWithoutLines(fileName, "//REMOVE", "//ME");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
createdDuplicate.Should().BeTrue();
|
createdDuplicate.Should().BeTrue();
|
||||||
duplicateFilePath.Should().Be(expectedDuplicateFilePath);
|
duplicateFilePath.Should().Be(expectedDuplicateFilePath);
|
||||||
File.Exists(expectedDuplicateFilePath).Should().BeTrue();
|
File.Exists(expectedDuplicateFilePath).Should().BeTrue();
|
||||||
|
|
||||||
|
var contents = File.ReadAllText(expectedDuplicateFilePath);
|
||||||
|
contents.Should().NotContain("//REMOVE");
|
||||||
|
contents.Should().NotContain("//ME");
|
||||||
|
contents.Should().Contain("hello");
|
||||||
|
contents.Should().Contain("world");
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<ItemGroup Label="Project References">
|
<ItemGroup Label="Project References">
|
||||||
<ProjectReference Include="..\Microsoft.ComponentDetection.TestsUtilities\Microsoft.ComponentDetection.TestsUtilities.csproj" />
|
<ProjectReference Include="..\Microsoft.ComponentDetection.TestsUtilities\Microsoft.ComponentDetection.TestsUtilities.csproj" />
|
||||||
|
@ -9,8 +9,8 @@
|
||||||
<PackageReference Include="FluentAssertions.Analyzers" PrivateAssets="all" />
|
<PackageReference Include="FluentAssertions.Analyzers" PrivateAssets="all" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" />
|
<PackageReference Include="Microsoft.Extensions.Logging" />
|
||||||
<PackageReference Include="System.Threading.Tasks.Dataflow" />
|
<PackageReference Include="System.Threading.Tasks.Dataflow" />
|
||||||
<PackageReference Include="MSTest.TestAdapter "/>
|
<PackageReference Include="MSTest.TestAdapter " />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk "/>
|
<PackageReference Include="Microsoft.NET.Test.Sdk " />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
hello
|
hello
|
||||||
//REMOVE
|
//REMOVE
|
||||||
world
|
world
|
||||||
|
//ME
|
||||||
|
|
|
@ -500,7 +500,7 @@ public class PipCommandServiceTests
|
||||||
this.envVarService.Setup(x => x.IsEnvironmentVariableValueTrue("PipReportIgnoreFileLevelIndexUrl")).Returns(true);
|
this.envVarService.Setup(x => x.IsEnvironmentVariableValueTrue("PipReportIgnoreFileLevelIndexUrl")).Returns(true);
|
||||||
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("pip", It.IsAny<IEnumerable<string>>(), "--version")).ReturnsAsync(true);
|
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("pip", It.IsAny<IEnumerable<string>>(), "--version")).ReturnsAsync(true);
|
||||||
|
|
||||||
this.fileUtilityService.Setup(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url"))
|
this.fileUtilityService.Setup(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url", "-i"))
|
||||||
.Returns(("C:/asdf/temp.requirements.txt", true))
|
.Returns(("C:/asdf/temp.requirements.txt", true))
|
||||||
.Verifiable();
|
.Verifiable();
|
||||||
this.fileUtilityService.Setup(x => x.ReadAllTextAsync(It.IsAny<FileInfo>()))
|
this.fileUtilityService.Setup(x => x.ReadAllTextAsync(It.IsAny<FileInfo>()))
|
||||||
|
@ -535,7 +535,7 @@ public class PipCommandServiceTests
|
||||||
this.envVarService.Setup(x => x.IsEnvironmentVariableValueTrue("PipReportIgnoreFileLevelIndexUrl")).Returns(true);
|
this.envVarService.Setup(x => x.IsEnvironmentVariableValueTrue("PipReportIgnoreFileLevelIndexUrl")).Returns(true);
|
||||||
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("pip", It.IsAny<IEnumerable<string>>(), "--version")).ReturnsAsync(true);
|
this.commandLineInvokationService.Setup(x => x.CanCommandBeLocatedAsync("pip", It.IsAny<IEnumerable<string>>(), "--version")).ReturnsAsync(true);
|
||||||
|
|
||||||
this.fileUtilityService.Setup(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url"))
|
this.fileUtilityService.Setup(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url", "-i"))
|
||||||
.Returns((null, false))
|
.Returns((null, false))
|
||||||
.Verifiable();
|
.Verifiable();
|
||||||
this.fileUtilityService.Setup(x => x.ReadAllTextAsync(It.IsAny<FileInfo>()))
|
this.fileUtilityService.Setup(x => x.ReadAllTextAsync(It.IsAny<FileInfo>()))
|
||||||
|
@ -590,7 +590,7 @@ public class PipCommandServiceTests
|
||||||
this.logger.Object);
|
this.logger.Object);
|
||||||
|
|
||||||
var (report, reportFile) = await service.GenerateInstallationReportAsync(testPath);
|
var (report, reportFile) = await service.GenerateInstallationReportAsync(testPath);
|
||||||
this.fileUtilityService.Verify(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url"), Times.Never);
|
this.fileUtilityService.Verify(x => x.DuplicateFileWithoutLines(It.IsAny<string>(), "--index-url", "-i"), Times.Never);
|
||||||
this.commandLineInvokationService.Verify();
|
this.commandLineInvokationService.Verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче