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
11 changes: 6 additions & 5 deletions src/Adapter/MSTest.Engine/Engine/TestExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ internal sealed class TestExecutionContext : ITestExecutionContext
private readonly PlatformTestNode _platformTestNode;
private readonly ITrxReportCapability? _trxReportCapability;
private readonly SessionUid _sessionUid;
private readonly Func<IData, Task> _publishDataAsync;
private readonly CancellationToken _originalCancellationToken;

public TestExecutionContext(IConfiguration configuration, TestNode testNode, PlatformTestNode platformTestNode,
ITrxReportCapability? trxReportCapability, SessionUid sessionUid, Func<IData, Task> publishDataAsync, CancellationToken cancellationToken)
ITrxReportCapability? trxReportCapability, SessionUid sessionUid, CancellationToken cancellationToken)
{
Configuration = configuration;
_platformTestNode = platformTestNode;
_trxReportCapability = trxReportCapability;
_sessionUid = sessionUid;
_publishDataAsync = publishDataAsync;
TestInfo = new TestInfo(testNode);
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
_originalCancellationToken = cancellationToken;
Expand Down Expand Up @@ -76,8 +74,11 @@ OperationCanceledException canceledException
}
}

public async Task AddTestAttachmentAsync(FileInfo file, string displayName, string? description = null)
=> await _publishDataAsync(new TestNodeFileArtifact(_sessionUid, _platformTestNode, file, displayName, description));
public Task AddTestAttachmentAsync(FileInfo file, string displayName, string? description = null)
{
_platformTestNode.Properties.Add(new FileArtifactProperty(_sessionUid, file, displayName, description));
return Task.CompletedTask;
}

