Skip to content

Commit

Permalink
make it work with either a list and/or a file
Browse files Browse the repository at this point in the history
  • Loading branch information
maxulysse committed Sep 30, 2024
1 parent 56d4c62 commit 384ddaa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
33 changes: 13 additions & 20 deletions src/main/java/nf_core/nf/test/utils/Methods.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,23 @@ public static Map<String, Map<String, Object>> removeNextflowVersion(CharSequenc

// Return all files in a directory and its sub-directories
// matching or not matching supplied glob
public static List<File> getAllFilesFromDir(String outdir, boolean includeDir, Object ignorePatterns)
public static List<File> getAllFilesFromDir(String outdir, boolean includeDir, List<String> ignoreGlobs,
String ignoreFilePath)
throws IOException {
List<File> output = new ArrayList<>();
Path directory = Paths.get(outdir);

List<String> ignoreGlobs = getIgnoreGlobs(ignorePatterns);
List<String> allIgnoreGlobs = new ArrayList<>();
if (ignoreGlobs != null) {
allIgnoreGlobs.addAll(ignoreGlobs);
}
if (ignoreFilePath != null && !ignoreFilePath.isEmpty()) {
allIgnoreGlobs.addAll(readGlobsFromFile(ignoreFilePath));
}

List<PathMatcher> excludeMatchers = new ArrayList<>();
if (ignoreGlobs != null && !ignoreGlobs.isEmpty()) {
for (String glob : ignoreGlobs) {
excludeMatchers.add(FileSystems.getDefault().getPathMatcher("glob:" + glob));
}
for (String glob : allIgnoreGlobs) {
excludeMatchers.add(FileSystems.getDefault().getPathMatcher("glob:" + glob));
}

Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
Expand Down Expand Up @@ -93,21 +98,9 @@ private boolean isExcluded(Path path) {
.collect(Collectors.toList());
}

private static List<String> getIgnoreGlobs(Object ignorePatterns) throws IOException {
if (ignorePatterns instanceof List) {
return (List<String>) ignorePatterns;
} else if (ignorePatterns instanceof File) {
return readGlobsFromFile((File) ignorePatterns);
} else if (ignorePatterns instanceof String) {
return readGlobsFromFile(new File((String) ignorePatterns));
} else {
throw new IllegalArgumentException("ignorePatterns must be a List<String>, File, or String (file path)");
}
}

private static List<String> readGlobsFromFile(File file) throws IOException {
private static List<String> readGlobsFromFile(String filePath) throws IOException {
List<String> globs = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
Expand Down
1 change: 0 additions & 1 deletion tests/getAllFilesFromDir/.nftignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pipeline_info/execution_trace_*.txt
**/stable_name.txt
4 changes: 2 additions & 2 deletions tests/getAllFilesFromDir/main.nf.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ nextflow_pipeline {

then {
// Use getAllFilesFromDir() to get a list of all files and folders from the output directory, minus the timestamped files
def stable_name = getAllFilesFromDir(params.outdir, true, ['**/*[0-9]*.txt'] )
def stable_name = getAllFilesFromDir(params.outdir, true, ['**/*[0-9]*.txt'], null )
// Use getAllFilesFromDir() to get a list of all files from the output directory, minus the non-stable files
def stable_content = getAllFilesFromDir(params.outdir, false, 'tests/getAllFilesFromDir/.nftignore' )
def stable_content = getAllFilesFromDir(params.outdir, false, ['**/*[0-9]*.txt'], 'tests/getAllFilesFromDir/.nftignore' )
assert snapshot(
// Only snapshot name
stable_name*.name,
Expand Down

0 comments on commit 384ddaa

Please sign in to comment.