Skip to content

Commit b231189

Browse files
committed
ErrorShouldRethrowTest now tests the new lint messages.
1 parent 11448d9 commit b231189

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

lib/src/main/java/com/diffplug/spotless/LintState.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
import java.util.Iterator;
2222
import java.util.LinkedHashMap;
2323
import java.util.List;
24+
import java.util.Objects;
2425

2526
import javax.annotation.Nullable;
2627

2728
public class LintState {
2829
private final DirtyState dirtyState;
29-
private final @Nullable List<List<Lint>> lintsPerStep;
30+
private @Nullable List<List<Lint>> lintsPerStep;
3031

31-
private LintState(DirtyState dirtyState, @Nullable List<List<Lint>> lintsPerStep) {
32+
LintState(DirtyState dirtyState, @Nullable List<List<Lint>> lintsPerStep) {
3233
this.dirtyState = dirtyState;
3334
this.lintsPerStep = lintsPerStep;
3435
}
@@ -67,6 +68,9 @@ public void removeSuppressedLints(Formatter formatter, String relativePath, List
6768
if (lintsPerStep == null) {
6869
return;
6970
}
71+
if (formatter.getSteps().size() != lintsPerStep.size()) {
72+
throw new IllegalStateException("LintState was created with a different formatter!");
73+
}
7074
for (int i = 0; i < lintsPerStep.size(); i++) {
7175
FormatterStep step = formatter.getSteps().get(i);
7276
List<Lint> lints = lintsPerStep.get(i);
@@ -83,6 +87,10 @@ public void removeSuppressedLints(Formatter formatter, String relativePath, List
8387
}
8488
if (lints.isEmpty()) {
8589
lintsPerStep.set(i, null);
90+
if (lintsPerStep.stream().allMatch(Objects::isNull)) {
91+
lintsPerStep = null;
92+
return;
93+
}
8694
}
8795
}
8896
}

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

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222

23-
import org.assertj.core.api.Assertions;
2423
import org.gradle.testkit.runner.BuildResult;
25-
import org.gradle.testkit.runner.TaskOutcome;
2624
import org.junit.jupiter.api.Test;
2725

28-
import com.diffplug.common.base.CharMatcher;
29-
import com.diffplug.common.base.Splitter;
30-
import com.diffplug.spotless.LineEnding;
26+
import com.diffplug.selfie.Selfie;
27+
import com.diffplug.selfie.StringSelfie;
3128

3229
/** Tests the desired behavior from https://github.com/diffplug/spotless/issues/46. */
3330
class ErrorShouldRethrowTest extends GradleIntegrationHarness {
@@ -41,7 +38,7 @@ private void writeBuild(String... toInsert) throws IOException {
4138
lines.add(" format 'misc', {");
4239
lines.add(" lineEndings 'UNIX'");
4340
lines.add(" target file('README.md')");
44-
lines.add(" custom 'no swearing', {");
41+
lines.add(" custom 'noSwearingStep', {");
4542
lines.add(" if (it.toLowerCase(Locale.ROOT).contains('fubar')) {");
4643
lines.add(" throw com.diffplug.spotless.Lint.atUndefinedLine('swearing', 'No swearing!').shortcut();");
4744
lines.add(" }");
@@ -56,7 +53,7 @@ void passesIfNoException() throws Exception {
5653
" } // format",
5754
"} // spotless");
5855
setFile("README.md").toContent("This code is fun.");
59-
runWithSuccess("> Task :spotlessMisc");
56+
expectSuccess();
6057
}
6158

6259
@Test
@@ -65,11 +62,15 @@ void anyExceptionShouldFail() throws Exception {
6562
" } // format",
6663
"} // spotless");
6764
setFile("README.md").toContent("This code is fubar.");
68-
runWithFailure(
69-
"> Task :spotlessMisc FAILED\n" +
70-
"Step 'no swearing' found problem in 'README.md':\n" +
71-
"LINE_UNDEFINED: (swearing) No swearing!\n" +
72-
"java.lang.Throwable: LINE_UNDEFINED: (swearing) No swearing!");
65+
expectFailureAndConsoleToBe().toBe("> Task :spotlessMisc",
66+
"> Task :spotlessMiscCheck FAILED",
67+
"",
68+
"FAILURE: Build failed with an exception.",
69+
"",
70+
"* What went wrong:",
71+
"Execution failed for task ':spotlessMiscCheck'.",
72+
"> There were 1 lint error(s), they must be fixed or suppressed.",
73+
" README.md:LINE_UNDEFINED noSwearingStep(swearing) No swearing!");
7374
}
7475

7576
@Test
@@ -79,17 +80,17 @@ void unlessEnforceCheckIsFalse() throws Exception {
7980
" enforceCheck false",
8081
"} // spotless");
8182
setFile("README.md").toContent("This code is fubar.");
82-
runWithSuccess("> Task :processResources NO-SOURCE");
83+
expectSuccess();
8384
}
8485

86+
@Test
8587
void unlessExemptedByStep() throws Exception {
8688
writeBuild(
8789
" ignoreErrorForStep 'no swearing'",
8890
" } // format",
8991
"} // spotless");
9092
setFile("README.md").toContent("This code is fubar.");
91-
runWithSuccess("> Task :spotlessMisc\n" +
92-
"Unable to apply step 'no swearing' to 'README.md'");
93+
expectSuccess();
9394
}
9495

9596
@Test
@@ -99,8 +100,7 @@ void unlessExemptedByPath() throws Exception {
99100
" } // format",
100101
"} // spotless");
101102
setFile("README.md").toContent("This code is fubar.");
102-
runWithSuccess("> Task :spotlessMisc\n" +
103-
"Unable to apply step 'no swearing' to 'README.md'");
103+
expectSuccess();
104104
}
105105

