1515 */
1616package com .diffplug .gradle .spotless ;
1717
18+ import com .diffplug .common .base .Predicates ;
19+
1820import org .assertj .core .api .Assertions ;
1921import org .gradle .testkit .runner .BuildResult ;
2022import org .junit .jupiter .api .Disabled ;
@@ -24,147 +26,175 @@ class NpmTestsWithoutNpmInstallationTest extends GradleIntegrationHarness {
2426
2527 @ Test
2628 void useNodeAndNpmFromNodeGradlePlugin () throws Exception {
27- setFile ("build.gradle" ).toLines (
28- "plugins {" ,
29- " id 'com.diffplug.spotless'" ,
30- " id 'com.github.node-gradle.node' version '3.5.1'" ,
31- "}" ,
32- "repositories { mavenCentral() }" ,
33- "node {" ,
34- " download = true" ,
35- " version = '18.13.0'" ,
36- " npmVersion = '8.19.2'" ,
37- " workDir = file(\" ${buildDir}/nodejs\" )" ,
38- " npmWorkDir = file(\" ${buildDir}/npm\" )" ,
39- "}" ,
40- "def prettierConfig = [:]" ,
41- "prettierConfig['printWidth'] = 50" ,
42- "prettierConfig['parser'] = 'typescript'" ,
43- "def npmExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.cmd' : ''" ,
44- "def nodeExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.exe' : ''" ,
45- "spotless {" ,
46- " format 'mytypescript', {" ,
47- " target 'test.ts'" ,
48- " prettier()" ,
49- " .npmExecutable(\" ${tasks.named('npmSetup').get().npmDir.get()}/bin/npm${npmExecExtension}\" )" ,
50- " .nodeExecutable(\" ${tasks.named('nodeSetup').get().nodeDir.get()}/bin/node${nodeExecExtension}\" )" ,
51- " .config(prettierConfig)" ,
52- " }" ,
53- "}" );
54- setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
55- // make sure node binary is there
56- gradleRunner ().withArguments ("nodeSetup" , "npmSetup" ).build ();
57- // then run spotless using that node installation
58- final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
59- Assertions .assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
60- assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile.clean" );
29+ try {
30+ setFile ("build.gradle" ).toLines (
31+ "plugins {" ,
32+ " id 'com.diffplug.spotless'" ,
33+ " id 'com.github.node-gradle.node' version '3.5.1'" ,
34+ "}" ,
35+ "repositories { mavenCentral() }" ,
36+ "node {" ,
37+ " download = true" ,
38+ " version = '18.13.0'" ,
39+ " npmVersion = '8.19.2'" ,
40+ " workDir = file(\" ${buildDir}/nodejs\" )" ,
41+ " npmWorkDir = file(\" ${buildDir}/npm\" )" ,
42+ "}" ,
43+ "def prettierConfig = [:]" ,
44+ "prettierConfig['printWidth'] = 50" ,
45+ "prettierConfig['parser'] = 'typescript'" ,
46+ "def npmExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'" ,
47+ "def nodeExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/node.exe' : '/bin/node'" ,
48+ "spotless {" ,
49+ " format 'mytypescript', {" ,
50+ " target 'test.ts'" ,
51+ " prettier()" ,
52+ " .npmExecutable(\" ${tasks.named('npmSetup').get().npmDir.get()}${npmExec}\" )" ,
53+ " .nodeExecutable(\" ${tasks.named('nodeSetup').get().nodeDir.get()}${nodeExec}\" )" ,
54+ " .config(prettierConfig)" ,
55+ " }" ,
56+ "}" );
57+ setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
58+ // make sure node binary is there
59+ gradleRunner ().withArguments ("nodeSetup" , "npmSetup" ).build ();
60+ // then run spotless using that node installation
61+ final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
62+ Assertions .assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
63+ assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile.clean" );
64+ } catch (Exception e ) {
65+ printContents ();
66+ throw e ;
67+ }
68+ }
69+
70+ private void printContents () {
71+ System .out .println ("************* Folder contents **************************" );
72+ System .out .println (listFiles (Predicates .and (path -> !path .startsWith (".gradle" ), path -> !path .contains ("/node_modules/" ), path -> !path .contains ("/include/" ))));
73+ System .out .println ("********************************************************" );
6174 }
6275
6376 @ Test
6477 @ Disabled ("This test is disabled because we currently don't support using npm/node installed by the" +
6578 "node-gradle-plugin as the installation takes place in the *execution* phase, but spotless needs it in the *configuration* phase." )
6679 void useNodeAndNpmFromNodeGradlePluginInOneSweep () throws Exception {
67- setFile ("build.gradle" ).toLines (
68- "plugins {" ,
69- " id 'com.diffplug.spotless'" ,
70- " id 'com.github.node-gradle.node' version '3.5.1'" ,
71- "}" ,
72- "repositories { mavenCentral() }" ,
73- "node {" ,
74- " download = true" ,
75- " version = '18.13.0'" ,
76- " npmVersion = '8.19.2'" ,
77- " workDir = file(\" ${buildDir}/nodejs\" )" ,
78- " npmWorkDir = file(\" ${buildDir}/npm\" )" ,
79- "}" ,
80- "def prettierConfig = [:]" ,
81- "prettierConfig['printWidth'] = 50" ,
82- "prettierConfig['parser'] = 'typescript'" ,
83- "def npmExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.cmd' : ''" ,
84- "def nodeExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.exe' : ''" ,
85- "spotless {" ,
86- " format 'mytypescript', {" ,
87- " target 'test.ts'" ,
88- " prettier()" ,
89- " .npmExecutable(\" ${tasks.named('npmSetup').get().npmDir.get()}/bin/npm${npmExecExtension}\" )" ,
90- " .nodeExecutable(\" ${tasks.named('nodeSetup').get().nodeDir.get()}/bin/node${nodeExecExtension}\" )" ,
91- " .config(prettierConfig)" ,
92- " }" ,
93- "}" ,
94- "tasks.named('spotlessMytypescript').configure {" ,
95- " it.dependsOn('nodeSetup', 'npmSetup')" ,
96- "}" );
97- setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
98- final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
99- Assertions .assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
100- assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile.clean" );
80+ try {
81+ setFile ("build.gradle" ).toLines (
82+ "plugins {" ,
83+ " id 'com.diffplug.spotless'" ,
84+ " id 'com.github.node-gradle.node' version '3.5.1'" ,
85+ "}" ,
86+ "repositories { mavenCentral() }" ,
87+ "node {" ,
88+ " download = true" ,
89+ " version = '18.13.0'" ,
90+ " npmVersion = '8.19.2'" ,
91+ " workDir = file(\" ${buildDir}/nodejs\" )" ,
92+ " npmWorkDir = file(\" ${buildDir}/npm\" )" ,
93+ "}" ,
94+ "def prettierConfig = [:]" ,
95+ "prettierConfig['printWidth'] = 50" ,
96+ "prettierConfig['parser'] = 'typescript'" ,
97+ "def npmExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'" ,
98+ "def nodeExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/node.exe' : '/bin/node'" ,
99+ "spotless {" ,
100+ " format 'mytypescript', {" ,
101+ " target 'test.ts'" ,
102+ " prettier()" ,
103+ " .npmExecutable(\" ${tasks.named('npmSetup').get().npmDir.get()}${npmExec}\" )" ,
104+ " .nodeExecutable(\" ${tasks.named('nodeSetup').get().nodeDir.get()}${nodeExec}\" )" ,
105+ " .config(prettierConfig)" ,
106+ " }" ,
107+ "}" ,
108+ "tasks.named('spotlessMytypescript').configure {" ,
109+ " it.dependsOn('nodeSetup', 'npmSetup')" ,
110+ "}" );
111+ setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
112+ final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
113+ Assertions .assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
114+ assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile.clean" );
115+ } catch (Exception e ) {
116+ printContents ();
117+ throw e ;
118+ }
101119 }
102120
103121 @ Test
104122 void useNpmFromNodeGradlePlugin () throws Exception {
105- setFile ("build.gradle" ).toLines (
106- "plugins {" ,
107- " id 'com.diffplug.spotless'" ,
108- " id 'com.github.node-gradle.node' version '3.5.1'" ,
109- "}" ,
110- "repositories { mavenCentral() }" ,
111- "node {" ,
112- " download = true" ,
113- " version = '18.13.0'" ,
114- " workDir = file(\" ${buildDir}/nodejs\" )" ,
115- "}" ,
116- "def prettierConfig = [:]" ,
117- "prettierConfig['printWidth'] = 50" ,
118- "prettierConfig['parser'] = 'typescript'" ,
119- "def npmExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.cmd' : ''" ,
120- "spotless {" ,
121- " format 'mytypescript', {" ,
122- " target 'test.ts'" ,
123- " prettier()" ,
124- " .npmExecutable(\" ${tasks.named('npmSetup').get().npmDir.get()}/bin/npm${npmExecExtension}\" )" ,
125- " .config(prettierConfig)" ,
126- " }" ,
127- "}" );
128- setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
129- // make sure node binary is there
130- gradleRunner ().withArguments ("nodeSetup" , "npmSetup" ).build ();
131- // then run spotless using that node installation
132- final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
133- Assertions .assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
134- assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile.clean" );
123+ try {
124+ setFile ("build.gradle" ).toLines (
125+ "plugins {" ,
126+ " id 'com.diffplug.spotless'" ,
127+ " id 'com.github.node-gradle.node' version '3.5.1'" ,
128+ "}" ,
129+ "repositories { mavenCentral() }" ,
130+ "node {" ,
131+ " download = true" ,
132+ " version = '18.13.0'" ,
133+ " workDir = file(\" ${buildDir}/nodejs\" )" ,
134+ "}" ,
135+ "def prettierConfig = [:]" ,
136+ "prettierConfig['printWidth'] = 50" ,
137+ "prettierConfig['parser'] = 'typescript'" ,
138+ "def npmExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'" ,
139+ "spotless {" ,
140+ " format 'mytypescript', {" ,
141+ " target 'test.ts'" ,
142+ " prettier()" ,
143+ " .npmExecutable(\" ${tasks.named('npmSetup').get().npmDir.get()}${npmExec}\" )" ,
144+ " .config(prettierConfig)" ,
145+ " }" ,
146+ "}" );
147+ setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
148+ // make sure node binary is there
149+ gradleRunner ().withArguments ("nodeSetup" , "npmSetup" ).build ();
150+ // then run spotless using that node installation
151+ final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
152+ Assertions .assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
153+ assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile.clean" );
154+ } catch (Exception e ) {
155+ printContents ();
156+ throw e ;
157+ }
135158 }
136159
137160 @ Test
138161 void useNpmNextToConfiguredNodePluginFromNodeGradlePlugin () throws Exception {
139- setFile ("build.gradle" ).toLines (
140- "plugins {" ,
141- " id 'com.diffplug.spotless'" ,
142- " id 'com.github.node-gradle.node' version '3.5.1'" ,
143- "}" ,
144- "repositories { mavenCentral() }" ,
145- "node {" ,
146- " download = true" ,
147- " version = '18.13.0'" ,
148- " workDir = file(\" ${buildDir}/nodejs\" )" ,
149- "}" ,
150- "def prettierConfig = [:]" ,
151- "prettierConfig['printWidth'] = 50" ,
152- "prettierConfig['parser'] = 'typescript'" ,
153- "def nodeExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.exe' : ''" ,
154- "spotless {" ,
155- " format 'mytypescript', {" ,
156- " target 'test.ts'" ,
157- " prettier()" ,
158- " .nodeExecutable(\" ${tasks.named('nodeSetup').get().nodeDir.get()}/bin/node${nodeExecExtension}\" )" ,
159- " .config(prettierConfig)" ,
160- " }" ,
161- "}" );
162- setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
163- // make sure node binary is there
164- gradleRunner ().withArguments ("nodeSetup" , "npmSetup" ).build ();
165- // then run spotless using that node installation
166- final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
167- Assertions .assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
168- assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile.clean" );
162+ try {
163+ setFile ("build.gradle" ).toLines (
164+ "plugins {" ,
165+ " id 'com.diffplug.spotless'" ,
166+ " id 'com.github.node-gradle.node' version '3.5.1'" ,
167+ "}" ,
168+ "repositories { mavenCentral() }" ,
169+ "node {" ,
170+ " download = true" ,
171+ " version = '18.13.0'" ,
172+ " workDir = file(\" ${buildDir}/nodejs\" )" ,
173+ "}" ,
174+ "def prettierConfig = [:]" ,
175+ "prettierConfig['printWidth'] = 50" ,
176+ "prettierConfig['parser'] = 'typescript'" ,
177+ "def nodeExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/node.exe' : '/bin/node'" ,
178+ "spotless {" ,
179+ " format 'mytypescript', {" ,
180+ " target 'test.ts'" ,
181+ " prettier()" ,
182+ " .nodeExecutable(\" ${tasks.named('nodeSetup').get().nodeDir.get()}${nodeExec}\" )" ,
183+ " .config(prettierConfig)" ,
184+ " }" ,
185+ "}" );
186+ setFile ("test.ts" ).toResource ("npm/prettier/config/typescript.dirty" );
187+ // make sure node binary is there
188+ gradleRunner ().withArguments ("nodeSetup" , "npmSetup" ).build ();
189+ // then run spotless using that node installation
190+ final BuildResult spotlessApply = gradleRunner ().withArguments ("--stacktrace" , "spotlessApply" ).build ();
191+ Assertions .assertThat (spotlessApply .getOutput ()).contains ("BUILD SUCCESSFUL" );
192+ if (true )
193+ throw new RuntimeException ("failed" );
194+ assertFile ("test.ts" ).sameAsResource ("npm/prettier/config/typescript.configfile.clean" );
195+ } catch (Exception e ) {
196+ printContents ();
197+ throw e ;
198+ }
169199 }
170200}
0 commit comments