Skip to content

Commit

Permalink
ft: add fluent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Winner-Timothy Bolorunduro committed Mar 18, 2021
1 parent d27ba65 commit d43ac23
Showing 1 changed file with 88 additions and 39 deletions.
127 changes: 88 additions & 39 deletions tests/dotenv.net.Tests/DotEnv.Fluent.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,85 +10,134 @@ namespace dotenv.net.Tests
public class DotEnvFluentTests
{
private const string WhitespacesEnvFileName = "whitespaces.env";
private const string WhitespacesCopyEnvFileName = "values-with-whitespaces-too.env";
private const string ValuesAndCommentsEnvFileName = "values-and-comments.env";
private const string NonExistentEnvFileName = "non-existent.env";
private const string QuotationsEnvFileName = "quotations.env";
private const string AsciiEnvFileName = "ascii.env";
private const string GenericEnvFileName = "generic.env";
private const string IncompleteEnvFileName = "incomplete.env";

[Fact]
public void Config_ShouldInitializeEnvOptions_WithDefaultOptions()
public void ConfigShouldThrowWithNonExistentEnvAndTrackedExceptions()
{
var config = DotEnv.Fluent();

config.Encoding
.Should()
.Be(Encoding.UTF8);
}

[Fact]
public void Config_ShouldNotLoadEnv_WithDefaultOptions_AsThereIsNoEnvFile()
{
var action = new Action(() => DotEnv.Config(new DotEnvOptions(ignoreExceptions: false)));
var action = new Action(() => DotEnv.Fluent()
.WithExceptions()
.WithEnvFiles(NonExistentEnvFileName)
.Load());

action.Should()
.ThrowExactly<FileNotFoundException>();
}

[Fact]
public void Config_ShouldLoadEnv_WithProbeEnvOptions()
public void ConfigShouldLoadEnvWithProvidedEncoding()
{
DotEnv.Config(new DotEnvOptions(probeForEnv: true));
DotEnv.Fluent()
.WithEncoding(Encoding.ASCII)
.WithEnvFiles(AsciiEnvFileName)
.Load();

EnvReader.GetStringValue("hello")
EnvReader.GetStringValue("ENCODING")
.Should()
.Be("world");
.Be("ASCII");
}

[Fact]
public void AutoConfig_ShouldLocateAndLoadEnv()
public void ConfigShouldLoadEnvWithTrimOptions()
{
var success = DotEnv.AutoConfig();
DotEnv.Fluent()
.WithEnvFiles(WhitespacesEnvFileName)
.WithTrimValues()
.Load();

EnvReader.GetStringValue("DB_DATABASE")
.Should()
.Be("laravel");

DotEnv.Fluent()
.WithEnvFiles(WhitespacesEnvFileName)
.WithoutTrimValues()
.Load();

success.Should().BeTrue();
EnvReader.GetStringValue("uniquekey")
EnvReader.GetStringValue("DB_DATABASE")
.Should()
.Be("kjdjkd");
.Be(" laravel ");
}

[Fact]
public void Read_Should_ReturnTheReadValues()
public void ConfigShouldLoadEnvWithExistingVarOverwriteOptions()
{
var values =
DotEnv.Read(new DotEnvOptions(trimValues: true, envFilePaths: new[] {WhitespacesEnvFileName}));
Environment.SetEnvironmentVariable("Generic", "Existing");

DotEnv.Fluent()
.WithEnvFiles(GenericEnvFileName)
.WithoutOverwriteExistingVars()
.Load();

values.Count
EnvReader.GetStringValue("Generic")
.Should()
.BeGreaterThan(0);
values["DB_DATABASE"]
.Be("Existing");

DotEnv.Fluent()
.WithEnvFiles(GenericEnvFileName)
.WithOverwriteExistingVars()
.Load();

EnvReader.GetStringValue("Generic")
.Should()
.Be("laravel");
.Be("Value");
}

[Fact]
public void Read_Should_ThrowAnException_WithEmptyFileNameAndConfig()
public void ConfigShouldLoadDefaultEnvWithProbeOptions()
{
var action = new Action(() =>
DotEnv.Read(new DotEnvOptions(ignoreExceptions: false, envFilePaths: new[] {string.Empty})));
var action = new Action(() => DotEnv.Fluent()
.WithProbeForEnv(2)
.WithExceptions()
.Load());

action.Should()
.ThrowExactly<ArgumentException>();

action = () => DotEnv.Fluent()
.WithProbeForEnv(5)
.WithExceptions()
.Load();

action.Should()
.NotThrow();

EnvReader.GetStringValue("hello")
.Should()
.Be("world");
}

[Fact]
public void ConfigShouldLoadEnvWithQuotedValues()
{
DotEnv.Fluent()
.WithEnvFiles(QuotationsEnvFileName)
.WithTrimValues()
.Load();

EnvReader.GetStringValue("DOUBLE")
.Should()
.Be("double");
EnvReader.GetStringValue("SINGLE")
.Should()
.Be("single");
}

[Fact]
public void Load_Should_IgnoreFieldsThatHaveExistingValues_WithConfig()
public void ConfigShouldLoadEnvWithInvalidEnvEntries()
{
Environment.SetEnvironmentVariable("me", "whoIam");
DotEnv.Load(new DotEnvOptions(overwriteExistingVars: false,
envFilePaths: new[] {ValuesAndCommentsEnvFileName}));
DotEnv.Fluent()
.WithEnvFiles(IncompleteEnvFileName)
.WithoutTrimValues()
.Load();

EnvReader.GetStringValue("me")
EnvReader.HasValue("KeyWithNoValue")
.Should()
.Be("whoIam");
.BeFalse();
}
}
}

0 comments on commit d43ac23

Please sign in to comment.