Skip to content

Commit

Permalink
rename EnvVarSnapshot to ActualValuesSnapshot; move direct usages fro…
Browse files Browse the repository at this point in the history
…m Env inside Parsers;
  • Loading branch information
Philipp-Binder committed Oct 20, 2024
1 parent 9574121 commit dd61524
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
7 changes: 3 additions & 4 deletions src/DotNetEnv/Env.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ private static IEnumerable<KeyValuePair<string, string>> LoadContents(string con
? CreateDictionaryOption.TakeLast
: CreateDictionaryOption.TakeFirst;

Parsers.EnvVarSnapshot =
new ConcurrentDictionary<string, string>(envVarSnapshot.Concat(previousValues)
.ToDotEnvDictionary(dictionaryOption));
var actualValues = new ConcurrentDictionary<string, string>(envVarSnapshot.Concat(previousValues)
.ToDotEnvDictionary(dictionaryOption));

var pairs = Parsers.ParseDotenvFile(contents, options.ClobberExistingVars);
var pairs = Parsers.ParseDotenvFile(contents, options.ClobberExistingVars, actualValues);

// for NoClobber, remove pairs which are exactly contained in previousValues or present in EnvironmentVariables
var unClobberedPairs = (options.ClobberExistingVars
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetEnv/IValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ValueInterpolated (string id)

public string GetValue ()
{
return Parsers.EnvVarSnapshot.TryGetValue(_id, out var val) ? val : string.Empty;
return Parsers.ActualValuesSnapshot.TryGetValue(_id, out var val) ? val : string.Empty;
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/DotNetEnv/Parsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace DotNetEnv
{
class Parsers
{
public static ConcurrentDictionary<string, string> EnvVarSnapshot = new ConcurrentDictionary<string, string>();
public static ConcurrentDictionary<string, string> ActualValuesSnapshot = new ConcurrentDictionary<string, string>();

// helpful blog I discovered only after digging through all the Sprache source myself:
// https://justinpealing.me.uk/post/2020-03-11-sprache1-chars/
Expand Down Expand Up @@ -273,11 +273,11 @@ from _c in Comment.OptionalOrDefault()
from _lt in LineTerminator
select new KeyValuePair<string, string>(null, null));

public static IEnumerable<KeyValuePair<string, string>> ParseDotenvFile (
string contents,
bool clobberExistingVariables = true
)
public static IEnumerable<KeyValuePair<string, string>> ParseDotenvFile(string contents,
bool clobberExistingVariables = true, IDictionary<string, string> actualValues = null)
{
ActualValuesSnapshot = new ConcurrentDictionary<string, string>(actualValues ?? new Dictionary<string, string>());

return Assignment.Select(UpdateEnvVarSnapshot).Or(Empty)
.Many()
.AtEnd()
Expand All @@ -286,8 +286,8 @@ public static IEnumerable<KeyValuePair<string, string>> ParseDotenvFile (

KeyValuePair<string, string> UpdateEnvVarSnapshot(KeyValuePair<string, string> pair)
{
if (clobberExistingVariables || !EnvVarSnapshot.ContainsKey(pair.Key))
EnvVarSnapshot.AddOrUpdate(pair.Key, pair.Value, (key, oldValue) => pair.Value);
if (clobberExistingVariables || !ActualValuesSnapshot.ContainsKey(pair.Key))
ActualValuesSnapshot.AddOrUpdate(pair.Key, pair.Value, (key, oldValue) => pair.Value);

return pair;
}
Expand Down
18 changes: 7 additions & 11 deletions test/DotNetEnv.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,22 @@

namespace DotNetEnv.Tests
{
public class ParserTests : IDisposable
public class ParserTests
{
// C# wow that you can't handle 32 bit unicode as chars. wow. strings for 4 byte chars.
private static readonly string RocketChar = char.ConvertFromUtf32(0x1F680); // 🚀

private const string EXCEPT_CHARS = "'\"$";

private const string EV_TEST = "ENVVAR_TEST";

public ParserTests ()
private readonly IDictionary<string, string> _actualValuesDictionary = new Dictionary<string, string>()
{
Parsers.EnvVarSnapshot = new ConcurrentDictionary<string, string>()
{
[EV_TEST] = "ENV value"
};
}
[EV_TEST] = "ENV value"
};

public void Dispose ()
public ParserTests()
{
Parsers.EnvVarSnapshot.Clear();
Parsers.ActualValuesSnapshot = new ConcurrentDictionary<string, string>(_actualValuesDictionary);
}

[Fact]
Expand Down Expand Up @@ -446,7 +442,7 @@ public void ParseDotenvFile ()
{
void TestParse(KeyValuePair<string, string>[] expecteds, string input)
{
var outputs = Parsers.ParseDotenvFile(input).ToArray();
var outputs = Parsers.ParseDotenvFile(input, actualValues: _actualValuesDictionary).ToArray();
Assert.Equal(expecteds.Length, outputs.Length);

for (var i = 0; i < outputs.Length; i++)
Expand Down

0 comments on commit dd61524

Please sign in to comment.