Skip to content

Commit 0570c6a

Browse files
committed
feat: allow specifying idea execs not on path
1 parent 619051c commit 0570c6a

File tree

4 files changed

+110
-7
lines changed

4 files changed

+110
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Project-specific stuff
88
userHome/
99
workspace/
10+
testenv.properties
1011

1112
### Gradle ###
1213
.gradle

gradle/special-tests.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,9 @@ tasks.named('test').configure {
3838
special.forEach { tag ->
3939
tasks.register("test${tag.capitalize()}", Test) {
4040
useJUnitPlatform { includeTags tag }
41+
if (rootProject.file('testenv.properties').exists()) {
42+
systemProperty 'testenv.properties.path', rootProject.file('testenv.properties').canonicalPath
43+
}
4144
}
4245
}
46+

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,22 @@ private static String resolveFullBinaryPathAndCheckVersion(String binaryPath) {
161161

162162
@CheckForNull
163163
private static String pathToExe(String binaryPath) {
164-
return macOsFix(binaryPath);
164+
String testEnvBinaryPath = TestEnvVars.read().get(String.format("%s.%s", IdeaStep.class.getName(), "binaryPath"));
165+
if (testEnvBinaryPath != null) {
166+
return testEnvBinaryPath;
167+
}
168+
if (isMacOs()) {
169+
return macOsFix(binaryPath);
170+
}
171+
if (new File(binaryPath).exists()) {
172+
return binaryPath;
173+
}
174+
return null; // search in PATH
165175
}
166176

167177
@CheckForNull
168178
private static String macOsFix(String binaryPath) {
169-
if (!isMacOs()) {
170-
if (new File(binaryPath).exists()) {
171-
return binaryPath;
172-
}
173-
return null; // search in PATH
174-
}
179+
175180
// on macOS, the binary is located in the .app bundle which might be invisible to the user
176181
// we try need to append the path to the binary
177182
File binary = new File(binaryPath);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.generic;
17+
18+
import java.io.IOException;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import java.util.Optional;
24+
import java.util.stream.Stream;
25+
26+
import javax.annotation.Nullable;
27+
28+
class TestEnvVars {
29+
30+
private final Map<String, String> envVars;
31+
32+
private static TestEnvVars INSTANCE;
33+
34+
private TestEnvVars(Map<String, String> envVars) {
35+
this.envVars = Map.copyOf(envVars);
36+
}
37+
38+
public static synchronized TestEnvVars read() {
39+
if (INSTANCE == null) {
40+
INSTANCE = new TestEnvVars(readTestEnvVars());
41+
}
42+
return INSTANCE;
43+
}
44+
45+
private static Map<String, String> readTestEnvVars() {
46+
Map<String, String> envVars = new HashMap<>();
47+
Optional<Path> resolvedTestenvProps = candidateTestEnvLocations().filter(Files::exists).findFirst();
48+
resolvedTestenvProps.ifPresent(testenvProps -> {
49+
try (var reader = Files.newBufferedReader(testenvProps)) {
50+
java.util.Properties properties = new java.util.Properties();
51+
properties.load(reader);
52+
for (String name : properties.stringPropertyNames()) {
53+
envVars.put(name, properties.getProperty(name));
54+
}
55+
} catch (IOException e) {
56+
throw new RuntimeException("Failed to read test environment variables", e);
57+
}
58+
});
59+
return envVars;
60+
}
61+
62+
private static Stream<Path> candidateTestEnvLocations() {
63+
Stream.Builder<Path> builder = Stream.builder();
64+
if (System.getProperty("testenv.properties.path") != null) {
65+
builder.add(Path.of(System.getProperty("testenv.properties.path")));
66+
}
67+
builder.add(
68+
Path.of(System.getProperty("user.dir"), "testenv.properties"));
69+
builder.add(
70+
Path.of(System.getProperty("user.dir")).getParent().resolve("testenv.properties"));
71+
return builder.build();
72+
}
73+
74+
public @Nullable String get(String key) {
75+
return envVars.get(key);
76+
}
77+
78+
public String getOrDefault(String key, String defaultValue) {
79+
return envVars.getOrDefault(key, defaultValue);
80+
}
81+
82+
public String getOrThrow(String key) {
83+
String value = envVars.get(key);
84+
if (value == null) {
85+
throw new IllegalArgumentException("Environment variable " + key + " not found");
86+
}
87+
return value;
88+
}
89+
90+
public boolean hasKey(String key) {
91+
return envVars.containsKey(key);
92+
}
93+
}

0 commit comments

Comments
 (0)