|
| 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