From fe87d2b39dfb348b04816f5c04415dede157a3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Tue, 25 Aug 2026 16:57:39 +0200 Subject: [PATCH] Handle report merge directory creation failures Return no merged artifact when Trx or Ctrf cannot create the fixed output directory. Cover portable file collisions and dangling directory links while verifying input artifacts remain intact. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 86687a11-fb1e-4b64-933a-7fbd6f5bcba3 --- .../CtrfArtifactPostProcessor.cs | 10 ++- .../TrxArtifactPostProcessor.cs | 10 ++- .../CtrfArtifactPostProcessorTests.cs | 67 ++++++++++++++++ .../TrxArtifactPostProcessorTests.cs | 77 +++++++++++++++++++ 4 files changed, 162 insertions(+), 2 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.CtrfReport/CtrfArtifactPostProcessor.cs b/src/Platform/Microsoft.Testing.Extensions.CtrfReport/CtrfArtifactPostProcessor.cs index c6ac85bf1a..b79e64bab2 100644 --- a/src/Platform/Microsoft.Testing.Extensions.CtrfReport/CtrfArtifactPostProcessor.cs +++ b/src/Platform/Microsoft.Testing.Extensions.CtrfReport/CtrfArtifactPostProcessor.cs @@ -60,7 +60,15 @@ .. orderedInputs.Select(input => $"{Path.GetFullPath(input.Path)}\0{input.Execut ]; Guid artifactId = CtrfReportMerger.CreateDeterministicId(identityInputs); string mergedDirectory = Path.Combine(outputDirectory, MergedReportDirectoryName); - Directory.CreateDirectory(mergedDirectory); + try + { + Directory.CreateDirectory(mergedDirectory); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + return null; + } + if (ArtifactPostProcessingHelper.IsReparsePoint(mergedDirectory)) { return null; diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxArtifactPostProcessor.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxArtifactPostProcessor.cs index 913bd18cec..12dc98a172 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxArtifactPostProcessor.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxArtifactPostProcessor.cs @@ -69,7 +69,15 @@ .. ArtifactPostProcessingHelper.OrderInputs(inputs, includeModuleMetadata: false // outside the supplied output directory (Directory.CreateDirectory succeeds on an existing link). // Materialize the directory here and refuse to merge through a reparse point instead. Returning // null leaves the per-module reports untouched, matching the never-fail-the-run invariant. - Directory.CreateDirectory(mergedDirectory); + try + { + Directory.CreateDirectory(mergedDirectory); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + return null; + } + if (ArtifactPostProcessingHelper.IsReparsePoint(mergedDirectory)) { return null; diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/CtrfArtifactPostProcessorTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/CtrfArtifactPostProcessorTests.cs index 701535ca0a..f12692d057 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/CtrfArtifactPostProcessorTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/CtrfArtifactPostProcessorTests.cs @@ -223,6 +223,34 @@ public async Task ProcessAsync_WithDifferentExecutionProvenance_ProducesDifferen } } + [TestMethod] + public async Task ProcessAsync_WhenMergedPathIsAFile_DoesNotMerge() + { + string root = CreateTemporaryDirectory(); + string resultsDirectory = Path.Combine(root, "results"); + Directory.CreateDirectory(resultsDirectory); + File.WriteAllText(Path.Combine(resultsDirectory, "merged"), string.Empty); + try + { + string firstPath = WriteReport(resultsDirectory, "first.ctrf.json", "first"); + string secondPath = WriteReport(resultsDirectory, "second.ctrf.json", "second"); + + ProcessedArtifact? output = await new CtrfArtifactPostProcessor().ProcessAsync( + [CreateInput(firstPath, "execution-1"), CreateInput(secondPath, "execution-2")], + resultsDirectory, + new ArtifactPostProcessingContext(ArtifactPostProcessingTruncationReason.None), + CancellationToken.None); + + Assert.IsNull(output); + Assert.IsTrue(File.Exists(firstPath)); + Assert.IsTrue(File.Exists(secondPath)); + } + finally + { + Directory.Delete(root, recursive: true); + } + } + #if NETCOREAPP [TestMethod] public async Task ProcessAsync_WhenMergedDirectoryIsAReparsePoint_DoesNotMerge() @@ -262,6 +290,45 @@ public async Task ProcessAsync_WhenMergedDirectoryIsAReparsePoint_DoesNotMerge() Directory.Delete(root, recursive: true); } } + + [TestMethod] + public async Task ProcessAsync_WhenMergedDirectoryIsADanglingReparsePoint_DoesNotMerge() + { + string root = CreateTemporaryDirectory(); + string resultsDirectory = Path.Combine(root, "results"); + string missingDirectory = Path.Combine(root, "missing"); + Directory.CreateDirectory(resultsDirectory); + try + { + try + { + Directory.CreateSymbolicLink(Path.Combine(resultsDirectory, "merged"), missingDirectory); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or PlatformNotSupportedException) + { + Assert.Inconclusive("Host cannot create directory symbolic links."); + return; + } + + string firstPath = WriteReport(resultsDirectory, "first.ctrf.json", "first"); + string secondPath = WriteReport(resultsDirectory, "second.ctrf.json", "second"); + + ProcessedArtifact? output = await new CtrfArtifactPostProcessor().ProcessAsync( + [CreateInput(firstPath, "execution-1"), CreateInput(secondPath, "execution-2")], + resultsDirectory, + new ArtifactPostProcessingContext(ArtifactPostProcessingTruncationReason.None), + CancellationToken.None); + + Assert.IsNull(output); + Assert.IsTrue(File.Exists(firstPath)); + Assert.IsTrue(File.Exists(secondPath)); + Assert.IsFalse(Directory.Exists(missingDirectory)); + } + finally + { + Directory.Delete(root, recursive: true); + } + } #endif private static InputArtifact CreateInput(string path, string executionId) diff --git a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxArtifactPostProcessorTests.cs b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxArtifactPostProcessorTests.cs index 8c70ecd171..1ccb98bd0a 100644 --- a/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxArtifactPostProcessorTests.cs +++ b/test/UnitTests/Microsoft.Testing.Extensions.UnitTests/TrxArtifactPostProcessorTests.cs @@ -177,6 +177,39 @@ public async Task ProcessAsync_NestsAttachmentDeploymentRootBesideTheMergedRepor } } + [TestMethod] + public async Task ProcessAsync_WhenMergedPathIsAFile_DoesNotMerge() + { + string root = Path.Combine(Path.GetTempPath(), $"trx-post-processor-{Guid.NewGuid():N}"); + string resultsDirectory = Path.Combine(root, "results"); + Directory.CreateDirectory(resultsDirectory); + File.WriteAllText(Path.Combine(resultsDirectory, "merged"), string.Empty); + try + { + string firstPath = Path.Combine(resultsDirectory, "first.trx"); + string secondPath = Path.Combine(resultsDirectory, "second.trx"); + WriteMinimalReport(firstPath, "first"); + WriteMinimalReport(secondPath, "second"); + + ProcessedArtifact? output = await new TrxArtifactPostProcessor().ProcessAsync( + [ + new InputArtifact(firstPath, TrxReportEngine.TrxArtifactKind, null, null, null, "execution-1"), + new InputArtifact(secondPath, TrxReportEngine.TrxArtifactKind, null, null, null, "execution-2"), + ], + resultsDirectory, + new ArtifactPostProcessingContext(ArtifactPostProcessingTruncationReason.None), + CancellationToken.None); + + Assert.IsNull(output); + Assert.IsTrue(File.Exists(firstPath)); + Assert.IsTrue(File.Exists(secondPath)); + } + finally + { + Directory.Delete(root, recursive: true); + } + } + #if NETCOREAPP [TestMethod] public async Task ProcessAsync_WhenMergedDirectoryIsAReparsePoint_DoesNotMerge() @@ -226,6 +259,50 @@ public async Task ProcessAsync_WhenMergedDirectoryIsAReparsePoint_DoesNotMerge() Directory.Delete(root, recursive: true); } } + + [TestMethod] + public async Task ProcessAsync_WhenMergedDirectoryIsADanglingReparsePoint_DoesNotMerge() + { + string root = Path.Combine(Path.GetTempPath(), $"trx-post-processor-{Guid.NewGuid():N}"); + string resultsDirectory = Path.Combine(root, "results"); + string missingDirectory = Path.Combine(root, "missing"); + Directory.CreateDirectory(resultsDirectory); + try + { + try + { + Directory.CreateSymbolicLink(Path.Combine(resultsDirectory, "merged"), missingDirectory); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or PlatformNotSupportedException) + { + Assert.Inconclusive("Host cannot create directory symbolic links."); + return; + } + + string firstPath = Path.Combine(resultsDirectory, "first.trx"); + string secondPath = Path.Combine(resultsDirectory, "second.trx"); + WriteMinimalReport(firstPath, "first"); + WriteMinimalReport(secondPath, "second"); + + ProcessedArtifact? output = await new TrxArtifactPostProcessor().ProcessAsync( + [ + new InputArtifact(firstPath, TrxReportEngine.TrxArtifactKind, null, null, null, "execution-1"), + new InputArtifact(secondPath, TrxReportEngine.TrxArtifactKind, null, null, null, "execution-2"), + ], + resultsDirectory, + new ArtifactPostProcessingContext(ArtifactPostProcessingTruncationReason.None), + CancellationToken.None); + + Assert.IsNull(output); + Assert.IsTrue(File.Exists(firstPath)); + Assert.IsTrue(File.Exists(secondPath)); + Assert.IsFalse(Directory.Exists(missingDirectory)); + } + finally + { + Directory.Delete(root, recursive: true); + } + } #endif [TestMethod]