diff --git a/implementation/src/main/java/io/smallrye/config/AbstractLocationConfigSourceLoader.java b/implementation/src/main/java/io/smallrye/config/AbstractLocationConfigSourceLoader.java index f2909d88e..68c7f09ba 100644 --- a/implementation/src/main/java/io/smallrye/config/AbstractLocationConfigSourceLoader.java +++ b/implementation/src/main/java/io/smallrye/config/AbstractLocationConfigSourceLoader.java @@ -80,11 +80,11 @@ protected boolean failOnMissingFile() { protected abstract ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException; protected List loadConfigSources(final String location, final int ordinal) { - return loadConfigSources(new String[] { location }, ordinal); + return loadConfigSources(location != null ? new String[] { location } : null, ordinal); } protected List loadConfigSources(final String location, final int ordinal, final ClassLoader classLoader) { - return loadConfigSources(new String[] { location }, ordinal, classLoader); + return loadConfigSources(location != null ? new String[] { location } : null, ordinal, classLoader); } protected List loadConfigSources(final String[] locations, final int ordinal) { diff --git a/implementation/src/main/java/io/smallrye/config/DotEnvConfigSourceProvider.java b/implementation/src/main/java/io/smallrye/config/DotEnvConfigSourceProvider.java index b3a76c768..7ccff65f6 100644 --- a/implementation/src/main/java/io/smallrye/config/DotEnvConfigSourceProvider.java +++ b/implementation/src/main/java/io/smallrye/config/DotEnvConfigSourceProvider.java @@ -2,6 +2,8 @@ import java.io.IOException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.List; @@ -17,7 +19,7 @@ public class DotEnvConfigSourceProvider extends AbstractLocationConfigSourceLoad private final String location; public DotEnvConfigSourceProvider() { - this(Paths.get(System.getProperty("user.dir"), ".env").toUri().toString()); + this(getDotEnvFile(".env")); } public DotEnvConfigSourceProvider(final String location) { @@ -55,4 +57,9 @@ public static List dotEnvSources(final ClassLoader classLoader) { public static List dotEnvSources(final String location, final ClassLoader classLoader) { return new DotEnvConfigSourceProvider(location).getConfigSources(classLoader); } + + private static String getDotEnvFile(final String filename) { + Path dotEnvFile = Paths.get(System.getProperty("user.dir"), filename); + return Files.isDirectory(dotEnvFile) ? null : dotEnvFile.toUri().toString(); + } } diff --git a/testsuite/extra/src/test/java/io/smallrye/config/test/location/DotEnvTest.java b/testsuite/extra/src/test/java/io/smallrye/config/test/location/DotEnvTest.java new file mode 100644 index 000000000..3b611e988 --- /dev/null +++ b/testsuite/extra/src/test/java/io/smallrye/config/test/location/DotEnvTest.java @@ -0,0 +1,56 @@ +package io.smallrye.config.test.location; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.FileOutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import io.smallrye.config.DotEnvConfigSourceProvider; +import io.smallrye.config.SmallRyeConfig; +import io.smallrye.config.SmallRyeConfigBuilder; + +public class DotEnvTest { + @Test + void dotEnv(@TempDir Path tempDir) throws Exception { + Properties dotEnv = new Properties(); + dotEnv.setProperty("FOO_BAR", "value"); + try (FileOutputStream out = new FileOutputStream(tempDir.resolve(".env").toFile())) { + dotEnv.store(out, null); + } + + String previousUserDir = System.getProperty("user.dir"); + System.setProperty("user.dir", tempDir.toString()); + + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withSources(new DotEnvConfigSourceProvider()) + .build(); + + assertEquals("value", config.getRawValue("foo.bar")); + + System.setProperty("user.dir", previousUserDir); + } + + @Test + void dotEnvFolder(@TempDir Path tempDir) throws Exception { + Path dotEnvFolder = tempDir.resolve(".env"); + Files.createDirectories(dotEnvFolder); + Files.createDirectories(dotEnvFolder.resolve("foo")); + + String previousUserDir = System.getProperty("user.dir"); + System.setProperty("user.dir", tempDir.toString()); + + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withSources(new DotEnvConfigSourceProvider()) + .build(); + + assertNull(config.getRawValue("foo.bar")); + + System.setProperty("user.dir", previousUserDir); + } +}