15
15
*/
16
16
package com .diffplug .gradle .spotless ;
17
17
18
+ import static org .assertj .core .api .Assertions .assertThat ;
19
+
18
20
import java .io .IOException ;
19
21
import java .util .ArrayList ;
20
22
import java .util .Arrays ;
21
23
import java .util .List ;
22
24
23
- import org .assertj .core .api .Assertions ;
24
25
import org .gradle .testkit .runner .BuildResult ;
25
26
import org .gradle .testkit .runner .TaskOutcome ;
26
27
import org .junit .Test ;
27
28
28
- import com .diffplug .common .base .CharMatcher ;
29
- import com .diffplug .common .base .Splitter ;
30
- import com .diffplug .common .base .StringPrinter ;
31
29
import com .diffplug .spotless .LineEnding ;
32
30
33
31
/** Tests the desired behavior from https://github.com/diffplug/spotless/issues/46. */
@@ -67,8 +65,8 @@ public void anyExceptionShouldFail() throws Exception {
67
65
"} // spotless" );
68
66
setFile ("README.md" ).toContent ("This code is fubar." );
69
67
runWithFailure (
70
- ":spotlessMiscStep 'no swearing' found problem in 'README.md': " ,
71
- "No swearing! " ,
68
+ ":spotlessMisc " ,
69
+ "Step 'no swearing' found problem in 'README.md' " ,
72
70
"java.lang.RuntimeException: No swearing!" );
73
71
}
74
72
@@ -79,7 +77,7 @@ public void unlessEnforceCheckIsFalse() throws Exception {
79
77
" enforceCheck false" ,
80
78
"} // spotless" );
81
79
setFile ("README.md" ).toContent ("This code is fubar." );
82
- runWithSuccess (":compileJava UP-TO-DATE " );
80
+ runWithSuccess (":compileJava NO-SOURCE " );
83
81
}
84
82
85
83
@ Test
@@ -113,28 +111,25 @@ public void failsIfNeitherStepNorFileExempted() throws Exception {
113
111
"} // spotless" );
114
112
setFile ("README.md" ).toContent ("This code is fubar." );
115
113
runWithFailure (
116
- ":spotlessMiscStep 'no swearing' found problem in 'README.md': " ,
117
- "No swearing! " ,
114
+ ":spotlessMisc " ,
115
+ "Step 'no swearing' found problem in 'README.md' " ,
118
116
"java.lang.RuntimeException: No swearing!" );
119
117
}
120
118
121
- private void runWithSuccess (String ... messages ) throws Exception {
119
+ private void runWithSuccess (String ... messagePartsInOrder ) throws Exception {
122
120
BuildResult result = gradleRunner ().withArguments ("check" ).build ();
123
- assertResultAndMessages (result , TaskOutcome .SUCCESS , messages );
121
+ assertResultAndOutputContainsMessages (result , TaskOutcome .SUCCESS , messagePartsInOrder );
124
122
}
125
123
126
- private void runWithFailure (String ... messages ) throws Exception {
124
+ private void runWithFailure (String ... messagePartsInOrder ) throws Exception {
127
125
BuildResult result = gradleRunner ().withArguments ("check" ).buildAndFail ();
128
- assertResultAndMessages (result , TaskOutcome .FAILED , messages );
126
+ assertResultAndOutputContainsMessages (result , TaskOutcome .FAILED , messagePartsInOrder );
129
127
}
130
128
131
- private void assertResultAndMessages (BuildResult result , TaskOutcome outcome , String ... messages ) {
132
- String expectedToStartWith = StringPrinter .buildStringFromLines (messages ).trim ();
133
- int numNewlines = CharMatcher .is ('\n' ).countIn (expectedToStartWith );
134
- List <String > actualLines = Splitter .on ('\n' ).splitToList (LineEnding .toUnix (result .getOutput ()));
135
- String actualStart = String .join ("\n " , actualLines .subList (0 , numNewlines + 1 ));
136
- Assertions .assertThat (actualStart ).isEqualTo (expectedToStartWith );
137
- Assertions .assertThat (result .tasks (outcome ).size () + result .tasks (TaskOutcome .UP_TO_DATE ).size ())
129
+ private void assertResultAndOutputContainsMessages (BuildResult result , TaskOutcome outcome , String ... messagePartsInOrder ) {
130
+ String actualOutput = LineEnding .toUnix (result .getOutput ());
131
+ assertThat (actualOutput ).containsSequence (messagePartsInOrder );
132
+ assertThat (result .tasks (outcome ).size () + result .tasks (TaskOutcome .UP_TO_DATE ).size () + result .tasks (TaskOutcome .NO_SOURCE ).size ())
138
133
.isEqualTo (result .getTasks ().size ());
139
134
}
140
135
}
0 commit comments