-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
DotenvTests.java
116 lines (94 loc) · 3.33 KB
/
DotenvTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package tests;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvEntry;
import io.github.cdimascio.dotenv.DotenvException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
class DotenvTests {
private Map<String, String> envVars;
@BeforeEach
void setUp() {
envVars = new HashMap<>();
envVars.put("MY_TEST_EV1", "my test ev 1");
envVars.put("MY_TEST_EV2", "my test ev 2");
envVars.put("WITHOUT_VALUE", "");
}
@Test
void throwIfMalconfigured() {
assertThrows(DotenvException.class, () -> Dotenv.configure().load());
}
@Test
void load() {
assertThrows(DotenvException.class, () -> {
final var dotenv = Dotenv.load();
envVars.keySet().forEach(envName -> assertEquals(envVars.get(envName), dotenv.get(envName)));
});
}
@Test
void iteratorOverDotenv() {
final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
dotenv
.entries()
.forEach(e -> assertEquals(dotenv.get(e.getKey()), e.getValue()));
for (final var e : dotenv.entries()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
}
@Test
void iteratorOverDotenvWithFilter() {
final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
final var entriesInFile = dotenv.entries(Dotenv.Filter.DECLARED_IN_ENV_FILE);
final var entriesAll = dotenv.entries();
assertTrue(entriesInFile.size() < entriesAll.size());
for (final var e: envVars.entrySet()) {
assertEquals(dotenv.get(e.getKey()), e.getValue());
}
}
@Test
void failToRemoveFromDotenv() {
final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
assertThrows(UnsupportedOperationException.class, () -> iterateEntries(dotenv));
}
private static void iterateEntries(final Dotenv dotenv) {
final var iter = dotenv.entries().iterator();
while (iter.hasNext()) {
iter.next();
iter.remove();
}
}
@Test
void failToAddToDotenv() {
final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
final var entries = dotenv.entries();
assertThrows(UnsupportedOperationException.class, () -> entries.add(new DotenvEntry("new", "value")));
}
@Test
void configureWithIgnoreMalformed() {
final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
for (final var envName : envVars.keySet()) {
assertEquals(envVars.get(envName), dotenv.get(envName));
}
}
@Test
void configureWithIgnoreMissingAndMalformed() {
final var dotenv = Dotenv.configure()
.directory("/missing/dir")
.ignoreIfMalformed()
.ignoreIfMissing()
.load();
assertNotNull(dotenv.get("PATH"));
}
@Test
void malformedWithUncloseQuote() {
final var dotenv = Dotenv.configure()
.directory("/unclosed.quote")
.ignoreIfMalformed()
.load();
assertNull(dotenv.get("FOO"));
assertEquals(dotenv.get("BAR"), "bar");
assertNull(dotenv.get("BAZ"), "baz");
}
}