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
6 changes: 5 additions & 1 deletion docs/winui-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ Requirements and limitations:
- `packagedClassicApp`/`win32App` hosts receive MTP arguments as normal process `argv`, including classic hosts whose trust level is `appContainer`.
- `windowsApp`/UWP hosts receive one opaque string through `LaunchActivatedEventArgs.Arguments`. Restore the platform argument array with `PackagedAppExtensions.GetTestApplicationArguments(args.Arguments)` before `TestApplication.CreateBuilderAsync`; see [Launch activation](#launch-activation).
- The controller named pipe additionally authorizes the exact package SID of an AppContainer host, which a restricted AppContainer token needs in order to connect at all; see [Controller pipe access for AppContainer hosts](#controller-pipe-access-for-appcontainer-hosts).
- Native .NET SDK `dotnet test` execution-ID handoff is supported for packaged full-trust WinUI. Use the `InvokeTestingPlatform` MSBuild target for AppContainer-hosted WinUI and UWP projects so the sidecar can recover sandbox-owned result artifacts.
- Native .NET SDK 10 project-based `dotnet test` (including `--no-build`) starts the sidecar for packaged full-trust WinUI. Only the activated host connects to the SDK pipe, reporting its actual runtime, architecture, execution ID and test results. Controller-generated TRX and other reports remain on disk in the requested results directory; they are not advertised to the native SDK.
- Native `--help` and `--list-tests` activate the selected host to report its own options and tests, not the sidecar's dummy framework. Both require Developer Mode and register the package. SDK 10 can hide a help launch failure behind its built-in help and exit zero; use `--list-tests` to surface that failure. Help lists host options (including MSTest's `--filter`), while registration of those options for controller-backed execution is separate from routing.
- Native AppContainer execution is explicitly rejected. Use the `InvokeTestingPlatform` MSBuild target for AppContainer-hosted WinUI and UWP projects so the sidecar can recover sandbox-owned result artifacts. Ctrl+C tears down the owned informational host, but externally hard-killing the sidecar can leave an AUMID-activated help/discovery host running.
- Project-based `dotnet run` uses the same SDK launch properties and therefore also starts the sidecar for packaged applications. Startup metadata lives beside a project-local controller in the intermediate output directory, leaving application arguments empty when needed so SDK launch-profile arguments and explicit-argument precedence are preserved. The staged files are incremental and cleaned by `dotnet clean`. Customized `RunCommand` and `TestingPlatformExecutablePath` values are not replaced. A relative `TestingPlatformPackagedAppTargetPath` is resolved against the project directory; `UseAppHost=false` requires a staged packaged executable override.
- `dotnet run -- --help` and `dotnet run -- --list-tests` activate the actual full-trust host and relay its options or test names to the caller. An incomplete informational response is an error rather than successful empty output. AppContainer informational launches still require the `InvokeTestingPlatform` path.

See [#9933](https://github.com/microsoft/testfx/issues/9933) for the implementation of this path.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// 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.Extensions.PackagedApp;
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions.TestFramework;
using Microsoft.Testing.Platform.Extensions.TestHostControllers;

using CodeCoverageBuilderHook = Microsoft.Testing.Extensions.CodeCoverage.TestingPlatformBuilderHook;
using HangDumpBuilderHook = Microsoft.Testing.Extensions.HangDump.TestingPlatformBuilderHook;
Expand All @@ -20,9 +23,96 @@ internal static class Program

public static async Task<int> Main(string[] args)
{
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args).ConfigureAwait(false);
bool isInformationalRequest;
try
{
string bootstrapFile = Path.Combine(AppContext.BaseDirectory, PackagedAppControllerArguments.BootstrapFileName);
string[]? bootstrapArguments = File.Exists(bootstrapFile)
? await File.ReadAllLinesAsync(bootstrapFile).ConfigureAwait(false)
: null;
(args, isInformationalRequest) = PackagedAppControllerArguments.Configure(
args, Environment.GetEnvironmentVariable, Environment.SetEnvironmentVariable, bootstrapArguments);
}
catch (FormatException exception)
{
await Console.Error.WriteLineAsync(exception.Message).ConfigureAwait(false);
return 5;
}
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException)
{
await Console.Error.WriteLineAsync(exception.Message).ConfigureAwait(false);
return 4;
}

HashSet<string> enabledExtensions = GetEnabledExtensions();
if (isInformationalRequest)
{
using CancellationTokenSource cancellation = new();
ConsoleCancelEventHandler cancelHandler = (_, eventArgs) =>
{
eventArgs.Cancel = true;
cancellation.Cancel();
};
Console.CancelKeyPress += cancelHandler;
try
{
var environment = Environment.GetEnvironmentVariables()
.Cast<DictionaryEntry>()
.ToDictionary(entry => entry.Key.ToString()!, entry => entry.Value?.ToString(), StringComparer.OrdinalIgnoreCase);
TestHostLaunchContext context = new(
Environment.GetEnvironmentVariable(PackagedAppControllerArguments.TargetEnvironmentVariable)!,
args,
environment,
Environment.CurrentDirectory);
CommandLineInformationalOutput? output =
Environment.GetEnvironmentVariable(PackagedAppControllerArguments.NativePipeEnvironmentVariable) is null or ""
? new(context.FileName, args, Console.Out)
: null;
try
{
if (output is not null)
{
context = new(
context.FileName,
PackagedAppControllerArguments.AddNativeTransport(context.Arguments, output.PipeName),
context.EnvironmentVariables,
context.WorkingDirectory);
}

int exitCode = await PackagedAppNativeInformationalLaunch.RunAsync(
new PackagedAppTestHostLauncher(),
context,
enabledExtensions.Contains("packagedapp"),
Console.Error,
cancellation.Token).ConfigureAwait(false);
if (output is not null && exitCode == 0)
{
await output.CompleteAsync().ConfigureAwait(false);
}

return exitCode;
}
finally
{
if (output is not null)
{
await output.DisposeAsync().ConfigureAwait(false);
}
}
}
catch (Exception exception) when (exception is IOException or InvalidOperationException)
{
await Console.Error.WriteLineAsync(exception.Message).ConfigureAwait(false);
return 4;
}
finally
{
Console.CancelKeyPress -= cancelHandler;
}
}

ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args).ConfigureAwait(false);

