Repository navigation
Expand file tree
/
Copy pathToolLocatorExtraTests.cs
More file actions
147 lines (128 loc) · 5.2 KB
/
Copy pathToolLocatorExtraTests.cs
File metadata and controls
147 lines (128 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
namespace XISOSharp.Tests;
/// <summary>
/// Additional unit tests for <see cref="ToolLocator"/>: file-name mapping, override
/// resolution, PATH lookup, and the <c>-v</c> probe, complementing <c>ToolLocatorTests</c>.
/// </summary>
[Collection("Sequential")]
public class ToolLocatorExtraTests : IDisposable
{
private readonly List<string> _tempDirs = [];
private readonly string? _origPath = Environment.GetEnvironmentVariable("PATH");
/// <summary>Restores PATH and removes temporary directories.</summary>
public void Dispose()
{
Environment.SetEnvironmentVariable("PATH", _origPath);
foreach (string dir in _tempDirs)
{
try
{
Directory.Delete(dir, recursive: true);
}
catch
{
// Best effort.
}
}
}
private string CreateTempDir()
{
string dir = Path.Combine(Path.GetTempPath(), $"xiso_toolloc_{Guid.NewGuid():N}");
Directory.CreateDirectory(dir);
_tempDirs.Add(dir);
return dir;
}
/// <summary>Verifies base names are rejected when null or empty.</summary>
[Theory]
[InlineData(null)]
[InlineData("")]
public void GetFileName_Invalid_Throws(string? baseName)
{
Assert.ThrowsAny<ArgumentException>(() => ToolLocator.GetFileName(baseName!));
}
/// <summary>Verifies the OS-aware extension convention.</summary>
[Fact]
public void GetFileName_IsOsAware()
{
string name = ToolLocator.GetFileName("extract-xiso");
Assert.Equal(OperatingSystem.IsWindows() ? "extract-xiso.exe" : "extract-xiso", name);
}
/// <summary>Verifies base names are rejected when resolving by base name.</summary>
[Fact]
public void ResolveByBaseName_EmptyBase_Throws()
{
Assert.ThrowsAny<ArgumentException>(() => ToolLocator.ResolveByBaseName(null, string.Empty));
}
/// <summary>Verifies an existing override path wins over sibling/PATH lookup.</summary>
[Fact]
public void Resolve_ExistingOverride_ReturnsOverride()
{
string dir = CreateTempDir();
string file = Path.Combine(dir, "my-tool.bin");
File.WriteAllText(file, "x");
Assert.Equal(file, ToolLocator.Resolve(file, "my-tool.exe", "my-tool"));
}
/// <summary>Verifies a missing override falls through to a null result for an unknown tool.</summary>
[Fact]
public void Resolve_MissingOverrideUnknownTool_ReturnsNull()
{
string missing = Path.Combine(CreateTempDir(), "missing.bin");
Assert.Null(ToolLocator.Resolve(missing, "xisosharp-no-such-tool-xyz.exe", "xisosharp-no-such-tool-xyz"));
}
/// <summary>Verifies an empty PATH yields no result.</summary>
[Fact]
public void FindOnPath_EmptyFileName_Throws()
{
Assert.ThrowsAny<ArgumentException>(() => ToolLocator.FindOnPath(string.Empty));
}
/// <summary>Verifies a missing executable yields null.</summary>
[Fact]
public void FindOnPath_Missing_ReturnsNull()
{
Assert.Null(ToolLocator.FindOnPath("xisosharp-no-such-tool-xyz"));
}
/// <summary>Verifies executables in PATH directories are found.</summary>
[Fact]
public void FindOnPath_FindsFileOnPath()
{
string dir = CreateTempDir();
string name = OperatingSystem.IsWindows() ? "xisosharp-tooltest-xyz.exe" : "xisosharp-tooltest-xyz";
string file = Path.Combine(dir, name);
File.WriteAllText(file, string.Empty);
Environment.SetEnvironmentVariable("PATH", dir + Path.PathSeparator + _origPath);
Assert.Equal(file, ToolLocator.FindOnPath(name));
}
/// <summary>Verifies blank probe paths yield null.</summary>
[Fact]
public async Task ProbeVersionAsync_BlankPath_ReturnsNull()
{
Assert.Null(await ToolLocator.ProbeVersionAsync(" "));
}
/// <summary>Verifies missing tools yield null instead of throwing.</summary>
[Fact]
public async Task ProbeVersionAsync_MissingFile_ReturnsNull()
{
string missing = Path.Combine(CreateTempDir(), "missing-tool-xyz");
Assert.Null(await ToolLocator.ProbeVersionAsync(missing));
}
/// <summary>Verifies a pre-canceled token yields null instead of throwing.</summary>
[Fact]
public async Task ProbeVersionAsync_Canceled_ReturnsNull()
{
using CancellationTokenSource cts = new();
cts.Cancel();
string cliPath = Path.Combine(AppContext.BaseDirectory,
OperatingSystem.IsWindows() ? "XISOSharp.Cli.exe" : "XISOSharp.Cli");
Assert.Null(await ToolLocator.ProbeVersionAsync(cliPath, cancellationToken: cts.Token));
}
/// <summary>Verifies probing the shipped CLI returns its version banner.</summary>
[Fact]
public async Task ProbeVersionAsync_CliBinary_ReturnsBanner()
{
string cliPath = Path.Combine(AppContext.BaseDirectory,
OperatingSystem.IsWindows() ? "XISOSharp.Cli.exe" : "XISOSharp.Cli");
Assert.True(File.Exists(cliPath), $"expected CLI binary at {cliPath}");
string? version = await ToolLocator.ProbeVersionAsync(cliPath);
Assert.NotNull(version);
Assert.StartsWith("XISOSharp v", version, StringComparison.Ordinal);
}
}