diff --git a/docs/docs/configuration.md b/docs/docs/configuration.md index 4e95ee2f..d0e1e328 100644 --- a/docs/docs/configuration.md +++ b/docs/docs/configuration.md @@ -5,7 +5,7 @@ The `nf-test.config` file is a configuration file used to customize settings and behavior for `nf-test`. This file must be located in the root of your project, and it is automatically loaded when you run `nf-test test`. Below are the parameters that can be adapted: | Parameter | Description | Default Value | -| ------------ |--------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------| +|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------| ------------------------- | | `testsDir` | Location for storing all nf-test cases (test scripts). If you want all test files to be in the same directory as the script itself, you can set the testDir to `.` | `"tests"` | | `workDir` | Directory for storing temporary files and working directories for each test. This directory should be added to `.gitignore`. | `".nf-test"` | | `configFile` | Location of an optional `nextflow.config` file specifically used for executing tests. [Learn more](#testsnextflowconfig). | `"tests/nextflow.config"` | @@ -14,7 +14,8 @@ The `nf-test.config` file is a configuration file used to customize settings and | `withTrace` | Enable or disable tracing options during testing. Disable tracing if your containers don't include the `procps` tool. | `true` | | `autoSort` | Enable or disable sorted channels by default when running tests. | `true` | | `options` | Custom Nextflow command-line options to be applied when running tests. For example `"-dump-channels -stub-run"` | | - | `ignore` | List of filenames or patterns that should be ignored when building the dependency graph. For example: `ignore ['folder/**/*.nf', 'modules/module.nf']` | `` | +| `ignore` | List of filenames or patterns that should be ignored when building the dependency graph. For example: `ignore ['folder/**/*.nf', 'modules/module.nf']` | `` | +| `requires` | Can be used to specify the minimum required version of nf-test. Requires nf-test > 0.9.0 | `` | Here's an example of what an `nf-test.config` file could look like: @@ -31,6 +32,17 @@ config { } ``` +The `requires` keyword can be used to specify the minimum required version of nf-test. +For instance, to ensure the use of at least nf-test version 0.9.0, define it as follows: + +```groovy +config { + requires ( + "nf-test": "0.9.0" + ) +} +``` + ## `tests/nextflow.config` This optional `nextflow.config` file is used to execute tests. This is a good place to set default `params` for all your tests. Example number of threads: diff --git a/src/main/java/com/askimed/nf/test/config/Config.java b/src/main/java/com/askimed/nf/test/config/Config.java index e22d53e9..effa0196 100644 --- a/src/main/java/com/askimed/nf/test/config/Config.java +++ b/src/main/java/com/askimed/nf/test/config/Config.java @@ -3,7 +3,11 @@ import java.io.File; import java.util.List; import java.util.Vector; +import java.util.Map; +import com.askimed.nf.test.App; +import com.askimed.nf.test.nextflow.NextflowCommand; +import com.askimed.nf.test.util.Version; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.customizers.ImportCustomizer; @@ -20,6 +24,8 @@ public class Config { public static final String DEFAULT_HOME = ".nf-test"; + public static final String KEY_NF_TEST_VERSION = "nf-test"; + private String workDir = ".nf-test"; private String testsDir = "tests"; @@ -43,6 +49,8 @@ public class Config { private String stageMode = FileStaging.MODE_COPY; private List ignore = new Vector(); + + private Map requires = null; public void testsDir(String testsDir) { this.testsDir = testsDir; @@ -60,6 +68,18 @@ public String getWorkDir() { return workDir; } + public void requires(Map requires) { + setRequires(requires); + } + + public void setRequires(Map requires) { + this.requires = requires; + } + + public Map getRequires() { + return requires; + } + public void profile(String profile) { this.profile = profile; } @@ -212,6 +232,17 @@ public static Config parse(File script) throws Exception { Object object = shell.evaluate(script); Config config = (Config) object; + // no requirements + if (config.getRequires() == null ){ + return config; + } + + // check nf-test version + String appVersion = config.getRequires().getOrDefault(KEY_NF_TEST_VERSION, App.VERSION).toString(); + if (Version.compare(appVersion, App.VERSION) >= 1) { + throw new Exception("nf-test " + appVersion + " or above is required to run this project"); + } + return config; } diff --git a/src/main/java/com/askimed/nf/test/util/Version.java b/src/main/java/com/askimed/nf/test/util/Version.java new file mode 100644 index 00000000..e63b70f2 --- /dev/null +++ b/src/main/java/com/askimed/nf/test/util/Version.java @@ -0,0 +1,41 @@ +package com.askimed.nf.test.util; + +public class Version { + + public static int compare(String version1, String version2) { + + String parts1[] = version1.split("-", 2); + String parts2[] = version2.split("-", 2); + + String tiles1[] = parts1[0].split("\\."); + String tiles2[] = parts2[0].split("\\."); + + for (int i = 0; i < tiles1.length; i++) { + int number1 = Integer.parseInt(tiles1[i].trim()); + int number2 = Integer.parseInt(tiles2[i].trim()); + + if (number1 != number2) { + + return number1 > number2 ? 1 : -1; + + } + + } + + if (parts1.length > 1) { + if (parts2.length > 1) { + return parts1[1].compareTo(parts2[1]); + } else { + return -1; + } + } else { + if (parts2.length > 1) { + return 1; + } + } + + return 0; + + } + +} diff --git a/src/test/java/com/askimed/nf/test/lang/ProcessTest.java b/src/test/java/com/askimed/nf/test/lang/ProcessTest.java index 1e79e4c1..4a5492c1 100644 --- a/src/test/java/com/askimed/nf/test/lang/ProcessTest.java +++ b/src/test/java/com/askimed/nf/test/lang/ProcessTest.java @@ -290,6 +290,23 @@ public void testProfilesOverwriteInConfig() throws Exception { } + @Test + public void testRequires() throws Exception { + + App app = new App(); + int exitCode = app.run(new String[] { "test", "test-data/process/profiles/hello.a.nf.test", "--config", + "test-data/process/requires/nf-test-0.1.0.config" }); + assertEquals(0, exitCode); + + app = new App(); + exitCode = app.run(new String[] { "test", "test-data/process/profiles/hello.a.nf.test", "--config", + "test-data/process/requires/nf-test-100.0.0.config" }); + assertEquals(2, exitCode); + + + } + + @Test public void testUniquenessSnapshots() throws Exception { App app = new App(); diff --git a/test-data/process/requires/nf-test-0.1.0.config b/test-data/process/requires/nf-test-0.1.0.config new file mode 100644 index 00000000..a272c01f --- /dev/null +++ b/test-data/process/requires/nf-test-0.1.0.config @@ -0,0 +1,6 @@ +config { + requires ( + "nf-test": "0.1.0" + ) + configFile "" +} \ No newline at end of file diff --git a/test-data/process/requires/nf-test-100.0.0.config b/test-data/process/requires/nf-test-100.0.0.config new file mode 100644 index 00000000..d5255ef8 --- /dev/null +++ b/test-data/process/requires/nf-test-100.0.0.config @@ -0,0 +1,6 @@ +config { + requires ( + "nf-test": "100.0.0" + ) + configFile "" +} \ No newline at end of file