Skip to content

Commit d0fd9e7

Browse files
committed
fix: include checksum calculation for setter-cli-options
1 parent b2d55dc commit d0fd9e7

File tree

3 files changed

+120
-17
lines changed

3 files changed

+120
-17
lines changed

app/src/main/java/com/diffplug/spotless/cli/core/ChecksumCalculator.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.ByteArrayOutputStream;
1919
import java.io.OutputStream;
20+
import java.lang.annotation.Annotation;
2021
import java.lang.reflect.Field;
2122
import java.nio.charset.StandardCharsets;
2223
import java.util.ArrayList;
@@ -25,6 +26,8 @@
2526
import java.util.Objects;
2627
import java.util.stream.Stream;
2728

29+
import org.jetbrains.annotations.Nullable;
30+
2831
import com.diffplug.common.hash.Hashing;
2932
import com.diffplug.spotless.ThrowingEx;
3033
import com.diffplug.spotless.cli.SpotlessAction;
@@ -93,17 +96,32 @@ private static List<Class<?>> classHierarchy(Object obj) {
9396

9497
private static Stream<FieldOnObject> expandOptionField(Field field, Object obj) {
9598
if (field.isAnnotationPresent(CommandLine.Option.class)
96-
|| field.isAnnotationPresent(CommandLine.Parameters.class)) {
99+
|| field.isAnnotationPresent(CommandLine.Parameters.class)
100+
|| isAnnotatedSetterPresent(CommandLine.Option.class, field, obj)
101+
|| isAnnotatedSetterPresent(CommandLine.Parameters.class, field, obj)) {
97102
return Stream.of(new FieldOnObject(field, obj));
98103
}
99-
if (field.isAnnotationPresent(CommandLine.ArgGroup.class)) {
104+
if (field.isAnnotationPresent(CommandLine.ArgGroup.class)
105+
|| isAnnotatedSetterPresent(CommandLine.ArgGroup.class, field, obj)) {
100106
Object fieldValue = new FieldOnObject(field, obj).getValue();
107+
if (fieldValue == null) {
108+
return Stream.empty();
109+
}
101110
return Arrays.stream(fieldValue.getClass().getDeclaredFields())
102111
.flatMap(subField -> expandOptionField(subField, fieldValue));
103112
}
104113
return Stream.empty(); // nothing to expand
105114
}
106115

116+
private static <T extends Annotation> boolean isAnnotatedSetterPresent(
117+
Class<T> annotation, Field field, Object obj) {
118+
return Arrays.stream(obj.getClass().getDeclaredMethods())
119+
.filter(method -> method.getName().startsWith("set"))
120+
.filter(method -> method.getParameterCount() == 1)
121+
.filter(method -> method.getParameterTypes()[0].equals(field.getType()))
122+
.anyMatch(method -> method.isAnnotationPresent(annotation));
123+
}
124+
107125
private static String toHashedHexBytes(byte[] bytes) {
108126
byte[] hash = Hashing.murmur3_128().hashBytes(bytes).asBytes();
109127
StringBuilder builder = new StringBuilder();
@@ -122,7 +140,7 @@ private static class FieldOnObject {
122140
this.obj = obj;
123141
}
124142

125-
Object getValue() {
143+
@Nullable Object getValue() {
126144
ThrowingEx.run(() -> field.setAccessible(true));
127145
return ThrowingEx.get(() -> field.get(obj));
128146
}

app/src/test/java/com/diffplug/spotless/cli/core/ChecksumCalculatorTest.java

Lines changed: 99 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ class ChecksumCalculatorTest {
4343
@Test
4444
void itCalculatesAChecksumForStep() {
4545
Step step = step(
46-
randomPath(), randomString(), argGroup(null, randomByteArray()), List.of(randomPath(), randomPath()));
46+
randomPath(),
47+
randomString(),
48+
argGroup(null, randomByteArray()),
49+
argGroup(randomString(), null),
50+
List.of(randomPath(), randomPath()));
4751

4852
String checksum = checksumCalculator.calculateChecksum(step);
4953

@@ -52,10 +56,18 @@ void itCalculatesAChecksumForStep() {
5256

5357
@Test
5458
void itCalculatesDifferentChecksumsForSteps() {
55-
Step step1 =
56-
step(randomPath(), randomString(), argGroup(randomString(), null), List.of(randomPath(), randomPath()));
59+
Step step1 = step(
60+
randomPath(),
61+
randomString(),
62+
argGroup(randomString(), null),
63+
argGroup(null, randomByteArray()),
64+
List.of(randomPath(), randomPath()));
5765
Step step2 = step(
58-
randomPath(), randomString(), argGroup(null, randomByteArray()), List.of(randomPath(), randomPath()));
66+
randomPath(),
67+
randomString(),
68+
argGroup(null, randomByteArray()),
69+
argGroup(randomString(), null),
70+
List.of(randomPath(), randomPath()));
5971

6072
String checksum1 = checksumCalculator.calculateChecksum(step1);
6173
String checksum2 = checksumCalculator.calculateChecksum(step2);
@@ -66,7 +78,11 @@ void itCalculatesDifferentChecksumsForSteps() {
6678
@Test
6779
void itRecalculatesSameChecksumsForStep() {
6880
Step step = step(
69-
randomPath(), randomString(), argGroup(null, randomByteArray()), List.of(randomPath(), randomPath()));
81+
randomPath(),
82+
randomString(),
83+
argGroup(null, randomByteArray()),
84+
argGroup(randomString(), null),
85+
List.of(randomPath(), randomPath()));
7086

7187
String checksum1 = checksumCalculator.calculateChecksum(step);
7288
String checksum2 = checksumCalculator.calculateChecksum(step);
@@ -77,7 +93,11 @@ void itRecalculatesSameChecksumsForStep() {
7793
@Test
7894
void itCalculatesAChecksumForCommandLineStream() {
7995
Step step = step(
80-
randomPath(), randomString(), argGroup(null, randomByteArray()), List.of(randomPath(), randomPath()));
96+
randomPath(),
97+
randomString(),
98+
argGroup(null, randomByteArray()),
99+
argGroup(randomString(), null),
100+
List.of(randomPath(), randomPath()));
81101
Action action = action(randomPath());
82102
SpotlessCommandLineStream commandLineStream = commandLine(action, step);
83103

@@ -89,7 +109,11 @@ void itCalculatesAChecksumForCommandLineStream() {
89109
@Test
90110
void itCalculatesDifferentChecksumForDifferentCommandLineStreamDueToAction() {
91111
Step step = step(
92-
randomPath(), randomString(), argGroup(null, randomByteArray()), List.of(randomPath(), randomPath()));
112+
randomPath(),
113+
randomString(),
114+
argGroup(null, randomByteArray()),
115+
argGroup(randomString(), null),
116+
List.of(randomPath(), randomPath()));
93117
Action action1 = action(randomPath());
94118
Action action2 = action(randomPath());
95119
SpotlessCommandLineStream commandLineStream1 = commandLine(action1, step);
@@ -104,9 +128,17 @@ void itCalculatesDifferentChecksumForDifferentCommandLineStreamDueToAction() {
104128
@Test
105129
void itCalculatesDifferentChecksumForDifferentCommandLineStreamDueToSteps() {
106130
Step step1 = step(
107-
randomPath(), randomString(), argGroup(null, randomByteArray()), List.of(randomPath(), randomPath()));
131+
randomPath(),
132+
randomString(),
133+
argGroup(null, randomByteArray()),
134+
argGroup(randomString(), randomByteArray()),
135+
List.of(randomPath(), randomPath()));
108136
Step step2 = step(
109-
randomPath(), randomString(), argGroup(null, randomByteArray()), List.of(randomPath(), randomPath()));
137+
randomPath(),
138+
randomString(),
139+
argGroup(null, randomByteArray()),
140+
argGroup(randomString(), randomByteArray()),
141+
List.of(randomPath(), randomPath()));
110142
Action action = action(randomPath());
111143
SpotlessCommandLineStream commandLineStream1 = commandLine(action, step1);
112144
SpotlessCommandLineStream commandLineStream2 = commandLine(action, step2);
@@ -120,9 +152,17 @@ void itCalculatesDifferentChecksumForDifferentCommandLineStreamDueToSteps() {
120152
@Test
121153
void itCalculatesDifferentChecksumForDifferentCommandLineStreamDueToStepOrder() {
122154
Step step1 = step(
123-
randomPath(), randomString(), argGroup(null, randomByteArray()), List.of(randomPath(), randomPath()));
155+
randomPath(),
156+
randomString(),
157+
argGroup(null, randomByteArray()),
158+
argGroup(null, randomByteArray()),
159+
List.of(randomPath(), randomPath()));
124160
Step step2 = step(
125-
randomPath(), randomString(), argGroup(null, randomByteArray()), List.of(randomPath(), randomPath()));
161+
randomPath(),
162+
randomString(),
163+
argGroup(null, randomByteArray()),
164+
argGroup(null, randomByteArray()),
165+
List.of(randomPath(), randomPath()));
126166
Action action = action(randomPath());
127167
SpotlessCommandLineStream commandLineStream1 = commandLine(action, step1, step2);
128168
SpotlessCommandLineStream commandLineStream2 = commandLine(action, step2, step1);
@@ -133,11 +173,41 @@ void itCalculatesDifferentChecksumForDifferentCommandLineStreamDueToStepOrder()
133173
assertThat(checksum1).isNotEqualTo(checksum2);
134174
}
135175

136-
private static Step step(Path test1, String test2, StepArgGroup argGroup, List<Path> parameters) {
176+
@Test
177+
void itCalculatesDifferentChecksumDueToDifferentSetterOption() {
178+
Path test1 = randomPath();
179+
String test2 = randomString();
180+
Step step1 = step(test1, test2, randomString(), null, null, List.of());
181+
Step step2 = step(test1, test2, randomString(), null, null, List.of());
182+
183+
Action action = action(randomPath());
184+
SpotlessCommandLineStream commandLineStream1 = commandLine(action, step1);
185+
SpotlessCommandLineStream commandLineStream2 = commandLine(action, step2);
186+
187+
String checksum1 = checksumCalculator.calculateChecksum(commandLineStream1);
188+
String checksum2 = checksumCalculator.calculateChecksum(commandLineStream2);
189+
190+
assertThat(checksum1).isNotEqualTo(checksum2);
191+
}
192+
193+
private static Step step(
194+
Path test1, String test2, StepArgGroup argGroup, StepArgGroup argGroup2, List<Path> parameters) {
195+
return step(test1, test2, test1 + test2, argGroup, argGroup2, parameters);
196+
}
197+
198+
private static Step step(
199+
Path test1,
200+
String test2,
201+
String test3,
202+
StepArgGroup argGroup,
203+
StepArgGroup argGroup2,
204+
List<Path> parameters) {
137205
Step step = new Step();
138206
step.test1 = test1;
139207
step.test2 = test2;
208+
step.setTest5(test3);
140209
step.argGroup = argGroup;
210+
step.setArgGroup2(argGroup2);
141211
step.parameters = parameters;
142212
return step;
143213
}
@@ -169,9 +239,26 @@ static class Step extends SpotlessFormatterStep {
169239
@CommandLine.Option(names = "--test2")
170240
String test2;
171241

242+
private String test5;
243+
244+
@CommandLine.Option(names = "--test5")
245+
void setTest5(String test5) {
246+
if (test5 == null) {
247+
throw new NullPointerException("test5");
248+
}
249+
this.test5 = test5;
250+
}
251+
172252
@CommandLine.ArgGroup(exclusive = true)
173253
StepArgGroup argGroup;
174254

255+
private StepArgGroup argGroup2;
256+
257+
@CommandLine.ArgGroup(exclusive = true)
258+
void setArgGroup2(StepArgGroup argGroup2) {
259+
this.argGroup2 = argGroup2;
260+
}
261+
175262
@CommandLine.Parameters
176263
List<Path> parameters;
177264

app/src/test/java/com/diffplug/spotless/cli/steps/PrettierTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
@NpmTest
2727
public class PrettierTest extends CLIIntegrationHarness {
2828

29-
// TODO
30-
3129
@Test
3230
void itRunsPrettierForTsFilesWithOptions() throws IOException {
3331
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");

0 commit comments

Comments
 (0)