Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore .env folder in the .env Config provider #1046

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ protected boolean failOnMissingFile() {
protected abstract ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException;

protected List<ConfigSource> loadConfigSources(final String location, final int ordinal) {
return loadConfigSources(new String[] { location }, ordinal);
return loadConfigSources(location != null ? new String[] { location } : null, ordinal);
}

protected List<ConfigSource> 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<ConfigSource> loadConfigSources(final String[] locations, final int ordinal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -55,4 +57,9 @@ public static List<ConfigSource> dotEnvSources(final ClassLoader classLoader) {
public static List<ConfigSource> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading