Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-856231 easy logging - not fail on directory serch error #805

Merged
merged 3 commits into from
Oct 30, 2023
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
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);
sfc-gh-dstempniak marked this conversation as resolved.
Show resolved Hide resolved

// 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)
sfc-gh-pfus marked this conversation as resolved.
Show resolved Hide resolved
{
if (string.IsNullOrEmpty(directory))
try
{
var directory = directoryProvider.Invoke();
if (string.IsNullOrEmpty(directory))
{
return null;
sfc-gh-mhofman marked this conversation as resolved.
Show resolved Hide resolved
}

var filePath = Path.Combine(directory, ClientConfigFileName);
return OnlyIfFileExists(filePath);
sfc-gh-dstempniak marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading