Skip to content
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Reflection;

#if NETFRAMEWORK
using System.Diagnostics;
#endif

using Microsoft.Testing.Extensions.TrxReport.Abstractions;

namespace Microsoft.Testing.Extensions.UnitTests;

[TestClass]
public sealed class ArtifactPostProcessingHelperTests
{
// ArtifactPostProcessingHelper is linked into each report-engine assembly, so resolve the
// TrxReport copy through reflection to avoid an ambiguous type reference.
private static readonly MethodInfo IsReparsePointMethod =
(typeof(TrxReportEngine).Assembly.GetType("Microsoft.Testing.Extensions.ArtifactPostProcessingHelper")
?? throw new InvalidOperationException("Could not find type ArtifactPostProcessingHelper in the Trx report engine assembly."))
.GetMethod("IsReparsePoint", BindingFlags.NonPublic | BindingFlags.Static)
?? throw new InvalidOperationException("Could not resolve ArtifactPostProcessingHelper.IsReparsePoint.");
Comment thread
Evangelink marked this conversation as resolved.
Resolved

private readonly List<string> _trackedDirectories = [];

[TestCleanup]
public void Cleanup()
{
for (int i = _trackedDirectories.Count - 1; i >= 0; i--)
{
string directory = _trackedDirectories[i];
if (Directory.Exists(directory))
{
Directory.Delete(directory, recursive: true);
}
}
Comment thread
Evangelink marked this conversation as resolved.
Resolved
}

[TestMethod]
public void IsReparsePoint_RegularDirectory_ReturnsFalse()
{
string directory = CreateTrackedDirectory();

bool result = Invoke(directory);

Assert.IsFalse(result);
}

[TestMethod]
public void IsReparsePoint_NonExistentPath_ReturnsTrue()
{
string path = Path.Combine(Path.GetTempPath(), nameof(ArtifactPostProcessingHelperTests), Guid.NewGuid().ToString("N"));
Comment thread
Evangelink marked this conversation as resolved.
Resolved

bool result = Invoke(path);

Assert.IsTrue(result);
}

#if NETCOREAPP
[TestMethod]
public void IsReparsePoint_SymbolicLinkToDirectory_ReturnsTrue()
{
string target = CreateTrackedDirectory();
string linkPath = Path.Combine(Path.GetTempPath(), nameof(ArtifactPostProcessingHelperTests), Guid.NewGuid().ToString("N"));
_trackedDirectories.Add(linkPath);

try
{
Directory.CreateSymbolicLink(linkPath, target);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or PlatformNotSupportedException)
{
Assert.Inconclusive($"Symbolic link creation is not supported or permitted in this environment: {ex.Message}");
return;
}

bool result = Invoke(linkPath);

Assert.IsTrue(result);
}
#endif

#if NETFRAMEWORK
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public async Task IsReparsePoint_JunctionToDirectory_ReturnsTrue()
{
string target = CreateTrackedDirectory();
string junctionPath = Path.Combine(Path.GetTempPath(), nameof(ArtifactPostProcessingHelperTests), Guid.NewGuid().ToString("N"));
_trackedDirectories.Add(junctionPath);

using Process process = Process.Start(new ProcessStartInfo
{
FileName = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe",
Arguments = $"/d /c mklink /J \"{junctionPath}\" \"{target}\"",
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
})!;
Task<string> standardOutputTask = process.StandardOutput.ReadToEndAsync();
Task<string> standardErrorTask = process.StandardError.ReadToEndAsync();
process.WaitForExit();
string standardOutput = await standardOutputTask;
string standardError = await standardErrorTask;

if (process.ExitCode != 0)
{
Assert.Inconclusive(
$"Junction creation failed with exit code {process.ExitCode}: "
+ $"{standardOutput}{standardError}");
return;
}

bool result = Invoke(junctionPath);

Assert.IsTrue(result);
}
#endif

[TestMethod]
public void IsReparsePoint_File_ReturnsFalse()
{
string directory = CreateTrackedDirectory();
string filePath = Path.Combine(directory, "file.txt");
File.WriteAllText(filePath, "content");

bool result = Invoke(filePath);

Assert.IsFalse(result);
}

private string CreateTrackedDirectory()
{
string path = Path.Combine(Path.GetTempPath(), nameof(ArtifactPostProcessingHelperTests), Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(path);
_trackedDirectories.Add(path);
return path;
}

private static bool Invoke(string path)
=> (bool)IsReparsePointMethod.Invoke(null, [path])!;
}
Loading