Skip to content

Commit 483ab5e

Browse files
committed
chore: move testing to use application dist instead of shadow jar
1 parent 59cb1af commit 483ab5e

8 files changed

Lines changed: 70 additions & 40 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.java-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
21

app/build.gradle

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import org.gradle.plugins.ide.eclipse.model.EclipseWtp
55
plugins {
66
id 'buildlogic.picocli-conventions'
77
id 'buildlogic.java-special-tests-conventions'
8-
id 'buildlogic.jar-shadow-conventions'
98
id 'buildlogic.spotless-json-conventions'
109
}
1110

@@ -41,25 +40,30 @@ application {
4140
archivesBaseName = 'spotless-cli'
4241
}
4342

44-
gradle.taskGraph.whenReady { TaskExecutionGraph graph ->
45-
// println "Graph: " + graph.allTasks*.name
46-
if (graph.hasTask(':app:nativeCompile') || graph.hasTask(':app:metadataCopy') || graph.hasTask(':app:shadowJar')) {
47-
// enable graalvm agent using property here instead of command line `-Pagent=standard`
48-
// this collects information about reflective access and resources used by the application (e.g. GJF)
49-
project.ext.agent = 'standard'
50-
}
51-
}
52-
5343
tasks.withType(Test).configureEach {
5444
if (it.name == 'test' || it.name == 'testNpm' || it.name == 'testSeparateJvm') {
5545
it.systemProperty 'spotless.cli.inSameThread', 'true' //mark tests
5646
}
5747
if (it.name == 'testCliProcess' || it.name == 'testCliProcessNpm') {
58-
it.dependsOn('shadowJar')
59-
it.systemProperty 'spotless.cli.shadowJar', tasks.shadowJar.archiveFile.get().asFile
48+
def taskInstallDist = tasks.named('installDist', org.gradle.api.tasks.Sync)
49+
it.dependsOn(taskInstallDist) //ensure the CLI is built before running tests
50+
it.systemProperty 'spotless.cli.processLauncher', processLauncherPathIninstallDist(taskInstallDist.get()) //pass the path to the CLI launcher
51+
52+
def toolchains = project.extensions.getByType(JavaToolchainService)
53+
def launcher = toolchains.launcherFor(java.toolchain).get()
54+
def javaHome = launcher.metadata.installationPath.asFile
55+
56+
it.systemProperty 'spotless.cli.processLauncher.javaHome', javaHome.absolutePath //pass the path to the JDK to use
6057
}
6158
}
6259

60+
def processLauncherPathIninstallDist(org.gradle.api.tasks.Sync installDistTask) {
61+
def binaryPath = installDistTask.destinationDir.absolutePath + "/bin/spotless"
62+
def suffix = System.getProperty("os.name").toLowerCase().contains("windows") ? ".bat" : ""
63+
println("Using spotless CLI launcher at: " + binaryPath + suffix)
64+
return binaryPath + suffix
65+
}
66+
6367
tasks.register("generateUsage", GenerateUsagePropertiesTask) {
6468
it.outputDir.set(layout.buildDirectory.dir("generated-usages").get().asFile)
6569
}

build-logic/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ repositories {
1717
}
1818

1919
dependencies {
20-
implementation 'com.gradleup.shadow:shadow-gradle-plugin:8.3.6'
2120
implementation libs.bundles.spotless.libs
2221
implementation libs.spotless.plugin.gradle
2322
}

build-logic/src/main/groovy/buildlogic.jar-shadow-conventions.gradle

Lines changed: 0 additions & 3 deletions
This file was deleted.

build-logic/src/main/groovy/buildlogic.release-prepare-packages-conventions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
// TODO: remove this completely once we fully migrate to JReleaser java-binary for packaging
22
File releaseBinariesRootDir = rootProject.file(project.findProperty('releaseBinariesRootDir') ?: 'app/build/release-binaries')
33
def releaseBinariesSubdirectories = [] as List<File>
44
File releaseBinariesZipDir = rootProject.file('app/build/prepared-release-zips')

