Skip to content

Commit

Permalink
remove extraneous this. and add code block syntaxes
Browse files Browse the repository at this point in the history
  • Loading branch information
rogusdev committed Jul 30, 2024
1 parent 63de4aa commit 172e139
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 63 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ DotNetEnv.Env.TraversePath().Load();

Integrating with the usual ConfigurationBuilder used in .NET is simple!

```
```csharp
var configuration = new ConfigurationBuilder()
.AddDotNetEnv(".env", LoadOptions.TraversePath()) // Simply add the DotNetEnv configuration source!
.Build();
Expand All @@ -180,15 +180,15 @@ The configuration provider will map `__` as `:` to allow sections!
All lines must be valid assignments or empty lines (with optional comments).

A minimal valid assignment looks like:
```
```sh
KEY=value
```

There can optionally be one of a few export or equivalent keywords at the beginning
and there can be a comment at the end, values can be quoted to include whitespace,
and interpolated references can be included (unquoted values as well as double quoted,
with optional braces in both cases -- but often more useful in unquoted), like:
```
```sh
export KEY="extra $ENVVAR value" # comment
set KEY2=extra${ENVVAR}value # comment
```
Expand Down Expand Up @@ -216,19 +216,19 @@ As these are the options bash recognizes. However, while bash does have

As a special note: if a value is unquoted, it can still include a `#` char,
which might look like it is starting a comment, like:
```
```sh
KEY=value#notcomment #actualcomment
```

This is how bash works as well:
```
```sh
export TEST=value#notcomment #actualcomment
env | grep TEST
# TEST=value#notcomment
```

However, unlike bash, a `#` directly after the `=` will be recognized as a comment:
```
```sh
KEY=#yesacomment
```

Expand All @@ -252,15 +252,15 @@ Capitalization on the hex chars is irrelevant, and leading zeroes are optional.
And standard escaped chars like `\t`, `\\``, `\n`, etc are also recognized
-- though quoted strings can also be multi line, e.g.:

```
```sh
KEY="value
and more"
OTHER='#not_comment
line2'
```

Loaded gives:
```
```csharp
"value\nand more" == System.Environment.GetEnvironmentVariable("KEY")
"#not_comment\nline2" == System.Environment.GetEnvironmentVariable("OTHER")
```
Expand All @@ -269,13 +269,13 @@ You can also include whitespace before and after the equals sign in assignments,
between the name/identifier, and the value, quoted or unquoted.
Note that the pre/trailing and post/leading whitespace will be ignored.
If you want leading whitepace on your values, quote them with whitespace.
```
```sh
WHITE_BOTH = value
WHITE_QUOTED=" value "
```

Loaded gives:
```
```csharp
"value" == System.Environment.GetEnvironmentVariable("WHITE_BOTH")
" value " == System.Environment.GetEnvironmentVariable("WHITE_QUOTED")
```
Expand Down
10 changes: 5 additions & 5 deletions src/DotNetEnv/Configuration/EnvConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public EnvConfigurationProvider(
public override void Load()
{
IEnumerable<KeyValuePair<string, string>> values;
if (this.paths == null)
if (paths == null)
{
values = Env.Load(options: this.options);
values = Env.Load(options: options);
}
else
{
if (this.paths.Length == 1)
if (paths.Length == 1)
{
values = Env.Load(this.paths[0], this.options);
values = Env.Load(paths[0], options);
}
else
{
values = Env.LoadMulti(this.paths, this.options);
values = Env.LoadMulti(paths, options);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/DotNetEnv/Configuration/EnvConfigurationSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public EnvConfigurationSource(

public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new EnvConfigurationProvider(this.paths, this.options);
return new EnvConfigurationProvider(paths, options);
}
}
}
91 changes: 45 additions & 46 deletions test/DotNetEnv.Tests/EnvConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class EnvConfigurationTests : IDisposable
private const string EV_TEST_1 = "EV_TEST_1";
private const string EV_TEST_2 = "EV_TEST_2";

private Dictionary<string,string> oldEnvvars = new Dictionary<string,string>();
private readonly Dictionary<string,string> oldEnvvars = new();
private static readonly string[] ALL_EVS = { EV_TEST, EV_DNE, EV_TEST_1, EV_TEST_2 };

public EnvConfigurationTests ()
Expand All @@ -45,74 +45,73 @@ public void Dispose()
}
}


