Skip to content

Commit

Permalink
which handles broken symlink & unit test added (actions#2150)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eli Entelis committed Oct 11, 2022
1 parent 0f13055 commit 5a7ff8a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Runner.Sdk/Util/WhichUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public static string Which(string command, bool require = false, ITraceWriter tr

if (matches != null && matches.Length > 0)
{
var fileInfo = new FileInfo(matches.First());
if (fileInfo.LinkTarget != null && !File.Exists(fileInfo.LinkTarget))
{
trace?.Info($"{command}: symlink {fileInfo.FullName} found but target {fileInfo.LinkTarget} is broken. make sure the symlink points to a valid target.");
continue;
}
trace?.Info($"Location: '{matches.First()}'");
return matches.First();
}
Expand All @@ -88,6 +94,12 @@ public static string Which(string command, bool require = false, ITraceWriter tr
string fullPath = Path.Combine(pathSegment, $"{command}{pathExtSegments[i]}");
if (matches.Any(p => p.Equals(fullPath, StringComparison.OrdinalIgnoreCase)))
{
var fileInfo = new FileInfo(fullPath);
if (fileInfo.LinkTarget != null && !File.Exists(fileInfo.LinkTarget))
{
trace?.Info($"{command}: symlink {fileInfo.FullName} found but target {fileInfo.LinkTarget} is broken. make sure the symlink points to a valid target.");
continue;
}
trace?.Info($"Location: '{fullPath}'");
return fullPath;
}
Expand All @@ -107,6 +119,12 @@ public static string Which(string command, bool require = false, ITraceWriter tr

if (matches != null && matches.Length > 0)
{
var fileInfo = new FileInfo(fullPath);
if (fileInfo.LinkTarget != null && !File.Exists(fileInfo.LinkTarget))
{
trace?.Info($"{command}: symlink {fileInfo.FullName} found but target {fileInfo.LinkTarget} is broken. make sure the symlink points to a valid target.");
continue;
}
trace?.Info($"Location: '{matches.First()}'");
return matches.First();
}
Expand Down
39 changes: 38 additions & 1 deletion src/Test/L0/Util/WhichUtilL0.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GitHub.Runner.Common.Util;
using GitHub.Runner.Common.Util;
using GitHub.Runner.Sdk;
using System;
using System.IO;
Expand Down Expand Up @@ -89,5 +89,42 @@ public void WhichHandleFullyQualifiedPath()
Assert.Equal(gitPath, gitPath2);
}
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
public void WhichThrowsWhenSymlinkBroken()
{
using TestHostContext hc = new TestHostContext(this);
// Arrange
Tracing trace = hc.GetTrace();
string oldValue = Environment.GetEnvironmentVariable(PathUtil.PathVariable);
string newValue = oldValue + Path.GetTempPath();
Environment.SetEnvironmentVariable(PathUtil.PathVariable, newValue);

string brokenSymlink = Path.GetTempPath() + "broken-symlink.exe";
string target = "no-such-file-cf7e351f";
File.CreateSymbolicLink(brokenSymlink, target);

// Act.
try
{
WhichUtil.Which("broken-symlink", require: true, trace: trace);
throw new Exception("which should have thrown");
}

// Assert
catch (FileNotFoundException ex)
{
Assert.Equal("broken-symlink", ex.FileName);
}

// Cleanup
finally
{
File.Delete(brokenSymlink);
Environment.SetEnvironmentVariable(PathUtil.PathVariable, oldValue);
}
}
}
}
2 changes: 1 addition & 1 deletion src/global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "6.0.300"
"version": "6.0.401"
}
}

0 comments on commit 5a7ff8a

Please sign in to comment.