Skip to content

Add unit tests for DOTNET_ROOT forwarding logic and remote process architecture detection#15668

Draft
nohwnd wants to merge 1 commit intomicrosoft:mainfrom
nohwnd:test/dotnet-root-forwarding-coverage
Draft

Add unit tests for DOTNET_ROOT forwarding logic and remote process architecture detection#15668
nohwnd wants to merge 1 commit intomicrosoft:mainfrom
nohwnd:test/dotnet-root-forwarding-coverage

Conversation

@nohwnd
Copy link
Copy Markdown
Member

@nohwnd nohwnd commented Apr 13, 2026

This PR adds unit tests for several under-tested code paths in dotnet test host startup.

What's covered

DotnetTestHostManagerTests.cs — 7 new tests targeting early DOTNET_ROOT path-setting and legacy testhost handling:

  • Verifies DOTNET_ROOT_<ARCH> is set early (before testhost.exe found) so xunit v3 child-processes pick it up
  • Verifies the early-set path is skipped when the feature flag is enabled
  • Verifies existing DOTNET_ROOT_<ARCH> env vars are not overwritten
  • Covers the legacy (old-testhost) x64 and x86 DOTNET_ROOT paths
  • Covers architecture-mismatch guard on old testhost path

ProcessHelperTests.cs (new file) — 3 tests for remote-process architecture detection:

  • GetCurrentProcessArchitecture returns a valid Architecture value
  • Repeated calls return the same cached result
  • GetProcessArchitecture throws NotImplementedException on non-Windows (expected guard)

Why these tests

These paths had no test coverage despite being critical to test host startup on Linux/macOS and to multi-architecture scenarios. The 7-test DotnetTestHostManager block and 3-test ProcessHelper block together close a meaningful reliability gap.

…icrosoft#15396

PR microsoft#15266 (Set dotnet_root_<arch> always):
- Test that DOTNET_ROOT_<ARCH> is set early even on non-Windows
- Test that the feature flag VSTEST_DISABLE_DOTNET_ROOT_ON_NONWINDOWS
  suppresses the early set
- Test that an existing DOTNET_ROOT_<ARCH> in the environment is never
  overridden (user's explicit value takes priority)
- Test that omitting VSTEST_DOTNET_ROOT_ARCHITECTURE while providing
  VSTEST_DOTNET_ROOT_PATH throws a descriptive InvalidOperationException

PR microsoft#15184 (Handle dotnet_root in testhost version-aware way):
- Test legacy DOTNET_ROOT forwarding for old x64 testhost (dll absent)
- Test legacy DOTNET_ROOT(x86) forwarding for old x86 testhost (dll absent)
- Test that architecture mismatch prevents any DOTNET_ROOT being set

PR microsoft#15396 (Determine architecture of remote process on Windows):
- Test GetCurrentProcessArchitecture returns a recognised PlatformArchitecture
- Test GetCurrentProcessArchitecture is consistent across calls (caching)
- Test GetProcessArchitecture throws NotImplementedException on non-Windows

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 13, 2026 11:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds unit tests to improve coverage of dotnet testhost startup behavior (DOTNET_ROOT forwarding) and process architecture detection logic used by hang-dumper/procdump scenarios.

Changes:

  • Added ProcessHelper unit tests around current/remote process architecture detection and platform guards.
  • Added DotnetTestHostManager unit tests for early DOTNET_ROOT_<ARCH> forwarding and legacy DOTNET_ROOT / DOTNET_ROOT(x86) handling paths.
  • Expanded InternalsVisibleTo to allow new tests to access internal FeatureFlag APIs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
test/Microsoft.TestPlatform.TestHostProvider.UnitTests/System/ProcessHelperTests.cs New tests for GetCurrentProcessArchitecture / GetProcessArchitecture behavior across OSes.
test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs New test coverage for early DOTNET_ROOT forwarding, feature-flag behavior, and old testhost legacy env vars.
src/Microsoft.TestPlatform.CoreUtilities/Friends.cs Grants internals access to additional unit test assemblies needed by the new tests.

Comment on lines +34 to +39
// Must be one of the known values — never throw.
Assert.IsTrue(
arch is PlatformArchitecture.X64
or PlatformArchitecture.X86
or PlatformArchitecture.ARM64
or PlatformArchitecture.ARM,
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetCurrentProcessArchitecture can return additional PlatformArchitecture values (e.g., S390x/Ppc64le/RiscV64/LoongArch64). This assertion currently only allows X86/X64/ARM/ARM64, so it will fail on those architectures; consider checking Enum.IsDefined(typeof(PlatformArchitecture), arch) (or including all enum values) instead of hard-coding a subset.

Suggested change
// Must be one of the known values — never throw.
Assert.IsTrue(
arch is PlatformArchitecture.X64
or PlatformArchitecture.X86
or PlatformArchitecture.ARM64
or PlatformArchitecture.ARM,
// Must be a defined PlatformArchitecture value — never throw.
Assert.IsTrue(
Enum.IsDefined(typeof(PlatformArchitecture), arch),

Copilot uses AI. Check for mistakes.
{
// Old testhost running x64 — DOTNET_ROOT must be set (not DOTNET_ROOT_X64).
// Triggered when: feature flag set + testhost.exe found + testhost.dll absent (null version).
var path = @"C:\dotnet";
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is named/structured as an x64 scenario but it relies on DotnetTestHostManager's current TargetPlatform default (from prior Initialize call). On ARM64 (or other) agents that default won’t be X64, and the legacy DOTNET_ROOT forwarding branch won’t run (architectureFromEnv != _architecture). To make the test robust, explicitly initialize the host manager with TargetPlatform=x64 within the test (similar to the x86 test below).

Suggested change
var path = @"C:\dotnet";
var path = @"C:\dotnet";
_dotnetHostManager.Initialize(
_messageLogger,
"<RunSettings><RunConfiguration><TargetPlatform>x64</TargetPlatform></RunConfiguration></RunSettings>");

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants