-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathFunctionTest.java
More file actions
206 lines (153 loc) · 5.24 KB
/
FunctionTest.java
File metadata and controls
206 lines (153 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.askimed.nf.test.lang.function;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.groovy.control.CompilationFailedException;
import com.askimed.nf.test.core.AbstractTest;
import com.askimed.nf.test.lang.TestCode;
import com.askimed.nf.test.nextflow.NextflowCommand;
import com.askimed.nf.test.util.FileUtil;
import groovy.lang.Closure;
import groovy.lang.DelegatesTo;
import groovy.lang.Writable;
import groovy.text.SimpleTemplateEngine;
public class FunctionTest extends AbstractTest {
private String name = "Unknown test";
private String function = null;
private TestCode setup;
private TestCode cleanup;
private TestCode when;
private TestCode then;
private FunctionContext context;
private FunctionTestSuite parent;
public FunctionTest(FunctionTestSuite parent) {
super(parent);
this.parent = parent;
context = new FunctionContext(this);
context.setName(parent.getFunction());
}
public void name(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void function(String function) {
this.function = function;
}
public String getFunction() {
return function;
}
public void setup(
@DelegatesTo(value = FunctionTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) {
setup = new TestCode(closure);
}
public void cleanup(
@DelegatesTo(value = FunctionTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) {
cleanup = new TestCode(closure);
}
public void then(@DelegatesTo(value = FunctionTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) {
then = new TestCode(closure);
}
public void when(@DelegatesTo(value = FunctionTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) {
when = new TestCode(closure);
}
@Override
public void execute() throws Throwable {
super.execute();
if (parent.getScript() != null) {
File script = new File(parent.getScript());
if (!script.exists()) {
throw new Exception("Script '" + script.getAbsolutePath() + "' not found.");
}
}
context.init(this);
if (setup != null) {
setup.execute(context);
}
if (when != null) {
when.execute(context);
}
context.evaluateParamsClosure();
context.evaluateFunctionClosure();
if (isDebug()) {
System.out.println();
}
// Create workflow mock
writeWorkflowMock(mockFile);
// Copy mock file in meta folder for debugging
FileUtil.copy(mockFile, new File(metaDir, FILE_MOCK));
context.getParams().put("nf_test_output", metaDir.getAbsolutePath());
File traceFile = new File(metaDir, FILE_TRACE);
File outFile = new File(metaDir, FILE_STD_OUT);
File errFile = new File(metaDir, FILE_STD_ERR);
File logFile = new File(metaDir, FILE_NEXTFLOW_LOG);
File paramsFile = new File(metaDir, FILE_PARAMS);
NextflowCommand nextflow = new NextflowCommand();
nextflow.setScript(mockFile.getAbsolutePath());
nextflow.setParams(context.getParams());
for (String profile: parent.getProfiles()) {
nextflow.addProfile(profile);
}
File projectConfig = new File("nextflow.config");
if (projectConfig.exists()) {
nextflow.addConfig(projectConfig);
}
nextflow.addConfig(parent.getGlobalConfigFile());
nextflow.addConfig(parent.getLocalConfig());
nextflow.addConfig(getConfig());
if (isWithTrace()) {
nextflow.setTrace(traceFile);
}
nextflow.setOut(outFile);
nextflow.setErr(errFile);
nextflow.setDebug(isDebug());
nextflow.setLog(logFile);
nextflow.setLaunchDir(baseDir);
nextflow.setWorkDir(workDir);
nextflow.setParamsFile(paramsFile);
nextflow.setOptions(getOptions());
int exitCode = nextflow.execute();
// Parse json output
context.getFunction().loadResult(metaDir);
context.getFunction().loadFromFolder(metaDir);
context.getFunction().exitStatus = exitCode;
context.getFunction().success = (exitCode == 0);
context.getFunction().failed = (exitCode != 0);
context.getWorkflow().loadFromFolder(metaDir);
context.getWorkflow().exitStatus = exitCode;
context.getWorkflow().success = (exitCode == 0);
context.getWorkflow().failed = (exitCode != 0);
then.execute(context);
}
public void cleanup() {
if (cleanup != null) {
cleanup.execute(context);
}
}
protected void writeWorkflowMock(File file) throws IOException, CompilationFailedException, ClassNotFoundException {
String script = parent.getScript();
if (script != null && !script.startsWith("/") && !script.startsWith("./")) {
script = new File(script).getAbsolutePath();
}
String name = function != null ? function : parent.getFunction();
String include = name;
// if function is a static method: include class.
if (name.contains(".")) {
String[] tiles = name.split("\\.", 2);
include = tiles[0];
}
Map<Object, Object> binding = new HashMap<Object, Object>();
binding.put("function", name);
binding.put("include", include);
binding.put("script", script);
// Get body of when closure
binding.put("mapping", context.getFunction().getMapping());
URL templateUrl = this.getClass().getResource("WorkflowMock.nf");
SimpleTemplateEngine engine = new SimpleTemplateEngine();
Writable template = engine.createTemplate(templateUrl).make(binding);
FileUtil.write(file, template);
}
}