Skip to content

Commit 362796c

Browse files
committed
Add environment variable to activate snapshot writing
1 parent 7e6ed40 commit 362796c

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.file.Paths;
1212
import java.util.ArrayList;
1313
import java.util.List;
14+
import java.util.Optional;
1415
import java.util.stream.Collectors;
1516
import java.util.stream.Stream;
1617

@@ -48,11 +49,14 @@ public boolean matchesSafely(T o) {
4849
if (Files.exists(snapshotPath)) {
4950
// File exists => Compare snapshot file to given object
5051
return compareSnapshot(o, snapshotPath);
51-
} else {
52+
} else if (isWriteSnapshotActivated()) {
5253
// File doesn't exist => Create snapshot file and return true
5354
createSnapshot(o, snapshotPath);
5455
return true;
5556
}
57+
System.out.println("Snapshot writing is not activated in this environment.");
58+
System.out.println("Activate snapshot writing by using -Dtest.snapshots.write");
59+
return false;
5660
}
5761

5862
/**
@@ -153,4 +157,7 @@ private StackTraceElement getCaller() {
153157
.orElse(null);
154158
}
155159

160+
private boolean isWriteSnapshotActivated() {
161+
return System.getProperty("test.snapshots.write") != null;
162+
}
156163
}

0 commit comments

Comments
 (0)