Skip to content

Commit 7a37f0d

Browse files
authored
Merge pull request #97 from rogusdev/noenvvar-interpolation
store fakeenvvar dict for interpolation in NoEnvVars() mode
2 parents f2eed72 + 15ed596 commit 7a37f0d

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed

src/DotNetEnv/Env.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class Env
1010
{
1111
public const string DEFAULT_ENVFILENAME = ".env";
1212

13+
public static Dictionary<string, string> FakeEnvVars = new Dictionary<string, string>();
14+
1315
public static IEnumerable<KeyValuePair<string, string>> LoadMulti (string[] paths, LoadOptions options = null)
1416
{
1517
return paths.Aggregate(

src/DotNetEnv/IValue.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public ValueInterpolated (string id)
2121

2222
public string GetValue ()
2323
{
24-
return Environment.GetEnvironmentVariable(_id) ?? string.Empty;
24+
var val = Environment.GetEnvironmentVariable(_id);
25+
return val ?? (Env.FakeEnvVars.TryGetValue(_id, out val) ? val : string.Empty);
2526
}
2627
}
2728

src/DotNetEnv/Parsers.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public static KeyValuePair<string, string> SetEnvVar (KeyValuePair<string, strin
1616

1717
public static KeyValuePair<string, string> DoNotSetEnvVar (KeyValuePair<string, string> kvp)
1818
{
19+
if (Env.FakeEnvVars.ContainsKey(kvp.Key)) {
20+
Env.FakeEnvVars.Remove(kvp.Key);
21+
}
22+
Env.FakeEnvVars.Add(kvp.Key, kvp.Value);
1923
return kvp;
2024
}
2125

test/DotNetEnv.Tests/EnvConfigurationTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public void AddSourceToBuilderAndParseInterpolatedTest()
125125

126126
// Have to remove since it's recursive and can be set by the `EnvTests.cs`
127127
Environment.SetEnvironmentVariable("TEST4", null);
128+
Env.FakeEnvVars.Clear();
128129

129130
configuration = new ConfigurationBuilder()
130131
.AddDotNetEnv("./.env_embedded")

test/DotNetEnv.Tests/EnvTests.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,59 @@ public class EnvTests
1212
{
1313
private static bool IsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
1414

15+
private static string[] OldEnvVars = new string[]
16+
{
17+
"NAME",
18+
"EMPTY",
19+
"QUOTE",
20+
"URL",
21+
"CONNECTION",
22+
"WHITEBOTH",
23+
"SSL_CERT",
24+
"IP",
25+
"PORT",
26+
"DOMAIN",
27+
"EMBEDEXPORT",
28+
"COMMENTLEAD",
29+
"WHITELEAD",
30+
"UNICODE",
31+
"CASING",
32+
33+
"TEST",
34+
"TEST1",
35+
"TEST2",
36+
"TEST3",
37+
"TEST4",
38+
"TEST5_DOUBLE",
39+
"TEST5_SINGLE",
40+
"TEST5_UNQUOTED",
41+
"TEST_UNQUOTED_WITH_INTERPOLATED_SURROUNDING_SPACES",
42+
"FIRST_KEY",
43+
"SECOND_KEY",
44+
"THIRD_KEY",
45+
"FOURTH_KEY",
46+
"GROUP_FILTER_REGEX",
47+
"DOLLAR1_U",
48+
"DOLLAR2_U",
49+
"DOLLAR3_U",
50+
"DOLLAR4_U",
51+
"DOLLAR1_S",
52+
"DOLLAR2_S",
53+
"DOLLAR3_S",
54+
"DOLLAR4_S",
55+
"DOLLAR1_D",
56+
"DOLLAR2_D",
57+
"DOLLAR3_D",
58+
"DOLLAR4_D",
59+
};
60+
61+
public EnvTests() {
62+
// Clear all env vars set from normal interpolated test
63+
for (var i = 0; i < OldEnvVars.Length; i++) {
64+
Environment.SetEnvironmentVariable(OldEnvVars[i], null);
65+
}
66+
}
67+
1568
[Fact]
1669
public void LoadTest()
1770
{
@@ -169,6 +222,7 @@ public void ParseInterpolatedTest()
169222
System.Environment.SetEnvironmentVariable("EXISTING_ENVIRONMENT_VARIABLE", "value");
170223
System.Environment.SetEnvironmentVariable("DNE_VAR", null);
171224
DotNetEnv.Env.Load("./.env_embedded");
225+
172226
Assert.Equal("test", Environment.GetEnvironmentVariable("TEST"));
173227
Assert.Equal("test1", Environment.GetEnvironmentVariable("TEST1"));
174228
Assert.Equal("test", Environment.GetEnvironmentVariable("TEST2"));
@@ -207,6 +261,56 @@ public void ParseInterpolatedTest()
207261
Assert.Equal("value$$", Environment.GetEnvironmentVariable("DOLLAR4_D"));
208262
}
209263

264+
[Fact]
265+
public void ParseInterpolatedNoEnvVarsTest()
266+
{
267+
System.Environment.SetEnvironmentVariable("EXISTING_ENVIRONMENT_VARIABLE", "value");
268+
System.Environment.SetEnvironmentVariable("DNE_VAR", null);
269+
var environmentDictionary = DotNetEnv.Env.NoEnvVars().Load("./.env_embedded").ToDotEnvDictionary();
270+
271+
Assert.Equal("test", environmentDictionary["TEST"]);
272+
Assert.Equal("test1", environmentDictionary["TEST1"]);
273+
Assert.Equal("test", environmentDictionary["TEST2"]);
274+
Assert.Equal("testtest", environmentDictionary["TEST3"]);
275+
Assert.Equal("testtest1", environmentDictionary["TEST4"]);
276+
277+
Assert.Equal("test:testtest1 $$ '\" ® and test1", environmentDictionary["TEST5_DOUBLE"]);
278+
Assert.Equal("$TEST:$TEST4 \\$\\$ \" \\uae and $TEST1", environmentDictionary["TEST5_SINGLE"]);
279+
Assert.Equal("test:testtest1\\uaeandtest1", environmentDictionary["TEST5_UNQUOTED"]);
280+
281+
// note that interpolated values will keep whitespace! (as they should, esp if surrounding them with other values)
282+
Assert.Equal(" surrounded by spaces ", environmentDictionary["TEST_UNQUOTED_WITH_INTERPOLATED_SURROUNDING_SPACES"]);
283+
284+
Assert.Equal("value1", environmentDictionary["FIRST_KEY"]);
285+
Assert.Equal("value2andvalue1", environmentDictionary["SECOND_KEY"]);
286+
// EXISTING_ENVIRONMENT_VARIABLE already set to "value"
287+
Assert.Equal("value;andvalue3", environmentDictionary["THIRD_KEY"]);
288+
// DNE_VAR does not exist (has no value)
289+
Assert.Equal(";nope", environmentDictionary["FOURTH_KEY"]);
290+
291+
Assert.Equal("^((?!Everyone).)*$", environmentDictionary["GROUP_FILTER_REGEX"]);
292+
293+
Assert.Equal("value$", environmentDictionary["DOLLAR1_U"]);
294+
Assert.Equal("valuevalue$$", environmentDictionary["DOLLAR2_U"]);
295+
Assert.Equal("value$.$", environmentDictionary["DOLLAR3_U"]);
296+
Assert.Equal("value$$", environmentDictionary["DOLLAR4_U"]);
297+
298+
Assert.Equal("value$", environmentDictionary["DOLLAR1_S"]);
299+
Assert.Equal("value$DOLLAR1_S$", environmentDictionary["DOLLAR2_S"]);
300+
Assert.Equal("value$.$", environmentDictionary["DOLLAR3_S"]);
301+
Assert.Equal("value$$", environmentDictionary["DOLLAR4_S"]);
302+
303+
Assert.Equal("value$", environmentDictionary["DOLLAR1_D"]);
304+
Assert.Equal("valuevalue$$", environmentDictionary["DOLLAR2_D"]);
305+
Assert.Equal("value$.$", environmentDictionary["DOLLAR3_D"]);
306+
Assert.Equal("value$$", environmentDictionary["DOLLAR4_D"]);
307+
308+
// Validate that the env vars are still not set
309+
for (var i = 0; i < OldEnvVars.Length; i++) {
310+
Assert.Null(Environment.GetEnvironmentVariable(OldEnvVars[i]));
311+
}
312+
}
313+
210314
[Fact]
211315
public void QuotedHashTest()
212316
{

0 commit comments

Comments
 (0)