Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ignoreErrorForStep, ignoreErrorForPath, and strict error-reporting in general. #96

Merged
merged 5 commits into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/main/java/com/diffplug/spotless/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public String computeLineEndings(String unix, File file) {
* The input must have unix line endings, and the output
* is guaranteed to also have unix line endings.
*/
public String compute(String unix, File file) throws Error {
public String compute(String unix, File file) {
for (FormatterStep step : steps) {
try {
String formatted = step.format(unix, file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public static List<File> check(File rootDir, File diagnoseDir, Formatter formatt
.encoding(formatter.getEncoding())
.rootDir(formatter.getRootDir())
.steps(Collections.singletonList(paddedCellStep))
.exceptionPolicy(formatter.getExceptionPolicy())
.build();

// empty out the diagnose folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public void eclipseFormatFile(Object eclipseFormatFile) {
eclipseFormatFile(EclipseFormatterStep.defaultVersion(), eclipseFormatFile);
}

public void eclipseFormatFile(String eclipseVersion, Object... eclipseFormatFiles) {
public void eclipseFormatFile(String eclipseVersion, Object eclipseFormatFile) {
Project project = getProject();
addStep(EclipseFormatterStep.create(eclipseVersion,
project.files(eclipseFormatFiles).getFiles(),
project.files(eclipseFormatFile).getFiles(),
GradleProvisioner.fromProject(project)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
.encoding(Charset.forName(encoding))
.rootDir(getProject().getProjectDir().toPath())
.steps(steps)
.exceptionPolicy(exceptionPolicy)
.build();
// find the outOfDate files
List<File> outOfDate = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.diffplug.gradle.spotless;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -30,55 +33,109 @@

/** Tests the desired behavior from https://github.com/diffplug/spotless/issues/46. */
public class ErrorShouldRethrow extends GradleIntegrationTest {
private void writeBuild(String... toInsert) throws IOException {
List<String> lines = new ArrayList<>();
lines.add("plugins {");
lines.add(" id 'com.diffplug.gradle.spotless'");
lines.add(" id 'java'");
lines.add("}");
lines.add("spotless {");
lines.add(" format 'misc', {");
lines.add(" lineEndings 'UNIX'");
lines.add(" target file('README.md')");
lines.add(" custom 'no swearing', {");
lines.add(" if (it.toLowerCase(Locale.ROOT).contains('fubar')) {");
lines.add(" throw new RuntimeException('No swearing!');");
lines.add(" }");
lines.add(" }");
lines.addAll(Arrays.asList(toInsert));
write("build.gradle", lines.toArray(new String[lines.size()]));
}

@Test
public void noSwearing() throws Exception {
write("build.gradle",
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
"spotless {",
" format 'misc', {",
" target file('README.md')",
" custom 'no swearing', {",
" if (it.toLowerCase(Locale.ROOT).contains('fubar')) {",
" throw new AssertionError('No swearing!');",
" }",
" }",
" }",
"}");
public void passesIfNoException() throws Exception {
writeBuild(
" } // format",
"} // spotless");
write("README.md", "This code is fun.");
runWithSuccess(":spotlessMisc");
}

@Test
public void anyExceptionShouldFail() throws Exception {
writeBuild(
" } // format",
"} // spotless");
write("README.md", "This code is fubar.");
BuildResult result = gradleRunner().withArguments("spotlessCheck").buildAndFail();
runWithFailure(
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
"No swearing!",
"java.lang.RuntimeException: No swearing!");
}

@Test
public void unlessEnforceCheckIsFalse() throws Exception {
writeBuild(
" } // format",
" enforceCheck false",
"} // spotless");
write("README.md", "This code is fubar.");
runWithSuccess(":compileJava UP-TO-DATE");
}

@Test
public void unlessExemptedByStep() throws Exception {
writeBuild(
" ignoreErrorForStep 'no swearing'",
" } // format",
"} // spotless");
write("README.md", "This code is fubar.");
runWithSuccess(":spotlessMisc",
"Unable to apply step 'no swearing' to 'README.md'");
}

String expectedToStartWith = StringPrinter.buildStringFromLines(
@Test
public void unlessExemptedByPath() throws Exception {
writeBuild(
" ignoreErrorForPath 'README.md'",
" } // format",
"} // spotless");
write("README.md", "This code is fubar.");
runWithSuccess(":spotlessMisc",
"Unable to apply step 'no swearing' to 'README.md'");
}

@Test
public void failsIfNeitherStepNorFileExempted() throws Exception {
writeBuild(
" ignoreErrorForStep 'nope'",
" ignoreErrorForPath 'nope'",
" } // format",
"} // spotless");
write("README.md", "This code is fubar.");
runWithFailure(
":spotlessMiscStep 'no swearing' found problem in 'README.md':",
"No swearing!",
"java.lang.AssertionError: No swearing!").trim();
"java.lang.RuntimeException: No swearing!");
}

private void runWithSuccess(String... messages) throws Exception {
BuildResult result = gradleRunner().withArguments("check").build();
assertResultAndMessages(result, TaskOutcome.SUCCESS, messages);
}

private void runWithFailure(String... messages) throws Exception {
BuildResult result = gradleRunner().withArguments("check").buildAndFail();
assertResultAndMessages(result, TaskOutcome.FAILED, messages);
}

private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String... messages) {
String expectedToStartWith = StringPrinter.buildStringFromLines(messages).trim();
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"));
Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith);
Assertions.assertThat(result.tasks(TaskOutcome.FAILED))
.isNotEmpty()
.hasSameSizeAs(result.getTasks());
}

@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(Locale.ROOT).contains('fubar')) {",
" throw new AssertionError('No swearing!');",
" }",
" }",
" }",
"}");
write("README.md", "This code is fun.");
Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size())
.isEqualTo(result.getTasks().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import org.junit.Test;

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

replace("build.gradle",
"googleJavaFormat('1.1')",
"googleJavaFormat('1.0')");
"googleJavaFormat('1.2')",
"googleJavaFormat('1.3')");
checkRunsThenUpToDate();
}
}
5 changes: 4 additions & 1 deletion spotlessSelf.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ plugins {
repositories { mavenCentral() }
spotless {
java {
target '**/*.java'
target fileTree('.') {
include '**/*.java'
exclude '_ext/*/build/**'
}

custom 'noInternalDeps', {
if (it.contains('import org.gradle.internal.')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,13 @@ public void invalidPropertyFiles() throws IOException {
}

@Test
public void nonExisitingFile() throws IOException {
public void nonExistingFile() throws IOException {
boolean exceptionCaught = false;
String filePath = "does/not/exist.properties";
boolean isWin = LineEnding.PLATFORM_NATIVE.str().equals(LineEnding.WINDOWS.str());
if (isWin) {
filePath = filePath.replace('/', '\\');
}
try {
FormatterProperties.from(new File(filePath));
} catch (IllegalArgumentException ex) {
Expand Down