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 environment variable to activate snapshot writing #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,21 @@ public class PlanetConverterTest {
}
```

## Activate snapshot writing
Snapshots must be written at the same time as the test.
mbernardeau marked this conversation as resolved.
Show resolved Hide resolved
It is necessary to avoid writing them in CI environments.

To activate snapshot writing, pass the VM option `-Dtest.snapshots.write`.

### With Maven
```bash
mvn -Dtest.snapshots.write test
```

### With Intellij
Set VM option inside Run configuration.

![](./writesnapshot-intellij.png)

## Limitations
- Only one snapshot assertion per test method
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import difflib.Patch;

public class SnapshotMatcher<T> extends TypeSafeMatcher<T> {

private static final String SNAPSHOT_WRITE_VARIABLE = "test.snapshots.write";

/**
* Factory method to instantiate a snapshot matcher with the given type
*
Expand All @@ -48,11 +51,14 @@ public boolean matchesSafely(T o) {
if (Files.exists(snapshotPath)) {
// File exists => Compare snapshot file to given object
return compareSnapshot(o, snapshotPath);
} else {
} else if (isWriteSnapshotActivated()) {
// File doesn't exist => Create snapshot file and return true
createSnapshot(o, snapshotPath);
return true;
}
System.out.println("Snapshot writing is not activated in this environment.");
System.out.println("Activate snapshot writing by using -D" + SNAPSHOT_WRITE_VARIABLE);
return false;
}

/**
Expand Down Expand Up @@ -153,4 +159,7 @@ private StackTraceElement getCaller() {
.orElse(null);
}

private boolean isWriteSnapshotActivated() {
return System.getProperty(SNAPSHOT_WRITE_VARIABLE) != null;
}
}
Binary file added writesnapshot-intellij.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.