Skip to content

Commit

Permalink
Checking PATH environment variable (#2107)
Browse files Browse the repository at this point in the history
* Checking PATH environment variable

* creating file searcher helper, adding tests

* code review & update in changelog

* updating error comment

* fixing tests
  • Loading branch information
eddynaka committed Oct 15, 2020
1 parent 062facb commit e57c5ff
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* DEPENDENCY BREAKING: SARIF now requires Newtonsoft.JSON 11.0.2 (rather than 10.0.3)
* DEPENDENCY: SARIF TypeScript package now requires minimist 1.2.3 or later (rather than >=1.2.0)
* FEATURE: Add a setter to `GitHelper.GitExePath`. [#2110](https://github.com/microsoft/sarif-sdk/pull/2110)
* FEATURE: `GitHelper` will search in %PATH% variable if `git.exe` isn't located in ProgramFiles folder

## **v2.3.6** [Sdk](https://www.nuget.org/packages/Sarif.Sdk/2.3.6) | [Driver](https://www.nuget.org/packages/Sarif.Driver/2.3.6) | [Converters](https://www.nuget.org/packages/Sarif.Converters/2.3.6) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/2.3.6) | [Multitool Library](https://www.nuget.org/packages/Sarif.Multitool.Library/2.3.6)
* BUGFIX: Restore multitool client app package build.
Expand Down
51 changes: 51 additions & 0 deletions src/Sarif/FileSearcherHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;

namespace Microsoft.CodeAnalysis.Sarif
{
internal static class FileSearcherHelper
{
/// <summary>
/// This method will search in the environment variable for a specific file name.
/// It will return the first file found.
/// </summary>
/// <param name="environmentVariable">Environment variable that we will look for</param>
/// <param name="fileName">Name of the file that we will look for in the environment variable</param>
/// <returns>Path to the file name or empty string.</returns>
public static string SearchForFileInEnvironmentVariable(string environmentVariable, string fileName)
{
string variable = Environment.GetEnvironmentVariable(environmentVariable);
if (string.IsNullOrEmpty(variable))
{
return null;
}

string[] paths = variable.Split(';');
foreach (string path in paths)
{
string returnedPath = SearchForFileNameInPath(path, fileName);
if (!string.IsNullOrEmpty(returnedPath))
{
return returnedPath;
}
}

return null;
}

/// <summary>
/// This method will search for a file name in a specific path.
/// </summary>
/// <param name="path">Path where it will search.</param>
/// <param name="fileName">Name of the file that it will search</param>
/// <returns>Path to the file name or empty string.</returns>
public static string SearchForFileNameInPath(string path, string fileName)
{
string filePath = $@"{path}\{fileName}";
return File.Exists(filePath) ? filePath : null;
}
}
}
11 changes: 9 additions & 2 deletions src/Sarif/GitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ public void Checkout(string repoPath, string commitSha)
args: $"checkout {commitSha}");
}

private string GetGitExePath()
=> this.fileSystem.FileExists(s_expectedGitExePath) ? s_expectedGitExePath : null;
internal string GetGitExePath()
{
if (this.fileSystem.FileExists(s_expectedGitExePath))
{
return s_expectedGitExePath;
}

return FileSearcherHelper.SearchForFileInEnvironmentVariable("PATH", "git.exe");
}

public string GetCurrentBranch(string repoPath)
{
Expand Down
43 changes: 43 additions & 0 deletions src/Test.UnitTests.Sarif/GitHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,49 @@ public void GetRepositoryRoot_WhenCalledOnTheDefaultInstanceWithCachingDisabled_
action.Should().NotThrow();
}

[Fact]
public void GetGitExePath_WhenPathExistsInProgramFiles()
{
var mockFileSystem = new Mock<IFileSystem>();

mockFileSystem.Setup(x => x.FileExists(It.IsAny<string>())).Returns(true);

var gitHelper = new GitHelper(mockFileSystem.Object);

gitHelper.GetGitExePath().Should().NotBeNullOrEmpty();
}

[Fact]
public void GetGitExePath_WhenPathDoesNotExistInProgramFiles()
{
var mockFileSystem = new Mock<IFileSystem>();

mockFileSystem.Setup(x => x.FileExists(It.IsAny<string>())).Returns(false);

var gitHelper = new GitHelper(mockFileSystem.Object);

gitHelper.GetGitExePath().Should().NotBeNull();
}

[Fact]
public void SearchForFileInEnvironmentVariable_WhenVariableDoesNotExist()
{
FileSearcherHelper.SearchForFileInEnvironmentVariable("PATH_THAT_DOES_NOT_EXIST", "filename.exe").Should().BeNull();
}

[Fact]
public void SearchForFileInEnvironmentVariable_WhenVariableExistsButFileDoesnt()
{
// The error in the ntdll name here is intentional.
FileSearcherHelper.SearchForFileInEnvironmentVariable("PATH", "ntdll.dlll").Should().BeNull();
}

[Fact]
public void SearchForFileInEnvironmentVariable_WhenVariableAndFileExists()
{
FileSearcherHelper.SearchForFileInEnvironmentVariable("PATH", "ntdll.dll").Should().NotBeNull();
}

[Fact]
public void GitExePath_WhenPathDoesntExist_SettingManuallyShouldWork()
{
Expand Down

0 comments on commit e57c5ff

Please sign in to comment.