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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
### Features
* [@Chris-Wolfgang]: Add support for keyed dependency injection via `[FromKeyedServices]` attribute ([#560])

### Fixes
* [@claude]: Validate dotnet path exists before returning from `TryFindDotNetExePath` ([#600])

[#560]: https://github.com/natemcmaster/CommandLineUtils/pull/560
[#600]: https://github.com/natemcmaster/CommandLineUtils/issues/600

## [v5.0.1](https://github.com/natemcmaster/CommandLineUtils/compare/v5.0.0...v5.0.1)

Expand Down
8 changes: 7 additions & 1 deletion src/CommandLineUtils/Utilities/DotNetExe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ public static string FullPathOrDefault()
dotnetRoot = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\Program Files\\dotnet" : "/usr/local/share/dotnet";
}

return Path.Combine(dotnetRoot, fileName);
return FindDotNetInRoot(dotnetRoot, fileName);
}

internal static string? FindDotNetInRoot(string dotnetRoot, string fileName)
{
var dotnetPath = Path.Combine(dotnetRoot, fileName);
return File.Exists(dotnetPath) ? dotnetPath : null;
}
}
}
3 changes: 3 additions & 0 deletions src/CommandLineUtils/releasenotes.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changes since 5.0:

Features:
* @Chris-Wolfgang: Add support for keyed dependency injection via [FromKeyedServices] attribute (#560)

Fixes:
* @claude: Validate dotnet path exists before returning from TryFindDotNetExePath (#600)
</PackageReleaseNotes>
<PackageReleaseNotes Condition="$(VersionPrefix.StartsWith('5.0.'))">
Changes since 4.1:
Expand Down
57 changes: 57 additions & 0 deletions test/CommandLineUtils.Tests/DotNetExeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#if NET6_0_OR_GREATER
using System.IO;
using System.Runtime.InteropServices;
using Xunit;

namespace McMaster.Extensions.CommandLineUtils.Tests
Expand All @@ -20,6 +21,62 @@ public void FindsTheDotNetPath()
Assert.True(Path.IsPathRooted(dotnetPath), "The path should be rooted");
Assert.Equal("dotnet", Path.GetFileNameWithoutExtension(dotnetPath), ignoreCase: true);
}

[Fact]
public void FullPathOrDefaultReturnsPathOrDotnet()
{
var result = DotNetExe.FullPathOrDefault();
Assert.NotNull(result);
Assert.NotEmpty(result);
// Should either be a rooted path that exists, or just "dotnet"
if (Path.IsPathRooted(result))
{
Assert.True(File.Exists(result), "The file did not exist");
}
else
{
Assert.Equal("dotnet", result);
}
}

[Fact]
public void FindDotNetInRoot_ReturnsNull_WhenDirectoryDoesNotExist()
{
var fileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dotnet.exe" : "dotnet";
var result = DotNetExe.FindDotNetInRoot("/nonexistent/path/that/does/not/exist", fileName);
Assert.Null(result);
}

[Fact]
public void FindDotNetInRoot_ReturnsNull_WhenFileDoesNotExist()
{
// Use a directory that exists but won't contain dotnet
var tempDir = Path.GetTempPath();
var result = DotNetExe.FindDotNetInRoot(tempDir, "dotnet-nonexistent-file");
Assert.Null(result);
}

[Fact]
public void FindDotNetInRoot_ReturnsPath_WhenFileExists()
{
// Create a temp file to simulate dotnet existing
var tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDir);
try
{
var fakeDotnet = "dotnet-test";
var fakePath = Path.Combine(tempDir, fakeDotnet);
File.WriteAllText(fakePath, "");

var result = DotNetExe.FindDotNetInRoot(tempDir, fakeDotnet);
Assert.NotNull(result);
Assert.Equal(fakePath, result);
}
finally
{
Directory.Delete(tempDir, recursive: true);
}
}
}
}
#elif NET472_OR_GREATER
Expand Down
Loading