Skip to content

Commit cee4af4

Browse files
authored
#patch: Restrict characters in snapshot name
Fixes #122 Make '=', '[' and ']' characters illegal in snapshot name and scenario fields
1 parent e2dbb69 commit cee4af4

File tree

7 files changed

+111
-18
lines changed

7 files changed

+111
-18
lines changed

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ We welcome contributions to this project by both internal and external parties
2121
## Building
2222

2323
```java
24-
./gradlew shadowJar
24+
./gradlew spotlessApply shadowJar
2525
```
2626

2727
## Deploying locally and deploying to `.m2/`

java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/Expect.java

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public void toMatchSnapshot(Object object) {
5252
}
5353
snapshotContext.header.putAll(headers);
5454

55+
snapshotContext.checkValidContext();
56+
5557
snapshotContext.toMatchSnapshot();
5658
}
5759

java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotContext.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import au.com.origin.snapshots.annotations.SnapshotName;
44
import au.com.origin.snapshots.comparators.SnapshotComparator;
55
import au.com.origin.snapshots.config.SnapshotConfig;
6+
import au.com.origin.snapshots.exceptions.ReservedWordException;
67
import au.com.origin.snapshots.exceptions.SnapshotMatchException;
78
import au.com.origin.snapshots.reporters.SnapshotReporter;
89
import au.com.origin.snapshots.serializers.SnapshotSerializer;
910
import java.lang.reflect.Method;
10-
import java.util.ArrayList;
11-
import java.util.Collection;
12-
import java.util.List;
13-
import java.util.Set;
11+
import java.util.*;
1412
import java.util.stream.Collectors;
1513
import lombok.Getter;
1614
import lombok.Setter;
@@ -19,6 +17,8 @@
1917
@Slf4j
2018
public class SnapshotContext {
2119

20+
private static final List<String> RESERVED_WORDS = Arrays.asList("=", "[", "]");
21+
2222
private final SnapshotConfig snapshotConfig;
2323
private final SnapshotFile snapshotFile;
2424

@@ -141,11 +141,24 @@ private Snapshot takeSnapshot() {
141141

142142
String resolveSnapshotIdentifier() {
143143
String scenarioFormat = scenario == null ? "" : "[" + scenario + "]";
144+
return snapshotName() + scenarioFormat;
145+
}
146+
147+
private String snapshotName() {
144148
SnapshotName snapshotName = testMethod.getAnnotation(SnapshotName.class);
145-
String pathFormat =
146-
snapshotName == null
147-
? testClass.getName() + "." + testMethod.getName()
148-
: snapshotName.value();
149-
return pathFormat + scenarioFormat;
149+
return snapshotName == null
150+
? testClass.getName() + "." + testMethod.getName()
151+
: snapshotName.value();
152+
}
153+
154+
void checkValidContext() {
155+
for (String rw : RESERVED_WORDS) {
156+
if (snapshotName().contains(rw)) {
157+
throw new ReservedWordException("snapshot name", rw, RESERVED_WORDS);
158+
}
159+
if (scenario != null && scenario.contains(rw)) {
160+
throw new ReservedWordException("scenario name", rw, RESERVED_WORDS);
161+
}
162+
}
150163
}
151164
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package au.com.origin.snapshots.exceptions;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class ReservedWordException extends RuntimeException {
7+
8+
public ReservedWordException(String message) {
9+
super(message);
10+
}
11+
12+
public ReservedWordException(String element, String reservedWord, List<String> reservedWords) {
13+
super(
14+
String.format(
15+
"You cannot use the '%s' character inside '%s'. Reserved characters are ",
16+
reservedWord, element, reservedWords.stream().collect(Collectors.joining(","))));
17+
}
18+
}

java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/SnapshotNameAnnotationTest.java

+60
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package au.com.origin.snapshots;
22

3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
35
import au.com.origin.snapshots.annotations.SnapshotName;
46
import au.com.origin.snapshots.config.BaseSnapshotConfig;
7+
import au.com.origin.snapshots.exceptions.ReservedWordException;
58
import org.junit.jupiter.api.BeforeEach;
69
import org.junit.jupiter.api.Test;
710
import org.junit.jupiter.api.TestInfo;
@@ -32,4 +35,61 @@ void canUseSnapshotNameAnnotationWithSpaces(TestInfo testInfo) {
3235
expect.toMatchSnapshot("Hello World");
3336
snapshotVerifier.validateSnapshots();
3437
}
38+
39+
@SnapshotName("can't use '=' character in snapshot name")
40+
@Test
41+
void cannotUseEqualsInsideSnapshotName(TestInfo testInfo) {
42+
SnapshotVerifier snapshotVerifier =
43+
new SnapshotVerifier(new BaseSnapshotConfig(), testInfo.getTestClass().get());
44+
Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get());
45+
assertThrows(ReservedWordException.class, () -> expect.toMatchSnapshot("FooBar"));
46+
}
47+
48+
@SnapshotName("can't use '[' character in snapshot name")
49+
@Test
50+
void cannotUseOpeningSquareBracketInsideSnapshotName(TestInfo testInfo) {
51+
SnapshotVerifier snapshotVerifier =
52+
new SnapshotVerifier(new BaseSnapshotConfig(), testInfo.getTestClass().get());
53+
Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get());
54+
assertThrows(ReservedWordException.class, () -> expect.toMatchSnapshot("FooBar"));
55+
}
56+
57+
@SnapshotName("can't use ']' character in snapshot name")
58+
@Test
59+
void cannotUseClosingSquareBracketInsideSnapshotName(TestInfo testInfo) {
60+
SnapshotVerifier snapshotVerifier =
61+
new SnapshotVerifier(new BaseSnapshotConfig(), testInfo.getTestClass().get());
62+
Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get());
63+
assertThrows(ReservedWordException.class, () -> expect.toMatchSnapshot("FooBar"));
64+
}
65+
66+
@Test
67+
void cannotUseEqualsInsideScenarioName(TestInfo testInfo) {
68+
SnapshotVerifier snapshotVerifier =
69+
new SnapshotVerifier(new BaseSnapshotConfig(), testInfo.getTestClass().get());
70+
Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get());
71+
assertThrows(
72+
ReservedWordException.class,
73+
() -> expect.scenario("can't use = symbol in scenario").toMatchSnapshot("FooBar"));
74+
}
75+
76+
@Test
77+
void cannotUseOpeningSquareBracketInsideScenarioName(TestInfo testInfo) {
78+
SnapshotVerifier snapshotVerifier =
79+
new SnapshotVerifier(new BaseSnapshotConfig(), testInfo.getTestClass().get());
80+
Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get());
81+
assertThrows(
82+
ReservedWordException.class,
83+
() -> expect.scenario("can't use [ symbol in scenario").toMatchSnapshot("FooBar"));
84+
}
85+
86+
@Test
87+
void cannotUseClosingSquareBracketInsideScenarioName(TestInfo testInfo) {
88+
SnapshotVerifier snapshotVerifier =
89+
new SnapshotVerifier(new BaseSnapshotConfig(), testInfo.getTestClass().get());
90+
Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get());
91+
assertThrows(
92+
ReservedWordException.class,
93+
() -> expect.scenario("can't use ] symbol in scenario").toMatchSnapshot("FooBar"));
94+
}
3595
}

