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
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@ private void AddResultSummary(XElement testRun, string resultSummaryOutcome, str
AddArtifactsToCollection(_artifactsByExtension, collectorDataEntries, runDeploymentRoot);
}

private string CopyArtifactIntoTrxDirectoryAndReturnHrefValue(FileInfo artifact, string runDeploymentRoot)
private string CopyArtifactIntoTrxDirectoryAndReturnHrefValue(FileInfo artifact, string runDeploymentRoot, string? relativeResultsDirectory = null)
{
string artifactDirectory = CreateOrGetTrxArtifactDirectory(runDeploymentRoot);
string artifactDirectory = CreateOrGetTrxArtifactDirectory(runDeploymentRoot, relativeResultsDirectory);
string fileName = artifact.Name;

string destination = Path.Combine(artifactDirectory, fileName);
Expand All @@ -386,9 +386,11 @@ private string CopyArtifactIntoTrxDirectoryAndReturnHrefValue(FileInfo artifact,
return Path.Combine(_environment.MachineName, Path.GetFileName(destination));
}

private string CreateOrGetTrxArtifactDirectory(string runDeploymentRoot)
private string CreateOrGetTrxArtifactDirectory(string runDeploymentRoot, string? relativeResultsDirectory = null)
{
string directoryName = Path.Combine(_configuration.GetTestResultDirectory(), runDeploymentRoot, "In", _environment.MachineName);
string directoryName = relativeResultsDirectory is null
? Path.Combine(_configuration.GetTestResultDirectory(), runDeploymentRoot, "In", _environment.MachineName)
: Path.Combine(_configuration.GetTestResultDirectory(), runDeploymentRoot, "In", relativeResultsDirectory, _environment.MachineName);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
Expand Down Expand Up @@ -580,7 +582,7 @@ private SummaryCounts AddResults(TestNodeUpdateMessage[] testNodeUpdateMessages,
{
resultFiles ??= new XElement("ResultFiles");

string href = CopyArtifactIntoTrxDirectoryAndReturnHrefValue(testFileArtifact.FileInfo, runDeploymentRoot);
string href = CopyArtifactIntoTrxDirectoryAndReturnHrefValue(testFileArtifact.FileInfo, runDeploymentRoot, executionId);
resultFiles.Add(new XElement(
"ResultFile",
new XAttribute("path", href)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.Xml.Linq;

namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;

[TestClass]
Expand Down Expand Up @@ -36,6 +38,38 @@ public async Task Trx_WhenReportTrxIsSpecified_TrxReportIsGeneratedInDefaultLoca
await AssertTrxReportWasGeneratedAsync(testHostResult, trxPathPattern, 1);
}

[DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))]
[TestMethod]
public async Task Trx_WhenReportTrxAndResultsDirectoryAreSpecifiedWithArtifact_ArtifactIsCopiedUnderRelativeResultsDirectory(string tfm)
{
string fileName = Guid.NewGuid().ToString("N");
string testResultsPath = Path.Combine(AssetFixture.TargetAssetPath, Guid.NewGuid().ToString("N"));
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, TestAssetFixture.AssetName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync(
$"--report-trx --report-trx-filename {fileName}.trx --results-directory \"{testResultsPath}\"",
new() { ["WITH_ARTIFACT"] = "1" },
cancellationToken: TestContext.CancellationToken);

testHostResult.AssertExitCodeIs(ExitCode.Success);

string[] trxFiles = Directory.GetFiles(testResultsPath, $"{fileName}.trx", SearchOption.AllDirectories);
Assert.HasCount(1, trxFiles, $"Expected exactly one trx file but found {trxFiles.Length}: {string.Join(", ", trxFiles)}");

var trxDocument = XDocument.Parse(File.ReadAllText(trxFiles[0]));
XNamespace ns = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010";
XElement unitTestResult = trxDocument.Descendants(ns + "UnitTestResult").Single();
string relativeResultsDirectory = unitTestResult.Attribute("relativeResultsDirectory")!.Value;
string resultFilePath = unitTestResult.Descendants(ns + "ResultFile").Single().Attribute("path")!.Value;
string runDeploymentRoot = trxDocument.Descendants(ns + "Deployment").Single().Attribute("runDeploymentRoot")!.Value;
string normalizedResultFilePath = resultFilePath.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);

string copiedArtifactPath = Path.Combine(testResultsPath, runDeploymentRoot, "In", relativeResultsDirectory, normalizedResultFilePath);
Assert.IsTrue(File.Exists(copiedArtifactPath), $"Expected copied artifact at '{copiedArtifactPath}' but it was not found.");

string legacyArtifactPath = Path.Combine(testResultsPath, runDeploymentRoot, "In", normalizedResultFilePath);
Assert.IsFalse(File.Exists(legacyArtifactPath), $"Artifact was copied to legacy path '{legacyArtifactPath}'.");
}

[DynamicData(nameof(TargetFrameworks.NetForDynamicData), typeof(TargetFrameworks))]
[TestMethod]
public async Task Trx_WhenTestHostCrash_ErrorIsDisplayedInsideTheTrx(string tfm)
Expand Down Expand Up @@ -252,8 +286,16 @@ public async Task ExecuteRequestAsync(ExecuteRequestContext context)
}

var testMethodIdentifier = new TestMethodIdentifierProperty(string.Empty, string.Empty, "DummyClassName", "Test", 0, Array.Empty<string>(), string.Empty);
PropertyBag properties = new(PassedTestNodeStateProperty.CachedInstance, testMethodIdentifier);
if (Environment.GetEnvironmentVariable("WITH_ARTIFACT") == "1")
{
string artifactPath = Path.Combine(Directory.GetCurrentDirectory(), "test-artifact.txt");
File.WriteAllText(artifactPath, "artifact");
properties.Add(new FileArtifactProperty(new FileInfo(artifactPath), "TestMethod", "description"));
}

await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
new TestNode() { Uid = "0", DisplayName = "Test", Properties = new(PassedTestNodeStateProperty.CachedInstance, testMethodIdentifier) }));
new TestNode() { Uid = "0", DisplayName = "Test", Properties = properties }));
context.Complete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ public async Task TrxReportEngine_GenerateReportAsync_WithArtifactsByTestNode_Tr
Assert.IsNotNull(memoryStream.TrxContent);
XDocument xml = memoryStream.TrxContent;
AssertTrxOutcome(xml, "Completed");
string relativeResultsDirectory = xml.Descendants().Single(x => x.Name.LocalName == "UnitTestResult").Attribute("relativeResultsDirectory")!.Value;
string expectedDestinationSuffix = Path.Combine("_MachineName_0001-01-01_00_00_00.0000000", "In", relativeResultsDirectory, "MachineName", "fileName");
string trxContent = xml.ToString();
string trxContentsPattern = @"
<UnitTestResult .* testName=""TestMethod"" .* outcome=""Passed"" .*>
Expand All @@ -478,6 +480,13 @@ public async Task TrxReportEngine_GenerateReportAsync_WithArtifactsByTestNode_Tr
</UnitTestResult>
";
Assert.IsTrue(Regex.IsMatch(trxContent, trxContentsPattern));
_fileSystem.Verify(
x => x.CopyFile(
It.Is<string>(source => source.EndsWith("fileName", StringComparison.Ordinal)),
It.Is<string>(destination => destination.EndsWith(
expectedDestinationSuffix,
StringComparison.Ordinal))),
Times.Once);
}

[TestMethod]
Expand Down