Skip to content

Commit f896bea

Browse files
rename EnvVarSnapshot to ActualValuesSnapshot; move direct usages from Env inside Parsers;
1 parent 17ed70d commit f896bea

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

src/DotNetEnv/Env.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ private static IEnumerable<KeyValuePair<string, string>> LoadContents(string con
9191
? CreateDictionaryOption.TakeLast
9292
: CreateDictionaryOption.TakeFirst;
9393

94-
Parsers.EnvVarSnapshot =
95-
new ConcurrentDictionary<string, string>(envVarSnapshot.Concat(previousValues)
96-
.ToDotEnvDictionary(dictionaryOption));
94+
var actualValues = new ConcurrentDictionary<string, string>(envVarSnapshot.Concat(previousValues)
95+
.ToDotEnvDictionary(dictionaryOption));
9796

98-
var pairs = Parsers.ParseDotenvFile(contents, options.ClobberExistingVars);
97+
var pairs = Parsers.ParseDotenvFile(contents, options.ClobberExistingVars, actualValues);
9998

10099
// for NoClobber, remove pairs which are exactly contained in previousValues or present in EnvironmentVariables
101100
var unClobberedPairs = (options.ClobberExistingVars

src/DotNetEnv/IValue.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public ValueInterpolated (string id)
1919

2020
public string GetValue ()
2121
{
22-
return Parsers.EnvVarSnapshot.TryGetValue(_id, out var val) ? val : string.Empty;
22+
return Parsers.ActualValuesSnapshot.TryGetValue(_id, out var val) ? val : string.Empty;
2323
}
2424
}
2525

src/DotNetEnv/Parsers.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace DotNetEnv
1212
{
1313
class Parsers
1414
{
15-
public static ConcurrentDictionary<string, string> EnvVarSnapshot = new ConcurrentDictionary<string, string>();
15+
public static ConcurrentDictionary<string, string> ActualValuesSnapshot = new ConcurrentDictionary<string, string>();
1616

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

263-
public static IEnumerable<KeyValuePair<string, string>> ParseDotenvFile (
264-
string contents,
265-
bool clobberExistingVariables = true
266-
)
263+
public static IEnumerable<KeyValuePair<string, string>> ParseDotenvFile(string contents,
264+
bool clobberExistingVariables = true, IDictionary<string, string> actualValues = null)
267265
{
266+
ActualValuesSnapshot = new ConcurrentDictionary<string, string>(actualValues ?? new Dictionary<string, string>());
267+
268268
return Assignment.Select(UpdateEnvVarSnapshot).Or(Empty)
269269
.Many()
270270
.AtEnd()
@@ -273,8 +273,8 @@ public static IEnumerable<KeyValuePair<string, string>> ParseDotenvFile (
273273

274274
KeyValuePair<string, string> UpdateEnvVarSnapshot(KeyValuePair<string, string> pair)
275275
{
276-
if (clobberExistingVariables || !EnvVarSnapshot.ContainsKey(pair.Key))
277-
EnvVarSnapshot.AddOrUpdate(pair.Key, pair.Value, (key, oldValue) => pair.Value);
276+
if (clobberExistingVariables || !ActualValuesSnapshot.ContainsKey(pair.Key))
277+
ActualValuesSnapshot.AddOrUpdate(pair.Key, pair.Value, (key, oldValue) => pair.Value);
278278

279279
return pair;
280280
}

test/DotNetEnv.Tests/ParserTests.cs

+7-11
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,22 @@
1010

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

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

2020
private const string EV_TEST = "ENVVAR_TEST";
21-
22-
public ParserTests ()
21+
private readonly IDictionary<string, string> _actualValuesDictionary = new Dictionary<string, string>()
2322
{
24-
Parsers.EnvVarSnapshot = new ConcurrentDictionary<string, string>()
25-
{
26-
[EV_TEST] = "ENV value"
27-
};
28-
}
23+
[EV_TEST] = "ENV value"
24+
};
2925

30-
public void Dispose ()
26+
public ParserTests()
3127
{
32-
Parsers.EnvVarSnapshot.Clear();
28+
Parsers.ActualValuesSnapshot = new ConcurrentDictionary<string, string>(_actualValuesDictionary);
3329
}
3430

3531
[Fact]
@@ -441,7 +437,7 @@ public void ParseDotenvFile ()
441437
{
442438
void TestParse(KeyValuePair<string, string>[] expecteds, string input)
443439
{
444-
var outputs = Parsers.ParseDotenvFile(input).ToArray();
440+
var outputs = Parsers.ParseDotenvFile(input, actualValues: _actualValuesDictionary).ToArray();
445441
Assert.Equal(expecteds.Length, outputs.Length);
446442

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

0 commit comments

Comments
 (0)