From 4e682fe70585bd3fadd9398aae3f80545301b6a6 Mon Sep 17 00:00:00 2001 From: maxulysse Date: Mon, 7 Oct 2024 13:02:42 +0200 Subject: [PATCH 1/4] add removeFromYaml --- README.md | 29 +++++++++++++++++++ .../java/nf_core/nf/test/utils/Methods.java | 21 ++++++++++++-- tests/removeFromYaml/main.nf | 11 +++++++ tests/removeFromYaml/main.nf.test | 18 ++++++++++++ tests/removeFromYaml/main.nf.test.snap | 17 +++++++++++ 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 tests/removeFromYaml/main.nf create mode 100644 tests/removeFromYaml/main.nf.test create mode 100644 tests/removeFromYaml/main.nf.test.snap diff --git a/README.md b/README.md index 23934cb..4df2820 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,35 @@ assert snapshot(removeNextflowVersion("$outputDir/pipeline_info/nf_core_rnaseq_s The only argument is path to the file which must be a versions file in YAML format as per the nf-core standard. +## `removeFromYaml()` + +Remove any key from a YAML file. + +```yaml +UNTAR: + untar: 1.34 +Workflow: + nf-core/rnaseq: v3.16.0dev + Nextflow: 24.04.4 +``` + +This function remove the Nextflow version from this yml file, as it is not relevant for the snapshot. Therefore for the purpose of the snapshot, it would consider this to be the contents of the YAML file: + +```yaml +UNTAR: + untar: 1.34 +Workflow: + nf-core/rnaseq: v3.16.0dev +``` + +Usage: + +```groovy +assert snapshot(removeFromYaml("$outputDir/pipeline_info/nf_core_pipeline_software_mqc_versions.yml", "Workflow", "Nextflow")).match() +``` + +The first argument is path to the YAML file, the second and third arguments are the key and subkey to remove. + ## `getAllFilesFromDir()` Files produced by a pipeline can be compared to a snapshot. diff --git a/src/main/java/nf_core/nf/test/utils/Methods.java b/src/main/java/nf_core/nf/test/utils/Methods.java index 80f6c99..78d1145 100644 --- a/src/main/java/nf_core/nf/test/utils/Methods.java +++ b/src/main/java/nf_core/nf/test/utils/Methods.java @@ -45,17 +45,32 @@ public static Map> removeNextflowVersion(CharSequenc return yamlData; } - //wrapper functions for getAllFilesFromDir with default options + // Removed the Key2 entry from the Key1 entry + // within the input Version YAML file + public static Map> removeFromYaml(CharSequence versionFile, String Key1, String Key2) { + String yamlFilePath = versionFile.toString(); + Map> yamlData = readYamlFile(yamlFilePath); + + if (yamlData != null) { + // Access and use the YAML data + if (yamlData.containsKey("Key1")) { + yamlData.get("Key1").remove("Key2"); + } + } + return yamlData; + } + + // wrapper functions for getAllFilesFromDir with default options public static List getAllFilesFromDir(String path) throws IOException { return getAllFilesFromDir(new LinkedHashMap(), path); } - //wrapper functions for getAllFilesFromDir with named options + // wrapper functions for getAllFilesFromDir with named options public static List getAllFilesFromDir(LinkedHashMap options, String path) throws IOException { if (path == null || path.isEmpty()) { throw new IllegalArgumentException("The 'path' parameter is required."); } - //TODO: check if path exists + // TODO: check if path exists // Extract optional parameters from the map (use defaults if not provided) Boolean includeDir = (Boolean) options.getOrDefault("includeDir", true); diff --git a/tests/removeFromYaml/main.nf b/tests/removeFromYaml/main.nf new file mode 100644 index 0000000..23cdf45 --- /dev/null +++ b/tests/removeFromYaml/main.nf @@ -0,0 +1,11 @@ +workflow { + + ch_version = Channel.of( + """ + Workflow: + Pipeline: 1.0.0 + Nextflow: $workflow.nextflow.version + """.stripIndent().trim()) + .collectFile(storeDir: "${params.outdir}/pipeline_info", name: 'nf_core_pipeline_software_mqc_versions.yml', sort: true, newLine: true) + +} diff --git a/tests/removeFromYaml/main.nf.test b/tests/removeFromYaml/main.nf.test new file mode 100644 index 0000000..e4b5c28 --- /dev/null +++ b/tests/removeFromYaml/main.nf.test @@ -0,0 +1,18 @@ +nextflow_pipeline { + + name "Test removeFromYaml" + script "./main.nf" + tag "removeFromYaml" + + test("removeFromYaml") { + when { + params { + outdir = "$outputDir" + } + } + + then { + assert snapshot(removeFromYaml("$outputDir/pipeline_info/nf_core_pipeline_software_mqc_versions.yml", "Workflow", "Nextflow")).match() + } + } +} diff --git a/tests/removeFromYaml/main.nf.test.snap b/tests/removeFromYaml/main.nf.test.snap new file mode 100644 index 0000000..fc3ceef --- /dev/null +++ b/tests/removeFromYaml/main.nf.test.snap @@ -0,0 +1,17 @@ +{ + "removeFromYaml": { + "content": [ + { + "Workflow": { + "Pipeline": "1.0.0", + "Nextflow": "24.09.0-edge" + } + } + ], + "meta": { + "nf-test": "0.9.0", + "nextflow": "24.09.0" + }, + "timestamp": "2024-10-07T13:00:09.562381" + } +} \ No newline at end of file From ff1cf945088cf4d148a1ede8161c0143216aa915 Mon Sep 17 00:00:00 2001 From: maxulysse Date: Mon, 7 Oct 2024 17:01:25 +0200 Subject: [PATCH 2/4] fix function and update tests --- src/main/java/nf_core/nf/test/utils/Methods.java | 4 ++-- tests/removeFromYaml/main.nf.test.snap | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/nf_core/nf/test/utils/Methods.java b/src/main/java/nf_core/nf/test/utils/Methods.java index 78d1145..1421646 100644 --- a/src/main/java/nf_core/nf/test/utils/Methods.java +++ b/src/main/java/nf_core/nf/test/utils/Methods.java @@ -53,8 +53,8 @@ public static Map> removeFromYaml(CharSequence versi if (yamlData != null) { // Access and use the YAML data - if (yamlData.containsKey("Key1")) { - yamlData.get("Key1").remove("Key2"); + if (yamlData.containsKey(Key1)) { + yamlData.get(Key1).remove(Key2); } } return yamlData; diff --git a/tests/removeFromYaml/main.nf.test.snap b/tests/removeFromYaml/main.nf.test.snap index fc3ceef..5f78a59 100644 --- a/tests/removeFromYaml/main.nf.test.snap +++ b/tests/removeFromYaml/main.nf.test.snap @@ -3,8 +3,7 @@ "content": [ { "Workflow": { - "Pipeline": "1.0.0", - "Nextflow": "24.09.0-edge" + "Pipeline": "1.0.0" } } ], @@ -12,6 +11,6 @@ "nf-test": "0.9.0", "nextflow": "24.09.0" }, - "timestamp": "2024-10-07T13:00:09.562381" + "timestamp": "2024-10-07T17:00:34.208865" } } \ No newline at end of file From aa5ab933bcd8b5b5680eda473de496e468582318 Mon Sep 17 00:00:00 2001 From: maxulysse Date: Mon, 7 Oct 2024 17:33:28 +0200 Subject: [PATCH 3/4] rename removeFromYaml -> removeFromYamlMap --- .../java/nf_core/nf/test/utils/Methods.java | 2 +- tests/removeFromYaml/main.nf.test | 18 ------------------ .../main.nf | 0 tests/removeFromYamlMap/main.nf.test | 18 ++++++++++++++++++ .../main.nf.test.snap | 4 ++-- 5 files changed, 21 insertions(+), 21 deletions(-) delete mode 100644 tests/removeFromYaml/main.nf.test rename tests/{removeFromYaml => removeFromYamlMap}/main.nf (100%) create mode 100644 tests/removeFromYamlMap/main.nf.test rename tests/{removeFromYaml => removeFromYamlMap}/main.nf.test.snap (76%) diff --git a/src/main/java/nf_core/nf/test/utils/Methods.java b/src/main/java/nf_core/nf/test/utils/Methods.java index 1421646..8c4d97b 100644 --- a/src/main/java/nf_core/nf/test/utils/Methods.java +++ b/src/main/java/nf_core/nf/test/utils/Methods.java @@ -47,7 +47,7 @@ public static Map> removeNextflowVersion(CharSequenc // Removed the Key2 entry from the Key1 entry // within the input Version YAML file - public static Map> removeFromYaml(CharSequence versionFile, String Key1, String Key2) { + public static Map> removeFromYamlMap(CharSequence versionFile, String Key1, String Key2) { String yamlFilePath = versionFile.toString(); Map> yamlData = readYamlFile(yamlFilePath); diff --git a/tests/removeFromYaml/main.nf.test b/tests/removeFromYaml/main.nf.test deleted file mode 100644 index e4b5c28..0000000 --- a/tests/removeFromYaml/main.nf.test +++ /dev/null @@ -1,18 +0,0 @@ -nextflow_pipeline { - - name "Test removeFromYaml" - script "./main.nf" - tag "removeFromYaml" - - test("removeFromYaml") { - when { - params { - outdir = "$outputDir" - } - } - - then { - assert snapshot(removeFromYaml("$outputDir/pipeline_info/nf_core_pipeline_software_mqc_versions.yml", "Workflow", "Nextflow")).match() - } - } -} diff --git a/tests/removeFromYaml/main.nf b/tests/removeFromYamlMap/main.nf similarity index 100% rename from tests/removeFromYaml/main.nf rename to tests/removeFromYamlMap/main.nf diff --git a/tests/removeFromYamlMap/main.nf.test b/tests/removeFromYamlMap/main.nf.test new file mode 100644 index 0000000..7fd2594 --- /dev/null +++ b/tests/removeFromYamlMap/main.nf.test @@ -0,0 +1,18 @@ +nextflow_pipeline { + + name "Test removeFromYamlMap" + script "./main.nf" + tag "removeFromYamlMap" + + test("removeFromYamlMap") { + when { + params { + outdir = "$outputDir" + } + } + + then { + assert snapshot(removeFromYamlMap("$outputDir/pipeline_info/nf_core_pipeline_software_mqc_versions.yml", "Workflow", "Nextflow")).match() + } + } +} diff --git a/tests/removeFromYaml/main.nf.test.snap b/tests/removeFromYamlMap/main.nf.test.snap similarity index 76% rename from tests/removeFromYaml/main.nf.test.snap rename to tests/removeFromYamlMap/main.nf.test.snap index 5f78a59..92db94d 100644 --- a/tests/removeFromYaml/main.nf.test.snap +++ b/tests/removeFromYamlMap/main.nf.test.snap @@ -1,5 +1,5 @@ { - "removeFromYaml": { + "removeFromYamlMap": { "content": [ { "Workflow": { @@ -11,6 +11,6 @@ "nf-test": "0.9.0", "nextflow": "24.09.0" }, - "timestamp": "2024-10-07T17:00:34.208865" + "timestamp": "2024-10-07T17:32:04.309756" } } \ No newline at end of file From 64eaf6660ba8e626a060762f33dd53117ed277d2 Mon Sep 17 00:00:00 2001 From: maxulysse Date: Mon, 7 Oct 2024 17:35:11 +0200 Subject: [PATCH 4/4] update docs --- docs/usage.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/usage.md b/docs/usage.md index cd2d50e..2b3edd5 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -33,6 +33,35 @@ assert snapshot(removeNextflowVersion("$outputDir/pipeline_info/nf_core_rnaseq_s The only argument is path to the file which must be a versions file in YAML format as per the nf-core standard. +## `removeFromYamlMap()` + +Remove any key from a YAML file. + +```yaml +UNTAR: + untar: 1.34 +Workflow: + nf-core/rnaseq: v3.16.0dev + Nextflow: 24.04.4 +``` + +This function remove the Nextflow version from this yml file, as it is not relevant for the snapshot. Therefore for the purpose of the snapshot, it would consider this to be the contents of the YAML file: + +```yaml +UNTAR: + untar: 1.34 +Workflow: + nf-core/rnaseq: v3.16.0dev +``` + +Usage: + +```groovy +assert snapshot(removeFromYamlMap("$outputDir/pipeline_info/nf_core_pipeline_software_mqc_versions.yml", "Workflow", "Nextflow")).match() +``` + +The first argument is path to the YAML file, the second and third arguments are the key and subkey to remove. + ## `getAllFilesFromDir()` This function generates a list of all the contents within a directory, allowing for the exclusion of specific files using a glob pattern. It can be used to obtain filenames alone, enabling snapshotting of just the names when the content is not stable. Alternatively, it can snapshot the entire list of files with stable content.