testlib/src/main/java/com/diffplug/spotless/cli/CLIIntegrationHarness.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@
2626

2727
@EnabledIf("testEnvIsMatching")
2828
public abstract class CLIIntegrationHarness extends ResourceHarness {
29-
protected static final String SYSPROP_CLI_NATIVE =
30-
SpotlessCLIRunnerInNativeExternalProcess.SPOTLESS_CLI_NATIVE_IMAGE_SYSPROP;
31-
protected static final String SYSPROP_CLI_SHADOW_JAR =
32-
SpotlessCLIRunnerInExternalJavaProcess.SPOTLESS_CLI_SHADOW_JAR_SYSPROP;
29+
protected static final String SPOTLESS_CLI_PROCESS_LAUNCHER =
30+
SpotlessCLIRunnerInExternalJavaProcess.SPOTLESS_CLI_PROCESS_LAUNCHER;
3331
protected static final String SYSPROP_CLI_IN_SAME_THREAD =
3432
SpotlessCLIRunnerInSameThread.SPOTLESS_CLI_IN_SAME_THREAD;
3533

36-
protected static final Set<String> SYS_PROPS =
37-
Set.of(SYSPROP_CLI_NATIVE, SYSPROP_CLI_SHADOW_JAR, SYSPROP_CLI_IN_SAME_THREAD);
34+
protected static final Set<String> SYS_PROPS = Set.of(SPOTLESS_CLI_PROCESS_LAUNCHER, SYSPROP_CLI_IN_SAME_THREAD);
3835

3936
/**
4037
* Each test gets its own temp folder, and we create a gradle
@@ -60,10 +57,7 @@ protected SpotlessCLIRunner cliRunner() {
6057
}
6158

6259
protected SpotlessCLIRunner createRunnerBasedOnSysprop() {
63-
if (System.getProperties().containsKey(SYSPROP_CLI_NATIVE)) {
64-
return SpotlessCLIRunner.createNative();
65-
}
66-
if (System.getProperties().containsKey(SYSPROP_CLI_SHADOW_JAR)) {
60+
if (System.getProperties().containsKey(SPOTLESS_CLI_PROCESS_LAUNCHER)) {
6761
return SpotlessCLIRunner.createExternalProcess();
6862
}
6963
if (System.getProperties().containsKey(SYSPROP_CLI_IN_SAME_THREAD)) {
@@ -77,7 +71,7 @@ static boolean testEnvIsMatching() {
7771
}
7872

7973
protected static boolean isInExternalProcess() {
80-
return Stream.of(SYSPROP_CLI_NATIVE, SYSPROP_CLI_SHADOW_JAR)
74+
return Stream.of(SPOTLESS_CLI_PROCESS_LAUNCHER)
8175
.anyMatch(p -> System.getProperties().containsKey(p));
8276
}
8377

testlib/src/main/java/com/diffplug/spotless/cli/SpotlessCLIRunnerInExternalJavaProcess.java

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,82 @@
1515
*/
1616
package com.diffplug.spotless.cli;
1717

18+
import java.nio.file.Path;
1819
import java.util.ArrayList;
20+
import java.util.LinkedHashMap;
1921
import java.util.List;
22+
import java.util.Map;
23+
import java.util.Optional;
2024

2125
import com.diffplug.spotless.ProcessRunner;
2226
import com.diffplug.spotless.ThrowingEx;
2327

2428
public class SpotlessCLIRunnerInExternalJavaProcess extends SpotlessCLIRunner {
2529

26-
static final String SPOTLESS_CLI_SHADOW_JAR_SYSPROP = "spotless.cli.shadowJar";
30+
static final String SPOTLESS_CLI_PROCESS_LAUNCHER = "spotless.cli.processLauncher";
31+
static final String SPOTLESS_CLI_PROCESS_LAUNCHER_JAVA_HOME = "spotless.cli.processLauncher.javaHome"; // optional
2732

2833
public SpotlessCLIRunnerInExternalJavaProcess() {
2934
super();
30-
if (System.getProperty(SPOTLESS_CLI_SHADOW_JAR_SYSPROP) == null) {
35+
if (System.getProperty(SPOTLESS_CLI_PROCESS_LAUNCHER) == null) {
3136
throw new IllegalStateException(
32-
"spotless.cli.shadowJar system property must be set to the path of the shadow jar");
37+
SPOTLESS_CLI_PROCESS_LAUNCHER + " system property must be set to the path of the launcher");
3338
}
3439
}
3540

3641
protected Result executeCommand(List<String> args) {
3742
try (ProcessRunner runner = new ProcessRunner()) {
3843

3944
ProcessRunner.Result pResult =
40-
ThrowingEx.get(() -> runner.exec(workingDir(), System.getenv(), null, processArgs(args)));
45+
ThrowingEx.get(() -> runner.exec(workingDir(), envWithCurrentJava(), null, processArgs(args)));
4146

4247
return new Result(pResult.exitCode(), null, pResult.stdOutUtf8(), pResult.stdErrUtf8());
4348
}
4449
}
4550

4651
private List<String> processArgs(List<String> args) {
4752
List<String> processArgs = new ArrayList<>();
48-
processArgs.add(currentJavaExecutable());
49-
processArgs.add("-jar");
50-
String jarPath = System.getProperty(SPOTLESS_CLI_SHADOW_JAR_SYSPROP);
51-
processArgs.add(jarPath);
52-
53-
// processArgs.add(SpotlessCLI.class.getProtectionDomain().getCodeSource().getLocation().getPath());
54-
53+
processArgs.add(System.getProperty(SPOTLESS_CLI_PROCESS_LAUNCHER));
5554
processArgs.addAll(args);
5655
return processArgs;
5756
}
5857

58+
private Map<String, String> envWithCurrentJava() {
59+
// put current java
60+
Map<String, String> env = new LinkedHashMap<>(System.getenv());
61+
62+
// Override JAVA_HOME if specified via sysprop or use current java
63+
Optional<String> javaHomeOpt = javaHomeFromSyspropOrCurrent();
64+
javaHomeOpt.ifPresentOrElse(javaHome -> env.put("JAVA_HOME", javaHome), () -> env.remove("JAVA_HOME"));
65+
66+
// Prepend current java bin to PATH
67+
String currentJavaBin = currentJavaExecutable();
68+
if (!"java".equals(currentJavaBin)) {
69+
String currentJavaDir = Path.of(currentJavaBin).getParent().toString();
70+
String currentPath = env.get("PATH");
71+
if (currentPath == null || currentPath.isEmpty()) {
72+
currentPath = currentJavaDir;
73+
} else {
74+
currentPath = currentJavaDir + System.getProperty("path.separator") + currentPath;
75+
}
76+
env.put("PATH", currentPath);
77+
}
78+
return env;
79+
}
80+
81+
private Optional<String> javaHomeFromSyspropOrCurrent() {
82+
String javaHome = System.getProperty(SPOTLESS_CLI_PROCESS_LAUNCHER_JAVA_HOME);
83+
if (javaHome != null && !javaHome.isEmpty()) {
84+
return Optional.of(javaHome);
85+
}
86+
String currentJavaBin = currentJavaExecutable();
87+
if ("java".equals(currentJavaBin)) {
88+
return Optional.empty(); // unset
89+
}
90+
91+
return Optional.of(Path.of(currentJavaBin).getParent().getParent().toString());
92+
}
93+
5994
private String currentJavaExecutable() {
6095
return ProcessHandle.current().info().command().orElse("java");
6196
}

0 commit comments

Comments
 (0)