Skip to content

Commit

Permalink
Add requires to nf-test.config in order to specify minimal nf-tes…
Browse files Browse the repository at this point in the history
…t version (#191)

* Add `requires` to config to specify minimal nf-test version

* Add documentation of `requires` keyword

* Fix issue and add testcases

* Update syntax in documentation for `requires`
  • Loading branch information
lukfor authored May 14, 2024
1 parent d81908c commit 38cc083
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"` |
Expand All @@ -14,6 +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"` | |
| `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:

Expand All @@ -30,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:
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/askimed/nf/test/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.askimed.nf.test.config;

import java.io.File;
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;

Expand All @@ -18,6 +22,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";
Expand All @@ -40,6 +46,8 @@ public class Config {

private String stageMode = FileStaging.MODE_COPY;

private Map<String, Object> requires = null;

public void testsDir(String testsDir) {
this.testsDir = testsDir;
}
Expand All @@ -56,6 +64,18 @@ public String getWorkDir() {
return workDir;
}

public void requires(Map<String, Object> requires) {
setRequires(requires);
}

public void setRequires(Map<String, Object> requires) {
this.requires = requires;
}

public Map<String, Object> getRequires() {
return requires;
}

public void profile(String profile) {
this.profile = profile;
}
Expand Down Expand Up @@ -192,6 +212,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;
}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/askimed/nf/test/util/Version.java
Original file line number Diff line number Diff line change
@@ -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;

}

}
17 changes: 17 additions & 0 deletions src/test/java/com/askimed/nf/test/lang/ProcessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions test-data/process/requires/nf-test-0.1.0.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
config {
requires (
"nf-test": "0.1.0"
)
configFile ""
}
6 changes: 6 additions & 0 deletions test-data/process/requires/nf-test-100.0.0.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
config {
requires (
"nf-test": "100.0.0"
)
configFile ""
}

0 comments on commit 38cc083

Please sign in to comment.