Skip to content

Commit cfffeb3

Browse files
committed
Add integration tests for workflow.onComplete and workflow.onError defined in workflow script fix unit tests and workflow_oncomplete test
Signed-off-by: jorgee <jorge.ejarque@seqera.io>
1 parent d259c17 commit cfffeb3

File tree

6 files changed

+133
-5
lines changed

6 files changed

+133
-5
lines changed

modules/nextflow/src/main/groovy/nextflow/script/WorkflowMetadata.groovy

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ class WorkflowMetadata {
306306
onCompleteActions.add(action)
307307
}
308308

309+
void setOnCompleteFormConfig( Closure action ) {
310+
final clone = (Closure)action.clone()
311+
clone.delegate = NF.binding.variables
312+
clone.resolveStrategy = Closure.DELEGATE_FIRST
313+
314+
onCompleteActions.add(clone)
315+
}
316+
309317
/**
310318
* Implements the following idiom in the pipeline script:
311319
* <pre>
@@ -333,6 +341,14 @@ class WorkflowMetadata {
333341
onErrorActions.add(action)
334342
}
335343

344+
void setOnErrorFromConfig(Closure action ) {
345+
final clone = (Closure)action.clone()
346+
clone.delegate = NF.binding.variables
347+
clone.resolveStrategy = Closure.DELEGATE_FIRST
348+
349+
onErrorActions.add(clone)
350+
}
351+
336352
/**
337353
* Dynamic getter for workflow metadata attributes
338354
*
@@ -366,11 +382,11 @@ class WorkflowMetadata {
366382
if( !workflowConfig ) return
367383
// -- register `onComplete`
368384
if( workflowConfig.onComplete instanceof Closure ) {
369-
onComplete( (Closure)workflowConfig.onComplete )
385+
setOnCompleteFormConfig( (Closure)workflowConfig.onComplete )
370386
}
371387
// -- register `onError`
372388
if( workflowConfig.onError instanceof Closure ) {
373-
onError( (Closure)workflowConfig.onError )
389+
setOnErrorFromConfig( (Closure)workflowConfig.onError )
374390
}
375391
}
376392

modules/nextflow/src/test/groovy/nextflow/script/WorkflowMetadataTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class WorkflowMetadataTest extends Specification {
166166
}
167167

168168
when:
169-
metadata.onComplete(handler)
169+
metadata.setOnCompleteFormConfig(handler)
170170
metadata.invokeOnComplete()
171171

172172
then:
@@ -190,7 +190,7 @@ class WorkflowMetadataTest extends Specification {
190190
def metadata = new WorkflowMetadata(session, null)
191191

192192
when:
193-
metadata.onComplete {
193+
metadata.setOnCompleteFormConfig {
194194
throw new WorkflowScriptErrorException('You failed!')
195195
}
196196
metadata.invokeOnComplete()
@@ -236,7 +236,7 @@ class WorkflowMetadataTest extends Specification {
236236
}
237237

238238
when:
239-
metadata.onError(handler)
239+
metadata.setOnErrorFromConfig(handler)
240240
metadata.invokeOnError(Mock(TraceRecord))
241241

242242
then:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
set -e
2+
3+
#
4+
# run normal mode
5+
#
6+
echo ''
7+
$NXF_RUN | tee stdout
8+
9+
[[ `grep -c 'Workflow Executed!' stdout` == 1 ]] || false
10+
[[ `grep -c 'local variable: local' stdout` == 1 ]] || false
11+
[[ `grep -c 'param output dir: results' stdout` == 1 ]] || false
12+
[[ `grep -c 'Method executed!' stdout` == 1 ]] || false
13+
[[ `grep -c 'script-vars-oncomplete.nf' stdout` == 1 ]] || false
14+
[[ `grep -c "Can't access to a global variable" stdout` == 1 ]] || false
15+
[[ `grep -c "Can't access to a private workflow property" stdout` == 1 ]] || false
16+
[[ `grep -c 'Success!' stdout` == 1 ]] || false
17+
18+
19+
20+
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
set -e
2+
3+
#
4+
# run normal mode
5+
#
6+
echo ''
7+
$NXF_RUN | tee stdout
8+
9+
[[ `grep -c 'Workflow Failed!' stdout` == 1 ]] || false
10+
[[ `grep -c 'local variable: local' stdout` == 1 ]] || false
11+
[[ `grep -c 'param output dir: results' stdout` == 1 ]] || false
12+
[[ `grep -c 'Method executed!' stdout` == 1 ]] || false
13+
[[ `grep 'Executed script' stdout | grep -c 'script-vars-onerror.nf'` == 1 ]] || false
14+
[[ `grep -c "Can't access to a global variable" stdout` == 1 ]] || false
15+
[[ `grep -c "Can't access to a private workflow property" stdout` == 1 ]] || false
16+
[[ `grep -c 'Failure!' stdout` == 1 ]] || false
17+
18+
19+
20+
21+

tests/script-vars-oncomplete.nf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def global_var = 'global'
2+
3+
def method() {
4+
println("Method executed!")
5+
}
6+
7+
workflow {
8+
params.outdir = 'results'
9+
10+
def local = 'local'
11+
12+
workflow.onComplete {
13+
println("local variable: ${local}")
14+
println("param output dir: ${params.outdir}")
15+
method()
16+
println("Executed script : ${workflow.scriptFile}")
17+
// expected failure
18+
try {
19+
println("global variable: ${global}")
20+
}catch (groovy.lang.MissingPropertyException e){
21+
println("Can't access to a global variable")
22+
}
23+
try {
24+
println("workflow private variable: ${events}")
25+
}catch (Exception e){
26+
println("Can't access to a private workflow property")
27+
}
28+
if( workflow.success )
29+
println("Success!")
30+
else
31+
println("Failure!")
32+
}
33+
34+
println("Workflow Executed!")
35+
}

tests/script-vars-onerror.nf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def global_var = 'global'
2+
3+
def method() {
4+
println("Method executed!")
5+
}
6+
7+
workflow {
8+
params.outdir = 'results'
9+
10+
def local = 'local'
11+
12+
workflow.onError {
13+
println("local variable: ${local}")
14+
println("param output dir: ${params.outdir}")
15+
method()
16+
println("Executed script : ${workflow.scriptFile}")
17+
// expected failures
18+
try {
19+
println("global variable: ${global}")
20+
}catch (groovy.lang.MissingPropertyException e){
21+
println("Can't access to a global variable")
22+
}
23+
try {
24+
println("workflow private variable: ${events}")
25+
}catch (Exception e){
26+
println("Can't access to a private workflow property")
27+
}
28+
if( workflow.success )
29+
println("Success!")
30+
else
31+
println("Failure!")
32+
}
33+
34+
throw new Exception("Workflow Failed!")
35+
}

0 commit comments

Comments
 (0)