diff --git a/Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigFinderTest.cs b/Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigFinderTest.cs index 07260fea9..c73feab9a 100644 --- a/Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigFinderTest.cs +++ b/Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigFinderTest.cs @@ -126,6 +126,34 @@ public void TestThatReturnsNullIfNoWayOfGettingTheFile() Assert.IsNull(filePath); } + [Test] + public void TestThatDoesNotFailWhenSearchForOneOfDirectoriesFails() + { + // arrange + MockHomeDirectoryFails(); + + // act + var filePath = t_finder.FindConfigFilePath(null); + + // assert + Assert.IsNull(filePath); + t_environmentOperations.Verify(e => e.GetFolderPath(Environment.SpecialFolder.UserProfile), Times.Once); + } + + [Test] + public void TestThatDoesNotFailWhenOneOfDirectoriesNotDefined() + { + // arrange + MockHomeDirectoryReturnsNull(); + + // act + var filePath = t_finder.FindConfigFilePath(null); + + // assert + Assert.IsNull(filePath); + t_environmentOperations.Verify(e => e.GetFolderPath(Environment.SpecialFolder.UserProfile), Times.Once); + } + private static void MockHomeDirectory() { t_environmentOperations @@ -133,6 +161,20 @@ private static void MockHomeDirectory() .Returns(HomeDirectory); } + private static void MockHomeDirectoryFails() + { + t_environmentOperations + .Setup(e => e.GetFolderPath(Environment.SpecialFolder.UserProfile)) + .Throws(() => new Exception("No home directory")); + } + + private static void MockHomeDirectoryReturnsNull() + { + t_environmentOperations + .Setup(e => e.GetFolderPath(Environment.SpecialFolder.UserProfile)) + .Returns((string) null); + } + private static void MockFileFromEnvironmentalVariable() { t_environmentOperations diff --git a/Snowflake.Data/Configuration/EasyLoggingConfigFinder.cs b/Snowflake.Data/Configuration/EasyLoggingConfigFinder.cs index 625da5e06..1d0c375b3 100644 --- a/Snowflake.Data/Configuration/EasyLoggingConfigFinder.cs +++ b/Snowflake.Data/Configuration/EasyLoggingConfigFinder.cs @@ -5,11 +5,14 @@ using System; using System.IO; using Snowflake.Data.Core.Tools; +using Snowflake.Data.Log; namespace Snowflake.Data.Configuration { internal class EasyLoggingConfigFinder { + private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); + internal const string ClientConfigFileName = "sf_client_config.json"; internal const string ClientConfigEnvironmentName = "SF_CLIENT_CONFIG_FILE"; @@ -43,24 +46,34 @@ private string GetFilePathEnvironmentVariable() return GetFilePathFromInputParameter(filePath); } - private string GetFilePathFromTempDirectory() => SearchForConfigInDirectory(Path.GetTempPath()); + private string GetFilePathFromTempDirectory() => SearchForConfigInDirectory(Path.GetTempPath, "temp"); - private string GetFilePathFromHomeDirectory() => SearchForConfigInDirectory(GetHomeDirectory()); + private string GetFilePathFromHomeDirectory() => SearchForConfigInDirectory(GetHomeDirectory, "home"); private string GetFilePathFromInputParameter(string filePath) => string.IsNullOrEmpty(filePath) ? null : filePath; private string GetHomeDirectory() =>_environmentOperations.GetFolderPath(Environment.SpecialFolder.UserProfile); - private string GetFilePathFromDriverLocation() => SearchForConfigInDirectory("."); + private string GetFilePathFromDriverLocation() => SearchForConfigInDirectory(() => ".", "driver"); - private string SearchForConfigInDirectory(string directory) + private string SearchForConfigInDirectory(Func directoryProvider, string directoryDescription) { - if (string.IsNullOrEmpty(directory)) + try + { + var directory = directoryProvider.Invoke(); + if (string.IsNullOrEmpty(directory)) + { + return null; + } + + var filePath = Path.Combine(directory, ClientConfigFileName); + return OnlyIfFileExists(filePath); + } + catch (Exception e) { + s_logger.Error($"Error while searching for the client config in {directoryDescription} directory: {e}"); return null; } - var filePath = Path.Combine(directory, ClientConfigFileName); - return OnlyIfFileExists(filePath); } private string OnlyIfFileExists(string filePath) => _fileOperations.Exists(filePath) ? filePath : null;