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

Enable the execution of dependent modules for generating test data #127

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
155 changes: 155 additions & 0 deletions docs/docs/testcases/setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Setup Method

The setup method allows you to specify processes or workflows that need to be executed before the primary `when` block. It serves as a mechanism to prepare the required input data or set up essential steps prior to the primary processing block.

## Syntax

The setup method is typically used within the context of a test case. The basic syntax for the setup method is as follows:

```groovy
test("my test"){
setup {
// Define and execute dependent processes or workflows here
}
}
```

Within the setup block, you can use the `run` method to define and execute dependent processes or workflows.

The `run` method syntax for a process is as follows:

```groovy
run("ProcessName") {
script "path/to/process/script.nf"
process {
// Define the process inputs here
}
}
```

The `run` method syntax for a workflow is as follows:

```groovy
run("WorkflowName") {
script "path/to/workflow/script.nf"
workflow {
// Define the workflow inputs here
}
}
```

!!! warning

Please keep in mind that changes in procsses or workflows, which are executed in the setup method, can result in a failed test run.

## Example Usage

### 1. Local Setup Method

In this example, we create a setup method within a Nextflow process definition to execute a dependent process named "ABRICATE_RUN." This process generates input data that is required for the primary process "ABRICATE_SUMMARY." The `setup` block specifies the execution of "ABRICATE_RUN," and the `when` block defines the processing logic for "ABRICATE_SUMMARY."

```groovy
nextflow_process {

name "Test process data"

script "../main.nf"
process "ABRICATE_SUMMARY"
config "./nextflow.config"

test("Should use process ABRICATE_RUN to generate input data") {

setup {

run("ABRICATE_RUN") {
script "../../run/main.nf"
process {
"""
input[0] = Channel.fromList([
tuple([ id:'test1', single_end:false ], // meta map
file(params.test_data['bacteroides_fragilis']['genome']['genome_fna_gz'], checkIfExists: true)),
tuple([ id:'test2', single_end:false ],
file(params.test_data['haemophilus_influenzae']['genome']['genome_fna_gz'], checkIfExists: true))
])
"""
}
}

}

when {
process {
"""
input[0] = ABRICATE_RUN.out.report.collect{ meta, report -> report }.map{ report -> [[ id: 'test_summary'], report]}
"""
}
}

then {
assert process.success
assert snapshot(process.out).match()
}
}

}
```

### 2. Global Setup Method

In this example, a global setup method is defined for all tests within a Nextflow process definition. The setup method is applied to multiple test cases, ensuring consistent setup for each test. This approach is useful when multiple tests share the same setup requirements.

```groovy
nextflow_process {

name "Test process data"

script "../main.nf"
process "ABRICATE_SUMMARY"
config "./nextflow.config"

setup {
run("ABRICATE_RUN") {
script "../../run/main.nf"
process {
"""
input[0] = Channel.fromList([
tuple([ id:'test1', single_end:false ], // meta map
file(params.test_data['bacteroides_fragilis']['genome']['genome_fna_gz'], checkIfExists: true)),
tuple([ id:'test2', single_end:false ],
file(params.test_data['haemophilus_influenzae']['genome']['genome_fna_gz'], checkIfExists: true))
])
"""
}
}
}

test("first test") {
when {
process {
"""
input[0] = ABRICATE_RUN.out.report.collect{ meta, report -> report }.map{ report -> [[ id: 'test_summary'], report]}
"""
}
}
then {
assert process.success
assert snapshot(process.out).match()
}
}

test("second test") {
when {
process {
"""
input[0] = ABRICATE_RUN.out.report.collect{ meta, report -> report }.map{ report -> [[ id: 'test_summary'], report]}
"""
}
}
then {
assert process.success
assert snapshot(process.out).match()
}
}

}
```
80 changes: 40 additions & 40 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ copyright: "© 2021 - 2023 Lukas Forer and Sebastian Schönherr"
theme:
name: material
features:
- content.tooltips
#- navigation.expand
- navigation.footer
- navigation.tabs
#- navigation.tabs.sticky
- search.highlight
- search.share
- search.suggest
- toc.integrate
- content.tabs.link
- content.tooltips
#- navigation.expand
- navigation.footer
- navigation.tabs
#- navigation.tabs.sticky
- search.highlight
- search.share
- search.suggest
- toc.integrate
- content.tabs.link
font:
text: Segoe UI

