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 1 commit
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,6 +126,21 @@ public void TestThatReturnsNullIfNoWayOfGettingTheFile()
Assert.IsNull(filePath);
}

[Test]
public void TestThatReturnsNullIfDirectoryBasedSearchFailsWithUnexpectedError()
sfc-gh-pfus marked this conversation as resolved.
Show resolved Hide resolved
{
// arrange
t_environmentOperations
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
.Setup(e => e.GetFolderPath(Environment.SpecialFolder.UserProfile))
.Throws(() => new Exception("No home directory"));

// act
var filePath = t_finder.FindConfigFilePath(null);
sfc-gh-dstempniak marked this conversation as resolved.
Show resolved Hide resolved

// assert
Assert.IsNull(filePath);
}

private static void MockHomeDirectory()
{
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 @@
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;

Check warning on line 66 in Snowflake.Data/Configuration/EasyLoggingConfigFinder.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Configuration/EasyLoggingConfigFinder.cs#L65-L66

Added lines #L65 - L66 were not covered by tests
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