diff --git a/src/BinSkim.Driver/ExpandArguments.cs b/src/BinSkim.Driver/ExpandArguments.cs index 0dea5395..f1cf5d7b 100644 --- a/src/BinSkim.Driver/ExpandArguments.cs +++ b/src/BinSkim.Driver/ExpandArguments.cs @@ -48,7 +48,16 @@ private static void ExpandResponseFile(string[] responseFileLines, List { foreach (string responseFileLine in responseFileLines) { - List 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 fileList = ArgumentSplitter.CommandLineToArgvW(responseFilePath.Trim()) ?? throw new InvalidOperationException("Could not parse response file line:" + responseFileLine); expandedArguments.AddRange(fileList); diff --git a/src/Test.UnitTests.BinSkim.Driver/ExpandArgumentsUnitTests.cs b/src/Test.UnitTests.BinSkim.Driver/ExpandArgumentsUnitTests.cs index d516466f..7dee14ae 100644 --- a/src/Test.UnitTests.BinSkim.Driver/ExpandArgumentsUnitTests.cs +++ b/src/Test.UnitTests.BinSkim.Driver/ExpandArgumentsUnitTests.cs @@ -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 fileSystemMock, + out Mock 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)