Skip to content

Commit 50e2589

Browse files
committed
fix(mvn): pass developer root dir to maven tests
1 parent 6d71e4d commit 50e2589

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

lib/src/main/java/com/diffplug/spotless/generic/TestEnvVars.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ private static Stream<Path> candidateTestEnvLocations() {
6464
if (System.getProperty("testenv.properties.path") != null) {
6565
builder.add(Path.of(System.getProperty("testenv.properties.path")));
6666
}
67+
if (System.getProperty("spotlessProjectDir") != null) {
68+
builder.add(Path.of(System.getProperty("spotlessProjectDir"), "testenv.properties"));
69+
}
6770
builder.add(
6871
Path.of(System.getProperty("user.dir"), "testenv.properties"));
6972
builder.add(

plugin-maven/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dependencies {
5555
apply from: rootProject.file('gradle/special-tests.gradle')
5656
tasks.withType(Test).configureEach {
5757
systemProperty 'spotlessMavenPluginVersion', project.version
58+
systemProperty 'spotlessProjectDir', "${project.rootProject.projectDir}".toString()
5859
dependsOn 'publishToMavenLocal'
5960
dependsOn ':lib:publishToMavenLocal'
6061
dependsOn ':lib-extra:publishToMavenLocal'

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Idea.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ public class Idea implements FormatterStepFactory {
2828
private String binaryPath;
2929

3030
@Parameter
31-
private String configPath;
31+
private String codeStyleSettingsPath;
3232

3333
@Parameter
34-
private Boolean withDefaults = false;
34+
private Boolean withDefaults = true;
3535

3636
@Override
3737
public FormatterStep newFormatterStep(FormatterStepConfig config) {
3838
return IdeaStep.newBuilder(config.getFileLocator().getBuildDir())
3939
.setUseDefaults(withDefaults)
40-
.setCodeStyleSettingsPath(configPath)
40+
.setCodeStyleSettingsPath(codeStyleSettingsPath)
4141
.setBinaryPath(binaryPath)
4242
.build();
4343
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 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.
@@ -213,9 +213,15 @@ protected void writePom(String[] executions, String[] configuration, String[] de
213213
}
214214

215215
protected MavenRunner mavenRunner() throws IOException {
216-
return MavenRunner.create()
216+
MavenRunner mavenRunner = MavenRunner.create()
217217
.withProjectDir(rootFolder())
218218
.withRunner(runner);
219+
System.getProperties().forEach((key, value) -> {
220+
if (key instanceof String && ((String) key).startsWith("spotless") && value instanceof String) {
221+
mavenRunner.withSystemProperty((String) key, (String) value);
222+
}
223+
});
224+
return mavenRunner;
219225
}
220226

221227
private static ProcessRunner runner;

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenRunner.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2025 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.
@@ -23,6 +23,8 @@
2323
import java.util.HashMap;
2424
import java.util.Map;
2525
import java.util.Objects;
26+
import java.util.stream.Collectors;
27+
import java.util.stream.Stream;
2628

2729
import com.diffplug.spotless.Jvm;
2830
import com.diffplug.spotless.ProcessRunner;
@@ -41,6 +43,7 @@ private MavenRunner() {}
4143
private File projectDir;
4244
private String[] args;
4345
private Map<String, String> environment = new HashMap<>();
46+
private Map<String, String> systemProperties = new HashMap<>();
4447
private ProcessRunner runner;
4548

4649
public MavenRunner withProjectDir(File projectDir) {
@@ -64,12 +67,31 @@ public MavenRunner withRemoteDebug(int port) {
6467
return this;
6568
}
6669

70+
public MavenRunner withSystemProperty(String key, String value) {
71+
systemProperties.put(key, value);
72+
return this;
73+
}
74+
75+
private Map<String, String> calculateEnvironment() {
76+
Map<String, String> env = new HashMap<>(environment);
77+
if (!systemProperties.isEmpty()) {
78+
// add system properties as environment variables as MAVEN_OPTS or append if already there
79+
String sysProps = systemProperties.entrySet().stream()
80+
.map(entry -> String.format("-D%s=%s", entry.getKey(), entry.getValue()))
81+
.collect(Collectors.joining(" "));
82+
String mavenOpts = Stream.of(env.getOrDefault("MAVEN_OPTS", ""), sysProps)
83+
.collect(Collectors.joining(" "));
84+
env.put("MAVEN_OPTS", mavenOpts.trim());
85+
}
86+
return env;
87+
}
88+
6789
private ProcessRunner.Result run() throws IOException, InterruptedException {
6890
Objects.requireNonNull(projectDir, "Need to call withProjectDir() first");
6991
Objects.requireNonNull(args, "Need to call withArguments() first");
7092
// run Maven with the given args in the given directory
7193
String argsString = "-e " + String.join(" ", Arrays.asList(args));
72-
return runner.shellWinUnix(projectDir, environment, "mvnw " + argsString, "./mvnw " + argsString);
94+
return runner.shellWinUnix(projectDir, calculateEnvironment(), "mvnw " + argsString, "./mvnw " + argsString);
7395
}
7496

7597
/** Runs the command and asserts that exit code is 0. */

0 commit comments

Comments
 (0)