Skip to content

Commit

Permalink
Refactore directory creation and fix remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Aug 30, 2023
1 parent b0f1a8b commit 49efa92
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public Integer execute() throws Exception {

String defaultProfile = null;
File defaultConfigFile = null;
File workDir = new File(".nf-test");
String libDir = lib;
boolean defaultWithTrace = true;
try {
Expand All @@ -89,7 +88,6 @@ public Integer execute() throws Exception {
defaultProfile = config.getProfile();
defaultConfigFile = config.getConfigFile();
defaultWithTrace = config.isWithTrace();
workDir = new File(config.getWorkDir());
if (!libDir.isEmpty()) {
libDir += ":";
}
Expand Down Expand Up @@ -146,7 +144,6 @@ public Integer execute() throws Exception {
engine.setScripts(scripts);
engine.setTagQuery(tagQuery);
engine.setDebug(debug);
engine.setWorkDir(workDir);
engine.setUpdateSnapshot(updateSnapshot);
engine.setLibDir(libDir);
engine.setPluginManager(manager);
Expand Down
77 changes: 20 additions & 57 deletions src/main/java/com/askimed/nf/test/core/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.List;
import java.util.Vector;

import com.askimed.nf.test.config.Config;
import com.askimed.nf.test.util.FileUtil;

public abstract class AbstractTest implements ITest {
Expand All @@ -23,9 +22,9 @@ public abstract class AbstractTest implements ITest {

public String baseDir = System.getProperty("user.dir");

public boolean skipped = false;
public String projectDir = System.getProperty("user.dir");

private ITestSuite suite;
public boolean skipped = false;

public static String[] SHARED_DIRECTORIES = { "bin", "lib", "assets" };

Expand Down Expand Up @@ -56,41 +55,18 @@ public File getConfig() {
return config;
}

protected String getWorkDir() {

File workDir = new File("nf-test");

try {

Config config = Config.parse(new File(Config.FILENAME));
workDir = new File(config.getWorkDir());
} catch (Exception e) {

}
return workDir.getAbsolutePath();
}

@Override
public void setup(File baseDir) throws IOException {

String launchDir = FileUtil.path(baseDir.getAbsolutePath(), "tests", getHash());
public void setup(String testDirectory) throws IOException {

try {
this.launchDir = new File(launchDir);
FileUtil.deleteDirectory(this.launchDir);
FileUtil.createDirectory(this.launchDir);
} catch (Exception e) {
throw new IOException("Launch Directory '" + launchDir + "' could not be deleted or created:\n" + e);
}
launchDir = new File(FileUtil.path(testDirectory, "tests", getHash()));
metaDir = new File(FileUtil.path(launchDir.getAbsolutePath(), "meta"));
outputDir = new File(FileUtil.path(launchDir.getAbsolutePath(), "output"));
workDir = new File(FileUtil.path(launchDir.getAbsolutePath(), "work"));

String metaDir = FileUtil.path(launchDir, "meta");
this.metaDir = new File(metaDir);
try {
FileUtil.deleteDirectory(this.metaDir);
FileUtil.createDirectory(this.metaDir);
} catch (Exception e) {
throw new IOException("Meta Directory '" + metaDir + "' could not be deleted:\n" + e);
}
initDirectory("Launch Directory", launchDir);
initDirectory("Meta Directory", metaDir);
initDirectory("Output Directory", outputDir);
initDirectory("Working Directory", workDir);

try {
// copy bin and lib to metaDir. TODO: use symlinks and read additional "mapping"
Expand All @@ -100,24 +76,16 @@ public void setup(File baseDir) throws IOException {
throw new IOException("Directories could not be shared:\n" + e);
}

String outputDir = FileUtil.path(launchDir, "output");
}

public void initDirectory(String name, File directory) throws IOException {
try {
this.outputDir = new File(outputDir);
FileUtil.deleteDirectory(this.outputDir);
FileUtil.createDirectory(this.outputDir);
FileUtil.deleteDirectory(directory);
FileUtil.createDirectory(directory);
} catch (Exception e) {
throw new IOException("Output Directory '" + outputDir + "' could not be deleted:\n" + e);
throw new IOException(name + " '" + directory + "' could not be deleted or created:\n" + e);
}

String workDir = FileUtil.path(launchDir, "work");
try {
this.workDir = new File(workDir);
FileUtil.deleteDirectory(this.workDir);
FileUtil.createDirectory(this.workDir);
} catch (Exception e) {
throw new IOException("Working Directory '" + workDir + "' could not be deleted:\n" + e);
}
}

@Override
Expand Down Expand Up @@ -145,7 +113,7 @@ public String getErrorReport() throws Throwable {
@Override
public String getHash() {

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

}

Expand Down Expand Up @@ -195,14 +163,9 @@ public boolean isSkipped() {
return skipped;
}

@Override
public void setTestSuite(ITestSuite suite) {
this.suite = suite;
}

@Override
public ITestSuite getTestSuite() {
return suite;
return parent;
}

public void debug(boolean debug) {
Expand All @@ -227,11 +190,11 @@ public boolean isWithTrace() {
return withTrace;
}

protected void shareDirectories(String[] directories, String metaDir) throws IOException {
protected void shareDirectories(String[] directories, File metaDir) throws IOException {
for (String directory : directories) {
File localDirectory = new File(directory);
if (localDirectory.exists()) {
String metaDirectory = FileUtil.path(metaDir, directory);
String metaDirectory = FileUtil.path(metaDir.getAbsolutePath(), directory);
FileUtil.copyDirectory(localDirectory.getAbsolutePath(), metaDirectory);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ public abstract class AbstractTestSuite implements ITestSuite {
private String options = "";

private String directory = "";

private String homeDirectory = Config.DEFAULT_HOME;

private List<String> tags = new Vector<String>();

@Override
public void configure(Config config) {
autoSort = config.isAutoSort();
options = config.getOptions();
homeDirectory = config.getWorkDir();
}

public void name(String name) {
Expand Down Expand Up @@ -94,6 +97,10 @@ public void setLocalConfig(File localConfig) {
public File getLocalConfig() {
return localConfig;
}

public String getHomeDirectory() {
return homeDirectory;
}

@Override
public void setFilename(String filename) {
Expand All @@ -118,7 +125,6 @@ public List<ITest> getTests() {

protected void addTest(ITest test) {
tests.add(test);
test.setTestSuite(this);
}

public void tag(String tag) {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/askimed/nf/test/core/ITest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.askimed.nf.test.core;

import java.io.File;

public interface ITest extends ITaggable {

public void setup(File baseDir) throws Throwable;
public void setup(String homeDirectory) throws Throwable;

public void execute() throws Throwable;

Expand All @@ -22,8 +20,6 @@ public interface ITest extends ITaggable {

public String getHash();

public void setTestSuite(ITestSuite suite);

public ITestSuite getTestSuite();

public void setWithTrace(boolean withTrace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class TestExecutionEngine {

private String profile = null;

private File workDir = null;

private File configFile = null;

private File baseDir = new File(System.getProperty("user.dir"));
Expand Down Expand Up @@ -57,10 +55,6 @@ public void setConfigFile(File configFile) {
this.configFile = configFile;
}

public void setWorkDir(File workDir) {
this.workDir = workDir;
}

public void setWithTrace(boolean withTrace) {
if (withTrace == false) {
System.out.println("Warning: Tracing is disabled. `workflow.trace` is not supported.");
Expand Down Expand Up @@ -179,7 +173,6 @@ public int execute() throws Throwable {
}
listener.executionStarted(test);
TestExecutionResult result = new TestExecutionResult(test);
test.setup(workDir);
test.setWithTrace(withTrace);
test.setUpdateSnapshot(updateSnapshot);
try {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/askimed/nf/test/lang/ParamsMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ protected Map<String, Object> createNestedMap(Map<String, Object> map) {
Map<String, Object> nestedMap = new HashMap<String, Object>();
nestedMap.put("baseDir", baseDir);
nestedMap.put("outputDir", outputDir);
nestedMap.put("projectDir", projectDir);
nestedMap.put("launchDir", launchDir);
nestedMap.put("workDir", workDir);
if (map != null) {
nestedMap.putAll(map);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.askimed.nf.test.lang.function;

import java.io.IOException;

import com.askimed.nf.test.core.AbstractTestSuite;

import groovy.lang.Closure;
Expand Down Expand Up @@ -40,10 +42,12 @@ public void setScript(String script) {
}

public void test(String name,
@DelegatesTo(value = FunctionTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) {
@DelegatesTo(value = FunctionTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure)
throws IOException {

final FunctionTest test = new FunctionTest(this);
test.name(name);
test.setup(getHomeDirectory());
closure.setDelegate(test);
closure.setResolveStrategy(Closure.DELEGATE_ONLY);
closure.call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public void execute() throws Throwable {
if (!script.startsWith("/") && !script.startsWith("./")) {
script = new File(script).getAbsolutePath();
}
System.out.println("Script: " + script);

NextflowCommand nextflow = new NextflowCommand();
nextflow.setScript(script);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.askimed.nf.test.lang.pipeline;

import java.io.IOException;

import com.askimed.nf.test.core.AbstractTestSuite;

import groovy.lang.Closure;
Expand All @@ -26,10 +28,11 @@ public void setScript(String script) {
}

public void test(String name,
@DelegatesTo(value = PipelineTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) {
@DelegatesTo(value = PipelineTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) throws IOException {

final PipelineTest test = new PipelineTest(this);
test.name(name);
test.setup(getHomeDirectory());
closure.setDelegate(test);
closure.setResolveStrategy(Closure.DELEGATE_ONLY);
closure.call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ protected void writeWorkflowMock(File file) throws IOException, CompilationFaile
if (!script.startsWith("/") && !script.startsWith("./")) {
script = new File(script).getAbsolutePath();
}
System.out.println("Script: " + script);

Map<Object, Object> binding = new HashMap<Object, Object>();
binding.put("process", parent.getProcess());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.askimed.nf.test.lang.process;

import java.io.IOException;

import com.askimed.nf.test.core.AbstractTestSuite;

import groovy.lang.Closure;
Expand Down Expand Up @@ -40,10 +42,11 @@ public void setScript(String script) {
}

public void test(String name,
@DelegatesTo(value = ProcessTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) {
@DelegatesTo(value = ProcessTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) throws IOException {

final ProcessTest test = new ProcessTest(this);
test.name(name);
test.setup(getHomeDirectory());
closure.setDelegate(test);
closure.setResolveStrategy(Closure.DELEGATE_ONLY);
closure.call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void execute() throws Throwable {
if (!script.exists()) {
throw new Exception("Script '" + script.getAbsolutePath() + "' not found.");
}

context.init(this);

if (setup != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.askimed.nf.test.lang.workflow;

import java.io.IOException;

import com.askimed.nf.test.core.AbstractTestSuite;

import groovy.lang.Closure;
Expand Down Expand Up @@ -40,10 +42,11 @@ public void setScript(String script) {
}

public void test(String name,
@DelegatesTo(value = WorkflowTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) {
@DelegatesTo(value = WorkflowTest.class, strategy = Closure.DELEGATE_ONLY) final Closure closure) throws IOException {

final WorkflowTest test = new WorkflowTest(this);
test.name(name);
test.setup(getHomeDirectory());
closure.setDelegate(test);
closure.setResolveStrategy(Closure.DELEGATE_ONLY);
closure.call();
Expand Down
Loading

0 comments on commit 49efa92

Please sign in to comment.