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

Add capability for more complicated mocks #26

Merged
merged 2 commits into from
Mar 12, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Update output file name to explicitly specify `submodules`
- Update documentation action to use tags and 'development' versions
- Improved GitHub links in documentation links for tagged versions
- Add dynamic mocks to Nextflow regression tests

### Fixed
- No longer fail on missing `gh-pages` branch
Expand Down
43 changes: 43 additions & 0 deletions run-nextflow-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@ Any methods that access files outside of the repository must be listed in `mocks
}
```

### Dynamic Mocks

In very specific circumstances a mock function that returns a static value is insufficient - for example, a pipeline that requires paired input BAMs might check that they have separate sample IDs. That means that this mock...

```json
"mocks": {
"parse_bam_header": {
"read_group": [
{
"SM": "495723"
}
]
}
}
```

... would return the same sample ID for each input BAM and thus fail.

To solve this, a dynamic mock like the following may be used:

```json
"mocks": {
"DYNAMIC|parse_bam_header": {
"[\"/hot/software/pipeline/pipeline-SQC-DNA/Nextflow/development/test-input/HG002.N-n2.bam\"]": {
"read_group": [
{
"SM": "098765"
}
]
},
"[\"/hot/software/pipeline/pipeline-SQC-DNA/Nextflow/development/test-input/S2.T-n2.bam\"]": {
"read_group": [
{
"SM": "52345245"
}
]
}
}
}
```

Dynamic mocks have their method name prefixed with `DYNAMIC|`. Their values are maps with JSONified function arguments for keys and static return values as values. Note that the function arguments are always presented as a list, even for a single argument.

## Usage

### Workflow File
Expand Down
24 changes: 23 additions & 1 deletion run-nextflow-tests/betterconfig.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import java.nio.file.Paths

import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovy.lang.Closure
import groovy.lang.ProxyMetaClass
import groovy.util.ConfigObject
Expand All @@ -22,12 +23,27 @@ class UserInterceptor implements Interceptor {

boolean invokeMethod = true
Map mocks
Map dynamic_mocks = [:]

UserInterceptor(String mock_file) {
def jsonSlurper = new JsonSlurper()

this.mocks = jsonSlurper.parse(new File(mock_file))
assert this.mocks instanceof Map

// Decode the "dynamic" mocks
def dynamic_pattern = /^DYNAMIC\|(?<name>.*)/

this.mocks.each { key, value ->
def match = key =~ dynamic_pattern
if (match) {
assert value instanceof Map

// Remove it from the plain mocks
this.mocks.remove(key)
this.dynamic_mocks[match.group("name")] = value
}
}
}

boolean doInvoke() {
Expand All @@ -38,8 +54,14 @@ class UserInterceptor implements Interceptor {
if (mocks.containsKey(name)) {
invokeMethod = false
return mocks[name]
} else if (dynamic_mocks.containsKey(name)) {
def args_str = JsonOutput.toJson(args)
if (dynamic_mocks[name].containsKey(args_str)) {
invokeMethod = false
return dynamic_mocks[name][args_str]
}
throw new Exception("Dynamic mock $name does not contain $args_str")
}

}

Object afterInvoke(Object obj, String name, Object[] args, Object result) {
Expand Down
Loading