11/*
2- * Copyright 2016-2024 DiffPlug
2+ * Copyright 2016-2025 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1515 */
1616package com .diffplug .gradle .spotless ;
1717
18+ import static org .assertj .core .api .Assertions .assertThat ;
19+
20+ import java .io .File ;
1821import 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 ;
1927
20- import org .assertj .core .api .Assertions ;
2128import org .gradle .testkit .runner .BuildResult ;
29+ import org .junit .jupiter .api .Test ;
2230import org .junit .jupiter .params .ParameterizedTest ;
2331import org .junit .jupiter .params .provider .ValueSource ;
2432
@@ -51,7 +59,7 @@ void useInlineConfig(String prettierVersion) throws IOException {
5159 "}" );
5260 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
5361 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
54- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
62+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
5563 switch (prettierVersion ) {
5664 case PRETTIER_VERSION_2 :
5765 assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile_prettier_2.clean" );
@@ -81,7 +89,7 @@ void verifyCleanSpotlessCheckWorks(String prettierVersion) throws IOException {
8189 "}" );
8290 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
8391 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:" );
8593
8694 gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
8795 gradleRunner ().withArguments ("--stacktrace" , "spotlessCheck" ).build ();
@@ -104,7 +112,7 @@ void useFileConfig(String prettierVersion) throws IOException {
104112 "}" );
105113 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
106114 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
107- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
115+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
108116 switch (prettierVersion ) {
109117 case PRETTIER_VERSION_2 :
110118 assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile_prettier_2.clean" );
@@ -131,7 +139,7 @@ void chooseParserBasedOnFilename(String prettierVersion) throws IOException {
131139 "}" );
132140 setFile ("dirty.json" ).toResource ("npm/prettier/filename/dirty.json" );
133141 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
134- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
142+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
135143 assertFile ("dirty.json" ).sameAsResource ("npm/prettier/filename/clean.json" );
136144 }
137145
@@ -169,7 +177,7 @@ void useJavaCommunityPlugin(String prettierVersion) throws IOException {
169177 "}" );
170178 setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
171179 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
172- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
180+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
173181 assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
174182 }
175183
@@ -202,7 +210,7 @@ void useJavaCommunityPluginFileConfig(String prettierVersion) throws IOException
202210 "}" );
203211 setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
204212 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
205- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
213+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
206214 assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
207215 }
208216
@@ -226,8 +234,8 @@ void suggestsMissingJavaCommunityPlugin(String prettierVersion) throws IOExcepti
226234 "}" );
227235 setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
228236 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" );
231239 }
232240
233241 @ ParameterizedTest (name = "{index}: usePhpCommunityPlugin with prettier {0}" )
@@ -264,7 +272,7 @@ void usePhpCommunityPlugin(String prettierVersion) throws IOException {
264272 "}" );
265273 setFile ("php-example.php" ).toResource ("npm/prettier/plugins/php.dirty" );
266274 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
267- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
275+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
268276 assertFile ("php-example.php" ).sameAsResource ("npm/prettier/plugins/php.clean" );
269277 }
270278
@@ -324,9 +332,9 @@ void usePhpAndJavaCommunityPlugin(String prettierVersion) throws IOException {
324332 setFile ("php-example.php" ).toResource ("npm/prettier/plugins/php.dirty" );
325333 setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
326334 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" );
328336 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" );
330338 assertFile ("php-example.php" ).sameAsResource ("npm/prettier/plugins/php.clean" );
331339 assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
332340 }
@@ -355,7 +363,7 @@ void autodetectNpmrcFileConfig(String prettierVersion) throws IOException {
355363 "}" );
356364 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
357365 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" );
359367 }
360368
361369 @ ParameterizedTest (name = "{index}: verifyCleanAndSpotlessWorks with prettier {0}" )
@@ -377,9 +385,9 @@ void verifyCleanAndSpotlessWorks(String prettierVersion) throws IOException {
377385 "}" );
378386 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
379387 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
380- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
388+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
381389 final BuildResult spotlessApply2 = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
382- Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
390+ assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
383391 }
384392
385393 @ ParameterizedTest (name = "{index}: verifyCleanAndSpotlessWithNpmInstallCacheWorks with prettier {0}" )
@@ -401,9 +409,9 @@ void verifyCleanAndSpotlessWithNpmInstallCacheWorks(String prettierVersion) thro
401409 "}" );
402410 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
403411 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
404- Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
412+ assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
405413 final BuildResult spotlessApply2 = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
406- Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
414+ assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
407415 }
408416
409417 @ ParameterizedTest (name = "{index}: autodetectNpmrcFileConfig with prettier {0}" )
@@ -430,6 +438,51 @@ void pickupNpmrcFileConfig(String prettierVersion) throws IOException {
430438 "}" );
431439 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
432440 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+ }
434487 }
435488}
0 commit comments