java-snapshot-testing-junit4/src/test/java/au/com/origin/snapshots/ParameterizedTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ public class ParameterizedTest {
2020

2121
private Expect expect;
2222

23-
@Parameters(name = "letter={0}")
23+
@Parameters(name = "letter is {0}")
2424
public static Iterable<Object[]> data() {
25-
return Arrays.asList(new Object[][] {{'a'}, {'b'}, {'c'}});
25+
return Arrays.asList(new Object[][] {{"a"}, {"b"}, {"c"}});
2626
}
2727

28-
private char input;
28+
private String input;
2929

30-
public ParameterizedTest(char input) {
30+
public ParameterizedTest(String input) {
3131
this.input = input;
3232
}
3333

3434
@Test
3535
public void test() {
36-
expect.scenario(testName.getMethodName()).toMatchSnapshot(input);
36+
expect.scenario(input).toMatchSnapshot(input);
3737
}
3838
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
au.com.origin.snapshots.ParameterizedTest.test[test[letter=a]]=[
1+
au.com.origin.snapshots.ParameterizedTest.test[a]=[
22
a
33
]
44

55

6-
au.com.origin.snapshots.ParameterizedTest.test[test[letter=b]]=[
6+
au.com.origin.snapshots.ParameterizedTest.test[b]=[
77
b
88
]
99

1010

11-
au.com.origin.snapshots.ParameterizedTest.test[test[letter=c]]=[
11+
au.com.origin.snapshots.ParameterizedTest.test[c]=[
1212
c
1313
]

0 commit comments

Comments
 (0)