From 49efa92435b695fbebf6600af91691ed1fddafbe Mon Sep 17 00:00:00 2001 From: Lukas Forer Date: Wed, 30 Aug 2023 20:18:30 +0200 Subject: [PATCH] Refactore directory creation and fix remaining tests --- .../nf/test/commands/RunTestsCommand.java | 3 - .../askimed/nf/test/core/AbstractTest.java | 77 +++++-------------- .../nf/test/core/AbstractTestSuite.java | 8 +- .../java/com/askimed/nf/test/core/ITest.java | 6 +- .../nf/test/core/TestExecutionEngine.java | 7 -- .../com/askimed/nf/test/lang/ParamsMap.java | 3 + .../test/lang/function/FunctionTestSuite.java | 6 +- .../nf/test/lang/pipeline/PipelineTest.java | 1 - .../test/lang/pipeline/PipelineTestSuite.java | 5 +- .../nf/test/lang/process/ProcessTest.java | 1 - .../test/lang/process/ProcessTestSuite.java | 5 +- .../nf/test/lang/workflow/WorkflowTest.java | 2 +- .../test/lang/workflow/WorkflowTestSuite.java | 5 +- test-data/process/gzip/copy_gz.nf.test | 12 +-- test-data/workflow/issue34/trial.nf.test | 2 +- .../workflow/issue34/trial.setup.nf.test | 2 +- 16 files changed, 57 insertions(+), 88 deletions(-) diff --git a/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java b/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java index 2d456e63..ceea60c9 100644 --- a/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java @@ -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 { @@ -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 += ":"; } @@ -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); diff --git a/src/main/java/com/askimed/nf/test/core/AbstractTest.java b/src/main/java/com/askimed/nf/test/core/AbstractTest.java index 21d27823..c1c0a305 100644 --- a/src/main/java/com/askimed/nf/test/core/AbstractTest.java +++ b/src/main/java/com/askimed/nf/test/core/AbstractTest.java @@ -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 { @@ -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" }; @@ -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" @@ -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 @@ -145,7 +113,7 @@ public String getErrorReport() throws Throwable { @Override public String getHash() { - return hash(suite.getFilename() + getName()); + return hash(parent.getFilename() + getName()); } @@ -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) { @@ -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); } } diff --git a/src/main/java/com/askimed/nf/test/core/AbstractTestSuite.java b/src/main/java/com/askimed/nf/test/core/AbstractTestSuite.java index 3949e5c0..1cb4258e 100644 --- a/src/main/java/com/askimed/nf/test/core/AbstractTestSuite.java +++ b/src/main/java/com/askimed/nf/test/core/AbstractTestSuite.java @@ -25,6 +25,8 @@ public abstract class AbstractTestSuite implements ITestSuite { private String options = ""; private String directory = ""; + + private String homeDirectory = Config.DEFAULT_HOME; private List tags = new Vector(); @@ -32,6 +34,7 @@ public abstract class AbstractTestSuite implements ITestSuite { public void configure(Config config) { autoSort = config.isAutoSort(); options = config.getOptions(); + homeDirectory = config.getWorkDir(); } public void name(String name) { @@ -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) { @@ -118,7 +125,6 @@ public List getTests() { protected void addTest(ITest test) { tests.add(test); - test.setTestSuite(this); } public void tag(String tag) { diff --git a/src/main/java/com/askimed/nf/test/core/ITest.java b/src/main/java/com/askimed/nf/test/core/ITest.java index 17169fac..f94530a3 100644 --- a/src/main/java/com/askimed/nf/test/core/ITest.java +++ b/src/main/java/com/askimed/nf/test/core/ITest.java @@ -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; @@ -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); diff --git a/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java b/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java index d2f0a3c0..95190013 100644 --- a/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java +++ b/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java @@ -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")); @@ -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."); @@ -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 { diff --git a/src/main/java/com/askimed/nf/test/lang/ParamsMap.java b/src/main/java/com/askimed/nf/test/lang/ParamsMap.java index 6e09909e..bedb5a7a 100644 --- a/src/main/java/com/askimed/nf/test/lang/ParamsMap.java +++ b/src/main/java/com/askimed/nf/test/lang/ParamsMap.java @@ -197,6 +197,9 @@ protected Map createNestedMap(Map map) { Map nestedMap = new HashMap(); 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); } diff --git a/src/main/java/com/askimed/nf/test/lang/function/FunctionTestSuite.java b/src/main/java/com/askimed/nf/test/lang/function/FunctionTestSuite.java index c5db9843..f66399fb 100644 --- a/src/main/java/com/askimed/nf/test/lang/function/FunctionTestSuite.java +++ b/src/main/java/com/askimed/nf/test/lang/function/FunctionTestSuite.java @@ -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; @@ -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(); diff --git a/src/main/java/com/askimed/nf/test/lang/pipeline/PipelineTest.java b/src/main/java/com/askimed/nf/test/lang/pipeline/PipelineTest.java index 475af89d..a4a9e969 100644 --- a/src/main/java/com/askimed/nf/test/lang/pipeline/PipelineTest.java +++ b/src/main/java/com/askimed/nf/test/lang/pipeline/PipelineTest.java @@ -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); diff --git a/src/main/java/com/askimed/nf/test/lang/pipeline/PipelineTestSuite.java b/src/main/java/com/askimed/nf/test/lang/pipeline/PipelineTestSuite.java index c26d732c..f8e9586a 100644 --- a/src/main/java/com/askimed/nf/test/lang/pipeline/PipelineTestSuite.java +++ b/src/main/java/com/askimed/nf/test/lang/pipeline/PipelineTestSuite.java @@ -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; @@ -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(); diff --git a/src/main/java/com/askimed/nf/test/lang/process/ProcessTest.java b/src/main/java/com/askimed/nf/test/lang/process/ProcessTest.java index ec0b3e68..9d35ea9e 100644 --- a/src/main/java/com/askimed/nf/test/lang/process/ProcessTest.java +++ b/src/main/java/com/askimed/nf/test/lang/process/ProcessTest.java @@ -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 binding = new HashMap(); binding.put("process", parent.getProcess()); diff --git a/src/main/java/com/askimed/nf/test/lang/process/ProcessTestSuite.java b/src/main/java/com/askimed/nf/test/lang/process/ProcessTestSuite.java index 2c47cc9f..f72d772b 100644 --- a/src/main/java/com/askimed/nf/test/lang/process/ProcessTestSuite.java +++ b/src/main/java/com/askimed/nf/test/lang/process/ProcessTestSuite.java @@ -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; @@ -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(); diff --git a/src/main/java/com/askimed/nf/test/lang/workflow/WorkflowTest.java b/src/main/java/com/askimed/nf/test/lang/workflow/WorkflowTest.java index 97a6e133..0068815b 100644 --- a/src/main/java/com/askimed/nf/test/lang/workflow/WorkflowTest.java +++ b/src/main/java/com/askimed/nf/test/lang/workflow/WorkflowTest.java @@ -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) { diff --git a/src/main/java/com/askimed/nf/test/lang/workflow/WorkflowTestSuite.java b/src/main/java/com/askimed/nf/test/lang/workflow/WorkflowTestSuite.java index 95a0a4c6..d44c9dab 100644 --- a/src/main/java/com/askimed/nf/test/lang/workflow/WorkflowTestSuite.java +++ b/src/main/java/com/askimed/nf/test/lang/workflow/WorkflowTestSuite.java @@ -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; @@ -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(); diff --git a/test-data/process/gzip/copy_gz.nf.test b/test-data/process/gzip/copy_gz.nf.test index ac152c5f..4e063abb 100644 --- a/test-data/process/gzip/copy_gz.nf.test +++ b/test-data/process/gzip/copy_gz.nf.test @@ -11,7 +11,7 @@ nextflow_process { when { process { """ - input[0] = file("test-data/process/gzip/gwas.gz") + input[0] = file("$projectDir/test-data/process/gzip/gwas.gz") """ } } @@ -29,7 +29,7 @@ nextflow_process { when { process { """ - input[0] = file("test-data/process/gzip/gwas.gz") + input[0] = file("$projectDir/test-data/process/gzip/gwas.gz") """ } } @@ -48,7 +48,7 @@ nextflow_process { when { process { """ - input[0] = file("test-data/process/gzip/gwas.gz") + input[0] = file("$projectDir/test-data/process/gzip/gwas.gz") """ } } @@ -68,7 +68,7 @@ nextflow_process { when { process { """ - input[0] = file("test-data/process/gzip/gwas.gz") + input[0] = file("$projectDir/test-data/process/gzip/gwas.gz") """ } } @@ -86,7 +86,7 @@ nextflow_process { when { process { """ - input[0] = file("test-data/process/gzip/gwas.gz") + input[0] = file("$projectDir/test-data/process/gzip/gwas.gz") """ } } @@ -104,7 +104,7 @@ nextflow_process { when { process { """ - input[0] = file("test-data/process/gzip/gwas.gz") + input[0] = file("$projectDir/test-data/process/gzip/gwas.gz") """ } } diff --git a/test-data/workflow/issue34/trial.nf.test b/test-data/workflow/issue34/trial.nf.test index 117f9243..d54af04e 100644 --- a/test-data/workflow/issue34/trial.nf.test +++ b/test-data/workflow/issue34/trial.nf.test @@ -7,7 +7,7 @@ nextflow_workflow { test("test_passing_file_1") { when { params { - someFile = "${baseDir}/r9_file.txt" + someFile = "${launchDir}/r9_file.txt" } workflow { """ diff --git a/test-data/workflow/issue34/trial.setup.nf.test b/test-data/workflow/issue34/trial.setup.nf.test index 200eea41..bc0b4e40 100644 --- a/test-data/workflow/issue34/trial.setup.nf.test +++ b/test-data/workflow/issue34/trial.setup.nf.test @@ -6,7 +6,7 @@ nextflow_workflow { test("test_passing_file_1") { - def filename = "${baseDir}/r9_file.txt"; + def filename = "${launchDir}/r9_file.txt"; setup(){ def r1Writer = (new File(filename)).newWriter()