1
1
/*
2
- * Copyright 2016-2024 DiffPlug
2
+ * Copyright 2016-2025 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 ;
18
21
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 ;
19
27
20
- import org .assertj .core .api .Assertions ;
21
28
import org .gradle .testkit .runner .BuildResult ;
29
+ import org .junit .jupiter .api .Test ;
22
30
import org .junit .jupiter .params .ParameterizedTest ;
23
31
import org .junit .jupiter .params .provider .ValueSource ;
24
32
@@ -51,7 +59,7 @@ void useInlineConfig(String prettierVersion) throws IOException {
51
59
"}" );
52
60
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
53
61
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
54
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
62
+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
55
63
switch (prettierVersion ) {
56
64
case PRETTIER_VERSION_2 :
57
65
assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile_prettier_2.clean" );
@@ -81,7 +89,7 @@ void verifyCleanSpotlessCheckWorks(String prettierVersion) throws IOException {
81
89
"}" );
82
90
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
83
91
final BuildResult spotlessCheckFailsGracefully = gradleRunner ().withArguments ("--stacktrace" , "spotlessCheck" ).buildAndFail ();
84
- Assertions . assertThat (spotlessCheckFailsGracefully .getOutput ()).contains ("> The following files had format violations:" );
92
+ assertThat (spotlessCheckFailsGracefully .getOutput ()).contains ("> The following files had format violations:" );
85
93
86
94
gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
87
95
gradleRunner ().withArguments ("--stacktrace" , "spotlessCheck" ).build ();
@@ -104,7 +112,7 @@ void useFileConfig(String prettierVersion) throws IOException {
104
112
"}" );
105
113
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
106
114
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
107
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
115
+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
108
116
switch (prettierVersion ) {
109
117
case PRETTIER_VERSION_2 :
110
118
assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile_prettier_2.clean" );
@@ -131,7 +139,7 @@ void chooseParserBasedOnFilename(String prettierVersion) throws IOException {
131
139
"}" );
132
140
setFile ("dirty.json" ).toResource ("npm/prettier/filename/dirty.json" );
133
141
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
134
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
142
+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
135
143
assertFile ("dirty.json" ).sameAsResource ("npm/prettier/filename/clean.json" );
136
144
}
137
145
@@ -169,7 +177,7 @@ void useJavaCommunityPlugin(String prettierVersion) throws IOException {
169
177
"}" );
170
178
setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
171
179
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
172
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
180
+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
173
181
assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
174
182
}
175
183
@@ -202,7 +210,7 @@ void useJavaCommunityPluginFileConfig(String prettierVersion) throws IOException
202
210
"}" );
203
211
setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
204
212
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
205
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
213
+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
206
214
assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
207
215
}
208
216
@@ -226,8 +234,8 @@ void suggestsMissingJavaCommunityPlugin(String prettierVersion) throws IOExcepti
226
234
"}" );
227
235
setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
228
236
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).buildAndFail ();
229
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("Could not infer a parser" );
230
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("prettier-plugin-java" );
237
+ assertThat (spotlessApply .getOutput ()).contains ("Could not infer a parser" );
238
+ assertThat (spotlessApply .getOutput ()).contains ("prettier-plugin-java" );
231
239
}
232
240
233
241
@ ParameterizedTest (name = "{index}: usePhpCommunityPlugin with prettier {0}" )
@@ -264,7 +272,7 @@ void usePhpCommunityPlugin(String prettierVersion) throws IOException {
264
272
"}" );
265
273
setFile ("php-example.php" ).toResource ("npm/prettier/plugins/php.dirty" );
266
274
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
267
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
275
+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
268
276
assertFile ("php-example.php" ).sameAsResource ("npm/prettier/plugins/php.clean" );
269
277
}
270
278
@@ -324,9 +332,9 @@ void usePhpAndJavaCommunityPlugin(String prettierVersion) throws IOException {
324
332
setFile ("php-example.php" ).toResource ("npm/prettier/plugins/php.dirty" );
325
333
setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
326
334
final BuildResult spotlessApply = gradleRunner ().forwardOutput ().withArguments ("--stacktrace" , "--info" , "spotlessApply" ).build ();
327
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
335
+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
328
336
final BuildResult spotlessApply2 = gradleRunner ().forwardOutput ().withArguments ("--stacktrace" , "--info" , "spotlessApply" ).build ();
329
- Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
337
+ assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
330
338
assertFile ("php-example.php" ).sameAsResource ("npm/prettier/plugins/php.clean" );
331
339
assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
332
340
}
@@ -355,7 +363,7 @@ void autodetectNpmrcFileConfig(String prettierVersion) throws IOException {
355
363
"}" );
356
364
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
357
365
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).buildAndFail ();
358
- Assertions . assertThat (spotlessApply .getOutput ()).containsPattern ("Running npm command.*npm install.* failed with exit code: 1" );
366
+ assertThat (spotlessApply .getOutput ()).containsPattern ("Running npm command.*npm install.* failed with exit code: 1" );
359
367
}
360
368
361
369
@ ParameterizedTest (name = "{index}: verifyCleanAndSpotlessWorks with prettier {0}" )
@@ -377,9 +385,9 @@ void verifyCleanAndSpotlessWorks(String prettierVersion) throws IOException {
377
385
"}" );
378
386
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
379
387
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
380
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
388
+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
381
389
final BuildResult spotlessApply2 = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
382
- Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
390
+ assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
383
391
}
384
392
385
393
@ ParameterizedTest (name = "{index}: verifyCleanAndSpotlessWithNpmInstallCacheWorks with prettier {0}" )
@@ -401,9 +409,9 @@ void verifyCleanAndSpotlessWithNpmInstallCacheWorks(String prettierVersion) thro
401
409
"}" );
402
410
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
403
411
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
404
- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
412
+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
405
413
final BuildResult spotlessApply2 = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
406
- Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
414
+ assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
407
415
}
408
416
409
417
@ ParameterizedTest (name = "{index}: autodetectNpmrcFileConfig with prettier {0}" )
@@ -430,6 +438,51 @@ void pickupNpmrcFileConfig(String prettierVersion) throws IOException {
430
438
"}" );
431
439
setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
432
440
final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).buildAndFail ();
433
- Assertions .assertThat (spotlessApply .getOutput ()).containsPattern ("Running npm command.*npm install.* failed with exit code: 1" );
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
+ }
434
487
}
435
488
}
0 commit comments