askimed / nf-test

Simple test framework for Nextflow pipelines
https://www.nf-test.com
MIT License
136 stars 20 forks source link

Support for channels as function output is problematic #235

Open mahesh-panchal opened 2 months ago

mahesh-panchal commented 2 months ago

Support for channels as function output is not fully functional.

main.nf:

def defaultChannel(){
    Channel.value(1)
}

with main.nf.test:

nextflow_function {

    name "Test Functions"
    script "main.nf"

    test("Test Function defaultChannel") {

        function "defaultChannel"

        when {
            function {
                """
                // define inputs of the function here. Example:
                // input[0] = 1
                """
            }
        }

        then {
            println function.properties
            assert function.success
            assert snapshot(function.result).match()
        }

    }

}

results in:

$ nf-test test tests/main.nf.test 
Picked up JAVA_TOOL_OPTIONS: -XX:+UseContainerSupport -XX:ActiveProcessorCount=1

🚀 nf-test 0.8.4
https://code.askimed.com/nf-test
(c) 2021 - 2024 Lukas Forer and Sebastian Schoenherr

Test Functions

  Test [a7c7feae] 'Test Function defaultChannel' [stderr:[], class:class com.askimed.nf.test.lang.function.Function, errorReport:, exitStatus:1, mapping:
                // define inputs of the function here. Example:
                // input[0] = 1
                , failed:true, result:null, stdout:[ERROR ~ Cannot invoke "groovyx.gpars.serial.SerialContext.add(groovyx.gpars.serial.SerialHandle)" because "this.context" is null, ,  -- Check script 'meta/mock.nf' at line: 35 or see '/workspace/Nextflow_sandbox/.nf-test/tests/a7c7feae6c08f02d7cd531ca2156d60a/meta/nextflow.log' file for more details], errorMessage:, trace:com.askimed.nf.test.lang.WorkflowTrace@304b9f1a, name:null, success:false]
FAILED (8.337s)

  Assertion failed: 

  assert function.success
         |        |
         |        false
         com.askimed.nf.test.lang.function.Function@1059754c (toString() == null)

  Nextflow stdout:

  ERROR ~ Cannot invoke "groovyx.gpars.serial.SerialContext.add(groovyx.gpars.serial.SerialHandle)" because "this.context" is null

   -- Check script 'meta/mock.nf' at line: 35 or see '/workspace/Nextflow_sandbox/.nf-test/tests/a7c7feae6c08f02d7cd531ca2156d60a/meta/nextflow.log' file for more details
  Nextflow stderr:

FAILURE: Executed 1 tests in 8.346s (1 failed)

When using files in a channel as output, the test passes but the output is an empty map: main.nf:

def defaultChannel(){
    Channel.fromPath("$projectDir/tests/nextflow.config")
}

main.nf.test:

nextflow_function {

    name "Test Functions"
    script "main.nf"

    test("Test Function defaultChannel") {

        function "defaultChannel"

        when {
            function {
                """
                // define inputs of the function here. Example:
                // input[0] = 1
                """
            }
        }

        then {
            println function.properties
            assert function.success
            assert snapshot(function.result).match()
        }

    }

}

terminal:

$ nf-test test tests/main.nf.test 
Picked up JAVA_TOOL_OPTIONS: -XX:+UseContainerSupport -XX:ActiveProcessorCount=1

🚀 nf-test 0.8.4
https://code.askimed.com/nf-test
(c) 2021 - 2024 Lukas Forer and Sebastian Schoenherr

Test Functions

  Test [a7c7feae] 'Test Function defaultChannel' [stderr:[], class:class com.askimed.nf.test.lang.function.Function, errorReport:null, exitStatus:0, mapping:
                // define inputs of the function here. Example:
                // input[0] = 1
                , failed:false, result:[:], stdout:[], errorMessage:null, trace:com.askimed.nf.test.lang.WorkflowTrace@5b56b654, name:null, success:true]
PASSED (14.31s)
  Snapshots:
    1 created [Test Function defaultChannel]

Snapshot Summary:
  1 created

SUCCESS: Executed 1 tests in 14.321s

main.nf.test.snap:

{
    "Test Function defaultChannel": {
        "content": [
            {

            }
        ],
        "meta": {
            "nf-test": "0.8.4",
            "nextflow": "24.04.3"
        },
        "timestamp": "2024-07-17T14:16:19.672087809"
    }
}

Additional context.

I use functions to wrap common channel manipulations to help with code readability.

antunderwood commented 5 days ago

Adding my +1 to this since we likewise use functions to make the main.ng more readable by wrapping complex channel manipulations. Thanks