|
15 | 15 | */
|
16 | 16 | package com.diffplug.gradle.spotless;
|
17 | 17 |
|
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
18 | 21 | import java.util.List;
|
19 | 22 | import java.util.stream.Collectors;
|
20 | 23 |
|
|
30 | 33 |
|
31 | 34 | /** Tests the desired behavior from https://github.com/diffplug/spotless/issues/46. */
|
32 | 35 | public class ErrorShouldRethrow 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 | + write("build.gradle", lines.toArray(new String[lines.size()])); |
| 53 | + } |
| 54 | + |
33 | 55 | @Test
|
34 |
| - public void noSwearing() throws Exception { |
35 |
| - write("build.gradle", |
36 |
| - "plugins {", |
37 |
| - " id 'com.diffplug.gradle.spotless'", |
38 |
| - "}", |
39 |
| - "spotless {", |
40 |
| - " format 'misc', {", |
41 |
| - " target file('README.md')", |
42 |
| - " custom 'no swearing', {", |
43 |
| - " if (it.toLowerCase(Locale.ROOT).contains('fubar')) {", |
44 |
| - " throw new AssertionError('No swearing!');", |
45 |
| - " }", |
46 |
| - " }", |
47 |
| - " }", |
48 |
| - "}"); |
| 56 | + public void passesIfNoException() throws Exception { |
| 57 | + writeBuild( |
| 58 | + " } // format", |
| 59 | + "} // spotless"); |
| 60 | + write("README.md", "This code is fun."); |
| 61 | + runWithSuccess(":spotlessMisc"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void anyExceptionShouldFail() throws Exception { |
| 66 | + writeBuild( |
| 67 | + " } // format", |
| 68 | + "} // spotless"); |
49 | 69 | write("README.md", "This code is fubar.");
|
50 |
| - BuildResult result = gradleRunner().withArguments("spotlessCheck").buildAndFail(); |
| 70 | + runWithFailure( |
| 71 | + ":spotlessMiscStep 'no swearing' found problem in 'README.md':", |
| 72 | + "No swearing!", |
| 73 | + "java.lang.RuntimeException: No swearing!"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void unlessEnforceCheckIsFalse() throws Exception { |
| 78 | + writeBuild( |
| 79 | + " } // format", |
| 80 | + " enforceCheck false", |
| 81 | + "} // spotless"); |
| 82 | + write("README.md", "This code is fubar."); |
| 83 | + runWithSuccess(":compileJava UP-TO-DATE"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void unlessExemptedByStep() throws Exception { |
| 88 | + writeBuild( |
| 89 | + " ignoreErrorForStep 'no swearing'", |
| 90 | + " } // format", |
| 91 | + "} // spotless"); |
| 92 | + write("README.md", "This code is fubar."); |
| 93 | + runWithSuccess(":spotlessMisc", |
| 94 | + "Unable to apply step 'no swearing' to 'README.md'"); |
| 95 | + } |
51 | 96 |
|
52 |
| - String expectedToStartWith = StringPrinter.buildStringFromLines( |
| 97 | + @Test |
| 98 | + public void unlessExemptedByPath() throws Exception { |
| 99 | + writeBuild( |
| 100 | + " ignoreErrorForPath 'README.md'", |
| 101 | + " } // format", |
| 102 | + "} // spotless"); |
| 103 | + write("README.md", "This code is fubar."); |
| 104 | + runWithSuccess(":spotlessMisc", |
| 105 | + "Unable to apply step 'no swearing' to 'README.md'"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void failsIfNeitherStepNorFileExempted() throws Exception { |
| 110 | + writeBuild( |
| 111 | + " ignoreErrorForStep 'nope'", |
| 112 | + " ignoreErrorForPath 'nope'", |
| 113 | + " } // format", |
| 114 | + "} // spotless"); |
| 115 | + write("README.md", "This code is fubar."); |
| 116 | + runWithFailure( |
53 | 117 | ":spotlessMiscStep 'no swearing' found problem in 'README.md':",
|
54 | 118 | "No swearing!",
|
55 |
| - "java.lang.AssertionError: No swearing!").trim(); |
| 119 | + "java.lang.RuntimeException: No swearing!"); |
| 120 | + } |
| 121 | + |
| 122 | + private void runWithSuccess(String... messages) throws Exception { |
| 123 | + BuildResult result = gradleRunner().withArguments("check").build(); |
| 124 | + assertResultAndMessages(result, TaskOutcome.SUCCESS, messages); |
| 125 | + } |
| 126 | + |
| 127 | + private void runWithFailure(String... messages) throws Exception { |
| 128 | + BuildResult result = gradleRunner().withArguments("check").buildAndFail(); |
| 129 | + assertResultAndMessages(result, TaskOutcome.FAILED, messages); |
| 130 | + } |
| 131 | + |
| 132 | + private void assertResultAndMessages(BuildResult result, TaskOutcome outcome, String... messages) { |
| 133 | + String expectedToStartWith = StringPrinter.buildStringFromLines(messages).trim(); |
56 | 134 | int numNewlines = CharMatcher.is('\n').countIn(expectedToStartWith);
|
57 | 135 | List<String> actualLines = Splitter.on('\n').splitToList(LineEnding.toUnix(result.getOutput()));
|
58 | 136 | String actualStart = actualLines.subList(0, numNewlines + 1).stream().collect(Collectors.joining("\n"));
|
59 | 137 | Assertions.assertThat(actualStart).isEqualTo(expectedToStartWith);
|
60 |
| - Assertions.assertThat(result.tasks(TaskOutcome.FAILED)) |
61 |
| - .isNotEmpty() |
62 |
| - .hasSameSizeAs(result.getTasks()); |
63 |
| - } |
64 |
| - |
65 |
| - @Test |
66 |
| - public void noSwearingPassesIfNoSwears() throws Exception { |
67 |
| - write("build.gradle", |
68 |
| - "plugins {", |
69 |
| - " id 'com.diffplug.gradle.spotless'", |
70 |
| - "}", |
71 |
| - "spotless {", |
72 |
| - " format 'misc', {", |
73 |
| - " lineEndings 'UNIX'", |
74 |
| - " target file('README.md')", |
75 |
| - " custom 'no swearing', {", |
76 |
| - " if (it.toLowerCase(Locale.ROOT).contains('fubar')) {", |
77 |
| - " throw new AssertionError('No swearing!');", |
78 |
| - " }", |
79 |
| - " }", |
80 |
| - " }", |
81 |
| - "}"); |
82 |
| - write("README.md", "This code is fun."); |
| 138 | + Assertions.assertThat(result.tasks(outcome).size() + result.tasks(TaskOutcome.UP_TO_DATE).size()) |
| 139 | + .isEqualTo(result.getTasks().size()); |
83 | 140 | }
|
84 | 141 | }
|
0 commit comments