Skip to content

Commit

Permalink
Fixed a bug in custom rules that only check for errors and don't retu…
Browse files Browse the repository at this point in the history
…rn any fixes. Follow-on to #46.
  • Loading branch information
nedtwigg committed Oct 31, 2016
1 parent 112438f commit 503a24c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/com/diffplug/gradle/spotless/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,14 @@ String applySteps(String unix, File file) throws Error {
for (FormatterStep step : steps) {
try {
String formatted = step.format(unix, file);
// should already be unix-only, but
// some steps might misbehave
unix = LineEnding.toUnix(formatted);
if (formatted == null) {
// This probably means it was a step that only checks
// for errors and doesn't actually have any fixes.
// No exception was thrown so we can just continue.
} else {
// Should already be unix-only, but some steps might misbehave.
unix = LineEnding.toUnix(formatted);
}
} catch (Error e) {
logger.error("Step '" + step.getName() + "' found problem in '" + projectDirectory.relativize(file.toPath()) + "':\n" + e.getMessage());
throw e;
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,25 @@ public void noSwearing() throws Exception {
String actualStart = actualLines.subList(0, numNewlines + 1).stream().collect(Collectors.joining("\n"));
Assert.assertEquals(expectedToStartWith, actualStart);
}

@Test
public void noSwearingPassesIfNoSwears() throws Exception {
write("build.gradle",
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
"spotless {",
" format 'misc', {",
" lineEndings 'UNIX'",
" target file('README.md')",
" custom 'no swearing', {",
" if (it.toLowerCase().contains('fubar')) {",
" throw new AssertionError('No swearing!');",
" }",
" }",
" }",
"}");
write("README.md", "This code is fun.");
gradleRunner().withArguments("spotlessCheck").build();
}
}

0 comments on commit 503a24c

Please sign in to comment.