Skip to content

Commit

Permalink
Merge pull request #97 from rogusdev/noenvvar-interpolation
Browse files Browse the repository at this point in the history
store fakeenvvar dict for interpolation in NoEnvVars() mode
  • Loading branch information
rogusdev authored Jul 30, 2024
2 parents f2eed72 + 15ed596 commit 7a37f0d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/DotNetEnv/Env.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class Env
{
public const string DEFAULT_ENVFILENAME = ".env";

public static Dictionary<string, string> FakeEnvVars = new Dictionary<string, string>();

public static IEnumerable<KeyValuePair<string, string>> LoadMulti (string[] paths, LoadOptions options = null)
{
return paths.Aggregate(
Expand Down
3 changes: 2 additions & 1 deletion src/DotNetEnv/IValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public ValueInterpolated (string id)

public string GetValue ()
{
return Environment.GetEnvironmentVariable(_id) ?? string.Empty;
var val = Environment.GetEnvironmentVariable(_id);
return val ?? (Env.FakeEnvVars.TryGetValue(_id, out val) ? val : string.Empty);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/DotNetEnv/Parsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public static KeyValuePair<string, string> SetEnvVar (KeyValuePair<string, strin

public static KeyValuePair<string, string> DoNotSetEnvVar (KeyValuePair<string, string> kvp)
{
if (Env.FakeEnvVars.ContainsKey(kvp.Key)) {
Env.FakeEnvVars.Remove(kvp.Key);
}
Env.FakeEnvVars.Add(kvp.Key, kvp.Value);
return kvp;
}

Expand Down
1 change: 1 addition & 0 deletions test/DotNetEnv.Tests/EnvConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void AddSourceToBuilderAndParseInterpolatedTest()

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

configuration = new ConfigurationBuilder()
.AddDotNetEnv("./.env_embedded")
Expand Down
104 changes: 104 additions & 0 deletions test/DotNetEnv.Tests/EnvTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,59 @@ public class EnvTests
{
private static bool IsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

private static string[] OldEnvVars = new string[]
{
"NAME",
"EMPTY",
"QUOTE",
"URL",
"CONNECTION",
"WHITEBOTH",
"SSL_CERT",
"IP",
"PORT",
"DOMAIN",
"EMBEDEXPORT",
"COMMENTLEAD",
"WHITELEAD",
"UNICODE",
"CASING",

"TEST",
"TEST1",
"TEST2",
"TEST3",
"TEST4",
"TEST5_DOUBLE",
"TEST5_SINGLE",
"TEST5_UNQUOTED",
"TEST_UNQUOTED_WITH_INTERPOLATED_SURROUNDING_SPACES",
"FIRST_KEY",
"SECOND_KEY",
"THIRD_KEY",
"FOURTH_KEY",
"GROUP_FILTER_REGEX",
"DOLLAR1_U",
"DOLLAR2_U",
"DOLLAR3_U",
"DOLLAR4_U",
"DOLLAR1_S",
"DOLLAR2_S",
"DOLLAR3_S",
"DOLLAR4_S",
"DOLLAR1_D",
"DOLLAR2_D",
"DOLLAR3_D",
"DOLLAR4_D",
};

public EnvTests() {
// Clear all env vars set from normal interpolated test
for (var i = 0; i < OldEnvVars.Length; i++) {
Environment.SetEnvironmentVariable(OldEnvVars[i], null);
}
}

[Fact]
public void LoadTest()
{
Expand Down Expand Up @@ -169,6 +222,7 @@ public void ParseInterpolatedTest()
System.Environment.SetEnvironmentVariable("EXISTING_ENVIRONMENT_VARIABLE", "value");
System.Environment.SetEnvironmentVariable("DNE_VAR", null);
DotNetEnv.Env.Load("./.env_embedded");

Assert.Equal("test", Environment.GetEnvironmentVariable("TEST"));
Assert.Equal("test1", Environment.GetEnvironmentVariable("TEST1"));
Assert.Equal("test", Environment.GetEnvironmentVariable("TEST2"));
Expand Down Expand Up @@ -207,6 +261,56 @@ public void ParseInterpolatedTest()
Assert.Equal("value$$", Environment.GetEnvironmentVariable("DOLLAR4_D"));
}

[Fact]
public void ParseInterpolatedNoEnvVarsTest()
{
System.Environment.SetEnvironmentVariable("EXISTING_ENVIRONMENT_VARIABLE", "value");
System.Environment.SetEnvironmentVariable("DNE_VAR", null);
var environmentDictionary = DotNetEnv.Env.NoEnvVars().Load("./.env_embedded").ToDotEnvDictionary();

Assert.Equal("test", environmentDictionary["TEST"]);
Assert.Equal("test1", environmentDictionary["TEST1"]);
Assert.Equal("test", environmentDictionary["TEST2"]);
Assert.Equal("testtest", environmentDictionary["TEST3"]);
Assert.Equal("testtest1", environmentDictionary["TEST4"]);

Assert.Equal("test:testtest1 $$ '\" ® and test1", environmentDictionary["TEST5_DOUBLE"]);
Assert.Equal("$TEST:$TEST4 \\$\\$ \" \\uae and $TEST1", environmentDictionary["TEST5_SINGLE"]);
Assert.Equal("test:testtest1\\uaeandtest1", environmentDictionary["TEST5_UNQUOTED"]);

// note that interpolated values will keep whitespace! (as they should, esp if surrounding them with other values)
Assert.Equal(" surrounded by spaces ", environmentDictionary["TEST_UNQUOTED_WITH_INTERPOLATED_SURROUNDING_SPACES"]);

Assert.Equal("value1", environmentDictionary["FIRST_KEY"]);
Assert.Equal("value2andvalue1", environmentDictionary["SECOND_KEY"]);
// EXISTING_ENVIRONMENT_VARIABLE already set to "value"
Assert.Equal("value;andvalue3", environmentDictionary["THIRD_KEY"]);
// DNE_VAR does not exist (has no value)
Assert.Equal(";nope", environmentDictionary["FOURTH_KEY"]);

Assert.Equal("^((?!Everyone).)*$", environmentDictionary["GROUP_FILTER_REGEX"]);

Assert.Equal("value$", environmentDictionary["DOLLAR1_U"]);
Assert.Equal("valuevalue$$", environmentDictionary["DOLLAR2_U"]);
Assert.Equal("value$.$", environmentDictionary["DOLLAR3_U"]);
Assert.Equal("value$$", environmentDictionary["DOLLAR4_U"]);

Assert.Equal("value$", environmentDictionary["DOLLAR1_S"]);
Assert.Equal("value$DOLLAR1_S$", environmentDictionary["DOLLAR2_S"]);
Assert.Equal("value$.$", environmentDictionary["DOLLAR3_S"]);
Assert.Equal("value$$", environmentDictionary["DOLLAR4_S"]);

Assert.Equal("value$", environmentDictionary["DOLLAR1_D"]);
Assert.Equal("valuevalue$$", environmentDictionary["DOLLAR2_D"]);
Assert.Equal("value$.$", environmentDictionary["DOLLAR3_D"]);
Assert.Equal("value$$", environmentDictionary["DOLLAR4_D"]);

// Validate that the env vars are still not set
for (var i = 0; i < OldEnvVars.Length; i++) {
Assert.Null(Environment.GetEnvironmentVariable(OldEnvVars[i]));
}
}

[Fact]
public void QuotedHashTest()
{
Expand Down

0 comments on commit 7a37f0d

Please sign in to comment.