Skip to content

Commit 9f016c8

Browse files
committed
Because there are a few legitimate reasons to not roundtrip.
1 parent 63414a7 commit 9f016c8

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ public static StepHarness forFormatter(Formatter formatter) {
5252
return new StepHarness(formatter, RoundTrip.ASSERT_EQUAL);
5353
}
5454

55+
public static StepHarness forStepNoRoundtrip(FormatterStep step) {
56+
return new StepHarness(Formatter.builder()
57+
.steps(Arrays.asList(step))
58+
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
59+
.encoding(StandardCharsets.UTF_8)
60+
.rootDir(Paths.get(""))
61+
.exceptionPolicy(new FormatExceptionPolicyStrict())
62+
.build(), RoundTrip.DONT_ROUNDTRIP);
63+
}
64+
5565
/** Asserts that the given element is transformed as expected, and that the result is idempotent. */
5666
public StepHarness test(String before, String after) {
5767
String actual = formatter().compute(LineEnding.toUnix(before), new File(""));

testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@
2929
import com.diffplug.spotless.FormatterStep;
3030
import com.diffplug.spotless.ResourceHarness;
3131
import com.diffplug.spotless.StepHarness;
32-
import com.diffplug.spotless.StepHarnessWithFile;
3332

3433
class FenceStepTest extends ResourceHarness {
3534
@Test
3635
void single() {
3736
FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")
3837
.preserveWithin(Arrays.asList(createNeverUpToDateSerializable("lowercase", String::toLowerCase)));
39-
StepHarness harness = StepHarness.forSteps(fence);
38+
StepHarness harness = StepHarness.forStepNoRoundtrip(fence);
4039
harness.test(
4140
StringPrinter.buildStringFromLines(
4241
"A B C",
@@ -56,7 +55,7 @@ void single() {
5655
void multiple() {
5756
FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")
5857
.preserveWithin(Arrays.asList(createNeverUpToDateSerializable("lowercase", String::toLowerCase)));
59-
StepHarness harness = StepHarness.forSteps(fence);
58+
StepHarness harness = StepHarness.forStepNoRoundtrip(fence);
6059
harness.test(
6160
StringPrinter.buildStringFromLines(
6261
"A B C",
@@ -90,9 +89,8 @@ void multiple() {
9089
void broken() {
9190
FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")
9291
.preserveWithin(Arrays.asList(createNeverUpToDateSerializable("uppercase", String::toUpperCase)));
93-
StepHarnessWithFile harness = StepHarnessWithFile.forStep(this, fence);
9492
// this fails because uppercase turns spotless:off into SPOTLESS:OFF, etc
95-
harness.testExceptionMsg(newFile("test"), StringPrinter.buildStringFromLines("A B C",
93+
StepHarness.forStepNoRoundtrip(fence).testExceptionMsg(StringPrinter.buildStringFromLines("A B C",
9694
"spotless:off",
9795
"D E F",
9896
"spotless:on",
@@ -103,7 +101,7 @@ void broken() {
103101
void andApply() {
104102
FormatterStep fence = FenceStep.named("fence").openClose("<lower>", "</lower>")
105103
.applyWithin(Arrays.asList(createNeverUpToDateSerializable("lowercase", String::toLowerCase)));
106-
StepHarness.forSteps(fence).test(
104+
StepHarness.forStepNoRoundtrip(fence).test(
107105
StringPrinter.buildStringFromLines(
108106
"A B C",
109107
"<lower>",
@@ -152,5 +150,6 @@ public String getName() {
152150
public String format(String rawUnix, File file) throws Exception {
153151
return formatterFunc.apply(rawUnix, file);
154152
}
153+
155154
}
156155
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,7 +64,7 @@ void behavior() throws Exception {
6464
@Test
6565
void versionBelowMinimumRequiredVersionIsNotAllowed() throws Exception {
6666
FormatterStep step = GoogleJavaFormatStep.create("1.2", "AOSP", TestProvisioner.mavenCentral());
67-
StepHarness.forStep(step)
67+
StepHarness.forStepNoRoundtrip(step)
6868
.testResourceExceptionMsg("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test")
6969
.contains("you are using 1.2");
7070
}
@@ -73,7 +73,7 @@ void versionBelowMinimumRequiredVersionIsNotAllowed() throws Exception {
7373
@EnabledForJreRange(min = JAVA_16)
7474
void versionBelowOneDotTenIsNotAllowed() throws Exception {
7575
FormatterStep step = GoogleJavaFormatStep.create("1.9", "AOSP", TestProvisioner.mavenCentral());
76-
StepHarness.forStep(step)
76+
StepHarness.forStepNoRoundtrip(step)
7777
.testResourceExceptionMsg("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test")
7878
.contains("you are using 1.9");
7979
}

0 commit comments

Comments
 (0)