Repository navigation
Run Microsoft.Testing.Platform test apps under vstest.console and datacollector #16201
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
Jakub Jareš (nohwnd)
merged 15 commits into
microsoft:main
from
nohwnd:nohwnd-mtp-under-vstest
Jul 3, 2026
+2,481
−18
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
239053d
MTP: detect Microsoft.Testing.Platform apps and carry ExecutionPrefer…
nohwnd 14286e7
MTP: add JSON-RPC client, node converter, and proxies; route MTP sources
nohwnd ae8407a
Add pure-MTP MSTest test asset for MTP-under-vstest e2e
nohwnd 8862b97
Register sentinel runtime provider for MTP sources so they aren't rej…
nohwnd 9686819
Wire datacollector (code coverage) into the MTP execution path
nohwnd fc86406
Decline MTP sources in the vstest testhost providers
nohwnd 46c78f2
Add pure-MTP test asset with zero vstest dependency (Phase B proof)
nohwnd 00c8466
Make runtime-provider selection source-aware (two-pass)
nohwnd a479733
Detect MTP from the source in TestEngine, not ExecutionPreference
nohwnd e85429e
Remove ExecutionPreference; MTP is detected from the source
nohwnd 6052e57
Skip source-aware providers in the source-blind host-resolution pass
nohwnd c7007c8
Move MTP JSON-RPC wire from System.Text.Json to Jsonite (all TFMs)
nohwnd 6e2ba82
Make MTP routing a first-class runtime provider instead of TestEngine…
nohwnd 20496d2
Add MTP-under-vstest acceptance tests
nohwnd 6566441
Fix TypesToLoadAttributeTests for the new MTP runtime provider
nohwnd 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
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
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
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
139 changes: 139 additions & 0 deletions
139
src/Microsoft.TestPlatform.CoreUtilities/Helpers/MicrosoftTestingPlatformDetector.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,139 @@ | ||
| // 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; | ||
| using System.IO; | ||
| using System.Reflection.Metadata; | ||
| using System.Reflection.PortableExecutable; | ||
|
|
||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel; | ||
|
|
||
| namespace Microsoft.VisualStudio.TestPlatform.CoreUtilities.Helpers; | ||
|
|
||
| /// <summary> | ||
| /// Detects whether an assembly is a Microsoft.Testing.Platform application by reading the | ||
| /// <c>[assembly: AssemblyMetadata("Microsoft.Testing.Platform.Application", "true")]</c> attribute | ||
| /// that the Microsoft.Testing.Platform MSBuild targets stamp onto the entry assembly at build time. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This lives in CoreUtilities so both the up-front detection in vstest.console and the routing | ||
| /// decision in the CrossPlatEngine TestEngine can share the exact same logic instead of duplicating | ||
| /// the (subtle) custom-attribute blob parsing. | ||
| /// </remarks> | ||
| internal static class MicrosoftTestingPlatformDetector | ||
| { | ||
| private const string MicrosoftTestingPlatformApplicationMetadataKey = "Microsoft.Testing.Platform.Application"; | ||
|
|
||
| /// <summary> | ||
| /// Returns <see langword="true"/> if the assembly at <paramref name="filePath"/> is a | ||
| /// Microsoft.Testing.Platform application. Never throws; returns <see langword="false"/> on any error. | ||
| /// </summary> | ||
| public static bool IsMicrosoftTestingPlatformApp(string filePath) | ||
| { | ||
| try | ||
| { | ||
| using var assemblyStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); | ||
| var result = IsMicrosoftTestingPlatformApp(assemblyStream); | ||
| EqtTrace.Info("MicrosoftTestingPlatformDetector.IsMicrosoftTestingPlatformApp: '{0}' for source: '{1}'", result, filePath); | ||
| return result; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| EqtTrace.Warning("MicrosoftTestingPlatformDetector.IsMicrosoftTestingPlatformApp: failed to read assembly metadata, exception: {0} for assembly: {1}", ex, filePath); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns <see langword="true"/> if <paramref name="assemblyStream"/> is a Microsoft.Testing.Platform | ||
| /// application. The caller owns the stream lifetime. | ||
| /// </summary> | ||
| public static bool IsMicrosoftTestingPlatformApp(Stream assemblyStream) | ||
| { | ||
| using var peReader = new PEReader(assemblyStream); | ||
| if (!peReader.HasMetadata) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var metadataReader = peReader.GetMetadataReader(); | ||
|
|
||
| // Microsoft.Testing.Platform applications are marked at build time with | ||
| // [assembly: AssemblyMetadata("Microsoft.Testing.Platform.Application", "true")] by the | ||
| // Microsoft.Testing.Platform MSBuild targets. We only look at assembly-level attributes. | ||
| foreach (var handle in metadataReader.GetAssemblyDefinition().GetCustomAttributes()) | ||
| { | ||
| var attribute = metadataReader.GetCustomAttribute(handle); | ||
| if (!IsAssemblyMetadataAttribute(metadataReader, attribute)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| // AssemblyMetadataAttribute has a (string key, string value) constructor. The custom attribute | ||
| // blob is: 2-byte prolog (0x0001), then the two serialized strings, then the named-argument count. | ||
| var blob = metadataReader.GetBlobReader(attribute.Value); | ||
| if (blob.ReadUInt16() != 1) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var key = blob.ReadSerializedString(); | ||
| var value = blob.ReadSerializedString(); | ||
| if (string.Equals(key, MicrosoftTestingPlatformApplicationMetadataKey, StringComparison.Ordinal) | ||
| && string.Equals(value, "true", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| EqtTrace.Verbose("MicrosoftTestingPlatformDetector.IsMicrosoftTestingPlatformApp: could not decode AssemblyMetadata attribute: {0}", ex); | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static bool IsAssemblyMetadataAttribute(MetadataReader metadataReader, CustomAttribute attribute) | ||
| { | ||
| StringHandle typeNameHandle; | ||
| StringHandle typeNamespaceHandle; | ||
| switch (attribute.Constructor.Kind) | ||
| { | ||
| case HandleKind.MemberReference: | ||
| var memberReference = metadataReader.GetMemberReference((MemberReferenceHandle)attribute.Constructor); | ||
| switch (memberReference.Parent.Kind) | ||
| { | ||
| case HandleKind.TypeReference: | ||
| var typeReference = metadataReader.GetTypeReference((TypeReferenceHandle)memberReference.Parent); | ||
| typeNameHandle = typeReference.Name; | ||
| typeNamespaceHandle = typeReference.Namespace; | ||
| break; | ||
| case HandleKind.TypeDefinition: | ||
| var typeDefinition = metadataReader.GetTypeDefinition((TypeDefinitionHandle)memberReference.Parent); | ||
| typeNameHandle = typeDefinition.Name; | ||
| typeNamespaceHandle = typeDefinition.Namespace; | ||
| break; | ||
| default: | ||
| return false; | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| case HandleKind.MethodDefinition: | ||
| var methodDefinition = metadataReader.GetMethodDefinition((MethodDefinitionHandle)attribute.Constructor); | ||
| var declaringType = metadataReader.GetTypeDefinition(methodDefinition.GetDeclaringType()); | ||
| typeNameHandle = declaringType.Name; | ||
| typeNamespaceHandle = declaringType.Namespace; | ||
| break; | ||
|
|
||
| default: | ||
| return false; | ||
| } | ||
|
|
||
| return string.Equals(metadataReader.GetString(typeNameHandle), "AssemblyMetadataAttribute", StringComparison.Ordinal) | ||
| && string.Equals(metadataReader.GetString(typeNamespaceHandle), "System.Reflection", StringComparison.Ordinal); | ||
| } | ||
| } |
93 changes: 93 additions & 0 deletions
93
src/Microsoft.TestPlatform.CrossPlatEngine/Client/MTP/MtpClientHelpers.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,93 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| #if NETCOREAPP | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Text.Json; | ||
|
|
||
| using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; | ||
|
|
||
| namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.MTP; | ||
|
|
||
| /// <summary> | ||
| /// Shared helpers for the MTP proxies. | ||
| /// </summary> | ||
| internal static class MtpClientHelpers | ||
| { | ||
| public static Dictionary<string, object?> InitializeParameters() | ||
| => new() | ||
| { | ||
| ["processId"] = GetCurrentProcessId(), | ||
| ["clientInfo"] = new Dictionary<string, object?> | ||
| { | ||
| ["name"] = "vstest", | ||
| ["version"] = "1.0.0", | ||
| }, | ||
| ["capabilities"] = new Dictionary<string, object?> | ||
| { | ||
| ["testing"] = new Dictionary<string, object?> | ||
| { | ||
| ["debuggerProvider"] = false, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| public static TestMessageLevel MapLevel(string level) | ||
| => level switch | ||
| { | ||
| "Error" or "Critical" => TestMessageLevel.Error, | ||
| "Warning" => TestMessageLevel.Warning, | ||
| _ => TestMessageLevel.Informational, | ||
| }; | ||
|
|
||
| public static TimeSpan GetConnectionTimeout() | ||
| { | ||
| // Reuse vstest's connection timeout knob so users can extend it in slow environments. | ||
| string? value = Environment.GetEnvironmentVariable("VSTEST_CONNECTION_TIMEOUT"); | ||
| if (!string.IsNullOrEmpty(value) && int.TryParse(value, out int seconds) && seconds > 0) | ||
| { | ||
| return TimeSpan.FromSeconds(seconds); | ||
| } | ||
|
|
||
| return TimeSpan.FromSeconds(90); | ||
| } | ||
|
|
||
| private static int GetCurrentProcessId() | ||
| { | ||
| using var process = Process.GetCurrentProcess(); | ||
| return process.Id; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns true when a <c>testing/testUpdates/tests</c> notification is the completion sentinel | ||
| /// (its <c>changes</c> array is <c>null</c>). | ||
| /// </summary> | ||
| public static bool IsCompletionSentinel(JsonElement parameters) | ||
| => parameters.ValueKind != JsonValueKind.Object | ||
| || !parameters.TryGetProperty(MtpConstants.ChangesProperty, out JsonElement changes) | ||
| || changes.ValueKind == JsonValueKind.Null; | ||
|
|
||
| /// <summary> | ||
| /// Enumerates the <c>node</c> objects carried by a <c>testing/testUpdates/tests</c> notification. | ||
| /// </summary> | ||
| public static IEnumerable<JsonElement> EnumerateNodes(JsonElement parameters) | ||
| { | ||
| if (parameters.ValueKind != JsonValueKind.Object || !parameters.TryGetProperty(MtpConstants.ChangesProperty, out JsonElement changes) || changes.ValueKind != JsonValueKind.Array) | ||
| { | ||
| yield break; | ||
| } | ||
|
|
||
| foreach (JsonElement change in changes.EnumerateArray()) | ||
| { | ||
| if (change.ValueKind == JsonValueKind.Object && change.TryGetProperty(MtpConstants.NodeProperty, out JsonElement node) && node.ValueKind == JsonValueKind.Object) | ||
| { | ||
| yield return node; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
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.