Repository navigation
Add tests for artifact reparse point detection #10721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Amaury Levé (Evangelink)
merged 2 commits into
main
from
dev/amauryleve/test-artifact-reparse-points
Aug 25, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
test/UnitTests/Microsoft.Testing.Extensions.UnitTests/ArtifactPostProcessingHelperTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| // 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."); | ||
|
|
||
| private readonly List<string> _trackedDirectories = []; | ||
|
|
||
| [TestCleanup] | ||
| public void Cleanup() | ||
| { | ||
| IOException? cleanupException = null; | ||
|
|
||
| for (int i = _trackedDirectories.Count - 1; i >= 0; i--) | ||
| { | ||
| string directory = _trackedDirectories[i]; | ||
| try | ||
| { | ||
| if (Directory.Exists(directory)) | ||
| { | ||
| Directory.Delete(directory, recursive: true); | ||
| } | ||
| } | ||
| catch (IOException ex) | ||
| { | ||
| cleanupException ??= ex; | ||
| } | ||
| } | ||
|
Evangelink marked this conversation as resolved.
Resolved
|
||
|
|
||
| if (cleanupException is not null) | ||
| { | ||
| throw cleanupException; | ||
| } | ||
| } | ||
|
|
||
| [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")); | ||
|
Evangelink marked this conversation as resolved.
Resolved
|
||
|
|
||
| // A non-existent path cannot be inspected, so the method conservatively returns true. | ||
| 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])!; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.