Skip to content

Commit

Permalink
Errors thrown by a FormatterStep are now rethrown to kill the build. …
Browse files Browse the repository at this point in the history
…First cut at #46.
  • Loading branch information
nedtwigg committed Oct 31, 2016
1 parent f88eb8b commit 3974032
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/diffplug/gradle/spotless/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrow.java
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}

0 comments on commit 3974032

Please sign in to comment.