Skip to content

Commit

Permalink
SNOW-856231 easy logging - not fail on directory serch error (#805)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
sfc-gh-knozderko authored Oct 30, 2023
1 parent 635b214 commit eb2e0ab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,55 @@ 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
.Setup(e => e.GetFolderPath(Environment.SpecialFolder.UserProfile))
.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
Expand Down
27 changes: 20 additions & 7 deletions Snowflake.Data/Configuration/EasyLoggingConfigFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EasyLoggingConfigFinder>();

internal const string ClientConfigFileName = "sf_client_config.json";
internal const string ClientConfigEnvironmentName = "SF_CLIENT_CONFIG_FILE";

Expand Down Expand Up @@ -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<string> 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;
Expand Down

0 comments on commit eb2e0ab

Please sign in to comment.