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 b1cad182..cc97565b 100644 --- a/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java @@ -140,7 +140,7 @@ public class RunTestsCommand extends AbstractCommand { ) private boolean smartTesting = false; - @Option( + @Option( names = {"--stop-on-first-failure", "--stopOnFirstFailure"}, description = "Stop execution immediately after the first test failure", required = false, @@ -148,6 +148,14 @@ public class RunTestsCommand extends AbstractCommand { ) private boolean stopOnFirstFailure = false; + @Option( + names = {"--no-save", "--noSave"}, + description = "Delete test launch directory after successful run", + required = false, + showDefaultValue = Visibility.ALWAYS + ) + private boolean noSave = false; + private static Logger log = LoggerFactory.getLogger(RunTestsCommand.class); @Override @@ -306,6 +314,7 @@ public Integer execute() throws Exception { engine.setCIMode(ciMode); engine.addProfile(profile); engine.setStopOnFirstFailure(stopOnFirstFailure); + engine.setNoSave(noSave); engine.setDryRun(dryRun); if (withoutTrace) { engine.setWithTrace(false); 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 39159a4e..482899a1 100644 --- a/src/main/java/com/askimed/nf/test/core/AbstractTest.java +++ b/src/main/java/com/askimed/nf/test/core/AbstractTest.java @@ -96,6 +96,11 @@ public File getConfig() { return config; } + @Override + public File getLaunchDir() { + return launchDir; + } + public void defineDirectories(File testDirectory) throws IOException { if (testDirectory == null) { 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 0bbeab6e..67932811 100644 --- a/src/main/java/com/askimed/nf/test/core/ITest.java +++ b/src/main/java/com/askimed/nf/test/core/ITest.java @@ -28,6 +28,8 @@ public interface ITest extends ITaggable { public ITestSuite getTestSuite(); + public File getLaunchDir(); + public void setWithTrace(boolean withTrace); public void setUpdateSnapshot(boolean updateSnapshot); 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 66347406..7895afed 100644 --- a/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java +++ b/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java @@ -46,6 +46,8 @@ public class TestExecutionEngine { private boolean stopOnFirstFailure = false; + private boolean noSave = false; + private static Logger log = LoggerFactory.getLogger(TestExecutionEngine.class); public void setDebug(boolean debug) { @@ -100,6 +102,10 @@ public void setStopOnFirstFailure(boolean stopOnFirstFailure) { this.stopOnFirstFailure = stopOnFirstFailure; } + public void setNoSave(boolean noSave) { + this.noSave = noSave; + } + public int execute() throws Throwable { if (configFile != null) { @@ -184,6 +190,22 @@ public int execute() throws Throwable { } test.cleanup(); + + if (noSave + && result.getStatus() == TestExecutionResultStatus.PASSED + && !debug + ) { + try { + File launchDir = test.getLaunchDir(); + if (launchDir != null && launchDir.exists()) { + log.info("Deleting launch directory due to --no-save: {}", launchDir); + FileUtil.deleteDirectory(launchDir); + } + } catch (Exception e) { + log.warn("Failed to delete launch directory for test '{}': {}", test, e.getMessage()); + } + } + result.setEndTime(System.currentTimeMillis()); log.info("Test '{}' finished. status: {}", result.getTest(), result.getStatus(), result.getThrowable()); diff --git a/src/test/java/com/askimed/nf/test/lang/ProcessTest.java b/src/test/java/com/askimed/nf/test/lang/ProcessTest.java index d498a762..7ac73139 100644 --- a/src/test/java/com/askimed/nf/test/lang/ProcessTest.java +++ b/src/test/java/com/askimed/nf/test/lang/ProcessTest.java @@ -46,6 +46,42 @@ public void testExample() throws Exception { int exitCode = app.run(new String[] { "test", "test-data/process/example/test1.nf.test" }); assertEquals(0, exitCode); + File testsRoot = new File(".nf-test/tests"); + File[] testDirs = testsRoot.listFiles(File::isDirectory); + assertNotNull(testDirs, "Could not list test directories"); + + assertTrue( + testDirs.length == 5, + "Expected test directories to exist without --no-save" + ); + + for (File testDir : testDirs) { + File workDir = new File(testDir, "work"); + assertTrue( + workDir.exists(), + "Launch and Work directory should exist without --no-save" + ); + } + + } + + @Test + public void testExampleNoSaveDeletesLaunchDir() throws Exception { + + App app = new App(); + int exitCode = app.run(new String[] { "test", "test-data/process/example/test1.nf.test", "--no-save" }); + assertEquals(0, exitCode); + + File testsRoot = new File(".nf-test/tests"); + File[] testDirs = testsRoot.listFiles(File::isDirectory); + assertNotNull(testDirs, "Could not list test directories"); + + assertEquals( + 0, + testDirs.length, + "Expected all test directories to be deleted with --no-save" + ); + } /* @@ -125,6 +161,33 @@ public void testScriptFailed() throws Exception { } + @Test + public void testScriptFailedKeepsWorkDirWithNoSave() throws Exception { + + App app = new App(); + int exitCode = app.run(new String[] { "test", "test-data/process/default/test_process_failed.nf.test", "--no-save" }); + assertEquals(1, exitCode); + + File testsRoot = new File(".nf-test/tests"); + File[] testDirs = testsRoot.listFiles(File::isDirectory); + assertNotNull(testDirs, "Could not list test directories"); + + assertTrue( + testDirs.length == 1, + "Expected test directories to exist without --no-save" + ); + + for (File testDir : testDirs) { + File workDir = new File(testDir, "work"); + + assertTrue( + workDir.exists(), + "Launch and Work directory should still exist with --no-save if process failed" + ); + } + + } + @Test public void testScriptWithSyntaxError() throws Exception { diff --git a/src/test/java/com/askimed/nf/test/lang/WorkflowTest.java b/src/test/java/com/askimed/nf/test/lang/WorkflowTest.java index 25ad76b8..3613b555 100644 --- a/src/test/java/com/askimed/nf/test/lang/WorkflowTest.java +++ b/src/test/java/com/askimed/nf/test/lang/WorkflowTest.java @@ -1,6 +1,6 @@ package com.askimed.nf.test.lang; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; import java.io.File; import java.io.IOException; @@ -193,6 +193,66 @@ public void testRegex() throws Exception { } + @Test + public void testWorkflowKeepsLaunchDirByDefault() throws Exception { + + App app = new App(); + + int exitCode = app.run(new String[] { + "test", + "test-data/workflow/regex/workflow.nf.test" + }); + + assertEquals(0, exitCode); + + File testsRoot = new File(".nf-test/tests"); + File[] testDirs = testsRoot.listFiles(File::isDirectory); + assertNotNull(testDirs, "Could not list test directories"); + + assertTrue( + testDirs.length > 0, + "Expected test directories to exist without --no-save" + ); + + for (File testDir : testDirs) { + File workDir = new File(testDir, "work"); + + assertTrue( + workDir.exists(), + "Launch and Work directory should exist without --no-save" + ); + } + } + + @Test + public void testWorkflowNoSaveDeletesLaunchDir() throws Exception { + + App app = new App(); + + String testPath = "test-data/workflow/regex/workflow.nf.test"; + + // Run test with --no-save + int exitCode = app.run(new String[] { + "test", + testPath, + "--no-save" + }); + + assertEquals(0, exitCode); + + // Locate .nf-test directory + File nfTestDir = new File(".nf-test/tests"); + File[] remaining = nfTestDir.listFiles(file -> file.isDirectory()); + + assertNotNull(remaining, "Could not list test directories"); + + assertEquals( + 0, + remaining.length, + "Expected no test directories to remain when --no-save is enabled" + ); + } + @Test public void testWorkflowTopics() throws Exception {