Skip to content

Commit

Permalink
Add run method to setup block and fix relativ paths
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Oct 5, 2023
1 parent a451dee commit 9d5ac47
Show file tree
Hide file tree
Showing 22 changed files with 480 additions and 38 deletions.
17 changes: 12 additions & 5 deletions src/main/java/com/askimed/nf/test/core/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ public AbstractTest(AbstractTestSuite parent) {
}

public void config(String config) {
this.config = new File(config);
if (config == null) {
return;
}
if (parent.isRelative(config)) {
this.config = new File(parent.makeAbsolute(config));
} else {
this.config = new File(config);
}
}

public File getConfig() {
Expand All @@ -89,7 +96,7 @@ public void setup(File testDirectory) throws IOException {
if (testDirectory == null) {
throw new IOException("Testcase setup failed: No home directory set");
}

launchDir = initDirectory("Launch Directory", testDirectory, DIRECTORY_TESTS, getHash());
metaDir = initDirectory("Meta Directory", launchDir, DIRECTORY_META);
outputDir = initDirectory("Output Directory", launchDir, DIRECTORY_OUTPUT);
Expand Down Expand Up @@ -154,11 +161,11 @@ public String getErrorReport() throws Throwable {

@Override
public String getHash() {

if (parent == null || parent.getFilename() == null || getName() == null || getName().isEmpty()) {
throw new RuntimeException("Error generating hash");
}

return hash(parent.getFilename() + getName());

}
Expand Down Expand Up @@ -254,7 +261,7 @@ public void setUpdateSnapshot(boolean updateSnapshot) {
public boolean isUpdateSnapshot() {
return updateSnapshot;
}

@Override
public String toString() {
return getHash().substring(0, 8) + ": " + getName();
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/com/askimed/nf/test/core/AbstractTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class AbstractTestSuite implements ITestSuite {

private File globalConfig = null;

private File localConfig = null;
private String localConfig = null;

private List<ITest> tests = new Vector<ITest>();

Expand Down Expand Up @@ -53,7 +53,7 @@ public void script(String script) {
}

public String getScript() {
if (script != null && isRelative(script)) {
if (isRelative(script)) {
return makeAbsolute(script);
} else {
return script;
Expand All @@ -70,7 +70,7 @@ public void evalualteTestClosures() throws Throwable {
for (NamedClosure namedClosure : testClosures) {
String testName = namedClosure.name;
Closure closure = namedClosure.closure;

ITest test = getNewTestInstance(testName);
test.setup(getHomeDirectory());
closure.setDelegate(test);
Expand All @@ -95,7 +95,7 @@ public void profile(String profile) {
}

public void config(String config) {
this.localConfig = new File(config);
this.localConfig = config;
}

public void setName(String name) {
Expand Down Expand Up @@ -139,12 +139,15 @@ public File getGlobalConfigFile() {
return globalConfig;
}

public void setLocalConfig(File localConfig) {
this.localConfig = localConfig;
}

public File getLocalConfig() {
return localConfig;
if (localConfig == null) {
return null;
}
if (isRelative(localConfig)) {
return new File(makeAbsolute(localConfig));
} else {
return new File(localConfig);
}
}

public File getHomeDirectory() {
Expand Down Expand Up @@ -223,14 +226,17 @@ public boolean hasFailedTests() {
return failedTests;
}

protected String makeAbsolute(String path) {
public String makeAbsolute(String path) {
return new File(directory, path).getAbsolutePath();
}

protected boolean isRelative(String path) {
public boolean isRelative(String path) {
if (path == null) {
return false;
}
return path.startsWith("../") || path.startsWith("./");
}

@Override
public String toString() {
return name;
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/askimed/nf/test/lang/Dependency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.askimed.nf.test.lang;

import groovy.lang.Closure;

public class Dependency {

private String script;

private String name;

private String mapping;

public Dependency(String name, Closure closure) {
this.name = name;
closure.setDelegate(this);
closure.setResolveStrategy(Closure.DELEGATE_FIRST);
closure.call();
}

public void script(String script) {
this.script = script;
}

public String getScript() {
return script;
}

public void setScript(String script) {
this.script = script;
}

public void process(Closure closure) {
closure.setDelegate(this);
closure.setResolveStrategy(Closure.DELEGATE_FIRST);
Object mapping = closure.call();
if (mapping != null) {
this.mapping = mapping.toString();
}
}

public void workflow(Closure closure) {
process(closure);
}

public void function(Closure closure) {
process(closure);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMapping() {
return mapping;
}

public void setMapping(String mapping) {
this.mapping = mapping;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.askimed.nf.test.lang.process;

import java.util.List;
import java.util.Vector;

import com.askimed.nf.test.core.ITest;
import com.askimed.nf.test.lang.Dependency;
import com.askimed.nf.test.lang.TestContext;

import groovy.lang.Closure;
Expand All @@ -11,6 +15,8 @@ public class ProcessContext extends TestContext {

private Closure processClosure;

private List<Dependency> dependencies = new Vector<Dependency>();

public ProcessContext(ITest test) {
super(test);
}
Expand Down Expand Up @@ -43,5 +49,14 @@ public void evaluateProcessClosure() {
}

}


public void run(String process, Closure closure) {
Dependency dependency = new Dependency(process, closure);
dependencies.add(dependency);
}

public List<Dependency> getDependencies() {
return dependencies;
}

}
28 changes: 25 additions & 3 deletions src/main/java/com/askimed/nf/test/lang/process/ProcessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.codehaus.groovy.control.CompilationFailedException;

import com.askimed.nf.test.core.AbstractTest;
import com.askimed.nf.test.lang.Dependency;
import com.askimed.nf.test.lang.TestCode;
import com.askimed.nf.test.nextflow.NextflowCommand;
import com.askimed.nf.test.util.AnsiText;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void autoSort(boolean autoSort) {
public void execute() throws Throwable {

super.execute();

File script = new File(parent.getScript());

if (!script.exists()) {
Expand All @@ -91,6 +92,10 @@ public void execute() throws Throwable {

context.init(this);

if (parent.getSetup() != null) {
parent.getSetup().execute(context);
}

if (setup != null) {
setup.execute(context);
}
Expand Down Expand Up @@ -124,8 +129,8 @@ public void execute() throws Throwable {
nextflow.setProfile(parent.getProfile());
File projectConfig = new File("nextflow.config");
if (projectConfig.exists()) {
nextflow.addConfig(projectConfig);
}
nextflow.addConfig(projectConfig);
}
nextflow.addConfig(parent.getGlobalConfigFile());
nextflow.addConfig(parent.getLocalConfig());
nextflow.addConfig(getConfig());
Expand Down Expand Up @@ -177,9 +182,26 @@ protected void writeWorkflowMock(File file) throws IOException, CompilationFaile
script = new File(script).getAbsolutePath();
}

// update dependency paths
for (Dependency dependency : context.getDependencies()) {
String _script = dependency.getScript();
if (_script == null) {
dependency.setScript(script);
} else {
if (parent.isRelative(_script)) {
_script = parent.makeAbsolute(_script);
}
if (!_script.startsWith("/") && !_script.startsWith("./")) {
_script = new File(_script).getAbsolutePath();
}
dependency.setScript(_script);
}
}

Map<Object, Object> binding = new HashMap<Object, Object>();
binding.put("process", parent.getProcess());
binding.put("script", script);
binding.put("dependencies", context.getDependencies());

// Get body of when closure
binding.put("mapping", context.getProcess().getMapping());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

import com.askimed.nf.test.core.AbstractTestSuite;
import com.askimed.nf.test.core.ITest;
import com.askimed.nf.test.lang.TestCode;

import groovy.lang.Closure;
import groovy.lang.DelegatesTo;

public class ProcessTestSuite extends AbstractTestSuite {

private String process;

private TestCode setup;

public void process(String process) {
this.process = process;
}
Expand All @@ -27,6 +31,14 @@ public void test(String name, Closure closure) throws IOException {
addTestClosure(name, closure);
}

public void setup(@DelegatesTo(value = ProcessTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) {
setup = new TestCode(closure);
}

public TestCode getSetup() {
return setup;
}

@Override
protected ITest getNewTestInstance(String name) {
ProcessTest test = new ProcessTest(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.askimed.nf.test.lang.workflow;

import java.util.List;
import java.util.Vector;

import com.askimed.nf.test.core.ITest;
import com.askimed.nf.test.lang.Dependency;
import com.askimed.nf.test.lang.TestContext;

import groovy.lang.Closure;
Expand All @@ -11,10 +15,12 @@ public class WorkflowContext extends TestContext {

private Workflow workflow = new Workflow();

private List<Dependency> dependencies = new Vector<Dependency>();

public WorkflowContext(ITest test) {
super(test);
}

public void workflow(Closure<Object> closure) {
workflowClosure = closure;
}
Expand All @@ -40,4 +46,13 @@ public void setWorkflow(Workflow workflow) {
this.workflow = workflow;
}

public void run(String process, Closure closure) {
Dependency dependency = new Dependency(process, closure);
dependencies.add(dependency);
}

public List<Dependency> getDependencies() {
return dependencies;
}

}
Loading

0 comments on commit 9d5ac47

Please sign in to comment.