11/*
2- * Copyright 2016-2025 DiffPlug
2+ * Copyright 2016-2024 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 ;
2118import 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 ;
2719
20+ import org .assertj .core .api .Assertions ;
2821import org .gradle .testkit .runner .BuildResult ;
29- import org .junit .jupiter .api .Test ;
3022import org .junit .jupiter .params .ParameterizedTest ;
3123import org .junit .jupiter .params .provider .ValueSource ;
3224
@@ -59,7 +51,7 @@ void useInlineConfig(String prettierVersion) throws IOException {
5951 "}" );
6052 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
6153 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
62- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
54+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
6355 switch (prettierVersion ) {
6456 case PRETTIER_VERSION_2 :
6557 assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile_prettier_2.clean" );
@@ -89,7 +81,7 @@ void verifyCleanSpotlessCheckWorks(String prettierVersion) throws IOException {
8981 "}" );
9082 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
9183 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:" );
9385
9486 gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
9587 gradleRunner ().withArguments ("--stacktrace" , "spotlessCheck" ).build ();
@@ -112,7 +104,7 @@ void useFileConfig(String prettierVersion) throws IOException {
112104 "}" );
113105 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
114106 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
115- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
107+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
116108 switch (prettierVersion ) {
117109 case PRETTIER_VERSION_2 :
118110 assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile_prettier_2.clean" );
@@ -139,7 +131,7 @@ void chooseParserBasedOnFilename(String prettierVersion) throws IOException {
139131 "}" );
140132 setFile ("dirty.json" ).toResource ("npm/prettier/filename/dirty.json" );
141133 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
142- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
134+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
143135 assertFile ("dirty.json" ).sameAsResource ("npm/prettier/filename/clean.json" );
144136 }
145137
@@ -177,7 +169,7 @@ void useJavaCommunityPlugin(String prettierVersion) throws IOException {
177169 "}" );
178170 setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
179171 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
180- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
172+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
181173 assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
182174 }
183175
@@ -210,7 +202,7 @@ void useJavaCommunityPluginFileConfig(String prettierVersion) throws IOException
210202 "}" );
211203 setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
212204 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
213- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
205+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
214206 assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
215207 }
216208
@@ -234,8 +226,8 @@ void suggestsMissingJavaCommunityPlugin(String prettierVersion) throws IOExcepti
234226 "}" );
235227 setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
236228 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" );
239231 }
240232
241233 @ ParameterizedTest (name = "{index}: usePhpCommunityPlugin with prettier {0}" )
@@ -272,7 +264,7 @@ void usePhpCommunityPlugin(String prettierVersion) throws IOException {
272264 "}" );
273265 setFile ("php-example.php" ).toResource ("npm/prettier/plugins/php.dirty" );
274266 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
275- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
267+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
276268 assertFile ("php-example.php" ).sameAsResource ("npm/prettier/plugins/php.clean" );
277269 }
278270
@@ -332,9 +324,9 @@ void usePhpAndJavaCommunityPlugin(String prettierVersion) throws IOException {
332324 setFile ("php-example.php" ).toResource ("npm/prettier/plugins/php.dirty" );
333325 setFile ("JavaTest.java" ).toResource ("npm/prettier/plugins/java-test.dirty" );
334326 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" );
336328 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" );
338330 assertFile ("php-example.php" ).sameAsResource ("npm/prettier/plugins/php.clean" );
339331 assertFile ("JavaTest.java" ).sameAsResource ("npm/prettier/plugins/java-test.clean" );
340332 }
@@ -363,7 +355,7 @@ void autodetectNpmrcFileConfig(String prettierVersion) throws IOException {
363355 "}" );
364356 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
365357 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" );
367359 }
368360
369361 @ ParameterizedTest (name = "{index}: verifyCleanAndSpotlessWorks with prettier {0}" )
@@ -385,9 +377,9 @@ void verifyCleanAndSpotlessWorks(String prettierVersion) throws IOException {
385377 "}" );
386378 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
387379 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
388- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
380+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
389381 final BuildResult spotlessApply2 = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
390- assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
382+ Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
391383 }
392384
393385 @ ParameterizedTest (name = "{index}: verifyCleanAndSpotlessWithNpmInstallCacheWorks with prettier {0}" )
@@ -409,9 +401,9 @@ void verifyCleanAndSpotlessWithNpmInstallCacheWorks(String prettierVersion) thro
409401 "}" );
410402 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
411403 final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
412- assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
404+ Assertions . assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
413405 final BuildResult spotlessApply2 = gradleRunner ().withArguments ("--stacktrace" , "clean" , "spotlessApply" ).build ();
414- assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
406+ Assertions . assertThat (spotlessApply2 .getOutput ()).contains ("BUILD SUCCESSFUL" );
415407 }
416408
417409 @ ParameterizedTest (name = "{index}: autodetectNpmrcFileConfig with prettier {0}" )
@@ -438,51 +430,6 @@ void pickupNpmrcFileConfig(String prettierVersion) throws IOException {
438430 "}" );
439431 setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
440432 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" );
487434 }
488435}
0 commit comments