Skip to content

Commit

Permalink
fix: skip blank lines in requirements.txt (#6867)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylong authored Jul 25, 2024
1 parent 5f41b28 commit 9e14cc8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Used to analyze pip dependency files named requirements.txt.
Expand Down Expand Up @@ -174,15 +175,17 @@ protected void analyzeDependency(Dependency dependency, Engine engine) throws An
}

/**
* Retrieves the contents of a given file.
* Retrieves the contents of a given file without blank lines.
*
* @param actualFile the file to read
* @return the contents of the file
* @throws AnalysisException thrown if there is an IO Exception
*/
private String getFileContents(final File actualFile) throws AnalysisException {
try {
return new String(Files.readAllBytes(actualFile.toPath()), StandardCharsets.UTF_8).trim();
return Files.lines(actualFile.toPath(), Charset.defaultCharset())
.filter(line -> !line.trim().isEmpty())
.collect(Collectors.joining("\n"));
} catch (IOException e) {
throw new AnalysisException("Problem occurred while reading dependency file.", e);
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#
# This file has comments and blank lines to make sure they are ignored
#

certifi==2018.4.16
chardet==3.0.4
click==6.7
Expand All @@ -14,6 +18,7 @@ pymongo==3.6.1
PySimpleSOAP==1.16.2
pytz==2018.4
PyYAML==3.12

requests==2.19.1
sec-wall==1.2
six==1.11.0
Expand Down

0 comments on commit 9e14cc8

Please sign in to comment.