Skip to content

Commit

Permalink
Merge pull request #7 from maxulysse/getAllFilesFromDir
Browse files Browse the repository at this point in the history
Add getAllFilesFromDir function
  • Loading branch information
maxulysse authored Sep 30, 2024
2 parents c723e2f + f6aba50 commit bc0e6c9
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/main/java/nf_core/nf/test/utils/Methods.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package nf_core.nf.test.utils;

import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class Methods {

// Read a Version YAML file and return a Map of Map
public static Map<String, Map<String, Object>> readYamlFile(String filePath) {
Yaml yaml = new Yaml();


try (FileReader reader = new FileReader(filePath)) {
Map<String, Map<String, Object>> data = yaml.load(reader);
return data;
Expand All @@ -19,6 +25,8 @@ public static Map<String, Map<String, Object>> readYamlFile(String filePath) {
}
}

// Removed the Nextflow entry from the Workflow entry
// within the input Version YAML file
public static Map<String, Map<String, Object>> removeNextflowVersion(CharSequence versionFile) {
String yamlFilePath = versionFile.toString();
Map<String, Map<String, Object>> yamlData = readYamlFile(yamlFilePath);
Expand All @@ -31,4 +39,46 @@ public static Map<String, Map<String, Object>> removeNextflowVersion(CharSequenc
}
return yamlData;
}

// Return all files in a directory and its sub-directories
// matching or not matching supplied regexes
public static List<File> getAllFilesFromDir(String outdir, boolean includeDir, List<String> excludeRegexes) {
List<File> output = new ArrayList<>();
File directory = new File(outdir);

getAllFilesRecursively(directory, includeDir, excludeRegexes, output);

Collections.sort(output);
return output;
}

// Recursively list all files in a directory and its sub-directories
// matching or not matching supplied regexes
private static void getAllFilesRecursively(File directory, boolean includeDir, List<String> excludeRegexes,
List<File> output) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
boolean matchesInclusion = includeDir || file.isFile();
boolean matchesExclusion = false;

if (excludeRegexes != null) {
for (String regex : excludeRegexes) {
if (Pattern.matches(regex, file.getName())) {
matchesExclusion = true;
break;
}
}
}

if (matchesInclusion && !matchesExclusion) {
output.add(file);
}

if (file.isDirectory()) {
getAllFilesRecursively(file, includeDir, excludeRegexes, output);
}
}
}
}
}
23 changes: 23 additions & 0 deletions tests/getAllFilesFromDir/main.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
workflow {

def trace_timestamp = new java.util.Date().format( 'yyyy-MM-dd_HH-mm-ss')

ch_stable_content = Channel.of(
"""
I HAVE STABLE CONTENT
""".stripIndent().trim())
.collectFile(storeDir: "${params.outdir}/stable", name: 'stable_content.txt', sort: true, newLine: true)

ch_stable_name = Channel.of(
"""
I DO NOT HAVE STABLE CONTENT
${trace_timestamp}
""".stripIndent().trim())
.collectFile(storeDir: "${params.outdir}/stable", name: 'stable_name.txt', sort: true, newLine: true)

ch_unstable_name = Channel.of(
"""
I DO NOT HAVE STABLE NAME
""".stripIndent().trim())
.collectFile(storeDir: "${params.outdir}/not_stable", name: "${trace_timestamp}.txt", sort: true, newLine: true)
}
24 changes: 24 additions & 0 deletions tests/getAllFilesFromDir/main.nf.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
nextflow_pipeline {

name "Test getAllFilesFromDir"
script "./main.nf"
tag "getAllFilesFromDir"

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

then {
def timestamp = [/.*\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}.*/]
def stable_name = getAllFilesFromDir(params.outdir, false, timestamp)
def stable_content = getAllFilesFromDir(params.outdir, false, timestamp + [/stable_name\.txt/] )
assert snapshot(
stable_name*.name,
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
@@ -0,0 +1,18 @@
{
"getAllFilesFromDir": {
"content": [
[
"stable_content.txt",
"stable_name.txt"
],
[
"stable_content.txt:md5,f6d73f703cda1c725ea78369a6c3a48d"
]
],
"meta": {
"nf-test": "0.9.0",
"nextflow": "24.04.4"
},
"timestamp": "2024-09-30T10:55:42.424751"
}
}
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions tests/removeNextflowVersion/main.nf.test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"removeNextflowVersion": {
"content": [
{
"Workflow": {
"Pipeline": "1.0.0"
}
}
],
"meta": {
"nf-test": "0.9.0",
"nextflow": "24.04.4"
},
"timestamp": "2024-09-27T19:17:42.066036"
}
}

0 comments on commit bc0e6c9

Please sign in to comment.