|
| 1 | +/* |
| 2 | + * Copyright 2016 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.gradle.spotless; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.assertj.core.api.Assertions; |
| 24 | +import org.gradle.testkit.runner.BuildResult; |
| 25 | +import org.gradle.testkit.runner.TaskOutcome; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import com.diffplug.common.base.CharMatcher; |
| 29 | +import com.diffplug.common.base.Splitter; |
| 30 | +import com.diffplug.common.base.StringPrinter; |
| 31 | +import com.diffplug.spotless.JreVersion; |
| 32 | +import com.diffplug.spotless.LineEnding; |
| 33 | + |
| 34 | +/** Tests the desired behavior from https://github.com/diffplug/spotless/issues/46. */ |
| 35 | +public class ErrorShouldRethrowJre11 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 | + setFile("build.gradle").toContent(String.join("\n", lines)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void passesIfNoException() throws Exception { |
| 57 | + writeBuild( |
| 58 | + " } // format", |
| 59 | + "} // spotless"); |
| 60 | + setFile("README.md").toContent("This code is fun."); |
| 61 | + runWithSuccess("> Task :spotlessMisc"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void anyExceptionShouldFail() throws Exception { |
| 66 | + writeBuild( |
| 67 | + " } // format", |
| 68 | + "} // spotless"); |
| 69 | + setFile("README.md").toContent("This code is fubar."); |
| 70 | + runWithFailure( |
| 71 | + "> Task :spotlessMisc FAILED\n" + |
| 72 | + "Step 'no swearing' found problem in 'README.md':\n" + |
| 73 | + "No swearing!\n" + |
| 74 | + "java.lang.RuntimeException: No swearing!\n"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void unlessEnforceCheckIsFalse() throws Exception { |
| 79 | + writeBuild( |
| 80 | + " } // format", |
| 81 | + " enforceCheck false", |
| 82 | + "} // spotless"); |
| 83 | + setFile("README.md").toContent("This code is fubar."); |
| 84 | + runWithSuccess("> Task :compileJava NO-SOURCE"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void unlessExemptedByStep() throws Exception { |
| 89 | + writeBuild( |
| 90 | + " ignoreErrorForStep 'no swearing'", |
| 91 | + " } // format", |
| 92 | + "} // spotless"); |
| 93 | + setFile("README.md").toContent("This code is fubar."); |
| 94 | + runWithSuccess("> Task :spotlessMisc\n" + |
| 95 | + "Unable to apply step 'no swearing' to 'README.md'"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void unlessExemptedByPath() throws Exception { |
| 100 | + writeBuild( |
| 101 | + " ignoreErrorForPath 'README.md'", |
| 102 | + " } // format", |
| 103 | + "} // spotless"); |
| 104 | + setFile("README.md").toContent("This code is fubar."); |
| 105 | + runWithSuccess("> Task :spotlessMisc", |
| 106 | + "Unable to apply step 'no swearing' to 'README.md'"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void failsIfNeitherStepNorFileExempted() throws Exception { |
| 111 | + writeBuild( |
| 112 | + " ignoreErrorForStep 'nope'", |
| 113 | + " ignoreErrorForPath 'nope'", |
| 114 | + " } // format", |
| 115 | + "} // spotless"); |
| 116 | + setFile("README.md").toContent("This code is fubar."); |
| 117 | + runWithFailure("> Task :spotlessMisc FAILED\n" + |
| 118 | + "Step 'no swearing' found problem in 'README.md':\n" + |
| 119 | + "No swearing!\n" + |
| 120 | + "java.lang.RuntimeException: No swearing!\n"); |
| 121 | + } |
| 122 | + |
| 123 | + private void runWithSuccess(String... messages) throws Exception { |
| 124 | + if (JreVersion.thisVm() != JreVersion._11) { |
| 125 | + return; |
| 126 | + } |
| 127 | + BuildResult result = gradleRunner().withArguments("check").build(); |
| 128 | + assertResultAndMessages(result, TaskOutcome.SUCCESS, messages); |
| 129 | + } |
| 130 | + |
| 131 | + private void runWithFailure(String... messages) throws Exception { |
| 132 | + if (JreVersion.thisVm() != JreVersion._11) { |
| 133 | + return; |
| 134 | + } |
| 135 | + BuildResult result = gradleRunner().withArguments("check").buildAndFail(); |
| 136 | + assertResultAndMessages(result, TaskOutcome.FAILED, messages); |
| 137 | + } |
| 138 | + |
| 139 | + private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String... messages) { |
| 140 | + String expectedToStartWith = StringPrinter.buildStringFromLines(messages).trim(); |
| 141 | + int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith); |
| 142 | + List<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput().trim())); |
| 143 | + String actualStart = String.join("\n", actualLines.subList(0, numNewlines + 1)); |
| 144 | + Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith); |
| 145 | + Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size() + result.tasks(TaskOutcome.NO_SOURCE).size()) |
| 146 | + .isEqualTo(result.getTasks().size()); |
| 147 | + } |
| 148 | +} |
0 commit comments