106106
@Test
@@ -111,33 +111,28 @@ void failsIfNeitherStepNorFileExempted() throws Exception {
111111
" } // format",
112112
"} // spotless");
113113
setFile("README.md").toContent("This code is fubar.");
114-
runWithFailure("> Task :spotlessMisc FAILED\n" +
115-
"Step 'no swearing' found problem in 'README.md':\n" +
116-
"LINE_UNDEFINED: (swearing) No swearing!\n" +
117-
"java.lang.Throwable: LINE_UNDEFINED: (swearing) No swearing!");
114+
expectFailureAndConsoleToBe().toBe("> Task :spotlessMisc",
115+
"> Task :spotlessMiscCheck FAILED",
116+
"",
117+
"FAILURE: Build failed with an exception.",
118+
"",
119+
"* What went wrong:",
120+
"Execution failed for task ':spotlessMiscCheck'.",
121+
"> There were 1 lint error(s), they must be fixed or suppressed.",
122+
" README.md:LINE_UNDEFINED noSwearingStep(swearing) No swearing!");
118123
}
119124

120-
private void runWithSuccess(String expectedToStartWith) throws Exception {
121-
BuildResult result = gradleRunner().withArguments("check").build();
122-
assertResultAndMessages(result, TaskOutcome.SUCCESS, expectedToStartWith);
125+
private void expectSuccess() throws Exception {
126+
gradleRunner().withArguments("check", "--stacktrace").build();
123127
}
124128

125-
private void runWithFailure(String expectedToStartWith) throws Exception {
129+
private StringSelfie expectFailureAndConsoleToBe() throws Exception {
126130
BuildResult result = gradleRunner().withArguments("check").buildAndFail();
127-
assertResultAndMessages(result, TaskOutcome.FAILED, expectedToStartWith);
128-
}
129-
130-
private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String expectedToStartWith) {
131131
String output = result.getOutput();
132132
int register = output.indexOf(":spotlessInternalRegisterDependencies");
133133
int firstNewlineAfterThat = output.indexOf('\n', register + 1);
134-
String useThisToMatch = output.substring(firstNewlineAfterThat);
135-
136-
int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith);
137-
List<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(useThisToMatch.trim()));
138-
String actualStart = String.join("\n", actualLines.subList(0, numNewlines + 1));
139-
Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith);
140-
Assertions.assertThat(outcomes(result, outcome).size() + outcomes(result, TaskOutcome.UP_TO_DATE).size() + outcomes(result, TaskOutcome.NO_SOURCE).size())
141-
.isEqualTo(outcomes(result).size());
134+
int firstTry = output.indexOf("\n* Try:");
135+
String useThisToMatch = output.substring(firstNewlineAfterThat, firstTry).trim();
136+
return Selfie.expectSelfie(useThisToMatch);
142137
}
143138
}

0 commit comments

Comments
 (0)