[Fact]
public void AddSourceToBuilderAndLoad()
{
this.configuration = new ConfigurationBuilder()
configuration = new ConfigurationBuilder()
.AddDotNetEnv(options: LoadOptions.NoEnvVars())
.Build();

Assert.Empty(this.configuration["EMPTY"]);
Assert.Equal("'", this.configuration["QUOTE"]);
Assert.Equal("https://github.com/tonerdo", this.configuration["URL"]);
Assert.Equal("user=test;password=secret", this.configuration["CONNECTION"]);
Assert.Equal(" leading and trailing white space ", this.configuration["WHITEBOTH"]);
Assert.Equal("SPECIAL STUFF---\nLONG-BASE64\\ignore\"slash", this.configuration["SSL_CERT"]);
Assert.Empty(configuration["EMPTY"]);
Assert.Equal("'", configuration["QUOTE"]);
Assert.Equal("https://github.com/tonerdo", configuration["URL"]);
Assert.Equal("user=test;password=secret", configuration["CONNECTION"]);
Assert.Equal(" leading and trailing white space ", configuration["WHITEBOTH"]);
Assert.Equal("SPECIAL STUFF---\nLONG-BASE64\\ignore\"slash", configuration["SSL_CERT"]);
}

[Fact]
public void AddSourceToBuilderAndLoadDotenvHigherSkip()
{
// ./DotNetEnv.Tests/bin/Debug/netcoreapp3.1/DotNetEnv.Tests.dll -- get to the ./ (root of `test` folder)
this.configuration = new ConfigurationBuilder()
configuration = new ConfigurationBuilder()
.AddDotNetEnv("../../../../", LoadOptions.NoEnvVars())
.Build();

Assert.Null(this.configuration["NAME"]);
Assert.Equal("here", this.configuration["TEST"]);
Assert.Null(configuration["NAME"]);
Assert.Equal("here", configuration["TEST"]);
}

[Fact]
public void AddSourceToBuilderAndLoadMulti()
{
this.configuration = new ConfigurationBuilder()
configuration = new ConfigurationBuilder()
.AddDotNetEnvMulti(new[] { "./.env", "./.env2" }, LoadOptions.NoEnvVars())
.Build();

Assert.Equal("Other", this.configuration["NAME"]);
Assert.Equal("Other", configuration["NAME"]);
Assert.Equal("overridden_2", configuration["ENVVAR_TEST"]);
}

[Fact]
public void AddSourceToBuilderAndLoadMultiWithNoClobber()
{
this.configuration = new ConfigurationBuilder()
configuration = new ConfigurationBuilder()
.AddDotNetEnvMulti(new[] { "./.env", "./.env2" }, LoadOptions.NoEnvVars().NoClobber())
.Build();

Assert.Equal("Toni", this.configuration["NAME"]);
Assert.Equal("Toni", configuration["NAME"]);
Assert.Equal("ENV value", configuration["ENVVAR_TEST"]);
}

[Fact]
public void AddSourceToBuilderAndFileDoesNotExist()
{
this.configuration = new ConfigurationBuilder()
configuration = new ConfigurationBuilder()
.AddDotNetEnv("./.env_DNE", LoadOptions.NoEnvVars())
.Build();

Assert.Empty(this.configuration.AsEnumerable());
Assert.Empty(configuration.AsEnumerable());
}

[Fact]
public void AddSourceToBuilderAndGetSection()
{
this.configuration = new ConfigurationBuilder()
configuration = new ConfigurationBuilder()
.AddDotNetEnv("./.env_sections", LoadOptions.NoEnvVars())
.Build();

var section = this.configuration.GetSection("SECTION");
var section = configuration.GetSection("SECTION");

Assert.Equal("value1", section["Key1"]);
Assert.Equal("value2", section["Key2"]);
Expand All @@ -127,43 +126,43 @@ public void AddSourceToBuilderAndParseInterpolatedTest()
// Have to remove since it's recursive and can be set by the `EnvTests.cs`
Environment.SetEnvironmentVariable("TEST4", null);

this.configuration = new ConfigurationBuilder()
configuration = new ConfigurationBuilder()
.AddDotNetEnv("./.env_embedded")
.Build();

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

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

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

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

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

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

Assert.Equal("value$", this.configuration["DOLLAR1_D"]);
Assert.Equal("valuevalue$$", this.configuration["DOLLAR2_D"]);
Assert.Equal("value$.$", this.configuration["DOLLAR3_D"]);
Assert.Equal("value$$", this.configuration["DOLLAR4_D"]);
Assert.Equal("value$", configuration["DOLLAR1_D"]);
Assert.Equal("valuevalue$$", configuration["DOLLAR2_D"]);
Assert.Equal("value$.$", configuration["DOLLAR3_D"]);
Assert.Equal("value$$", configuration["DOLLAR4_D"]);
}
}
}
2 changes: 1 addition & 1 deletion test/DotNetEnv.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ParserTests : IDisposable
private const string EV_TEST_1 = "EV_TEST_1";
private const string EV_TEST_2 = "EV_TEST_2";

private Dictionary<string,string> oldEnvvars = new Dictionary<string,string>();
private readonly Dictionary<string,string> oldEnvvars = new();
private static readonly string[] ALL_EVS = { EV_TEST, EV_DNE, EV_TEST_1, EV_TEST_2 };

public ParserTests ()
Expand Down

0 comments on commit 172e139

Please sign in to comment.