Skip to content

Commit

Permalink
incorporate feedback continued
Browse files Browse the repository at this point in the history
  • Loading branch information
ssparach committed Sep 18, 2024
1 parent 692eee5 commit 01e723c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics;
using FileExplorerGitIntegration.Models;

namespace FileExplorerGitIntegration.UnitTest;
Expand Down Expand Up @@ -52,21 +53,21 @@ public void GetDistributionNamePositiveTest(string repositoryPath, string value)
}

[TestMethod]
[DataRow("C:\\Distribution\\home\\user\\testRepo", "the repository path must be a valid wsl path")]
[DataRow("\\Ubuntu-18.04\\wsl$\\home\\user\\testRepo", "the repository path must be a valid wsl path")]
[DataRow("wslg\\Ubuntu-18.04\\wsl.localhost\\home\\user\\testRepo", "the repository path must be a valid wsl path")]
[DataRow("", "Value cannot be null")]
[DataRow("\\wsl$", "Failed to get the distribution name from the repository path")]
public void GetDistributionNameNegativeTest(string repositoryPath, string value)
[DataRow("C:\\Distribution\\home\\user\\testRepo")]
[DataRow("\\Ubuntu-18.04\\wsl$\\home\\user\\testRepo")]
[DataRow("wslg\\Ubuntu-18.04\\wsl.localhost\\home\\user\\testRepo")]
[DataRow("")]
[DataRow("\\wsl$")]
public void GetDistributionNameNegativeTest(string repositoryPath)
{
try
Trace.Listeners.Clear();
if (string.IsNullOrEmpty(repositoryPath))
{
var distributionName = WslIntegrator.GetWslDistributionName(repositoryPath);
Assert.AreEqual(value, distributionName);
Assert.ThrowsException<ArgumentNullException>(() => WslIntegrator.GetWslDistributionName(repositoryPath));
}
catch (Exception ex)
else
{
Assert.IsTrue(ex.Message.Contains(value));
Assert.ThrowsException<ArgumentException>(() => WslIntegrator.GetWslDistributionName(repositoryPath));
}
}

Expand All @@ -84,20 +85,20 @@ public void GetWorkingDirectoryPositiveTest(string repositoryPath, string value)
}

[TestMethod]
[DataRow("C:\\Distribution\\home\\user\\testRepo", "the repository path must be a valid wsl path")]
[DataRow("\\Ubuntu-18.04\\wsl$\\home\\user\\testRepo", "the repository path must be a valid wsl path")]
[DataRow("wslg\\Ubuntu-18.04\\wsl.localhost\\home\\user\\testRepo", "the repository path must be a valid wsl path")]
[DataRow("", "Value cannot be null")]
public void GetWorkingDirectoryNegativeTest(string repositoryPath, string value)
[DataRow("C:\\Distribution\\home\\user\\testRepo")]
[DataRow("\\Ubuntu-18.04\\wsl$\\home\\user\\testRepo")]
[DataRow("wslg\\Ubuntu-18.04\\wsl.localhost\\home\\user\\testRepo")]
[DataRow("")]
public void GetWorkingDirectoryNegativeTest(string repositoryPath)
{
try
Trace.Listeners.Clear();
if (string.IsNullOrEmpty(repositoryPath))
{
var workingDir = WslIntegrator.GetWorkingDirectory(repositoryPath);
Assert.AreEqual(value, workingDir);
Assert.ThrowsException<ArgumentNullException>(() => WslIntegrator.GetWorkingDirectory(repositoryPath));
}
catch (Exception ex)
else
{
Assert.IsTrue(ex.Message.Contains(value));
Assert.ThrowsException<ArgumentException>(() => WslIntegrator.GetWorkingDirectory(repositoryPath));
}
}

Expand All @@ -106,7 +107,6 @@ public void GetWorkingDirectoryNegativeTest(string repositoryPath, string value)
[DataRow("\\wsl.localhost\\Ubuntu-20.04\\home\\user\\repo", "-d Ubuntu-20.04 git ")]
[DataRow("\\wsl$\\Debian\\home\\user\\repo", "-d Debian git ")]
[DataRow("\\wsl.localhost\\kali-linux\\home\\user\\repo", "-d kali-linux git ")]
[DataRow("C:\\Users\\foo\\bar", "")]
[DataRow("\\wsl$\\Ubuntu-18.04\\home\\user\\testRepo", "-d Ubuntu-18.04 git ")]
[DataRow("\\wsl.localhost\\Ubuntu-18.04\\home\\user\\testRepo", "-d Ubuntu-18.04 git ")]
[DataRow("\\wsl.localhost\\CustomDistribution\\home\\user\\testRepo", "-d CustomDistribution git ")]
Expand All @@ -117,18 +117,19 @@ public void GetArgumentPrefixForWslPositiveTest(string repositoryPath, string va
}

