From eb2e0abbfee327b2169adcea7b3a84c96949b022 Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Mon, 30 Oct 2023 16:03:34 +0100 Subject: [PATCH] SNOW-856231 easy logging - not fail on directory serch error (#805) ### Description Prevent to fail if client config file could not be found because of an unexpected error during the phase of searching in predefined directory. It could happen if for example there is no home folder in the operating system. ### Checklist - [x] Code compiles correctly - [x] Code is formatted according to [Coding Conventions](../CodingConventions.md) - [x] Created tests which fail without the change (if possible) - [x] All tests passing (`dotnet test`) - [x] Extended the README / documentation, if necessary - [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name --- .../EasyLoggingConfigFinderTest.cs | 42 +++++++++++++++++++ .../Configuration/EasyLoggingConfigFinder.cs | 27 ++++++++---- 2 files changed, 62 insertions(+), 7 deletions(-) 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;