diff --git a/docs/docs/cli/test.md b/docs/docs/cli/test.md index ed608383..195c72fa 100644 --- a/docs/docs/cli/test.md +++ b/docs/docs/cli/test.md @@ -46,6 +46,11 @@ Writes test results in [JUnit XML format](https://junit.org/) to file, which con Writes test results in csv file. +#### `--ci` + +By default,nf-test automatically stores a new snapshot. When CI mode is activated, nf-test will fail the test instead of storing the snapshot automatically. + + ### Optimizing Test Execution #### `--related-tests ` 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 0b00765b..07fe6274 100644 --- a/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java @@ -71,7 +71,10 @@ public class RunTestsCommand extends AbstractCommand { "--updateSnapshot" }, description = "Use this flag to re-record every snapshot that fails during this test run.", required = false, showDefaultValue = Visibility.ALWAYS) private boolean updateSnapshot = false; - @Option(names = { "--related-tests", "--relatedTests"}, description = "Finds all related tests for the provided .nf or nf.test files.", required = false, showDefaultValue = Visibility.ALWAYS) + @Option(names = { "--ci" }, description = "Activates CI mode. Instead of automatically storing a new snapshot as per usual, it will now fail the test.", required = false, showDefaultValue = Visibility.ALWAYS) + private boolean ciMode = false; + + @Option(names = { "--related-tests", "--relatedTests"}, description = "Finds and executes all related tests for the provided .nf or nf.test files.", required = false, showDefaultValue = Visibility.ALWAYS) private boolean findRelatedTests = false; @Option(names = { "--follow-dependencies", "--followDependencies"}, description = "Follows all dependencies when related-tests is set.", required = false, showDefaultValue = Visibility.ALWAYS) @@ -281,6 +284,7 @@ public Integer execute() throws Exception { engine.setDebug(debug); engine.setUpdateSnapshot(updateSnapshot); engine.setCleanSnapshot(cleanSnapshot); + engine.setCIMode(ciMode); engine.addProfile(profile); engine.setDryRun(dryRun); if (withoutTrace) { 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 ff7675f1..73af19e7 100644 --- a/src/main/java/com/askimed/nf/test/core/AbstractTest.java +++ b/src/main/java/com/askimed/nf/test/core/AbstractTest.java @@ -62,6 +62,8 @@ public abstract class AbstractTest implements ITest { private boolean updateSnapshot = false; + private boolean ciMode = false; + private boolean debug = false; private boolean withTrace = true; @@ -244,6 +246,16 @@ public boolean isUpdateSnapshot() { return updateSnapshot; } + @Override + public void setCIMode(boolean ciMode) { + this.ciMode = ciMode; + } + + @Override + public boolean isCIMode() { + return false; + } + @Override public String toString() { return getHash().substring(0, 8) + ": " + getName(); 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 180eb236..0bbeab6e 100644 --- a/src/main/java/com/askimed/nf/test/core/ITest.java +++ b/src/main/java/com/askimed/nf/test/core/ITest.java @@ -34,4 +34,8 @@ public interface ITest extends ITaggable { public boolean isUpdateSnapshot(); + public void setCIMode(boolean ciMode); + + public boolean isCIMode(); + } 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 93628e1b..652a56d6 100644 --- a/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java +++ b/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java @@ -38,6 +38,8 @@ public class TestExecutionEngine { private boolean updateSnapshot = false; + private boolean ciMode = false; + private boolean cleanSnapshot = false; private boolean dryRun = false; @@ -73,6 +75,13 @@ public void setUpdateSnapshot(boolean updateSnapshot) { this.updateSnapshot = updateSnapshot; } + public void setCIMode(boolean ciMode) { + if (ciMode) { + System.out.println("nf-test runs in CI mode."); + } + this.ciMode = ciMode; + } + public void setCleanSnapshot(boolean cleanSnapshot) { this.cleanSnapshot = cleanSnapshot; } @@ -143,6 +152,7 @@ public int execute() throws Throwable { TestExecutionResult result = new TestExecutionResult(test); test.setWithTrace(withTrace); test.setUpdateSnapshot(updateSnapshot); + test.setCIMode(ciMode); try { // override debug flag from CLI diff --git a/src/main/java/com/askimed/nf/test/lang/extensions/Snapshot.java b/src/main/java/com/askimed/nf/test/lang/extensions/Snapshot.java index 6370ce9b..ad59dc1e 100644 --- a/src/main/java/com/askimed/nf/test/lang/extensions/Snapshot.java +++ b/src/main/java/com/askimed/nf/test/lang/extensions/Snapshot.java @@ -43,6 +43,9 @@ public boolean match(String id) throws IOException { SnapshotFileItem expected = file.getSnapshot(id); // new snapshot --> create snapshot if (expected == null) { + if (test.isCIMode()) { + throw new RuntimeException("CI mode activated and snapshot with id '" + id + "not found."); + } log.debug("Snapshot '{}' not found.", id); file.createSnapshot(id, actual); file.save();