Skip to content

Commit

Permalink
SNOW-990111: Refactor getting home directory
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Jan 26, 2024
1 parent 16335ab commit 0f266fc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void TestThatThrowsErrorWhenLogPathAndHomeDirectoryIsNotSet()

// assert
Assert.IsNotNull(thrown);
Assert.AreEqual(thrown.InnerException.Message, "No log path found for easy logging. Home directory is not configured and log path is not provided");
Assert.AreEqual(thrown.Message, "No log path found for easy logging. Home directory is not configured and log path is not provided");
}

[Test]
Expand All @@ -121,7 +121,7 @@ public void TestThatThrowsErrorWhenLogPathIsNotSetAndHomeDirectoryThrowsAnExcept

// assert
Assert.IsNotNull(thrown);
Assert.AreEqual(thrown.Message, $"Error while trying to retrieve the home directory: {ex}");
Assert.AreEqual(thrown.Message, "No log path found for easy logging. Home directory is not configured and log path is not provided");
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion Snowflake.Data/Configuration/EasyLoggingConfigFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private string GetFilePathFromInputParameter(string filePath, string inputDescri
return filePath;
}

private string GetHomeDirectory() =>_environmentOperations.GetFolderPath(Environment.SpecialFolder.UserProfile);
private string GetHomeDirectory() => HomeDirectoryProvider.HomeDirectory(_environmentOperations);

private string GetFilePathFromDriverLocation() => SearchForConfigInDirectory(() => ".", "driver");

Expand Down
15 changes: 3 additions & 12 deletions Snowflake.Data/Core/Session/EasyLoggingStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,10 @@ private string GetLogPath(string logPath)
if (string.IsNullOrEmpty(logPath))
{
s_logger.Warn("LogPath in client config not found. Using home directory as a default value");
try
logPathOrDefault = HomeDirectoryProvider.HomeDirectory(_environmentOperations);
if (string.IsNullOrEmpty(logPathOrDefault))
{
logPathOrDefault = _environmentOperations.GetFolderPath(Environment.SpecialFolder.UserProfile);
if (string.IsNullOrEmpty(logPathOrDefault))
{
throw new Exception("No log path found for easy logging. Home directory is not configured and log path is not provided");
}
}
catch(Exception e)
{
var errorMessage = $"Error while trying to retrieve the home directory: {e}";
s_logger.Error(errorMessage);
throw new Exception(errorMessage, e);
throw new Exception("No log path found for easy logging. Home directory is not configured and log path is not provided");
}
}
var pathWithDotnetSubdirectory = Path.Combine(logPathOrDefault, "dotnet");
Expand Down
31 changes: 31 additions & 0 deletions Snowflake.Data/Core/Tools/HomeDirectoryProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
*/

using Snowflake.Data.Log;
using System;

namespace Snowflake.Data.Core.Tools
{
internal class HomeDirectoryProvider
{
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<HomeDirectoryProvider>();

public static string HomeDirectory(EnvironmentOperations _environmentOperations) {
try
{
var homeDirectory = _environmentOperations.GetFolderPath(Environment.SpecialFolder.UserProfile);
if (string.IsNullOrEmpty(homeDirectory) || homeDirectory.Equals("/"))
{
return null;
}
return homeDirectory;
}
catch (Exception e)
{
s_logger.Error($"Error while trying to retrieve the home directory: {e}");
return null;
}
}
}
}

0 comments on commit 0f266fc

Please sign in to comment.