diff --git a/CHANGES.md b/CHANGES.md index 152ae699fe..b0397e665f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,9 @@ ### Version 2.4.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/gradle/spotless/spotless/)) +* If a formatter step throws an `Error` or any of its subclasses, such as the `AssertionError`s thrown by JUnit, AssertJ, etc. that error will kill the build. + + See [#46](https://github.com/diffplug/spotless/issues/46) for the full details. + ### Version 2.3.0 - October 27th 2016 ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [jcenter](https://bintray.com/diffplug/opensource/spotless/2.3.0/view)) * When `spotlessCheck` fails, the error message now contains a short diff of what is neccessary to fix the issue ([#10](https://github.com/diffplug/spotless/issues/10), thanks to Jonathan Bluett-Duncan). diff --git a/src/main/java/com/diffplug/gradle/spotless/Formatter.java b/src/main/java/com/diffplug/gradle/spotless/Formatter.java index 43f728b035..62990b9e65 100644 --- a/src/main/java/com/diffplug/gradle/spotless/Formatter.java +++ b/src/main/java/com/diffplug/gradle/spotless/Formatter.java @@ -109,15 +109,18 @@ String applyLineEndings(String unix, File file) { * The input must have unix line endings, and the output * is guaranteed to also have unix line endings. */ - String applySteps(String unix, File file) { + 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); + } catch (Error e) { + logger.error("Step '" + step.getName() + "' found problem in '" + projectDirectory.relativize(file.toPath()) + "':\n" + e.getMessage()); + throw e; } catch (Throwable e) { - logger.warn("Unable to apply step " + step.getName() + " to " + projectDirectory.relativize(file.toPath()) + ": " + e.getMessage()); + logger.warn("Unable to apply step '" + step.getName() + "' to '" + projectDirectory.relativize(file.toPath()) + "': " + e.getMessage()); logger.info("Exception is ", e); } } diff --git a/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrow.java b/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrow.java new file mode 100644 index 0000000000..610db56bb8 --- /dev/null +++ b/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrow.java @@ -0,0 +1,69 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.gradle.spotless; + +import java.io.File; +import java.util.List; +import java.util.stream.Collectors; + +import org.gradle.testkit.runner.BuildResult; +import org.junit.Assert; +import org.junit.Test; + +import com.diffplug.common.base.CharMatcher; +import com.diffplug.common.base.Splitter; +import com.diffplug.common.base.StringPrinter; + +/** Tests the desired behavior from https://github.com/diffplug/spotless/issues/46. */ +public class ErrorShouldRethrow extends GradleIntegrationTest { + @Test + public void noSwearing() throws Exception { + File build = write("build.gradle", + "plugins {", + " id 'com.diffplug.gradle.spotless'", + "}", + "spotless {", + " format 'misc', {", + " target file('README.md')", + " custom 'no swearing', {", + " if (it.toLowerCase().contains('fubar')) {", + " throw new AssertionError('No swearing!');", + " }", + " }", + " }", + "}"); + write("README.md", "This code is fubar."); + BuildResult result = gradleRunner().withArguments("spotlessCheck").buildAndFail(); + + String expectedToStartWith = StringPrinter.buildStringFromLines( + ":spotlessMiscCheckStep 'no swearing' found problem in 'README.md':", + "No swearing!", + " FAILED", + "", + "FAILURE: Build failed with an exception.", + "", + "* Where:", + "Build file '" + build + "' line: 9", + "", + "* What went wrong:", + "Execution failed for task ':spotlessMiscCheck'.", + "> No swearing!"); + int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith); + List actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput())); + String actualStart = actualLines.subList(0, numNewlines + 1).stream().collect(Collectors.joining("\n")); + Assert.assertEquals(expectedToStartWith, actualStart); + } +}