Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/dotenv.net/DotEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,37 @@ namespace dotenv.net
public static class DotEnv
{
/// <summary>
/// Initialize the fluent configuration API
/// </summary>
public static DotEnvOptions Config()
{
return new DotEnvOptions();
}

/// <summary>
/// Configure the environment variables from a .env file
/// [Deprecated] Configure the environment variables from a .env file
/// </summary>
/// <param name="options">Options on how to load the env file</param>
[Obsolete]
[Obsolete(
"This method would be removed in the next major release. Use the Fluent API, Load() or Read() methods instead.")]
public static void Config(DotEnvOptions options)
{
Helpers.ReadAndWrite(options);
}

/// <summary>
/// Searches the current directory and three directories up and loads the environment variables
/// [Deprecated] Searches the current directory and three directories up and loads the environment variables
/// </summary>
/// <param name="levelsToSearch">The number of top-level directories to search; the default is 4 top-level directories.</param>
/// <returns>States whether or not the operation succeeded</returns>
[Obsolete]
public static bool AutoConfig(int levelsToSearch = 4)
[Obsolete(
"This method would be removed in the next major release. Use the Fluent API, Load() or Read() methods instead.")]
public static bool AutoConfig(int levelsToSearch = DotEnvOptions.DefaultProbeDepth)
{
Helpers.ReadAndWrite(new DotEnvOptions(probeDirectoryDepth: levelsToSearch));
return true;
}

/// <summary>
/// Initialize the fluent configuration API
/// </summary>
public static DotEnvOptions Fluent()
{
return new();
}

/// <summary>
/// Read and return the values in the provided env files
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/dotenv.net/DotEnvOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace dotenv.net
public class DotEnvOptions
{
public const string DefaultEnvFileName = ".env";
private const int DefaultProbeDepth = 4;
public const int DefaultProbeDepth = 4;

/// <summary>
/// A value to state whether to throw or swallow exceptions. The default is true. <see cref="T:dotenv.net.DotEnvOptions"/>
Expand All @@ -24,7 +24,7 @@ public class DotEnvOptions
public Encoding Encoding { get; private set; }

/// <summary>
/// A value to state whether or not to trim whitespace from the values retrieved. The default is true. <see cref="T:dotenv.net.DotEnvOptions"/>
/// A value to state whether or not to trim whitespace from the values retrieved. The default is false. <see cref="T:dotenv.net.DotEnvOptions"/>
/// </summary>
public bool TrimValues { get; private set; }

Expand Down
25 changes: 14 additions & 11 deletions src/dotenv.net/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace dotenv.net
{
internal static class Parser
{
private static readonly char[] SingleQuote = {'\''};
private static readonly char[] DoubleQuotes = {'"'};

internal static ReadOnlySpan<KeyValuePair<string, string>> Parse(ReadOnlySpan<string> dotEnvRows,
bool shouldTrimValue)
{
Expand Down Expand Up @@ -39,25 +42,25 @@ private static bool HasNoKey(this ReadOnlySpan<char> row, out int index)
return index <= 0;
}

private static bool IsQuoted(this ReadOnlySpan<char> row) => (row.StartsWith(SingleQuote) && row.EndsWith(SingleQuote))
|| (row.StartsWith(DoubleQuotes) && row.EndsWith(DoubleQuotes));

private static ReadOnlySpan<char> StripQuotes(this ReadOnlySpan<char> row) => row.Trim('\'').Trim('\"');

private static string Key(this ReadOnlySpan<char> row, int index)
{
var untrimmedKey = row.Slice(0, index);
return untrimmedKey.Trim().ToString();
}

private static string Value(this ReadOnlySpan<char> row, int index, bool trimValue = false)
private static string Value(this ReadOnlySpan<char> row, int index, bool trimValue)
{
var untrimmedValue = row.Slice(index + 1);
var value = untrimmedValue.ToString();

var value = row.Slice(index + 1);

// handle quoted values
if (value.StartsWith("'") && value.EndsWith("'"))
{
value = value.Trim('\'');
}
else if (value.StartsWith("\"") && value.EndsWith("\""))
if (value.IsQuoted())
{
value = value.Trim('\"');
value = value.StripQuotes();
}

// trim output if requested
Expand All @@ -66,7 +69,7 @@ private static string Value(this ReadOnlySpan<char> row, int index, bool trimVal
value = value.Trim();
}

return value;
return value.ToString();
}
}
}
143 changes: 143 additions & 0 deletions tests/dotenv.net.Tests/DotEnv.Fluent.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System;
using System.IO;
using System.Text;
using dotenv.net.Utilities;
using FluentAssertions;
using Xunit;

namespace dotenv.net.Tests
{
public class DotEnvFluentTests
{
private const string WhitespacesEnvFileName = "whitespaces.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 ConfigShouldThrowWithNonExistentEnvAndTrackedExceptions()
{
var action = new Action(() => DotEnv.Fluent()
.WithExceptions()
.WithEnvFiles(NonExistentEnvFileName)
.Load());

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

[Fact]
public void ConfigShouldLoadEnvWithProvidedEncoding()
{
DotEnv.Fluent()
.WithEncoding(Encoding.ASCII)
.WithEnvFiles(AsciiEnvFileName)
.Load();

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

[Fact]
public void ConfigShouldLoadEnvWithTrimOptions()
{
DotEnv.Fluent()
.WithEnvFiles(WhitespacesEnvFileName)
.WithTrimValues()
.Load();

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

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

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

[Fact]
public void ConfigShouldLoadEnvWithExistingVarOverwriteOptions()
{
Environment.SetEnvironmentVariable("Generic", "Existing");

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

EnvReader.GetStringValue("Generic")
.Should()
.Be("Existing");

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

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

[Fact]
public void ConfigShouldLoadDefaultEnvWithProbeOptions()
{
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_QUOTES")
.Should()
.Be("double");
EnvReader.GetStringValue("SINGLE_QUOTES")
.Should()
.Be("single");
}

[Fact]
public void ConfigShouldLoadEnvWithInvalidEnvEntries()
{
DotEnv.Fluent()
.WithEnvFiles(IncompleteEnvFileName)
.WithoutTrimValues()
.Load();

EnvReader.HasValue("KeyWithNoValue")
.Should()
.BeFalse();
}
}
}
Loading