Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --ci flag #217

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/docs/cli/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <files>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/askimed/nf/test/core/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/askimed/nf/test/core/ITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public interface ITest extends ITaggable {

public boolean isUpdateSnapshot();

public void setCIMode(boolean ciMode);

public boolean isCIMode();

}
10 changes: 10 additions & 0 deletions src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class TestExecutionEngine {

private boolean updateSnapshot = false;

private boolean ciMode = false;

private boolean cleanSnapshot = false;

private boolean dryRun = false;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading