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
36 changes: 18 additions & 18 deletions src/dotenv.net/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ internal static class Reader
{
internal static ReadOnlySpan<string> Read(string envFilePath, bool ignoreExceptions, Encoding encoding)
{
try
{
if (string.IsNullOrWhiteSpace(envFilePath))
{
throw new ArgumentException("The file path cannot be null, empty or whitespace.", nameof(envFilePath));
}
var defaultResponse = ReadOnlySpan<string>.Empty;

// if configured to throw errors then throw otherwise return
if (!File.Exists(envFilePath))
// if configured to throw errors then throw otherwise return
if (string.IsNullOrWhiteSpace(envFilePath))
{
if (ignoreExceptions)
{
throw new FileNotFoundException(
$"An environment file with path \"{envFilePath}\" does not exist.");
return defaultResponse;
}

// default to UTF8 if encoding is not provided
encoding ??= Encoding.UTF8;

// read all lines from the env file
return new ReadOnlySpan<string>(File.ReadAllLines(envFilePath, encoding));
throw new ArgumentException("The file path cannot be null, empty or whitespace.", nameof(envFilePath));
}
catch (Exception)

// if configured to throw errors then throw otherwise return
if (!File.Exists(envFilePath))
{
if (ignoreExceptions)
{
return ReadOnlySpan<string>.Empty;
return defaultResponse;
}

throw;
throw new FileNotFoundException($"A file with provided path \"{envFilePath}\" does not exist.");
}

// default to UTF8 if encoding is not provided
encoding ??= Encoding.UTF8;

// read all lines from the env file
return new ReadOnlySpan<string>(File.ReadAllLines(envFilePath, encoding));
}
}
}
37 changes: 27 additions & 10 deletions src/dotenv.net/Utilities/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ internal static IDictionary<string, string> ReadAndReturn(DotEnvOptions options)
{
var response = new Dictionary<string, string>();
var envFilePaths = options.ProbeForEnv
? new[] {GetProbedEnvPath(options.ProbeLevelsToSearch)}
? new[] {GetProbedEnvPath(options.ProbeLevelsToSearch, options.IgnoreExceptions)}
: options.EnvFilePaths;

foreach (var envFilePath in envFilePaths)
{
var envRows = ReadAndParse(envFilePath, options.IgnoreExceptions, options.Encoding,
options.TrimValues);

foreach (var envRow in envRows)
{
if (response.ContainsKey(envRow.Key))
Expand Down Expand Up @@ -64,22 +65,38 @@ internal static void ReadAndWrite(DotEnvOptions options)
}
}

private static string GetProbedEnvPath(int levelsToSearch)
private static string GetProbedEnvPath(int levelsToSearch, bool ignoreExceptions)
{
var currentDirectory = new DirectoryInfo(AppContext.BaseDirectory);
var count = levelsToSearch;
var foundEnvPath = SearchPaths();

if (string.IsNullOrEmpty(foundEnvPath) && !ignoreExceptions)
{
throw new FileNotFoundException(
$"Failed to find a file matching the '{DotEnvOptions.DefaultEnvFileName}' search pattern." +
$"{Environment.NewLine}Current Directory: {currentDirectory}" +
$"{Environment.NewLine}Levels Searched: {levelsToSearch}");
}

return foundEnvPath;

for (;
currentDirectory != null && levelsToSearch > 0;
levelsToSearch--, currentDirectory = currentDirectory.Parent)

string SearchPaths()
{
foreach (var file in currentDirectory.GetFiles(DotEnvOptions.DefaultEnvFileName,
SearchOption.TopDirectoryOnly))
for (;
currentDirectory != null && count > 0;
count--, currentDirectory = currentDirectory.Parent)
{
return file.FullName;
foreach (var file in currentDirectory.GetFiles(DotEnvOptions.DefaultEnvFileName,
SearchOption.TopDirectoryOnly))
{
return file.FullName;
}
}
}

return null;
return null;
}
}
}
}
6 changes: 3 additions & 3 deletions src/dotenv.net/dotenv.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<PackageLicenseUrl>https://github.com/bolorundurowb/dotenv.net/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/bolorundurowb/dotenv.net</RepositoryUrl>
<LangVersion>default</LangVersion>
<PackageVersion>3.0.0</PackageVersion>
<PackageVersion>3.1.0</PackageVersion>
<RepositoryType>git</RepositoryType>
<PackageTags>dotnet, environment, variables, env, dotenv, net core, autofac</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>3.0.0</AssemblyVersion>
<FileVersion>3.0.0</FileVersion>
<AssemblyVersion>3.1.0</AssemblyVersion>
<FileVersion>3.1.0</FileVersion>
<NeutralLanguage>en-NG</NeutralLanguage>
<TargetFrameworks>net5.0;net5.0-windows;netstandard1.6;netstandard2.0;netstandard2.1</TargetFrameworks>
<Title>dotenv.net</Title>
Expand Down
2 changes: 1 addition & 1 deletion tests/dotenv.net.Tests/DotEnv.Fluent.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void ConfigShouldLoadDefaultEnvWithProbeOptions()
.Load());

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

action = () => DotEnv.Fluent()
.WithProbeForEnv(5)
Expand Down
2 changes: 1 addition & 1 deletion tests/dotenv.net.Tests/DotEnv.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void ConfigShouldLoadDefaultEnvWithProbeOptions()
var action = new Action(() => DotEnv.Config(new DotEnvOptions(probeForEnv: true, probeLevelsToSearch: 2, ignoreExceptions: false)));

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

action = () => DotEnv.Config(new DotEnvOptions(probeForEnv: true, probeLevelsToSearch: 5, ignoreExceptions: false));

Expand Down
6 changes: 4 additions & 2 deletions tests/dotenv.net.Tests/dotenv.net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<PackageVersion>3.0.0</PackageVersion>
<PackageVersion></PackageVersion>
<AssemblyVersion>3.1.0</AssemblyVersion>
<FileVersion>3.1.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down