Skip to content

Commit e62b8df

Browse files
committed
environment: add test for PosixEnv.TryLocateExec
1 parent f1a1ae5 commit e62b8df

File tree

2 files changed

+81
-20
lines changed

2 files changed

+81
-20
lines changed

src/shared/Core.Tests/EnvironmentTests.cs

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System;
12
using System.Collections.Generic;
3+
using GitCredentialManager.Interop.Posix;
24
using GitCredentialManager.Interop.Windows;
35
using GitCredentialManager.Tests.Objects;
46
using Xunit;
@@ -7,62 +9,116 @@ namespace GitCredentialManager.Tests
79
{
810
public class EnvironmentTests
911
{
12+
private const string WindowsPathVar = @"C:\Users\john.doe\bin;C:\Windows\system32;C:\Windows";
13+
private const string WindowsExecName = "foo.exe";
14+
private const string PosixPathVar = "/home/john.doe/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin";
15+
private const string PosixExecName = "foo";
16+
1017
[PlatformFact(Platforms.Windows)]
1118
public void WindowsEnvironment_TryLocateExecutable_NotExists_ReturnFalse()
1219
{
13-
string pathVar = @"C:\Users\john.doe\bin;C:\Windows\system32;C:\Windows";
14-
string execName = "foo.exe";
1520
var fs = new TestFileSystem();
16-
var envars = new Dictionary<string, string> {["PATH"] = pathVar};
21+
var envars = new Dictionary<string, string> {["PATH"] = WindowsPathVar};
1722
var env = new WindowsEnvironment(fs, envars);
1823

19-
bool actualResult = env.TryLocateExecutable(execName, out string actualPath);
24+
bool actualResult = env.TryLocateExecutable(WindowsExecName, out string actualPath);
2025

2126
Assert.False(actualResult);
2227
Assert.Null(actualPath);
2328
}
2429

2530
[PlatformFact(Platforms.Windows)]
26-
public void WindowsEnvironment_TryLocateExecutable_Windows_Exists_ReturnTrueAndPath()
31+
public void WindowsEnvironment_TryLocateExecutable_Exists_ReturnTrueAndPath()
2732
{
28-
string pathVar = @"C:\Users\john.doe\bin;C:\Windows\system32;C:\Windows";
29-
string execName = "foo.exe";
3033
string expectedPath = @"C:\Windows\system32\foo.exe";
3134
var fs = new TestFileSystem
3235
{
3336
Files = new Dictionary<string, byte[]>
3437
{
35-
[@"C:\Windows\system32\foo.exe"] = new byte[0],
38+
[expectedPath] = Array.Empty<byte>()
3639
}
3740
};
38-
var envars = new Dictionary<string, string> {["PATH"] = pathVar};
41+
var envars = new Dictionary<string, string> {["PATH"] = WindowsPathVar};
3942
var env = new WindowsEnvironment(fs, envars);
4043

41-
bool actualResult = env.TryLocateExecutable(execName, out string actualPath);
44+
bool actualResult = env.TryLocateExecutable(WindowsExecName, out string actualPath);
4245

4346
Assert.True(actualResult);
4447
Assert.Equal(expectedPath, actualPath);
4548
}
4649

4750
[PlatformFact(Platforms.Windows)]
48-
public void WindowsEnvironment_TryLocateExecutable_Windows_ExistsMultiple_ReturnTrueAndFirstPath()
51+
public void WindowsEnvironment_TryLocateExecutable_ExistsMultiple_ReturnTrueAndFirstPath()
4952
{
50-
string pathVar = @"C:\Users\john.doe\bin;C:\Windows\system32;C:\Windows";
51-
string execName = "foo.exe";
5253
string expectedPath = @"C:\Users\john.doe\bin\foo.exe";
5354
var fs = new TestFileSystem
5455
{
5556
Files = new Dictionary<string, byte[]>
5657
{
57-
[@"C:\Users\john.doe\bin\foo.exe"] = new byte[0],
58-
[@"C:\Windows\system32\foo.exe"] = new byte[0],
59-
[@"C:\Windows\foo.exe"] = new byte[0],
58+
[expectedPath] = Array.Empty<byte>(),
59+
[@"C:\Windows\system32\foo.exe"] = Array.Empty<byte>(),
60+
[@"C:\Windows\foo.exe"] = Array.Empty<byte>(),
6061
}
6162
};
62-
var envars = new Dictionary<string, string> {["PATH"] = pathVar};
63+
var envars = new Dictionary<string, string> {["PATH"] = WindowsPathVar};
6364
var env = new WindowsEnvironment(fs, envars);
6465

65-
bool actualResult = env.TryLocateExecutable(execName, out string actualPath);
66+
bool actualResult = env.TryLocateExecutable(WindowsExecName, out string actualPath);
67+
68+
Assert.True(actualResult);
69+
Assert.Equal(expectedPath, actualPath);
70+
}
71+
72+
[PlatformFact(Platforms.Posix)]
73+
public void PosixEnvironment_TryLocateExecutable_NotExists_ReturnFalse()
74+
{
75+
var fs = new TestFileSystem();
76+
var envars = new Dictionary<string, string> {["PATH"] = PosixPathVar};
77+
var env = new PosixEnvironment(fs, envars);
78+
79+
bool actualResult = env.TryLocateExecutable(PosixExecName, out string actualPath);
80+
81+
Assert.False(actualResult);
82+
Assert.Null(actualPath);
83+
}
84+
85+
[PlatformFact(Platforms.Posix)]
86+
public void PosixEnvironment_TryLocateExecutable_Exists_ReturnTrueAndPath()
87+
{
88+
string expectedPath = "/usr/local/bin/foo";
89+
var fs = new TestFileSystem
90+
{
91+
Files = new Dictionary<string, byte[]>
92+
{
93+
[expectedPath] = Array.Empty<byte>(),
94+
}
95+
};
96+
var envars = new Dictionary<string, string> {["PATH"] = PosixPathVar};
97+
var env = new PosixEnvironment(fs, envars);
98+
99+
bool actualResult = env.TryLocateExecutable(PosixExecName, out string actualPath);
100+
101+
Assert.True(actualResult);
102+
Assert.Equal(expectedPath, actualPath);
103+
}
104+
105+
[PlatformFact(Platforms.Posix)]
106+
public void PosixEnvironment_TryLocateExecutable_ExistsMultiple_ReturnTrueAndFirstPath()
107+
{
108+
string expectedPath = "/home/john.doe/bin/foo";
109+
var fs = new TestFileSystem
110+
{
111+
Files = new Dictionary<string, byte[]>
112+
{
113+
[expectedPath] = Array.Empty<byte>(),
114+
["/usr/local/bin/foo"] = Array.Empty<byte>(),
115+
["/bin/foo"] = Array.Empty<byte>(),
116+
}
117+
};
118+
var envars = new Dictionary<string, string> {["PATH"] = PosixPathVar};
119+
var env = new PosixEnvironment(fs, envars);
120+
121+
bool actualResult = env.TryLocateExecutable(PosixExecName, out string actualPath);
66122

67123
Assert.True(actualResult);
68124
Assert.Equal(expectedPath, actualPath);

src/shared/Core/Interop/Posix/PosixEnvironment.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ namespace GitCredentialManager.Interop.Posix
55
{
66
public class PosixEnvironment : EnvironmentBase
77
{
8-
public PosixEnvironment(IFileSystem fileSystem) : base(fileSystem)
8+
public PosixEnvironment(IFileSystem fileSystem)
9+
: this(fileSystem, GetCurrentVariables()) { }
10+
11+
internal PosixEnvironment(IFileSystem fileSystem, IReadOnlyDictionary<string, string> variables)
12+
: base(fileSystem)
913
{
10-
Variables = GetCurrentVariables();
14+
EnsureArgument.NotNull(variables, nameof(variables));
15+
Variables = variables;
1116
}
1217

1318
#region EnvironmentBase

0 commit comments

Comments
 (0)