Add support for .env-test file in new test framework (#36857)

Closes #36839

Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
Stian Thorgersen 2025-01-28 10:09:24 +01:00 committed by GitHub
parent 1912602a5a
commit 8e072e91f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 4 deletions

1
.gitignore vendored
View File

@ -96,3 +96,4 @@ quarkus/data/*.db
.java-version
.env
.env-test

View File

@ -254,7 +254,7 @@ There are a few options on how to configure the test framework, with the followi
* System properties
* Environment variables
* `.env` file in the current working directory
* `.env-test` file in the project hierarchy
* A properties file specified with `kc.test.config` system property or `KC_TEST_CONFIG` environment variable
### Using system properties
@ -291,17 +291,20 @@ KC_TEST_BROWSER=firefox mvn test
As with system properties, using environment variables within the IDE can be cumbersome.
### Using `.env` file
### Using `.env-test` file
When running tests from an IDE using the `.env` file is very convenient, especially as this can be added to `.gitignore`
When running tests from an IDE using the `.env-test` file is very convenient, especially as this can be added to `.gitignore`
allowing developers to quickly have their own personal preference when running tests.
Example `.env` file:
Example `.env-test` file:
```
KC_TEST_BROWSER=firefox
```
For multi-modal Maven projects the `.env-test` file can be located in the current module, or one of its parent modules.
This allows sharing configuration across multiple test modules.
### Using a properties file
Using a property file allows creating a set of configuration which can be committed to a Git repository to be shareable.

View File

@ -3,15 +3,23 @@ package org.keycloak.testframework.config;
import io.quarkus.runtime.configuration.CharsetConverter;
import io.quarkus.runtime.configuration.InetSocketAddressConverter;
import io.quarkus.runtime.configuration.MemorySizeConverter;
import io.smallrye.config.DotEnvConfigSourceProvider;
import io.smallrye.config.EnvConfigSource;
import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.common.utils.ConfigSourceUtil;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;
import org.keycloak.testframework.injection.ValueTypeAlias;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Config {
@ -44,6 +52,11 @@ public class Config {
.withConverters(new Converter[]{ new CharsetConverter(), new MemorySizeConverter(), new InetSocketAddressConverter() })
.withInterceptors(new LogConfigInterceptor());
ConfigSource testEnvConfigSource = initTestEnvConfigSource();
if (testEnvConfigSource != null) {
configBuilder.withSources(testEnvConfigSource);
}
ConfigSource testConfigSource = initTestConfigSource();
if (testConfigSource != null) {
configBuilder.withSources(testConfigSource);
@ -52,6 +65,25 @@ public class Config {
return configBuilder.build();
}
private static ConfigSource initTestEnvConfigSource() {
Path currentPath = Paths.get(System.getProperty("user.dir"));
while (Files.isDirectory(currentPath)) {
Path envTestPath = currentPath.resolve(".env-test");
if (Files.isRegularFile(envTestPath)) {
try {
return new EnvConfigSource(ConfigSourceUtil.urlToMap(envTestPath.toUri().toURL()), 350);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
currentPath = currentPath.getParent();
if (!Files.isRegularFile(currentPath.resolve("pom.xml"))) {
break;
}
}
return null;
}
private static ConfigSource initTestConfigSource() {
try {
URL testConfig;