Skip to content

Commit 9def582

Browse files
committed
Add environment variable to activate snapshot writing
1 parent 7e6ed40 commit 9def582

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

snapshot-matcher/src/main/java/com/zenika/snapshotmatcher/SnapshotMatcher.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import difflib.Patch;
2323

2424
public class SnapshotMatcher<T> extends TypeSafeMatcher<T> {
25+
26+
private static final String SNAPSHOT_WRITE_VARIABLE = "test.snapshots.write";
27+
2528
/**
2629
* Factory method to instantiate a snapshot matcher with the given type
2730
*
@@ -48,11 +51,14 @@ public boolean matchesSafely(T o) {
4851
if (Files.exists(snapshotPath)) {
4952
// File exists => Compare snapshot file to given object
5053
return compareSnapshot(o, snapshotPath);
51-
} else {
54+
} else if (isWriteSnapshotActivated()) {
5255
// File doesn't exist => Create snapshot file and return true
5356
createSnapshot(o, snapshotPath);
5457
return true;
5558
}
59+
System.out.println("Snapshot writing is not activated in this environment.");
60+
System.out.println("Activate snapshot writing by using -D" + SNAPSHOT_WRITE_VARIABLE);
61+
return false;
5662
}
5763

5864
/**
@@ -153,4 +159,7 @@ private StackTraceElement getCaller() {
153159
.orElse(null);
154160
}
155161

162+
private boolean isWriteSnapshotActivated() {
163+
return System.getProperty(SNAPSHOT_WRITE_VARIABLE) != null;
164+
}
156165
}

0 commit comments

Comments
 (0)