Skip to content

Commit b1bb965

Browse files
committed
Make NeverUpToDateStep actually roundtrip serializable for our tests.
1 parent 6d4e268 commit b1bb965

File tree

4 files changed

+13
-37
lines changed

4 files changed

+13
-37
lines changed

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131

3232
import com.diffplug.common.base.StringPrinter;
3333
import com.diffplug.spotless.FileSignature;
34-
import com.diffplug.spotless.FormatterFunc;
3534
import com.diffplug.spotless.FormatterStep;
3635
import com.diffplug.spotless.LineEnding;
3736
import com.diffplug.spotless.NeverUpToDateStep;
3837
import com.diffplug.spotless.ResourceHarness;
38+
import com.diffplug.spotless.SerializedFunction;
3939
import com.diffplug.spotless.TestProvisioner;
4040

4141
class PaddedCellTaskTest extends ResourceHarness {
@@ -54,7 +54,7 @@ public BuildServiceParameters.None getParameters() {
5454
SpotlessCheck check;
5555
SpotlessApply apply;
5656

57-
Bundle(String name, FormatterFunc function) throws IOException {
57+
Bundle(String name, SerializedFunction<String, String> function) throws IOException {
5858
this.name = name;
5959
file = setFile("src/test." + name).toContent("CCC");
6060
FormatterStep step = NeverUpToDateStep.create(name, function);

plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/PluginFingerprintTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.diffplug.spotless.FormatterStep;
4141
import com.diffplug.spotless.LineEnding;
4242
import com.diffplug.spotless.NeverUpToDateStep;
43+
import com.diffplug.spotless.SerializedFunction;
4344
import com.diffplug.spotless.maven.MavenIntegrationHarness;
4445

4546
class PluginFingerprintTest extends MavenIntegrationHarness {
@@ -163,7 +164,7 @@ private static Model readPom(String xml) throws Exception {
163164
}
164165

165166
private static FormatterStep formatterStep(String name) {
166-
return NeverUpToDateStep.create(name, input -> input);
167+
return NeverUpToDateStep.create(name, SerializedFunction.identity());
167168
}
168169

169170
private static Formatter formatter(FormatterStep... steps) {

testlib/src/main/java/com/diffplug/spotless/NeverUpToDateStep.java

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,11 @@
1616
package com.diffplug.spotless;
1717

1818
import java.io.File;
19-
import java.util.Objects;
2019

2120
/**
2221
* Formatter which is equal to itself, but not to any other Formatter.
2322
*/
2423
public class NeverUpToDateStep implements FormatterStep {
25-
/**
26-
* @param name
27-
* The name of the formatter step
28-
* @param functionSupplier
29-
* A supplier which will lazily generate the function
30-
* used by the formatter step
31-
* @return A FormatterStep which will never report that it is up-to-date, because
32-
* it is not equal to the serialized representation of itself.
33-
*/
34-
public static FormatterStep createLazy(
35-
String name,
36-
ThrowingEx.Supplier<FormatterFunc> functionSupplier) {
37-
return new NeverUpToDateStep(name, functionSupplier);
38-
}
39-
4024
/**
4125
* @param name
4226
* The name of the formatter step
@@ -47,20 +31,18 @@ public static FormatterStep createLazy(
4731
*/
4832
public static FormatterStep create(
4933
String name,
50-
FormatterFunc function) {
51-
Objects.requireNonNull(function, "function");
52-
return createLazy(name, () -> function);
34+
SerializedFunction<String, String> function) {
35+
return new NeverUpToDateStep(name, function);
5336
}
5437

5538
private static final long serialVersionUID = 1L;
5639

5740
private final String name;
58-
private final ThrowingEx.Supplier<FormatterFunc> formatterSupplier;
59-
private transient FormatterFunc formatter; // initialized lazily
41+
private final SerializedFunction<String, String> formatter; // initialized lazily
6042

61-
NeverUpToDateStep(String name, ThrowingEx.Supplier<FormatterFunc> formatterSupplier) {
43+
NeverUpToDateStep(String name, SerializedFunction<String, String> formatter) {
6244
this.name = name;
63-
this.formatterSupplier = Objects.requireNonNull(formatterSupplier, "formatterSupplier");
45+
this.formatter = formatter;
6446
}
6547

6648
@Override
@@ -70,20 +52,13 @@ public String getName() {
7052

7153
@Override
7254
public String format(String rawUnix, File file) throws Exception {
73-
if (formatter == null) {
74-
formatter = formatterSupplier.get();
75-
if (formatter instanceof FormatterFunc.Closeable) {
76-
throw new AssertionError("NeverUpToDate does not support FormatterFunc.Closeable. See https://github.com/diffplug/spotless/pull/284");
77-
}
78-
}
79-
return formatter.apply(rawUnix, file);
55+
return formatter.apply(rawUnix);
8056
}
8157

8258
@Override
8359
public void close() throws Exception {
8460
if (formatter instanceof FormatterFunc.Closeable) {
8561
((FormatterFunc.Closeable) formatter).close();
86-
formatter = null;
8762
}
8863
}
8964
}

testlib/src/test/java/com/diffplug/spotless/PaddedCellTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ class PaddedCellTest {
3737
@TempDir
3838
File rootFolder;
3939

40-
private void misbehaved(FormatterFunc step, String input, PaddedCell.Type expectedOutputType, String steps, String canonical) throws IOException {
40+
private void misbehaved(SerializedFunction<String, String> step, String input, PaddedCell.Type expectedOutputType, String steps, String canonical) throws IOException {
4141
testCase(step, input, expectedOutputType, steps, canonical, true);
4242
}
4343

44-
private void wellBehaved(FormatterFunc step, String input, PaddedCell.Type expectedOutputType, String canonical) throws IOException {
44+
private void wellBehaved(SerializedFunction<String, String> step, String input, PaddedCell.Type expectedOutputType, String canonical) throws IOException {
4545
testCase(step, input, expectedOutputType, canonical, canonical, false);
4646
}
4747

48-
private void testCase(FormatterFunc step, String input, PaddedCell.Type expectedOutputType, String expectedSteps, String canonical, boolean misbehaved) throws IOException {
48+
private void testCase(SerializedFunction<String, String> step, String input, PaddedCell.Type expectedOutputType, String expectedSteps, String canonical, boolean misbehaved) throws IOException {
4949
List<FormatterStep> formatterSteps = new ArrayList<>();
5050
formatterSteps.add(NeverUpToDateStep.create("step", step));
5151
try (Formatter formatter = Formatter.builder()

0 commit comments

Comments
 (0)