Skip to content

Commit 85730c6

Browse files
committed
try fix windows build: set binary name for win
1 parent a9c79ae commit 85730c6

File tree

3 files changed

+195
-140
lines changed

3 files changed

+195
-140
lines changed

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java

+36-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.util.List;
2323
import java.util.ListIterator;
24+
import java.util.function.BiConsumer;
2425
import java.util.function.Predicate;
2526
import java.util.stream.Collectors;
2627

@@ -117,28 +118,52 @@ protected GradleRunner gradleRunner() throws IOException {
117118
}
118119

119120
/** Dumps the complete file contents of the folder to the console. */
120-
protected String getContents() throws IOException {
121+
protected String getContents() {
121122
return getContents(subPath -> !subPath.startsWith(".gradle"));
122123
}
123124

124-
protected String getContents(Predicate<String> subpathsToInclude) throws IOException {
125+
protected String getContents(Predicate<String> subpathsToInclude) {
126+
return StringPrinter.buildString(printer -> Errors.rethrow().run(() -> iterateFiles(subpathsToInclude, (subpath, file) -> {
127+
printer.println("### " + subpath + " ###");
128+
try {
129+
printer.println(read(subpath));
130+
} catch (IOException e) {
131+
throw new RuntimeException(e);
132+
}
133+
})));
134+
}
135+
136+
/** Dumps the filtered file listing of the folder to the console. */
137+
protected String listFiles(Predicate<String> subpathsToInclude) {
138+
return StringPrinter.buildString(printer -> iterateFiles(subpathsToInclude, (subPath, file) -> {
139+
printer.println(subPath + " [" + getFileAttributes(file) + "]");
140+
}));
141+
}
142+
143+
/** Dumps the file listing of the folder to the console. */
144+
protected String listFiles() {
145+
return listFiles(subPath -> !subPath.startsWith(".gradle"));
146+
}
147+
148+
protected void iterateFiles(Predicate<String> subpathsToInclude, BiConsumer<String, File> consumer) {
125149
TreeDef<File> treeDef = TreeDef.forFile(Errors.rethrow());
126150
List<File> files = TreeStream.depthFirst(treeDef, rootFolder())
127151
.filter(File::isFile)
128152
.collect(Collectors.toList());
129153

130154
ListIterator<File> iterator = files.listIterator(files.size());
131155
int rootLength = rootFolder().getAbsolutePath().length() + 1;
132-
return StringPrinter.buildString(printer -> Errors.rethrow().run(() -> {
133-
while (iterator.hasPrevious()) {
134-
File file = iterator.previous();
135-
String subPath = file.getAbsolutePath().substring(rootLength);
136-
if (subpathsToInclude.test(subPath)) {
137-
printer.println("### " + subPath + " ###");
138-
printer.println(read(subPath));
139-
}
156+
while (iterator.hasPrevious()) {
157+
File file = iterator.previous();
158+
String subPath = file.getAbsolutePath().substring(rootLength);
159+
if (subpathsToInclude.test(subPath)) {
160+
consumer.accept(subPath, file);
140161
}
141-
}));
162+
}
163+
}
164+
165+
protected String getFileAttributes(File file) {
166+
return (file.canRead() ? "r" : "-") + (file.canWrite() ? "w" : "-") + (file.canExecute() ? "x" : "-");
142167
}
143168

144169
protected void checkRunsThenUpToDate() throws IOException {

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/NpmTestsWithoutNpmInstallationTest.java

+158-128
Original file line numberDiff line numberDiff line change
@@ -20,151 +20,181 @@
2020
import org.junit.jupiter.api.Disabled;
2121
import org.junit.jupiter.api.Test;
2222

23+
import com.diffplug.common.base.Predicates;
24+
2325
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
}

Diff for: plugin-maven/src/test/java/com/diffplug/spotless/maven/npm/NpmFrontendMavenPlugin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public static String installNpmMavenGoal() {
6161
}
6262

6363
public static String installedNpmPath() {
64-
return String.format("%s/node/npm", INSTALL_DIRECTORY);
64+
return String.format("%s/node/npm%s", INSTALL_DIRECTORY, System.getProperty("os.name").toLowerCase().contains("win") ? ".cmd" : "");
6565
}
6666
}

0 commit comments

Comments
 (0)