-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathDslTests.kt
185 lines (155 loc) · 4.45 KB
/
DslTests.kt
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package tests
import io.github.cdimascio.dotenv.Dotenv
import io.github.cdimascio.dotenv.DotenvException
import io.github.cdimascio.dotenv.dotenv
import kotlin.test.*
import org.junit.jupiter.api.Test as test
class DotEnvDslTest {
private val envVars = mapOf(
"MY_TEST_EV1" to "my test ev 1",
"MY_TEST_EV2" to "my test ev 2",
"WITHOUT_VALUE" to "",
"QUOTED_EV1" to "jdbc:hive2://[domain]:10000/default;principal=hive/_HOST@[REALM]",
"MULTI_LINE" to "hello\\nworld"
)
private val envVarsOverridenByHostEnv = mapOf(
"HOME" to "dotenv_test_home"
)
@test
fun dotenvMalformed() {
assertFailsWith<DotenvException> {
dotenv()
}
}
@test
fun dotenvQuotedEv() {
val env = dotenv {
ignoreIfMalformed = true
}
envVars.forEach {
val expected = it.value
val actual = env[it.key]
assertEquals(expected, actual)
}
assertEquals("jdbc:hive2://[domain]:10000/default;principal=hive/_HOST@[REALM]", env["QUOTED_EV1"])
}
@test
fun dotenvIgnoreMalformed() {
val env = dotenv {
ignoreIfMalformed = true
}
envVars.forEach {
val expected = it.value
val actual = env[it.key]
assertEquals(expected, actual)
}
assertHostEnvVar(env)
}
@test
fun resourceRelative() {
val env = dotenv {
directory = "./"
ignoreIfMalformed = true
}
assertEquals("my test ev 1", env["MY_TEST_EV1"])
assertHostEnvVar(env)
}
@test
fun resourceFilename() {
val env = dotenv {
filename = "env"
ignoreIfMalformed = true
}
assertEquals("my test ev 1", env["MY_TEST_EV1"])
assertHostEnvVar(env)
}
@test
fun iterateOverDotenv() {
val env = dotenv {
filename = "env"
ignoreIfMalformed = true
}
for (e in env.entries()) {
assertEquals(env[e.key], e.value)
}
assertHostEnvVar(env)
assertNotEquals(envVarsOverridenByHostEnv["HOME"], env["HOME"])
}
@test
fun iteratorOverDotenvWithFilter() {
val dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load()
val entriesInFile = dotenv.entries(Dotenv.Filter.DECLARED_IN_ENV_FILE)
val entriesAll = dotenv.entries()
assertTrue(entriesInFile.size < entriesAll.size)
for ((key, value) in envVars) {
assertEquals(dotenv[key], value)
}
}
@test
fun multiLine() {
val dotenv = Dotenv.configure()
.ignoreIfMalformed()
.load()
assertEquals(dotenv["MULTI_LINE"]!!, envVars["MULTI_LINE"]!!)
}
@test
fun resourceCurrent() {
val env = dotenv {
ignoreIfMalformed = true
}
assertEquals("my test ev 1", env["MY_TEST_EV1"])
assertHostEnvVar(env)
}
@test
fun dotenvMissing() {
assertFailsWith<DotenvException> {
dotenv {
directory = "/missing/.env"
}
}
}
@test
fun dotenvIgnoreMissing() {
val env = dotenv {
directory = "/missing/.env"
ignoreIfMissing = true
}
assertHostEnvVar(env)
assertNull(env["MY_TEST_EV1"])
}
@test
fun systemProperties() {
val env = dotenv {
ignoreIfMalformed = true
systemProperties = true
}
assertHostEnvVar(env)
assertEquals("my test ev 1", env["MY_TEST_EV1"])
assertEquals("my test ev 1", System.getProperty("MY_TEST_EV1"))
env.entries().forEach {
System.clearProperty(it.key)
}
}
@test
fun noSystemProperties() {
val env = dotenv {
ignoreIfMalformed = true
}
assertHostEnvVar(env)
assertEquals("my test ev 1", env["MY_TEST_EV1"])
assertNull(System.getProperty("MY_TEST_EV1"))
}
private fun assertHostEnvVar(env: Dotenv) {
val isWindows = System.getProperty("os.name").lowercase().indexOf("win") >= 0
if (isWindows) {
val path = env["PATH"]
assertNotNull(path)
} else {
val expectedHome = System.getProperty("user.home")
val actualHome = env["HOME"]
assertEquals(expectedHome, actualHome)
}
}
}