Skip to content

Commit

Permalink
Merge pull request #18 from lukfor/main
Browse files Browse the repository at this point in the history
Add wrapper functions for `getAllFilesFromDir` with named parameters
  • Loading branch information
maxulysse authored Oct 2, 2024
2 parents f309858 + 7694758 commit 10581d1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/main/java/nf_core/nf/test/utils/Methods.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import org.yaml.snakeyaml.Yaml;

Expand Down Expand Up @@ -48,6 +45,33 @@ public static Map<String, Map<String, Object>> removeNextflowVersion(CharSequenc
return yamlData;
}

//wrapper functions for getAllFilesFromDir with default options
public static List getAllFilesFromDir(String path) throws IOException {
return getAllFilesFromDir(new LinkedHashMap<String, Object>(), path);
}

//wrapper functions for getAllFilesFromDir with named options
public static List getAllFilesFromDir(LinkedHashMap<String, Object> options, String path) throws IOException {
if (path == null || path.isEmpty()) {
throw new IllegalArgumentException("The 'path' parameter is required.");
}
//TODO: check if path exists

// Extract optional parameters from the map (use defaults if not provided)
Boolean includeDir = (Boolean) options.getOrDefault("includeDir", true);
List<String> ignoreGlobs = (List<String>) options.getOrDefault("ignore", new ArrayList<String>());
String ignoreFilePath = (String) options.get("ignoreFile");
Boolean relative = (Boolean) options.getOrDefault("relative", false);

List<File> files = getAllFilesFromDir(path, includeDir, ignoreGlobs, ignoreFilePath);

if (relative) {
return getRelativePath(files, path);
} else {
return files;
}
}

// 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, List<String> ignoreGlobs,
Expand Down
24 changes: 24 additions & 0 deletions tests/getAllFilesFromDir/main.nf.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,28 @@ nextflow_pipeline {
).match()
}
}

test("getAllFilesFromDir with named params") {
when {
params {
outdir = "$outputDir"
}
}

then {
//with default params and relative
println(getAllFilesFromDir(params.outdir))
println(getAllFilesFromDir(params.outdir, relative: true))
// 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, ignore: ['pipeline_info/execution_*.{html,txt}'])
// works also with spaces.
def stable_content = getAllFilesFromDir params.outdir, includeDir: false, ignore: ['pipeline_info/execution_*.{html,txt}'], ignoreFile: 'tests/getAllFilesFromDir/.nftignore'
assert snapshot(
// Only snapshot name
stable_name*.name,
// Snapshot content
stable_content
).match()
}
}
}
18 changes: 18 additions & 0 deletions tests/getAllFilesFromDir/main.nf.test.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
{
"getAllFilesFromDir with named params": {
"content": [
[
"pipeline_info",
"stable",
"stable_content.txt",
"stable_name.txt"
],
[
"stable_content.txt:md5,f6d73f703cda1c725ea78369a6c3a48d"
]
],
"meta": {
"nf-test": "0.9.0",
"nextflow": "24.07.0"
},
"timestamp": "2024-10-02T10:15:47.491035"
},
"getAllFilesFromDir": {
"content": [
[
Expand Down

0 comments on commit 10581d1

Please sign in to comment.