Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getAllFilesFromDir function #7

Merged
merged 8 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 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 {
public static Map<String, Map<String, Object>> readYamlFile(String filePath) {
Yaml yaml = new Yaml();


// Read a Version YAML file and return a Map of Map
public static Map<String, Map<String, Object>> readVersionYamlFile(String filePath) {
maxulysse marked this conversation as resolved.
Show resolved Hide resolved
Yaml yaml = new Yaml();
try (FileReader reader = new FileReader(filePath)) {
Map<String, Map<String, Object>> data = yaml.load(reader);
return data;
Expand All @@ -19,9 +25,11 @@ 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);
Map<String, Map<String, Object>> yamlData = readVersionYamlFile(yamlFilePath);

if (yamlData != null) {
// Access and use the YAML data
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"
}
}