Expand All @@ -36,44 +36,44 @@ nav:
- Home: index.md
- Installation: installation.md
- Documentation:
- Getting Started: docs/getting-started.md
- Writing Tests:
- Pipeline Testing: docs/testcases/nextflow_pipeline.md
- Workflow Testing: docs/testcases/nextflow_workflow.md
- Process Testing: docs/testcases/nextflow_process.md
- Function Testing: docs/testcases/nextflow_function.md
- Params Dictionary: docs/testcases/params.md
- Global Variables: docs/testcases/global_variables.md
- Running Tests: docs/running-tests.md
- Writing Assertions:
- Power Assertions: docs/assertions/assertions.md
- Files: docs/assertions/files.md
- Snapshots: docs/assertions/snapshots.md
- Regular Expressions: docs/assertions/regular-expressions.md
- FASTA Files: docs/assertions/fasta.md
- Using Third-Party Libraries: docs/assertions/libraries.md
- Command Line Interface (CLI):
- init: docs/cli/init.md
- generate: docs/cli/generate.md
- test: docs/cli/test.md
- list: docs/cli/list.md
- clean: docs/cli/clean.md
- Configuration: docs/configuration.md
- Plugins:
- Available Plugins: https://code.askimed.com/nf-test-plugins
- Using Plugins: docs/plugins/using-plugins.md
- Developing Plugins: docs/plugins/developing-plugins.md
- Getting Started: docs/getting-started.md
- Writing Tests:
- Pipeline Testing: docs/testcases/nextflow_pipeline.md
- Workflow Testing: docs/testcases/nextflow_workflow.md
- Process Testing: docs/testcases/nextflow_process.md
- Function Testing: docs/testcases/nextflow_function.md
- Params Dictionary: docs/testcases/params.md
- Setup Method: docs/testcases/setup.md
- Global Variables: docs/testcases/global_variables.md
- Running Tests: docs/running-tests.md
- Writing Assertions:
- Power Assertions: docs/assertions/assertions.md
- Files: docs/assertions/files.md
- Snapshots: docs/assertions/snapshots.md
- Regular Expressions: docs/assertions/regular-expressions.md
- FASTA Files: docs/assertions/fasta.md
- Using Third-Party Libraries: docs/assertions/libraries.md
- Command Line Interface (CLI):
- init: docs/cli/init.md
- generate: docs/cli/generate.md
- test: docs/cli/test.md
- list: docs/cli/list.md
- clean: docs/cli/clean.md
- Configuration: docs/configuration.md
- Plugins:
- Available Plugins: https://code.askimed.com/nf-test-plugins
- Using Plugins: docs/plugins/using-plugins.md
- Developing Plugins: docs/plugins/developing-plugins.md
- Resources: resources.md
- About: about.md


markdown_extensions:
- admonition
- pymdownx.details
- pymdownx.superfences
- pymdownx.tabbed:
alternate_style: true
alternate_style: true
- attr_list
- pymdownx.emoji:
emoji_index: !!python/name:materialx.emoji.twemoji
emoji_generator: !!python/name:materialx.emoji.to_svg
emoji_generator: !!python/name:materialx.emoji.to_svg
9 changes: 8 additions & 1 deletion src/main/java/com/askimed/nf/test/core/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ public AbstractTest(AbstractTestSuite parent) {
}

public void config(String config) {
this.config = new File(config);
if (config == null) {
return;
}
if (parent.isRelative(config)) {
this.config = new File(parent.makeAbsolute(config));
} else {
this.config = new File(config);
}
}

public File getConfig() {
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/com/askimed/nf/test/core/AbstractTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class AbstractTestSuite implements ITestSuite {

private File globalConfig = null;

private File localConfig = null;
private String localConfig = null;

private List<ITest> tests = new Vector<ITest>();

Expand Down Expand Up @@ -62,7 +62,7 @@ public void script(String script) {
}

public String getScript() {
if (script != null && isRelative(script)) {
if (isRelative(script)) {
return makeAbsolute(script);
} else {
return script;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void profile(String profile) {
}

public void config(String config) {
this.localConfig = new File(config);
this.localConfig = config;
}

public void setName(String name) {
Expand Down Expand Up @@ -159,12 +159,15 @@ public File getGlobalConfigFile() {
return globalConfig;
}

public void setLocalConfig(File localConfig) {
this.localConfig = localConfig;
}

public File getLocalConfig() {
return localConfig;
if (localConfig == null) {
return null;
}
if (isRelative(localConfig)) {
return new File(makeAbsolute(localConfig));
} else {
return new File(localConfig);
}
}

public File getHomeDirectory() {
Expand Down Expand Up @@ -243,11 +246,14 @@ public boolean hasFailedTests() {
return failedTests;
}

protected String makeAbsolute(String path) {
public String makeAbsolute(String path) {
return new File(directory, path).getAbsolutePath();
}

protected boolean isRelative(String path) {
public boolean isRelative(String path) {
if (path == null) {
return false;
}
return path.startsWith("../") || path.startsWith("./");
}

Expand Down
Loading
Loading