Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/DiffEngine.Tests/GuardTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class GuardTests
{
/// <summary>
/// FileExists passed its arguments to AgainstEmpty the wrong way round, so the empty check was
/// run against the literal parameter name - which is never empty. An empty path therefore fell
/// through to "File not found. Path: " with no ParamName on it, naming nothing at all.
/// </summary>
[Test]
[Arguments("")]
[Arguments(" ")]
public async Task FileExistsRejectsAnEmptyPathByName(string path)
{
var exception = await Assert.That(() => Guard.FileExists(path, "tempFile"))
.Throws<ArgumentNullException>();

await Assert.That(exception!.ParamName).IsEqualTo("tempFile");
}

[Test]
public async Task FileExistsStillReportsAMissingFile()
{
var missing = Path.Combine(Path.GetTempPath(), $"missing{Guid.NewGuid():N}.txt");

await Assert.That(() => Guard.FileExists(missing, "tempFile"))
.Throws<ArgumentException>();
}
}
4 changes: 3 additions & 1 deletion src/DiffEngine/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public static void AgainstNegative(int value, string argumentName)

public static void FileExists(string path, string argumentName)
{
AgainstEmpty(argumentName, path);
// (value, name), not (name, value). Reversed, this validated the literal parameter name -
// never empty - so an empty path fell through to the message below with no ParamName on it
AgainstEmpty(path, argumentName);
if (!File.Exists(path))
{
throw new ArgumentException($"File not found. Path: {path}");
Expand Down
5 changes: 4 additions & 1 deletion src/DiffEngine/OsSettingsResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ static class OsSettingsResolver

static OsSettingsResolver()
{
var pathVariable = Environment.GetEnvironmentVariable("PATH")!;
// An unset PATH is a NullReferenceException in a static constructor, and so permanent
// for the process. `env -i` and some service launchers really do start a process without
// one; nothing on PATH simply means no tool is found that way
var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? "";

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand Down
Loading