Skip to content

Commit

Permalink
(fix) dotenv entries and entry filter
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Sep 19, 2020
1 parent 691e1c7 commit b222562
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>

<licenses>
<license>
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,20 @@ public DotenvImpl load() throws DotenvException {
}

public static class DotenvImpl implements Dotenv {
private final Map<String, String> map;
private final Map<String, String> envVars;
private final Set<DotenvEntry> set;
private final Set<DotenvEntry> setInFile;
private final Map<String, String> envVarsInFile;
public DotenvImpl(List<DotenvEntry> envVars) {
this.map = envVars.stream().collect(toMap(DotenvEntry::getKey, DotenvEntry::getValue));
this.set = buildEntries().entrySet().stream()
this.envVarsInFile = envVars.stream().collect(toMap(DotenvEntry::getKey, DotenvEntry::getValue));
this.envVars = new HashMap<>(this.envVarsInFile);
System.getenv().forEach(this.envVars::put);

this.set =this.envVars.entrySet().stream()
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
.collect(toUnmodifiableSet());

this.setInFile =this.envVarsInFile.entrySet().stream()
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
.collect(toUnmodifiableSet());
}
Expand All @@ -91,27 +100,20 @@ public Set<DotenvEntry> entries() {

@Override
public Set<DotenvEntry> entries(EntriesFilter filter) {
return this.map.entrySet().stream()
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
.collect(toUnmodifiableSet());
if (filter != null) return setInFile;
return entries();
}

@Override
public String get(String key) {
var value = System.getenv(key);
return value != null ? value : map.get(key);
return value != null ? value : envVars.get(key);
}

@Override
public String get(String key, String defaultValue) {
var value = this.get(key);
return value != null ? value : defaultValue;
}

private Map<String, String> buildEntries() {
var envMap = new HashMap<String, String>();
System.getenv().forEach(envMap::put);
return envMap;
}
}
}
144 changes: 144 additions & 0 deletions src/test/java/tests/BasicTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package tests;

import io.github.cdimascio.dotenv.DotenvException;
import io.github.cdimascio.dotenv.Dotenv;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

public class BasicTests {
private Map<String, String> envVars = new HashMap<>() {{
put("MY_TEST_EV1", "my test ev 1");
put("MY_TEST_EV2", "my test ev 2");
put("WITHOUT_VALUE", "");
put("MULTI_LINE", "hello\\nworld");
}};

@Test(expected = DotenvException.class)
public void dotenvMalformed() {
Dotenv.configure()
.directory("./src/test/resources")
.load();
}

@Test
public void dotenvIgnoreMalformed() {
var dotenv = Dotenv.configure()
.directory("./src/test/resources")
.ignoreIfMalformed()
.load();

envVars.forEach((key, expected) -> {
var actual = dotenv.get(key);
assertEquals(expected, actual);
});

assertHostEnvVar(dotenv);
}

@Test
public void dotenvFilename() {
var dotenv = Dotenv.configure()
.directory("./src/test/resources")
.filename("env")
.ignoreIfMalformed()
.load();

envVars.forEach((key, expected) -> {
var actual = dotenv.get(key);
assertEquals(expected, actual);
});

assertHostEnvVar(dotenv);
}

@Test
public void resourceRelative() {
var dotenv = Dotenv.configure()
.directory("./")
.ignoreIfMalformed()
.load();
assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));

assertHostEnvVar(dotenv);
}

@Test
public void resourceCurrent() {
var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();
assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));

assertHostEnvVar(dotenv);
}

@Test
public void systemProperties() {
var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.systemProperties()
.load();

assertHostEnvVar(dotenv);
assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
assertEquals("my test ev 1", System.getProperty("MY_TEST_EV1"));
dotenv.entries().forEach(entry -> System.clearProperty(entry.getKey()));
}

@Test
public void noSystemProperties() {
var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();

assertHostEnvVar(dotenv);
assertEquals("my test ev 1", dotenv.get("MY_TEST_EV1"));
assertNull(System.getProperty("MY_TEST_EV1"));
}

@Test
public void iterateOverDotenv() {
var dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load();

for (var e : dotenv.entries()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
}

@Test(expected = DotenvException.class)
public void dotenvMissing() {
Dotenv.configure()
.directory("/missing/.env")
.load();
}

@Test
public void dotenvIgnoreMissing() {
var dotenv = Dotenv.configure()
.directory("/missing/.env")
.ignoreIfMissing()
.load();

assertHostEnvVar(dotenv);

assertNull(dotenv.get("MY_TEST_EV1"));
}

private void assertHostEnvVar(Dotenv env) {
var isWindows = System.getProperty("os.name").toLowerCase().contains("win");
if (isWindows) {
var path = env.get("PATH");
assertNotNull(path);
} else {
var expectedHome = System.getProperty("user.home");
var actualHome = env.get("HOME");
assertEquals(expectedHome, actualHome);
}
}
}

0 comments on commit b222562

Please sign in to comment.