[TestMethod]
[DataRow("", "Value cannot be null")]
[DataRow("\\wsl.localhost", "Failed to get the distribution name from the repository path")]
public void GetArgumentPrefixForWslNegativeTest(string repositoryPath, string value)
[DataRow("")]
[DataRow("\\wsl.localhost")]
[DataRow("C:\\Users\\foo\\bar")]
public void GetArgumentPrefixForWslNegativeTest(string repositoryPath)
{
try
Trace.Listeners.Clear();
if (string.IsNullOrEmpty(repositoryPath))
{
var prefix = WslIntegrator.GetArgumentPrefixForWsl(repositoryPath);
Assert.AreEqual(value, prefix);
Assert.ThrowsException<ArgumentNullException>(() => WslIntegrator.GetArgumentPrefixForWsl(repositoryPath));
}
catch (Exception ex)
else
{
Assert.IsTrue(ex.Message.Contains(value));
Assert.ThrowsException<ArgumentException>(() => WslIntegrator.GetArgumentPrefixForWsl(repositoryPath));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public static GitCommandRunnerResultInfo ExecuteGitCommand(string gitApplication
try
{
var processStartInfo = new ProcessStartInfo();
var wslArgument = WslIntegrator.GetArgumentPrefixForWsl(repositoryDirectory);
if (wslArgument == string.Empty)
if (!WslIntegrator.IsWSLRepo(repositoryDirectory))
{
processStartInfo.FileName = gitApplication;
processStartInfo.Arguments = arguments;
Expand All @@ -27,7 +26,7 @@ public static GitCommandRunnerResultInfo ExecuteGitCommand(string gitApplication
{
Log.Information("Wsl.exe will be invoked to obtain property information from git");
processStartInfo.FileName = "wsl";
processStartInfo.Arguments = string.Concat(wslArgument, arguments);
processStartInfo.Arguments = string.Concat(WslIntegrator.GetArgumentPrefixForWsl(repositoryDirectory), arguments);
processStartInfo.WorkingDirectory = WslIntegrator.GetWorkingDirectory(repositoryDirectory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public static string GetWslDistributionName(string repositoryPath)
}

Debug.Assert(IsWSLRepo(repositoryPath), "the repository path must be a valid wsl path");
if (!IsWSLRepo(repositoryPath))
{
throw new ArgumentException($"Not a valid WSL path: {repositoryPath}");
}

// Parse the repository path to get the distribution name
string[] pathParts = repositoryPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);
Expand All @@ -69,6 +73,10 @@ public static string GetWorkingDirectory(string repositoryPath)
}

Debug.Assert(IsWSLRepo(repositoryPath), "the repository path must be a valid wsl path");
if (!IsWSLRepo(repositoryPath))
{
throw new ArgumentException($"Not a valid WSL path: {repositoryPath}");
}

string[] pathParts = repositoryPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);

Expand Down Expand Up @@ -99,15 +107,17 @@ public static string GetNormalizedLinuxPath(string repositoryPath)

public static string GetArgumentPrefixForWsl(string repositoryPath)
{
if (!IsWSLRepo(repositoryPath))
if (string.IsNullOrEmpty(repositoryPath))
{
_log.Debug("The repository path is not a WSL path");
return string.Empty;
throw new ArgumentNullException(nameof(repositoryPath));
}

var distributionName = GetWslDistributionName(repositoryPath);
Debug.Assert(IsWSLRepo(repositoryPath), "the repository path must be a valid wsl path");
if (!IsWSLRepo(repositoryPath))
{
throw new ArgumentException($"Not a valid WSL path: {repositoryPath}");
}

string argumentPrefix = $"-d {distributionName} git ";
return argumentPrefix;
return $"-d {GetWslDistributionName(repositoryPath)} git ";
}
}

0 comments on commit 01e723c

Please sign in to comment.