Skip to content

Commit b3a098f

Browse files
authored
Merge pull request #96 from nedtwigg/master
Fixed ignoreErrorForStep, ignoreErrorForPath, and strict error-reporting in general.
2 parents a8ab1c1 + ddefa18 commit b3a098f

File tree

8 files changed

+115
-52
lines changed

8 files changed

+115
-52
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public String computeLineEndings(String unix, File file) {
212212
* The input must have unix line endings, and the output
213213
* is guaranteed to also have unix line endings.
214214
*/
215-
public String compute(String unix, File file) throws Error {
215+
public String compute(String unix, File file) {
216216
for (FormatterStep step : steps) {
217217
try {
218218
String formatted = step.format(unix, file);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public static List<File> check(File rootDir, File diagnoseDir, Formatter formatt
118118
.encoding(formatter.getEncoding())
119119
.rootDir(formatter.getRootDir())
120120
.steps(Collections.singletonList(paddedCellStep))
121+
.exceptionPolicy(formatter.getExceptionPolicy())
121122
.build();
122123

123124
// empty out the diagnose folder

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ public void eclipseFormatFile(Object eclipseFormatFile) {
6161
eclipseFormatFile(EclipseFormatterStep.defaultVersion(), eclipseFormatFile);
6262
}
6363

64-
public void eclipseFormatFile(String eclipseVersion, Object... eclipseFormatFiles) {
64+
public void eclipseFormatFile(String eclipseVersion, Object eclipseFormatFile) {
6565
Project project = getProject();
6666
addStep(EclipseFormatterStep.create(eclipseVersion,
67-
project.files(eclipseFormatFiles).getFiles(),
67+
project.files(eclipseFormatFile).getFiles(),
6868
GradleProvisioner.fromProject(project)));
6969
}
7070

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
152152
.encoding(Charset.forName(encoding))
153153
.rootDir(getProject().getProjectDir().toPath())
154154
.steps(steps)
155+
.exceptionPolicy(exceptionPolicy)
155156
.build();
156157
// find the outOfDate files
157158
List<File> outOfDate = new ArrayList<>();

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

Lines changed: 98 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import java.io.IOException;
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
1821
import java.util.List;
1922
import java.util.stream.Collectors;
2023

@@ -30,55 +33,109 @@
3033

3134
/** Tests the desired behavior from https://github.com/diffplug/spotless/issues/46. */
3235
public class ErrorShouldRethrow extends GradleIntegrationTest {
36+
private void writeBuild(String... toInsert) throws IOException {
37+
List<String> lines = new ArrayList<>();
38+
lines.add("plugins {");
39+
lines.add(" id 'com.diffplug.gradle.spotless'");
40+
lines.add(" id 'java'");
41+
lines.add("}");
42+
lines.add("spotless {");
43+
lines.add(" format 'misc', {");
44+
lines.add(" lineEndings 'UNIX'");
45+
lines.add(" target file('README.md')");
46+
lines.add(" custom 'no swearing', {");
47+
lines.add(" if (it.toLowerCase(Locale.ROOT).contains('fubar')) {");
48+
lines.add(" throw new RuntimeException('No swearing!');");
49+
lines.add(" }");
50+
lines.add(" }");
51+
lines.addAll(Arrays.asList(toInsert));
52+
write("build.gradle", lines.toArray(new String[lines.size()]));
53+
}
54+
3355
@Test
34-
public void noSwearing() throws Exception {
35-
write("build.gradle",
36-
"plugins {",
37-
" id 'com.diffplug.gradle.spotless'",
38-
"}",
39-
"spotless {",
40-
" format 'misc', {",
41-
" target file('README.md')",
42-
" custom 'no swearing', {",
43-
" if (it.toLowerCase(Locale.ROOT).contains('fubar')) {",
44-
" throw new AssertionError('No swearing!');",
45-
" }",
46-
" }",
47-
" }",
48-
"}");
56+
public void passesIfNoException() throws Exception {
57+
writeBuild(
58+
" } // format",
59+
"} // spotless");
60+
write("README.md", "This code is fun.");
61+
runWithSuccess(":spotlessMisc");
62+
}
63+
64+
@Test
65+
public void anyExceptionShouldFail() throws Exception {
66+
writeBuild(
67+
" } // format",
68+
"} // spotless");
4969
write("README.md", "This code is fubar.");
50-
BuildResult result = gradleRunner().withArguments("spotlessCheck").buildAndFail();
70+
runWithFailure(
71+
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
72+
"No swearing!",
73+
"java.lang.RuntimeException: No swearing!");
74+
}
75+
76+
@Test
77+
public void unlessEnforceCheckIsFalse() throws Exception {
78+
writeBuild(
79+
" } // format",
80+
" enforceCheck false",
81+
"} // spotless");
82+
write("README.md", "This code is fubar.");
83+
runWithSuccess(":compileJava UP-TO-DATE");
84+
}
85+
86+
@Test
87+
public void unlessExemptedByStep() throws Exception {
88+
writeBuild(
89+
" ignoreErrorForStep 'no swearing'",
90+
" } // format",
91+
"} // spotless");
92+
write("README.md", "This code is fubar.");
93+
runWithSuccess(":spotlessMisc",
94+
"Unable to apply step 'no swearing' to 'README.md'");
95+
}
5196

52-
String expectedToStartWith = StringPrinter.buildStringFromLines(
97+
@Test
98+
public void unlessExemptedByPath() throws Exception {
99+
writeBuild(
100+
" ignoreErrorForPath 'README.md'",
101+
" } // format",
102+
"} // spotless");
103+
write("README.md", "This code is fubar.");
104+
runWithSuccess(":spotlessMisc",
105+
"Unable to apply step 'no swearing' to 'README.md'");
106+
}
107+
108+
@Test
109+
public void failsIfNeitherStepNorFileExempted() throws Exception {
110+
writeBuild(
111+
" ignoreErrorForStep 'nope'",
112+
" ignoreErrorForPath 'nope'",
113+
" } // format",
114+
"} // spotless");
115+
write("README.md", "This code is fubar.");
116+
runWithFailure(
53117
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
54118
"No swearing!",
55-
"java.lang.AssertionError: No swearing!").trim();
119+
"java.lang.RuntimeException: No swearing!");
120+
}
121+
122+
private void runWithSuccess(String... messages) throws Exception {
123+
BuildResult result = gradleRunner().withArguments("check").build();
124+
assertResultAndMessages(result, TaskOutcome.SUCCESS, messages);
125+
}
126+
127+
private void runWithFailure(String... messages) throws Exception {
128+
BuildResult result = gradleRunner().withArguments("check").buildAndFail();
129+
assertResultAndMessages(result, TaskOutcome.FAILED, messages);
130+
}
131+
132+
private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String... messages) {
133+
String expectedToStartWith = StringPrinter.buildStringFromLines(messages).trim();
56134
int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith);
57135
List<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput()));
58136
String actualStart = actualLines.subList(0, numNewlines + 1).stream().collect(Collectors.joining("\n"));
59137
Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith);
60-
Assertions.assertThat(result.tasks(TaskOutcome.FAILED))
61-
.isNotEmpty()
62-
.hasSameSizeAs(result.getTasks());
63-
}
64-
65-
@Test
66-
public void noSwearingPassesIfNoSwears() throws Exception {
67-
write("build.gradle",
68-
"plugins {",
69-
" id 'com.diffplug.gradle.spotless'",
70-
"}",
71-
"spotless {",
72-
" format 'misc', {",
73-
" lineEndings 'UNIX'",
74-
" target file('README.md')",
75-
" custom 'no swearing', {",
76-
" if (it.toLowerCase(Locale.ROOT).contains('fubar')) {",
77-
" throw new AssertionError('No swearing!');",
78-
" }",
79-
" }",
80-
" }",
81-
"}");
82-
write("README.md", "This code is fun.");
138+
Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size())
139+
.isEqualTo(result.getTasks().size());
83140
}
84141
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
import org.junit.Test;
2222

2323
public class GoogleJavaFormatIntegrationTest extends GradleIntegrationTest {
24-
// TODO: This test throws an exception because google-java-format-1.0 doesn't have
25-
// RemoveUnusedImports.java (only 1.1+ does), but despite that, the test passes!
26-
// Discover why this test passes and/or fix it or remove it.
2724
@Test
2825
public void integration() throws IOException {
2926
write("build.gradle",
@@ -35,7 +32,7 @@ public void integration() throws IOException {
3532
"spotless {",
3633
" java {",
3734
" target file('test.java')",
38-
" googleJavaFormat('1.1')",
35+
" googleJavaFormat('1.2')",
3936
" }",
4037
"}");
4138
String input = getTestResource("java/googlejavaformat/JavaCodeUnformatted.test");
@@ -49,8 +46,8 @@ public void integration() throws IOException {
4946
checkRunsThenUpToDate();
5047

5148
replace("build.gradle",
52-
"googleJavaFormat('1.1')",
53-
"googleJavaFormat('1.0')");
49+
"googleJavaFormat('1.2')",
50+
"googleJavaFormat('1.3')");
5451
checkRunsThenUpToDate();
5552
}
5653
}

spotlessSelf.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ plugins {
77
repositories { mavenCentral() }
88
spotless {
99
java {
10-
target '**/*.java'
10+
target fileTree('.') {
11+
include '**/*.java'
12+
exclude '_ext/*/build/**'
13+
}
1114

1215
custom 'noInternalDeps', {
1316
if (it.contains('import org.gradle.internal.')) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,13 @@ public void invalidPropertyFiles() throws IOException {
103103
}
104104

105105
@Test
106-
public void nonExisitingFile() throws IOException {
106+
public void nonExistingFile() throws IOException {
107107
boolean exceptionCaught = false;
108108
String filePath = "does/not/exist.properties";
109+
boolean isWin = LineEnding.PLATFORM_NATIVE.str().equals(LineEnding.WINDOWS.str());
110+
if (isWin) {
111+
filePath = filePath.replace('/', '\\');
112+
}
109113
try {
110114
FormatterProperties.from(new File(filePath));
111115
} catch (IllegalArgumentException ex) {

0 commit comments

Comments
 (0)