diff --git a/src/dotenv.net/Reader.cs b/src/dotenv.net/Reader.cs index 726b8fa..c6d365f 100644 --- a/src/dotenv.net/Reader.cs +++ b/src/dotenv.net/Reader.cs @@ -8,35 +8,35 @@ internal static class Reader { internal static ReadOnlySpan 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.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(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.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(File.ReadAllLines(envFilePath, encoding)); } } } \ No newline at end of file diff --git a/src/dotenv.net/Utilities/Helpers.cs b/src/dotenv.net/Utilities/Helpers.cs index 2bdd815..465b428 100644 --- a/src/dotenv.net/Utilities/Helpers.cs +++ b/src/dotenv.net/Utilities/Helpers.cs @@ -24,13 +24,14 @@ internal static IDictionary ReadAndReturn(DotEnvOptions options) { var response = new Dictionary(); 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)) @@ -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; + } } } } \ No newline at end of file diff --git a/src/dotenv.net/dotenv.net.csproj b/src/dotenv.net/dotenv.net.csproj index ed5fbd6..03a452c 100644 --- a/src/dotenv.net/dotenv.net.csproj +++ b/src/dotenv.net/dotenv.net.csproj @@ -14,12 +14,12 @@ https://github.com/bolorundurowb/dotenv.net/blob/master/LICENSE https://github.com/bolorundurowb/dotenv.net default - 3.0.0 + 3.1.0 git dotnet, environment, variables, env, dotenv, net core, autofac true - 3.0.0 - 3.0.0 + 3.1.0 + 3.1.0 en-NG net5.0;net5.0-windows;netstandard1.6;netstandard2.0;netstandard2.1 dotenv.net diff --git a/tests/dotenv.net.Tests/DotEnv.Fluent.Tests.cs b/tests/dotenv.net.Tests/DotEnv.Fluent.Tests.cs index 6a3ef55..da1d058 100644 --- a/tests/dotenv.net.Tests/DotEnv.Fluent.Tests.cs +++ b/tests/dotenv.net.Tests/DotEnv.Fluent.Tests.cs @@ -96,7 +96,7 @@ public void ConfigShouldLoadDefaultEnvWithProbeOptions() .Load()); action.Should() - .ThrowExactly(); + .ThrowExactly(); action = () => DotEnv.Fluent() .WithProbeForEnv(5) diff --git a/tests/dotenv.net.Tests/DotEnv.Tests.cs b/tests/dotenv.net.Tests/DotEnv.Tests.cs index ec2609f..0607254 100644 --- a/tests/dotenv.net.Tests/DotEnv.Tests.cs +++ b/tests/dotenv.net.Tests/DotEnv.Tests.cs @@ -75,7 +75,7 @@ public void ConfigShouldLoadDefaultEnvWithProbeOptions() var action = new Action(() => DotEnv.Config(new DotEnvOptions(probeForEnv: true, probeLevelsToSearch: 2, ignoreExceptions: false))); action.Should() - .ThrowExactly(); + .ThrowExactly(); action = () => DotEnv.Config(new DotEnvOptions(probeForEnv: true, probeLevelsToSearch: 5, ignoreExceptions: false)); diff --git a/tests/dotenv.net.Tests/dotenv.net.Tests.csproj b/tests/dotenv.net.Tests/dotenv.net.Tests.csproj index acf43a2..3df3993 100644 --- a/tests/dotenv.net.Tests/dotenv.net.Tests.csproj +++ b/tests/dotenv.net.Tests/dotenv.net.Tests.csproj @@ -2,7 +2,9 @@ net5.0 false - 3.0.0 + + 3.1.0 + 3.1.0 @@ -10,7 +12,7 @@ all - + all