1
1
/*
2
- * Copyright 2016-2025 DiffPlug
2
+ * Copyright 2016-2024 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package com .diffplug .gradle .spotless ;
17
17
18
- import static org .assertj .core .api .Assertions .assertThat ;
19
-
20
- import java .io .File ;
21
18
import java .io .IOException ;
22
- import java .nio .file .Files ;
23
- import java .nio .file .Path ;
24
- import java .util .List ;
25
- import java .util .stream .Collectors ;
26
- import java .util .stream .Stream ;
27
19
20
+ import org .assertj .core .api .Assertions ;
28
21
import org .gradle .testkit .runner .BuildResult ;
29
- import org .junit .jupiter .api .Test ;
30
22
import org .junit .jupiter .params .ParameterizedTest ;
31
23
import org .junit .jupiter .params .provider .ValueSource ;
32
24
@@ -59,7 +51,7 @@ void useInlineConfig(String prettierVersion) throws IOException {
59
51
"}" );
60
52
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
61
53
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
62
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
54
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
63
55
switch (prettierVersion ) {
64
56
case PRETTIER_VERSION_2 :
65
57
assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile_prettier_2.clean" );
@@ -89,7 +81,7 @@ void verifyCleanSpotlessCheckWorks(String prettierVersion) throws IOException {
89
81
"}" );
90
82
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
91
83
final BuildResult spotlessCheckFailsGracefully = gradleRunner ().withArguments ("--stacktrace" , "spotlessCheck" ).buildAndFail ();
92
- assertThat (spotlessCheckFailsGracefully .getOutput ()).contains ("> The following files had format violations:" );
84
+ Assertions . assertThat (spotlessCheckFailsGracefully .getOutput ()).contains ("> The following files had format violations:" );
93
85
94
86
gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
95
87
gradleRunner ().withArguments ("--stacktrace" , "spotlessCheck" ).build ();
@@ -112,7 +104,7 @@ void useFileConfig(String prettierVersion) throws IOException {
112
104
"}" );
113
105
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
114
106
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
115
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
107
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
116
108
switch (prettierVersion ) {
117
109
case PRETTIER_VERSION_2 :
118
110
assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile_prettier_2.clean" );
@@ -139,7 +131,7 @@ void chooseParserBasedOnFilename(String prettierVersion) throws IOException {
139
131
"}" );
140
132
setFile ("dirty.json" ).toResource ("npm/prettier/filename/dirty.json" );
141
133
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
142
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
134
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
143
135
assertFile ("dirty.json" ).sameAsResource ("npm/prettier/filename/clean.json" );
144
136
}
145
137
@@ -177,7 +169,7 @@ void useJavaCommunityPlugin(String prettierVersion) throws IOException {
177
169
"}" );
178
170
setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
179
171
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
180
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
172
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
181
173
assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
182
174
}
183
175
@@ -210,7 +202,7 @@ void useJavaCommunityPluginFileConfig(String prettierVersion) throws IOException
210
202
"}" );
211
203
setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
212
204
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
213
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
205
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
214
206
assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
215
207
}
216
208
@@ -234,8 +226,8 @@ void suggestsMissingJavaCommunityPlugin(String prettierVersion) throws IOExcepti
234
226
"}" );
235
227
setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
236
228
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).buildAndFail ();
237
- assertThat (spotlessApply .getOutput ()).contains ("Could not infer a parser" );
238
- assertThat (spotlessApply .getOutput ()).contains ("prettier-plugin-java" );
229
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("Could not infer a parser" );
230
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("prettier-plugin-java" );
239
231
}
240
232
241
233
@ ParameterizedTest (name = "{index}: usePhpCommunityPlugin with prettier {0}" )
@@ -272,7 +264,7 @@ void usePhpCommunityPlugin(String prettierVersion) throws IOException {
272
264
"}" );
273
265
setFile ("php-example.php" ).toResource ("npm/prettier/plugins/php.dirty" );
274
266
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
275
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
267
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
276
268
assertFile ("php-example.php" ).sameAsResource ("npm/prettier/plugins/php.clean" );
277
269
}
278
270
@@ -332,9 +324,9 @@ void usePhpAndJavaCommunityPlugin(String prettierVersion) throws IOException {
332
324
setFile ("php-example.php" ).toResource ("npm/prettier/plugins/php.dirty" );
333
325
setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
334
326
final BuildResult spotlessApply = gradleRunner ().forwardOutput ().withArguments ("--stacktrace" , "--info" , "spotlessApply" ).build ();
335
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
327
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
336
328
final BuildResult spotlessApply2 = gradleRunner ().forwardOutput ().withArguments ("--stacktrace" , "--info" , "spotlessApply" ).build ();
337
- assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
329
+ Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
338
330
assertFile ("php-example.php" ).sameAsResource ("npm/prettier/plugins/php.clean" );
339
331
assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
340
332
}
@@ -363,7 +355,7 @@ void autodetectNpmrcFileConfig(String prettierVersion) throws IOException {
363
355
"}" );
364
356
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
365
357
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).buildAndFail ();
366
- assertThat (spotlessApply .getOutput ()).containsPattern ("Running npm command.*npm install.* failed with exit code: 1" );
358
+ Assertions . assertThat (spotlessApply .getOutput ()).containsPattern ("Running npm command.*npm install.* failed with exit code: 1" );
367
359
}
368
360
369
361
@ ParameterizedTest (name = "{index}: verifyCleanAndSpotlessWorks with prettier {0}" )
@@ -385,9 +377,9 @@ void verifyCleanAndSpotlessWorks(String prettierVersion) throws IOException {
385
377
"}" );
386
378
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
387
379
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
388
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
380
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
389
381
final BuildResult spotlessApply2 = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
390
- assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
382
+ Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
391
383
}
392
384
393
385
@ ParameterizedTest (name = "{index}: verifyCleanAndSpotlessWithNpmInstallCacheWorks with prettier {0}" )
@@ -409,9 +401,9 @@ void verifyCleanAndSpotlessWithNpmInstallCacheWorks(String prettierVersion) thro
409
401
"}" );
410
402
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
411
403
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
412
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
404
+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
413
405
final BuildResult spotlessApply2 = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
414
- assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
406
+ Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
415
407
}
416
408
417
409
@ ParameterizedTest (name = "{index}: autodetectNpmrcFileConfig with prettier {0}" )
@@ -438,51 +430,6 @@ void pickupNpmrcFileConfig(String prettierVersion) throws IOException {
438
430
"}" );
439
431
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
440
432
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).buildAndFail ();
441
- assertThat (spotlessApply .getOutput ()).containsPattern ("Running npm command.*npm install.* failed with exit code: 1" );
442
- }
443
-
444
- @ Test
445
- void multiplePrettierSetupsDoNotIntersectOnNpmDir () throws IOException {
446
- setFile ("build.gradle" ).toLines (
447
- "plugins {" ,
448
- " id 'com.diffplug.spotless'" ,
449
- "}" ,
450
- "repositories { mavenCentral() }" ,
451
- "def prettierConfig = [:]" ,
452
- "prettierConfig['printWidth'] = 120" ,
453
- "spotless {" ,
454
- " format 'mytypescript', {" ,
455
- " target 'test.ts'" ,
456
- " prettier().config(prettierConfig)" ,
457
- " }" ,
458
- " format 'json', {" ,
459
- " target 'test.json'" ,
460
- " prettier().config(prettierConfig)" ,
461
- " }" ,
462
- " javascript {" ,
463
- " target 'test.js'" ,
464
- " prettier().config(prettierConfig)" ,
465
- " }" ,
466
- "}" );
467
-
468
- setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
469
- setFile ("test.json" ).toResource ("npm/prettier/filetypes/json/json.dirty" );
470
- setFile ("test.js" ).toResource ("npm/prettier/filetypes/javascript-es5/javascript-es5.dirty" );
471
-
472
- final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
473
- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
474
-
475
- File buildFolder = new File (rootFolder (), "build" );
476
- assertThat (buildFolder ).isNotEmptyDirectory ();
477
-
478
- // verify it contains 3 folders containing "spotless-prettier" in it (recursively) - one for each format
479
- try (Stream <Path > pathStream = Files .walk (buildFolder .toPath ())) {
480
- List <Path > nodeModulesDirs = pathStream
481
- .sorted ()
482
- .filter (Files ::isDirectory )
483
- .filter (path -> path .getFileName ().toString ().contains ("spotless-prettier" ))
484
- .collect (Collectors .toList ());
485
- assertThat (nodeModulesDirs ).hasSize (3 );
486
- }
433
+ Assertions .assertThat (spotlessApply .getOutput ()).containsPattern ("Running npm command.*npm install.* failed with exit code: 1" );
487
434
}
488
435
}
0 commit comments