Skip to content

Commit

Permalink
try fix windows build: set binary name for win
Browse files Browse the repository at this point in the history
  • Loading branch information
simschla committed Jan 19, 2023
1 parent a9c79ae commit e8f0d2d
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.List;
import java.util.ListIterator;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -117,28 +118,52 @@ protected GradleRunner gradleRunner() throws IOException {
}

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

protected String getContents(Predicate<String> subpathsToInclude) throws IOException {
protected String getContents(Predicate<String> subpathsToInclude) {
return StringPrinter.buildString(printer -> Errors.rethrow().run(() -> iterateFiles(subpathsToInclude, (subpath, file) -> {
printer.println("### " + subpath + " ###");
try {
printer.println(read(subpath));
} catch (IOException e) {
throw new RuntimeException(e);
}
})));
}

/** Dumps the filtered file listing of the folder to the console. */
protected String listFiles(Predicate<String> subpathsToInclude) {
return StringPrinter.buildString(printer -> iterateFiles(subpathsToInclude, (subPath, file) -> {
printer.println(subPath + " [" + getFileAttributes(file) + "]");
}));
}

/** Dumps the file listing of the folder to the console. */
protected String listFiles() {
return listFiles(subPath -> !subPath.startsWith(".gradle"));
}

protected void iterateFiles(Predicate<String> subpathsToInclude, BiConsumer<String, File> consumer) {
TreeDef<File> treeDef = TreeDef.forFile(Errors.rethrow());
List<File> files = TreeStream.depthFirst(treeDef, rootFolder())
.filter(File::isFile)
.collect(Collectors.toList());

ListIterator<File> iterator = files.listIterator(files.size());
int rootLength = rootFolder().getAbsolutePath().length() + 1;
return StringPrinter.buildString(printer -> Errors.rethrow().run(() -> {
while (iterator.hasPrevious()) {
File file = iterator.previous();
String subPath = file.getAbsolutePath().substring(rootLength);
if (subpathsToInclude.test(subPath)) {
printer.println("### " + subPath + " ###");
printer.println(read(subPath));
}
while (iterator.hasPrevious()) {
File file = iterator.previous();
String subPath = file.getAbsolutePath().substring(rootLength);
if (subpathsToInclude.test(subPath)) {
consumer.accept(subPath, file);
}
}));
}
}

protected String getFileAttributes(File file) {
return (file.canRead() ? "r" : "-") + (file.canWrite() ? "w" : "-") + (file.canExecute() ? "x" : "-");
}

protected void checkRunsThenUpToDate() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,151 +20,179 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import com.diffplug.common.base.Predicates;

class NpmTestsWithoutNpmInstallationTest extends GradleIntegrationHarness {

@Test
void useNodeAndNpmFromNodeGradlePlugin() throws Exception {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
" id 'com.github.node-gradle.node' version '3.5.1'",
"}",
"repositories { mavenCentral() }",
"node {",
" download = true",
" version = '18.13.0'",
" npmVersion = '8.19.2'",
" workDir = file(\"${buildDir}/nodejs\")",
" npmWorkDir = file(\"${buildDir}/npm\")",
"}",
"def prettierConfig = [:]",
"prettierConfig['printWidth'] = 50",
"prettierConfig['parser'] = 'typescript'",
"def npmExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.cmd' : ''",
"def nodeExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.exe' : ''",
"spotless {",
" format 'mytypescript', {",
" target 'test.ts'",
" prettier()",
" .npmExecutable(\"${tasks.named('npmSetup').get().npmDir.get()}/bin/npm${npmExecExtension}\")",
" .nodeExecutable(\"${tasks.named('nodeSetup').get().nodeDir.get()}/bin/node${nodeExecExtension}\")",
" .config(prettierConfig)",
" }",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
// make sure node binary is there
gradleRunner().withArguments("nodeSetup", "npmSetup").build();
// then run spotless using that node installation
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
try {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
" id 'com.github.node-gradle.node' version '3.5.1'",
"}",
"repositories { mavenCentral() }",
"node {",
" download = true",
" version = '18.13.0'",
" npmVersion = '8.19.2'",
" workDir = file(\"${buildDir}/nodejs\")",
" npmWorkDir = file(\"${buildDir}/npm\")",
"}",
"def prettierConfig = [:]",
"prettierConfig['printWidth'] = 50",
"prettierConfig['parser'] = 'typescript'",
"def npmExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'",
"def nodeExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/node.exe' : '/bin/node'",
"spotless {",
" format 'mytypescript', {",
" target 'test.ts'",
" prettier()",
" .npmExecutable(\"${tasks.named('npmSetup').get().npmDir.get()}${npmExec}\")",
" .nodeExecutable(\"${tasks.named('nodeSetup').get().nodeDir.get()}${nodeExec}\")",
" .config(prettierConfig)",
" }",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
// make sure node binary is there
gradleRunner().withArguments("nodeSetup", "npmSetup").build();
// then run spotless using that node installation
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
} catch (Exception e) {
printContents();
throw e;
}
}

private void printContents() {
System.out.println("************* Folder contents **************************");
System.out.println(listFiles(Predicates.and(path -> !path.startsWith(".gradle"), path -> !path.contains("/node_modules/"), path -> !path.contains("/include/"))));
System.out.println("********************************************************");
}

@Test
@Disabled("This test is disabled because we currently don't support using npm/node installed by the" +
"node-gradle-plugin as the installation takes place in the *execution* phase, but spotless needs it in the *configuration* phase.")
void useNodeAndNpmFromNodeGradlePluginInOneSweep() throws Exception {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
" id 'com.github.node-gradle.node' version '3.5.1'",
"}",
"repositories { mavenCentral() }",
"node {",
" download = true",
" version = '18.13.0'",
" npmVersion = '8.19.2'",
" workDir = file(\"${buildDir}/nodejs\")",
" npmWorkDir = file(\"${buildDir}/npm\")",
"}",
"def prettierConfig = [:]",
"prettierConfig['printWidth'] = 50",
"prettierConfig['parser'] = 'typescript'",
"def npmExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.cmd' : ''",
"def nodeExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.exe' : ''",
"spotless {",
" format 'mytypescript', {",
" target 'test.ts'",
" prettier()",
" .npmExecutable(\"${tasks.named('npmSetup').get().npmDir.get()}/bin/npm${npmExecExtension}\")",
" .nodeExecutable(\"${tasks.named('nodeSetup').get().nodeDir.get()}/bin/node${nodeExecExtension}\")",
" .config(prettierConfig)",
" }",
"}",
"tasks.named('spotlessMytypescript').configure {",
" it.dependsOn('nodeSetup', 'npmSetup')",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
try {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
" id 'com.github.node-gradle.node' version '3.5.1'",
"}",
"repositories { mavenCentral() }",
"node {",
" download = true",
" version = '18.13.0'",
" npmVersion = '8.19.2'",
" workDir = file(\"${buildDir}/nodejs\")",
" npmWorkDir = file(\"${buildDir}/npm\")",
"}",
"def prettierConfig = [:]",
"prettierConfig['printWidth'] = 50",
"prettierConfig['parser'] = 'typescript'",
"def npmExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'",
"def nodeExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/node.exe' : '/bin/node'",
"spotless {",
" format 'mytypescript', {",
" target 'test.ts'",
" prettier()",
" .npmExecutable(\"${tasks.named('npmSetup').get().npmDir.get()}${npmExec}\")",
" .nodeExecutable(\"${tasks.named('nodeSetup').get().nodeDir.get()}${nodeExec}\")",
" .config(prettierConfig)",
" }",
"}",
"tasks.named('spotlessMytypescript').configure {",
" it.dependsOn('nodeSetup', 'npmSetup')",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
} catch (Exception e) {
printContents();
throw e;
}
}

@Test
void useNpmFromNodeGradlePlugin() throws Exception {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
" id 'com.github.node-gradle.node' version '3.5.1'",
"}",
"repositories { mavenCentral() }",
"node {",
" download = true",
" version = '18.13.0'",
" workDir = file(\"${buildDir}/nodejs\")",
"}",
"def prettierConfig = [:]",
"prettierConfig['printWidth'] = 50",
"prettierConfig['parser'] = 'typescript'",
"def npmExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.cmd' : ''",
"spotless {",
" format 'mytypescript', {",
" target 'test.ts'",
" prettier()",
" .npmExecutable(\"${tasks.named('npmSetup').get().npmDir.get()}/bin/npm${npmExecExtension}\")",
" .config(prettierConfig)",
" }",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
// make sure node binary is there
gradleRunner().withArguments("nodeSetup", "npmSetup").build();
// then run spotless using that node installation
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
try {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
" id 'com.github.node-gradle.node' version '3.5.1'",
"}",
"repositories { mavenCentral() }",
"node {",
" download = true",
" version = '18.13.0'",
" workDir = file(\"${buildDir}/nodejs\")",
"}",
"def prettierConfig = [:]",
"prettierConfig['printWidth'] = 50",
"prettierConfig['parser'] = 'typescript'",
"def npmExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'",
"spotless {",
" format 'mytypescript', {",
" target 'test.ts'",
" prettier()",
" .npmExecutable(\"${tasks.named('npmSetup').get().npmDir.get()}${npmExec}\")",
" .config(prettierConfig)",
" }",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
// make sure node binary is there
gradleRunner().withArguments("nodeSetup", "npmSetup").build();
// then run spotless using that node installation
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
} catch (Exception e) {
printContents();
throw e;
}
}

@Test
void useNpmNextToConfiguredNodePluginFromNodeGradlePlugin() throws Exception {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
" id 'com.github.node-gradle.node' version '3.5.1'",
"}",
"repositories { mavenCentral() }",
"node {",
" download = true",
" version = '18.13.0'",
" workDir = file(\"${buildDir}/nodejs\")",
"}",
"def prettierConfig = [:]",
"prettierConfig['printWidth'] = 50",
"prettierConfig['parser'] = 'typescript'",
"def nodeExecExtension = System.getProperty('os.name').toLowerCase().contains('windows') ? '.exe' : ''",
"spotless {",
" format 'mytypescript', {",
" target 'test.ts'",
" prettier()",
" .nodeExecutable(\"${tasks.named('nodeSetup').get().nodeDir.get()}/bin/node${nodeExecExtension}\")",
" .config(prettierConfig)",
" }",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
// make sure node binary is there
gradleRunner().withArguments("nodeSetup", "npmSetup").build();
// then run spotless using that node installation
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
try {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
" id 'com.github.node-gradle.node' version '3.5.1'",
"}",
"repositories { mavenCentral() }",
"node {",
" download = true",
" version = '18.13.0'",
" workDir = file(\"${buildDir}/nodejs\")",
"}",
"def prettierConfig = [:]",
"prettierConfig['printWidth'] = 50",
"prettierConfig['parser'] = 'typescript'",
"def nodeExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/node.exe' : '/bin/node'",
"spotless {",
" format 'mytypescript', {",
" target 'test.ts'",
" prettier()",
" .nodeExecutable(\"${tasks.named('nodeSetup').get().nodeDir.get()}${nodeExec}\")",
" .config(prettierConfig)",
" }",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
// make sure node binary is there
gradleRunner().withArguments("nodeSetup", "npmSetup").build();
// then run spotless using that node installation
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
} catch (Exception e) {
printContents();
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public static String installNpmMavenGoal() {
}

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

0 comments on commit e8f0d2d

Please sign in to comment.