private static void AddTrxExceptionInformation(PropertyBag propertyBag, Exception? exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private async Task<Result> InvokeTestNodeAndPublishResultAsync(TestNode testNode
platformTestNode.Properties.Add(new TrxFullyQualifiedTypeNameProperty(platformTestNode.Uid.Value[..platformTestNode.Uid.Value.LastIndexOf('.')]));
}

TestExecutionContext testExecutionContext = new(_configuration, testNode, platformTestNode, _trxReportCapability, _sessionUid, _publishDataAsync, _cancellationToken);
TestExecutionContext testExecutionContext = new(_configuration, testNode, platformTestNode, _trxReportCapability, _sessionUid, _cancellationToken);
try
{
// If we're already enqueued we cancel the test before the start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ internal sealed class TrxReportGenerator :
private readonly TrxTestApplicationLifecycleCallbacks? _trxTestApplicationLifecycleCallbacks;
private readonly ILogger<TrxReportGenerator> _logger;
private readonly List<TestNodeUpdateMessage> _tests = [];
private readonly Dictionary<TestNodeUid, List<SessionFileArtifact>> _artifactsByTestNode = new();
private readonly Dictionary<IExtension, List<SessionFileArtifact>> _artifactsByExtension = new();
private readonly bool _isEnabled;

Expand Down Expand Up @@ -84,7 +83,6 @@ public TrxReportGenerator(
public Type[] DataTypesConsumed { get; } =
[
typeof(TestNodeUpdateMessage),
typeof(TestNodeFileArtifact),
typeof(SessionFileArtifact)
];

Expand Down Expand Up @@ -140,18 +138,7 @@ public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationTo
}

break;
case TestNodeFileArtifact testNodeFileArtifact:
if (!_artifactsByTestNode.TryGetValue(testNodeFileArtifact.TestNode.Uid, out List<SessionFileArtifact>? nodeFileArtifacts))
{
nodeFileArtifacts = [testNodeFileArtifact];
_artifactsByTestNode[testNodeFileArtifact.TestNode.Uid] = nodeFileArtifacts;
}
else
{
nodeFileArtifacts.Add(testNodeFileArtifact);
}

break;
case SessionFileArtifact fileArtifact:
if (!_artifactsByExtension.TryGetValue(dataProducer, out List<SessionFileArtifact>? sessionFileArtifacts))
{
Expand Down Expand Up @@ -235,7 +222,7 @@ public async Task OnTestSessionFinishingAsync(SessionUid sessionUid, Cancellatio

int exitCode = _testApplicationProcessExitCode.GetProcessExitCode();
TrxReportEngine trxReportGeneratorEngine = new(_testApplicationModuleInfo, _environment, _commandLineOptionsService, _configuration,
_clock, _tests.ToArray(), _failedTestsCount, _passedTestsCount, _notExecutedTestsCount, _timeoutTestsCount, _artifactsByExtension, _artifactsByTestNode,
_clock, _tests.ToArray(), _failedTestsCount, _passedTestsCount, _notExecutedTestsCount, _timeoutTestsCount, _artifactsByExtension,
_adapterSupportTrxCapability, _testFramework, _testStartTime.Value, exitCode, cancellationToken);
(string reportFileName, string? warning) = await trxReportGeneratorEngine.GenerateReportAsync();
if (warning is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ public async Task OnTestHostProcessExitedAsync(ITestHostProcessInformation testH
TrxReportEngine trxReportGeneratorEngine = new(_testApplicationModuleInfo, _environment, _commandLineOptions, _configuration,
_clock, [], 0, 0, 0, 0,
artifacts,
new Dictionary<TestNodeUid, List<SessionFileArtifact>>(),
adapterSupportTrxCapability: null,
new TestAdapterInfo(_testAdapterInformationRequest!.TestAdapterId, _testAdapterInformationRequest.TestAdapterVersion),
_startTime,
Expand Down Expand Up @@ -201,7 +200,6 @@ await _messageBus.PublishAsync(
TrxReportEngine trxReportGeneratorEngine = new(_testApplicationModuleInfo, _environment, _commandLineOptions, _configuration,
_clock, [], 0, 0, 0, 0,
artifacts,
new Dictionary<TestNodeUid, List<SessionFileArtifact>>(),
false,
new TestAdapterInfo(_testAdapterInformationRequest!.TestAdapterId, _testAdapterInformationRequest.TestAdapterVersion),
_startTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ internal sealed partial class TrxReportEngine
private readonly int _notExecutedTestsCount;
private readonly int _timeoutTestsCount;
private readonly Dictionary<IExtension, List<SessionFileArtifact>> _artifactsByExtension;
private readonly Dictionary<TestNodeUid, List<SessionFileArtifact>> _artifactsByTestNode;
private readonly bool? _adapterSupportTrxCapability;
private readonly ITestFramework _testFrameworkAdapter;
private readonly DateTimeOffset _testStartTime;
Expand All @@ -97,7 +96,7 @@ internal sealed partial class TrxReportEngine
private readonly IFileSystem _fileSystem;
private readonly bool _isCopyingFileAllowed;

public TrxReportEngine(ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, TestNodeUpdateMessage[] testNodeUpdatedMessages, int failedTestsCount, int passedTestsCount, int notExecutedTestsCount, int timeoutTestsCount, Dictionary<IExtension, List<SessionFileArtifact>> artifactsByExtension, Dictionary<TestNodeUid, List<SessionFileArtifact>> artifactsByTestNode, bool? adapterSupportTrxCapability, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, int exitCode, CancellationToken cancellationToken)
public TrxReportEngine(ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, TestNodeUpdateMessage[] testNodeUpdatedMessages, int failedTestsCount, int passedTestsCount, int notExecutedTestsCount, int timeoutTestsCount, Dictionary<IExtension, List<SessionFileArtifact>> artifactsByExtension, bool? adapterSupportTrxCapability, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, int exitCode, CancellationToken cancellationToken)
: this(
new SystemFileSystem(),
testApplicationModuleInfo,
Expand All @@ -111,7 +110,6 @@ public TrxReportEngine(ITestApplicationModuleInfo testApplicationModuleInfo, IEn
notExecutedTestsCount,
timeoutTestsCount,
artifactsByExtension,
artifactsByTestNode,
adapterSupportTrxCapability,
testFrameworkAdapter,
testStartTime,
Expand All @@ -120,7 +118,7 @@ public TrxReportEngine(ITestApplicationModuleInfo testApplicationModuleInfo, IEn
{
}

public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, TestNodeUpdateMessage[] testNodeUpdatedMessages, int failedTestsCount, int passedTestsCount, int notExecutedTestsCount, int timeoutTestsCount, Dictionary<IExtension, List<SessionFileArtifact>> artifactsByExtension, Dictionary<TestNodeUid, List<SessionFileArtifact>> artifactsByTestNode, bool? adapterSupportTrxCapability, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, int exitCode, CancellationToken cancellationToken, bool isCopyingFileAllowed = true)
public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment, ICommandLineOptions commandLineOptionsService, IConfiguration configuration, IClock clock, TestNodeUpdateMessage[] testNodeUpdatedMessages, int failedTestsCount, int passedTestsCount, int notExecutedTestsCount, int timeoutTestsCount, Dictionary<IExtension, List<SessionFileArtifact>> artifactsByExtension, bool? adapterSupportTrxCapability, ITestFramework testFrameworkAdapter, DateTimeOffset testStartTime, int exitCode, CancellationToken cancellationToken, bool isCopyingFileAllowed = true)
{
_testApplicationModuleInfo = testApplicationModuleInfo;
_environment = environment;
Expand All @@ -133,7 +131,6 @@ public TrxReportEngine(IFileSystem fileSystem, ITestApplicationModuleInfo testAp
_notExecutedTestsCount = notExecutedTestsCount;
_timeoutTestsCount = timeoutTestsCount;
_artifactsByExtension = artifactsByExtension;
_artifactsByTestNode = artifactsByTestNode;
_adapterSupportTrxCapability = adapterSupportTrxCapability;
_testFrameworkAdapter = testFrameworkAdapter;
_testStartTime = testStartTime;
Expand Down Expand Up @@ -521,17 +518,17 @@ private void AddResults(string testAppModule, XElement testRun, out XElement tes
unitTestResult.Add(output);
}

if (_artifactsByTestNode.TryGetValue(testNode.Uid, out List<SessionFileArtifact>? fileArtifacts))
XElement? resultFiles = null;
foreach (FileArtifactProperty testFileArtifact in testNode.Properties.OfType<FileArtifactProperty>())
{
var resultFiles = new XElement("ResultFiles");

foreach (SessionFileArtifact fileArtifact in fileArtifacts)
{
resultFiles.Add(new XElement(
"ResultFile",
new XAttribute("path", fileArtifact.FileInfo.FullName)));
}
resultFiles ??= new XElement("ResultFiles");
resultFiles.Add(new XElement(
"ResultFile",
new XAttribute("path", testFileArtifact.FileInfo.FullName)));
}

if (resultFiles is not null)
{
unitTestResult.Add(resultFiles);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void RecordAttachments(IList<AttachmentSet> attachmentSets)
{
_logger.LogTrace($"{nameof(FrameworkHandlerAdapter)}.RecordAttachments");
_frameworkHandle?.RecordAttachments(attachmentSets);
PublishAttachmentsAsync(attachmentSets).Await();
PublishTestSessionAttachmentsAsync(attachmentSets).Await();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -126,12 +126,10 @@ public void RecordResult(TestResult testResult)
_frameworkHandle?.RecordResult(testResult);

// Publish node state change to Microsoft Testing Platform
var testNode = testResult.ToTestNode(_isTrxEnabled, _clientInfo);
var testNode = testResult.ToTestNode(_isTrxEnabled, _clientInfo, _session.SessionUid);

var testNodeChange = new TestNodeUpdateMessage(_session.SessionUid, testNode);
_messageBus.PublishAsync(_adapterExtensionBase, testNodeChange).Await();

PublishAttachmentsAsync(testResult.Attachments, testNode).Await();
}

/// <inheritdoc/>
Expand All @@ -158,7 +156,7 @@ public void RecordStart(TestCase testCase)
public void SendMessage(TestMessageLevel testMessageLevel, string message)
=> _comboMessageLogger.SendMessage(testMessageLevel, message);

private async Task PublishAttachmentsAsync(IEnumerable<AttachmentSet> attachments, TestNode? testNode = null)
private async Task PublishTestSessionAttachmentsAsync(IEnumerable<AttachmentSet> attachments)
{
foreach (AttachmentSet attachmentSet in attachments)
{
Expand All @@ -169,9 +167,7 @@ private async Task PublishAttachmentsAsync(IEnumerable<AttachmentSet> attachment
throw new FormatException($"Test adapter {_adapterExtensionBase.DisplayName} only supports file attachments.");
}

SessionFileArtifact fileArtifact = testNode is null
? new SessionFileArtifact(_session.SessionUid, new(attachment.Uri.LocalPath), attachmentSet.DisplayName, attachment.Description)
: new TestNodeFileArtifact(_session.SessionUid, testNode, new(attachment.Uri.LocalPath), attachmentSet.DisplayName, attachment.Description);
var fileArtifact = new SessionFileArtifact(_session.SessionUid, new(attachment.Uri.LocalPath), attachmentSet.DisplayName, attachment.Description);
await _messageBus.PublishAsync(_adapterExtensionBase, fileArtifact);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static void CopyVSTestProperties(IEnumerable<TestProperty> testPropertie
/// <summary>
/// Converts a VSTest <see cref="TestResult"/> to a Microsoft Testing Platform <see cref="TestNode"/>.
/// </summary>
public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled, IClientInfo client)
public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled, IClientInfo client, SessionUid sessionUid)
{
var testNode = testResult.TestCase.ToTestNode(isTrxEnabled, client, testResult.DisplayName);
CopyVSTestProperties(testResult.Properties, testNode, testResult.TestCase, testResult.GetPropertyValue, isTrxEnabled, client);
Expand Down Expand Up @@ -179,6 +179,14 @@ public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled,
}
}

foreach (AttachmentSet attachmentSet in testResult.Attachments)
{
foreach (UriDataAttachment attachment in attachmentSet.Attachments)
{
testNode.Properties.Add(new FileArtifactProperty(sessionUid, new FileInfo(attachment.Uri.LocalPath), attachmentSet.DisplayName, attachment.Description));
}
}

if (standardErrorMessages.Count > 0)
{
testNode.Properties.Add(new StandardErrorProperty(string.Join(Environment.NewLine, standardErrorMessages)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ protected VSTestBridgedTestFrameworkBase(IServiceProvider serviceProvider, ITest
[
typeof(TestNodeUpdateMessage),
typeof(SessionFileArtifact),
typeof(TestNodeFileArtifact)
];

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public override string ToString()
/// <summary>
/// Represents a test node file artifact.
/// </summary>
[Obsolete("This API is obsolete and will be removed in v2. Instead of publishing this to MessageBus, add FileArtifactProperty property to the TestNode when publishing TestNodeUpdateMessage with a test result", error: true)]
public class TestNodeFileArtifact : SessionFileArtifact
{
/// <summary>
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 Microsoft.Testing.Platform.TestHost;

namespace Microsoft.Testing.Platform.Extensions.Messages;

/// <summary>
Expand Down Expand Up @@ -372,6 +374,15 @@ public record StandardOutputProperty(string StandardOutput) : IProperty;
[Experimental("TPEXP", UrlFormat = "https://aka.ms/testingplatform/diagnostics#{0}")]
public record StandardErrorProperty(string StandardError) : IProperty;

/// <summary>
/// Property that represents multiple artifacts/attachments to associate with a test node.
/// </summary>
/// <param name="SessionUid">The session UID.</param>
/// <param name="FileInfo">The file information.</param>
/// <param name="DisplayName">The display name.</param>
/// <param name="Description">The description.</param>
public record FileArtifactProperty(SessionUid SessionUid, FileInfo FileInfo, string DisplayName, string? Description = null) : IProperty;

internal sealed record SerializableKeyValuePairStringProperty(string Key, string Value) : KeyValuePairStringProperty(Key, Value);

internal sealed record SerializableNamedKeyValuePairsStringProperty(string Name, KeyValuePair<string, string>[] Pairs) : IProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public async Task InitializeAsync()
[
typeof(TestNodeUpdateMessage),
typeof(SessionFileArtifact),
typeof(TestNodeFileArtifact),
typeof(FileArtifact),
];

Expand Down Expand Up @@ -369,13 +368,10 @@ public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationTo
break;
}

break;

case TestNodeFileArtifact:
{
// TODO
}

// TODO:
// foreach (FileArtifactProperty testFileArtifact in testNodeStateChanged.TestNode.Properties.OfType<FileArtifactProperty>())
// {
// }
break;

case SessionFileArtifact:
Expand Down
Loading