@@ -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