if (enabledExtensions.Contains("msbuild"))
{
MSBuildBuilderHook.AddExtensions(builder, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,62 @@ override is preserved and receives no controller-specific environment values.
Missing or incomplete controller layouts fail explicitly. Runtime and protocol
errors remain failures; the targets never silently fall back to another host.

### Native `dotnet test`

With .NET SDK 10's Microsoft.Testing.Platform runner, project-based `dotnet test`
uses the same sidecar for packaged **full-trust** .NET applications, including
`--no-build`. `ComputeRunArguments` stages the controller under the project's
intermediate output directory, beside a versioned startup file containing the
selected executable and extension list. The application's argument tail remains
unchanged, including an empty tail, so the SDK retains ownership of launch-profile
selection and explicit-argument precedence. The sidecar also accepts the original
inline v1 startup prefix. Relative `TestingPlatformPackagedAppTargetPath`
values are resolved against the project directory. A customized `RunCommand` is
left unchanged. `UseAppHost=false` requires an explicit staged `.exe` target.

Only the activated test host connects to the native SDK pipe. Its real runtime,
architecture, tests and shared execution ID are reported for test runs, while the sidecar's exit
code reflects host failures and controller finalization. This avoids SDK 10's
requirement that all connected processes use the same runtime and architecture.
Retries retain that pipe and execution ID. Controller-generated reports such as
TRX are written to the requested results directory but are **not advertised as
artifacts to the native SDK**.

Native `--help` and `--list-tests` activate the selected host directly to describe
its actual options and tests, never the sidecar's dummy framework. They require
the same Developer Mode and package registration as execution, but use the host's
own execution ID rather than the controller-to-host handoff used by test runs. SDK 10 ignores
launch failures during `--help`: if only SDK options appear, use `--list-tests`
to surface the launch error. Help lists host options, including MSTest's `--filter`,
even when the controller has not yet registered them for execution.
SDK 10 can also forward a zero exit code from a host that exits before connecting,
producing an empty discovery. Host startup must actually initialize MTP rather
than exit or redirect activation before entering the test platform.
Native AppContainer execution is rejected: the SDK pipe does not authorize the
package SID. Use `InvokeTestingPlatform`
for AppContainer/modern UWP/classic UAP execution and controller-managed artifacts.
Framework-specific options such as MSTest's `--filter` require corresponding
registration in the controller; routing alone does not add that support.
Informational activation handles Ctrl+C by terminating its owned host, but an
external hard kill of the sidecar cannot guarantee teardown of an AUMID-activated
help/discovery host.

`ComputeRunArguments` is also used by `dotnet run`, so project-based `dotnet run`
now starts the sidecar for these same packaged .NET applications rather than
starting the host without package identity. Unpackaged projects and explicit
executable overrides retain their original run command.
Project launch-profile `commandLineArgs` are used when no explicit application
arguments are supplied; `--no-launch-profile` and explicit arguments keep the
SDK's normal precedence. The staged controller is incremental and removed by
`dotnet clean`, including when it was created by a no-build launch query.

`dotnet run -- --help` and `dotnet run -- --list-tests` also activate the actual
full-trust host. Since AUMID activation does not inherit the caller's standard
output, the sidecar relays host options and discovered tests over the existing MTP
named-pipe protocol. Missing or incomplete help/discovery responses are failures,
not successful empty output. This informational pipe has the same AppContainer
restriction as native SDK discovery; use `InvokeTestingPlatform` for those hosts.

The legacy executable name `mstest-appmodel-controller.exe` and the
`MSTEST_APPMODEL_CONTROLLER_EXTENSIONS` and `TESTINGPLATFORM_PACKAGEDAPP_TARGET`
environment identifiers are intentionally preserved for compatibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<_TestingPlatformPackagedAppTarget Condition="'$(_IsExecutable)' == 'true' AND '$(UseAppHost)' == 'true' AND Exists('$(TargetDir)$(AssemblyName)$(_NativeExecutableExtension)')">$(TargetDir)$(AssemblyName)$(_NativeExecutableExtension)</_TestingPlatformPackagedAppTarget>
<_TestingPlatformPackagedAppTarget Condition="$(TargetFramework.StartsWith('uap')) AND Exists('$(TargetDir)$(AssemblyName).exe')">$(TargetDir)$(AssemblyName).exe</_TestingPlatformPackagedAppTarget>
<_TestingPlatformPackagedAppTarget Condition="'$(TestingPlatformPackagedAppTargetPath)' != ''">$(TestingPlatformPackagedAppTargetPath)</_TestingPlatformPackagedAppTarget>
<_TestingPlatformPackagedAppTarget>$([MSBuild]::NormalizePath('$(MSBuildProjectDirectory)', '$(_TestingPlatformPackagedAppTarget)'))</_TestingPlatformPackagedAppTarget>
</PropertyGroup>
<ItemGroup Condition="'$(_TestingPlatformPackagedAppControllerPath)' != '' AND '$(TestingPlatformExecutablePath)' == '$(_TestingPlatformPackagedAppControllerPath)'">
<!-- Keep these protocol identifiers compatible with existing applications. -->
Expand All @@ -73,4 +74,64 @@
</ItemGroup>
</Target>

<!-- The native SDK evaluates this target in a fresh project, even when builds are skipped.
It consumes RunCommand/RunArguments, not TestingPlatformEnvironmentVariable. -->
<Target Name="_ConfigureTestingPlatformPackagedAppRunArguments"
AfterTargets="ComputeRunArguments"
DependsOnTargets="_PrepareTestingPlatformPackagedAppRunController">
<PropertyGroup Condition="'$(_TestingPlatformPackagedAppControllerPath)' != '' AND '$(TestingPlatformExecutablePath)' == '$(_TestingPlatformPackagedAppControllerPath)' AND '$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(StartAction)' != 'Program'">
<_TestingPlatformPackagedAppDefaultRunCommand Condition="'$(UseAppHost)' == 'true'">$(TargetDir)$(AssemblyName)$(_NativeExecutableExtension)</_TestingPlatformPackagedAppDefaultRunCommand>
<_TestingPlatformPackagedAppDefaultRunCommand Condition="'$(UseAppHost)' != 'true'">dotnet</_TestingPlatformPackagedAppDefaultRunCommand>
<_TestingPlatformPackagedAppConfigureRun Condition="'$(RunCommand)' == '$(_TestingPlatformPackagedAppDefaultRunCommand)'">true</_TestingPlatformPackagedAppConfigureRun>
<_TestingPlatformPackagedAppRunArguments>$(RunArguments)</_TestingPlatformPackagedAppRunArguments>
<_TestingPlatformPackagedAppRunArguments Condition="'$(UseAppHost)' != 'true' AND $(RunArguments.StartsWith('$(_NetCoreRunArguments)'))">$(RunArguments.Substring($(_NetCoreRunArguments.Length)))</_TestingPlatformPackagedAppRunArguments>
<_TestingPlatformPackagedAppConfigureRun Condition="'$(UseAppHost)' != 'true' AND !$(RunArguments.StartsWith('$(_NetCoreRunArguments)'))">false</_TestingPlatformPackagedAppConfigureRun>
<_TestingPlatformPackagedAppTargetExtension>$([System.IO.Path]::GetExtension('$(_TestingPlatformPackagedAppTarget)'))</_TestingPlatformPackagedAppTargetExtension>
</PropertyGroup>
<Error Condition="'$(_TestingPlatformPackagedAppConfigureRun)' == 'true' AND '$(_TestingPlatformPackagedAppTargetExtension)' != '.exe'"
Text="The packaged test host must be an executable. Enable UseAppHost or set TestingPlatformPackagedAppTargetPath to the staged packaged executable." />
<PropertyGroup Condition="'$(_TestingPlatformPackagedAppConfigureRun)' == 'true'">
<RunCommand>$(_TestingPlatformPackagedAppRunControllerDirectory)mstest-appmodel-controller.exe</RunCommand>
<!-- An empty argument tail must stay empty: dotnet run only applies launch-profile arguments
when RunArguments is empty. Keep startup metadata beside the project-local sidecar. -->
<RunArguments>$(_TestingPlatformPackagedAppRunArguments)</RunArguments>
<_TestingPlatformPackagedAppConfigureRun>false</_TestingPlatformPackagedAppConfigureRun>
</PropertyGroup>
</Target>

<Target Name="_PrepareTestingPlatformPackagedAppRunController"
AfterTargets="CopyFilesToOutputDirectory"
DependsOnTargets="_ConfigureTestingPlatformPackagedAppEnvironment"
Condition="'$(OS)' == 'Windows_NT' AND '$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(StartAction)' != 'Program'">
<PropertyGroup Condition="'$(_TestingPlatformPackagedAppControllerPath)' != '' AND '$(TestingPlatformExecutablePath)' == '$(_TestingPlatformPackagedAppControllerPath)' AND (('$(UseAppHost)' == 'true' AND '$(RunCommand)' == '$(TargetDir)$(AssemblyName)$(_NativeExecutableExtension)') OR ('$(UseAppHost)' != 'true' AND '$(RunCommand)' == 'dotnet'))">
<_TestingPlatformPackagedAppRunControllerDirectory>$([MSBuild]::NormalizeDirectory('$(MSBuildProjectDirectory)', '$(IntermediateOutputPath)', 'PackagedAppController'))</_TestingPlatformPackagedAppRunControllerDirectory>
<_TestingPlatformPackagedAppBootstrapFile>$(_TestingPlatformPackagedAppRunControllerDirectory)mstest-appmodel-controller.bootstrap</_TestingPlatformPackagedAppBootstrapFile>
</PropertyGroup>
<ItemGroup Condition="'$(_TestingPlatformPackagedAppRunControllerDirectory)' != ''">
<_TestingPlatformPackagedAppControllerFile Include="$(_TestingPlatformPackagedAppPackageRoot)tools\AppModelController\$(_TestingPlatformPackagedAppControllerTfm)\**\*" />
<_TestingPlatformPackagedAppBootstrapLine Include="--internal-packagedapp-controller-v1" />
<_TestingPlatformPackagedAppBootstrapLine Include="$([MSBuild]::Escape('$(_TestingPlatformPackagedAppTarget)'))" />
<_TestingPlatformPackagedAppBootstrapLine Include="$([MSBuild]::Escape('$(_TestingPlatformPackagedAppControllerExtensions)'))" />
<FileWrites Include="$(_TestingPlatformPackagedAppBootstrapFile)" />
</ItemGroup>
<Copy SourceFiles="@(_TestingPlatformPackagedAppControllerFile)"
DestinationFiles="@(_TestingPlatformPackagedAppControllerFile->'$(_TestingPlatformPackagedAppRunControllerDirectory)%(RecursiveDir)%(Filename)%(Extension)')"
SkipUnchangedFiles="true">
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
</Copy>
<WriteLinesToFile File="$(_TestingPlatformPackagedAppBootstrapFile)"
Lines="@(_TestingPlatformPackagedAppBootstrapLine)"
Overwrite="true"
WriteOnlyWhenDifferent="true"
Encoding="UTF-8"
Condition="'$(_TestingPlatformPackagedAppRunControllerDirectory)' != ''" />
</Target>

<!-- A no-build launch query has no IncrementalClean to persist FileWrites. Record the
staged files in MSBuild's normal clean manifest so a later clean still removes them. -->
<Target Name="_RecordTestingPlatformPackagedAppRunControllerFiles"
AfterTargets="_ConfigureTestingPlatformPackagedAppRunArguments"
DependsOnTargets="_CleanRecordFileWrites"
Condition="'$(_TestingPlatformPackagedAppBootstrapFile)' != ''" />

</Project>
Loading
Loading