20
20
import java .util .Arrays ;
21
21
import java .util .List ;
22
22
23
- import org .assertj .core .api .Assertions ;
24
23
import org .gradle .testkit .runner .BuildResult ;
25
- import org .gradle .testkit .runner .TaskOutcome ;
26
24
import org .junit .jupiter .api .Test ;
27
25
28
- import com .diffplug .common .base .CharMatcher ;
29
- import com .diffplug .common .base .Splitter ;
30
- import com .diffplug .spotless .LineEnding ;
26
+ import com .diffplug .selfie .Selfie ;
27
+ import com .diffplug .selfie .StringSelfie ;
31
28
32
29
/** Tests the desired behavior from https://github.com/diffplug/spotless/issues/46. */
33
30
class ErrorShouldRethrowTest extends GradleIntegrationHarness {
@@ -41,7 +38,7 @@ private void writeBuild(String... toInsert) throws IOException {
41
38
lines .add (" format 'misc', {" );
42
39
lines .add (" lineEndings 'UNIX'" );
43
40
lines .add (" target file('README.md')" );
44
- lines .add (" custom 'no swearing ', {" );
41
+ lines .add (" custom 'noSwearingStep ', {" );
45
42
lines .add (" if (it.toLowerCase(Locale.ROOT).contains('fubar')) {" );
46
43
lines .add (" throw com.diffplug.spotless.Lint.atUndefinedLine('swearing', 'No swearing!').shortcut();" );
47
44
lines .add (" }" );
@@ -56,7 +53,7 @@ void passesIfNoException() throws Exception {
56
53
" } // format" ,
57
54
"} // spotless" );
58
55
setFile ("README.md" ).toContent ("This code is fun." );
59
- runWithSuccess ( "> Task :spotlessMisc" );
56
+ expectSuccess ( );
60
57
}
61
58
62
59
@ Test
@@ -65,11 +62,15 @@ void anyExceptionShouldFail() throws Exception {
65
62
" } // format" ,
66
63
"} // spotless" );
67
64
setFile ("README.md" ).toContent ("This code is fubar." );
68
- runWithFailure (
69
- "> Task :spotlessMisc FAILED\n " +
70
- "Step 'no swearing' found problem in 'README.md':\n " +
71
- "LINE_UNDEFINED: (swearing) No swearing!\n " +
72
- "java.lang.Throwable: LINE_UNDEFINED: (swearing) No swearing!" );
65
+ expectFailureAndConsoleToBe ().toBe ("> Task :spotlessMisc" ,
66
+ "> Task :spotlessMiscCheck FAILED" ,
67
+ "" ,
68
+ "FAILURE: Build failed with an exception." ,
69
+ "" ,
70
+ "* What went wrong:" ,
71
+ "Execution failed for task ':spotlessMiscCheck'." ,
72
+ "> There were 1 lint error(s), they must be fixed or suppressed." ,
73
+ " README.md:LINE_UNDEFINED noSwearingStep(swearing) No swearing!" );
73
74
}
74
75
75
76
@ Test
@@ -79,17 +80,17 @@ void unlessEnforceCheckIsFalse() throws Exception {
79
80
" enforceCheck false" ,
80
81
"} // spotless" );
81
82
setFile ("README.md" ).toContent ("This code is fubar." );
82
- runWithSuccess ( "> Task :processResources NO-SOURCE" );
83
+ expectSuccess ( );
83
84
}
84
85
86
+ @ Test
85
87
void unlessExemptedByStep () throws Exception {
86
88
writeBuild (
87
89
" ignoreErrorForStep 'no swearing'" ,
88
90
" } // format" ,
89
91
"} // spotless" );
90
92
setFile ("README.md" ).toContent ("This code is fubar." );
91
- runWithSuccess ("> Task :spotlessMisc\n " +
92
- "Unable to apply step 'no swearing' to 'README.md'" );
93
+ expectSuccess ();
93
94
}
94
95
95
96
@ Test
@@ -99,8 +100,7 @@ void unlessExemptedByPath() throws Exception {
99
100
" } // format" ,
100
101
"} // spotless" );
101
102
setFile ("README.md" ).toContent ("This code is fubar." );
102
- runWithSuccess ("> Task :spotlessMisc\n " +
103
- "Unable to apply step 'no swearing' to 'README.md'" );
103
+ expectSuccess ();
104
104
}
105
105
106
106
@ Test
@@ -111,33 +111,28 @@ void failsIfNeitherStepNorFileExempted() throws Exception {
111
111
" } // format" ,
112
112
"} // spotless" );
113
113
setFile ("README.md" ).toContent ("This code is fubar." );
114
- runWithFailure ("> Task :spotlessMisc FAILED\n " +
115
- "Step 'no swearing' found problem in 'README.md':\n " +
116
- "LINE_UNDEFINED: (swearing) No swearing!\n " +
117
- "java.lang.Throwable: LINE_UNDEFINED: (swearing) No swearing!" );
114
+ expectFailureAndConsoleToBe ().toBe ("> Task :spotlessMisc" ,
115
+ "> Task :spotlessMiscCheck FAILED" ,
116
+ "" ,
117
+ "FAILURE: Build failed with an exception." ,
118
+ "" ,
119
+ "* What went wrong:" ,
120
+ "Execution failed for task ':spotlessMiscCheck'." ,
121
+ "> There were 1 lint error(s), they must be fixed or suppressed." ,
122
+ " README.md:LINE_UNDEFINED noSwearingStep(swearing) No swearing!" );
118
123
}
119
124
120
- private void runWithSuccess (String expectedToStartWith ) throws Exception {
121
- BuildResult result = gradleRunner ().withArguments ("check" ).build ();
122
- assertResultAndMessages (result , TaskOutcome .SUCCESS , expectedToStartWith );
125
+ private void expectSuccess () throws Exception {
126
+ gradleRunner ().withArguments ("check" , "--stacktrace" ).build ();
123
127
}
124
128
125
- private void runWithFailure ( String expectedToStartWith ) throws Exception {
129
+ private StringSelfie expectFailureAndConsoleToBe ( ) throws Exception {
126
130
BuildResult result = gradleRunner ().withArguments ("check" ).buildAndFail ();
127
- assertResultAndMessages (result , TaskOutcome .FAILED , expectedToStartWith );
128
- }
129
-
130
- private void assertResultAndMessages (BuildResult result , TaskOutcome outcome , String expectedToStartWith ) {
131
131
String output = result .getOutput ();
132
132
int register = output .indexOf (":spotlessInternalRegisterDependencies" );
133
133
int firstNewlineAfterThat = output .indexOf ('\n' , register + 1 );
134
- String useThisToMatch = output .substring (firstNewlineAfterThat );
135
-
136
- int numNewlines = CharMatcher .is ('\n' ).countIn (expectedToStartWith );
137
- List <String > actualLines = Splitter .on ('\n' ).splitToList (LineEnding .toUnix (useThisToMatch .trim ()));
138
- String actualStart = String .join ("\n " , actualLines .subList (0 , numNewlines + 1 ));
139
- Assertions .assertThat (actualStart ).isEqualTo (expectedToStartWith );
140
- Assertions .assertThat (outcomes (result , outcome ).size () + outcomes (result , TaskOutcome .UP_TO_DATE ).size () + outcomes (result , TaskOutcome .NO_SOURCE ).size ())
141
- .isEqualTo (outcomes (result ).size ());
134
+ int firstTry = output .indexOf ("\n * Try:" );
135
+ String useThisToMatch = output .substring (firstNewlineAfterThat , firstTry ).trim ();
136
+ return Selfie .expectSelfie (useThisToMatch );
142
137
}
143
138
}
0 commit comments