Skip to content

Commit

Permalink
Ignore Comments in .rsp files
Browse files Browse the repository at this point in the history
Example of an .rsp file:
"/example/to/binary/example.exe" # Binary
# ---- Libraries ----
"/example/to/libraries/library1.dll"
"/example/to/libraries/library2.dll"

would result in:
"/example/to/binary/example.exe"
"/example/to/libraries/library1.dll"
"/example/to/libraries/library2.dll"
  • Loading branch information
Lukas Kohl committed Oct 30, 2024
1 parent 8d31b06 commit a4d66a2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/BinSkim.Driver/ExpandArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ private static void ExpandResponseFile(string[] responseFileLines, List<string>
{
foreach (string responseFileLine in responseFileLines)
{
List<string> fileList = ArgumentSplitter.CommandLineToArgvW(responseFileLine.Trim()) ??
string responseFilePath = responseFileLine;

// Ignore comments from response file lines
int commentIndex = responseFileLine.IndexOf('#');
if (commentIndex >= 0)
{
responseFilePath = responseFileLine.Substring(0, commentIndex);
}

List<string> fileList = ArgumentSplitter.CommandLineToArgvW(responseFilePath.Trim()) ??
throw new InvalidOperationException("Could not parse response file line:" + responseFileLine);

expandedArguments.AddRange(fileList);
Expand Down
26 changes: 26 additions & 0 deletions src/Test.UnitTests.BinSkim.Driver/ExpandArgumentsUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ public void GenerateArguments_ExpandsResponseFileContents(string[] rspContent, s
environmentVariablesMock.Verify(ev => ev.ExpandEnvironmentVariables(ResponseFileName), Times.Once);
}

[Theory]
[InlineData(new[] { "/b", "/c:val /d", "# Random Comment" ," /e " }, new[] { "/a", "/b", "/c:val", "/d", "/e", "/f" })]
[InlineData(new[] { "/b", "/c:val /d#Another Comment", " /e " }, new[] { "/a", "/b", "/c:val", "/d", "/e", "/f" })]
public void GenerateArguments_TrimCommentsFromResponseFileContents(string[] rspContent, string[] expected)
{
const string ResponseFileName = "Mocked.rsp";
string[] args = new[] { "/a", "@" + ResponseFileName, "/f" };

SetupTestMocks(
ResponseFileName,
rspContent,
out Mock<IFileSystem> fileSystemMock,
out Mock<IEnvironmentVariables> environmentVariablesMock);

IFileSystem fileSystem = fileSystemMock.Object;
IEnvironmentVariables environmentVariables = environmentVariablesMock.Object;

string[] result = ExpandArguments.GenerateArguments(args, fileSystem, environmentVariables);

result.Should().ContainInOrder(expected);

fileSystemMock.Verify(fs => fs.PathGetFullPath(ResponseFileName), Times.Once);
fileSystemMock.Verify(fs => fs.FileReadAllLines(ResponseFileName), Times.Once);
environmentVariablesMock.Verify(ev => ev.ExpandEnvironmentVariables(ResponseFileName), Times.Once);
}

[Theory]
[InlineData(new[] { "a \"one two\" b" }, new[] { "a", "one two", "b" })]
public void GenerateArguments_StripsQuotesFromAroundArgsWithSpacesInResponseFiles(string[] rspContent, string[] expected)
Expand Down

0 comments on commit a4d66a2

Please sign in to comment.