Skip to content

Commit

Permalink
Add integration tests for workflow.onComplete and workflow.onError de…
Browse files Browse the repository at this point in the history
…fined in workflow script fix unit tests and workflow_oncomplete test

Signed-off-by: jorgee <[email protected]>
  • Loading branch information
jorgee committed Nov 21, 2024
1 parent d259c17 commit cfffeb3
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ class WorkflowMetadata {
onCompleteActions.add(action)
}

void setOnCompleteFormConfig( Closure action ) {
final clone = (Closure)action.clone()
clone.delegate = NF.binding.variables
clone.resolveStrategy = Closure.DELEGATE_FIRST

onCompleteActions.add(clone)
}

/**
* Implements the following idiom in the pipeline script:
* <pre>
Expand Down Expand Up @@ -333,6 +341,14 @@ class WorkflowMetadata {
onErrorActions.add(action)
}

void setOnErrorFromConfig(Closure action ) {
final clone = (Closure)action.clone()
clone.delegate = NF.binding.variables
clone.resolveStrategy = Closure.DELEGATE_FIRST

onErrorActions.add(clone)
}

/**
* Dynamic getter for workflow metadata attributes
*
Expand Down Expand Up @@ -366,11 +382,11 @@ class WorkflowMetadata {
if( !workflowConfig ) return
// -- register `onComplete`
if( workflowConfig.onComplete instanceof Closure ) {
onComplete( (Closure)workflowConfig.onComplete )
setOnCompleteFormConfig( (Closure)workflowConfig.onComplete )
}
// -- register `onError`
if( workflowConfig.onError instanceof Closure ) {
onError( (Closure)workflowConfig.onError )
setOnErrorFromConfig( (Closure)workflowConfig.onError )
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class WorkflowMetadataTest extends Specification {
}

when:
metadata.onComplete(handler)
metadata.setOnCompleteFormConfig(handler)
metadata.invokeOnComplete()

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

when:
metadata.onComplete {
metadata.setOnCompleteFormConfig {
throw new WorkflowScriptErrorException('You failed!')
}
metadata.invokeOnComplete()
Expand Down Expand Up @@ -236,7 +236,7 @@ class WorkflowMetadataTest extends Specification {
}

when:
metadata.onError(handler)
metadata.setOnErrorFromConfig(handler)
metadata.invokeOnError(Mock(TraceRecord))

then:
Expand Down
21 changes: 21 additions & 0 deletions tests/checks/script-vars-oncomplete.nf/.checks
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set -e

#
# run normal mode
#
echo ''
$NXF_RUN | tee stdout

[[ `grep -c 'Workflow Executed!' stdout` == 1 ]] || false
[[ `grep -c 'local variable: local' stdout` == 1 ]] || false
[[ `grep -c 'param output dir: results' stdout` == 1 ]] || false
[[ `grep -c 'Method executed!' stdout` == 1 ]] || false
[[ `grep -c 'script-vars-oncomplete.nf' stdout` == 1 ]] || false
[[ `grep -c "Can't access to a global variable" stdout` == 1 ]] || false
[[ `grep -c "Can't access to a private workflow property" stdout` == 1 ]] || false
[[ `grep -c 'Success!' stdout` == 1 ]] || false





21 changes: 21 additions & 0 deletions tests/checks/script-vars-onerror.nf/.checks
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set -e

#
# run normal mode
#
echo ''
$NXF_RUN | tee stdout

[[ `grep -c 'Workflow Failed!' stdout` == 1 ]] || false
[[ `grep -c 'local variable: local' stdout` == 1 ]] || false
[[ `grep -c 'param output dir: results' stdout` == 1 ]] || false
[[ `grep -c 'Method executed!' stdout` == 1 ]] || false
[[ `grep 'Executed script' stdout | grep -c 'script-vars-onerror.nf'` == 1 ]] || false
[[ `grep -c "Can't access to a global variable" stdout` == 1 ]] || false
[[ `grep -c "Can't access to a private workflow property" stdout` == 1 ]] || false
[[ `grep -c 'Failure!' stdout` == 1 ]] || false





35 changes: 35 additions & 0 deletions tests/script-vars-oncomplete.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def global_var = 'global'

def method() {
println("Method executed!")
}

workflow {
params.outdir = 'results'

def local = 'local'

workflow.onComplete {
println("local variable: ${local}")
println("param output dir: ${params.outdir}")
method()
println("Executed script : ${workflow.scriptFile}")
// expected failure
try {
println("global variable: ${global}")
}catch (groovy.lang.MissingPropertyException e){
println("Can't access to a global variable")
}
try {
println("workflow private variable: ${events}")
}catch (Exception e){
println("Can't access to a private workflow property")
}
if( workflow.success )
println("Success!")
else
println("Failure!")
}

println("Workflow Executed!")
}
35 changes: 35 additions & 0 deletions tests/script-vars-onerror.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def global_var = 'global'

def method() {
println("Method executed!")
}

workflow {
params.outdir = 'results'

def local = 'local'

workflow.onError {
println("local variable: ${local}")
println("param output dir: ${params.outdir}")
method()
println("Executed script : ${workflow.scriptFile}")
// expected failures
try {
println("global variable: ${global}")
}catch (groovy.lang.MissingPropertyException e){
println("Can't access to a global variable")
}
try {
println("workflow private variable: ${events}")
}catch (Exception e){
println("Can't access to a private workflow property")
}
if( workflow.success )
println("Success!")
else
println("Failure!")
}

throw new Exception("Workflow Failed!")
}

0 comments on commit cfffeb3

Please sign in to comment.