Skip to content

Refactor TestHostBuilder into focused partial modules#8201

Merged
Evangelink merged 2 commits into
mainfrom
copilot/refactor-testhostbuilder-into-modules
May 14, 2026
Merged

Refactor TestHostBuilder into focused partial modules#8201
Evangelink merged 2 commits into
mainfrom
copilot/refactor-testhostbuilder-into-modules

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 13, 2026

TestHostBuilder.cs had grown into a large, multi-responsibility file that was hard to navigate and review. This change splits the implementation into focused partial class files while keeping the existing API and host-building behavior intact.

  • What changed

    • Reduced TestHostBuilder.cs to the class declaration, shared state, BuildAsync, and a small internal build context used to coordinate the refactored flow.
    • Moved common service setup into TestHostBuilder.CommonServices.cs.
    • Moved mode selection and host construction logic into TestHostBuilder.Modes.cs.
    • Moved shared private helpers into TestHostBuilder.Utilities.cs.
    • Moved test framework construction into TestHostBuilder.Framework.cs.
  • Resulting structure

    • TestHostBuilder.cs — thin orchestration entry point
    • TestHostBuilder.CommonServices.cs — shared service/configuration setup
    • TestHostBuilder.Modes.cs — tools, help/info, orchestrator, controller, and test-host mode dispatch
    • TestHostBuilder.Utilities.cs — shared private helpers
    • TestHostBuilder.Framework.cs — framework/message bus construction
  • Why this split

    • Separates setup, dispatch, framework construction, and utilities into reviewable units.
    • Keeps the top-level flow readable without changing public/internal surface area.
  • Example

    public async Task<IHost> BuildAsync(
        ApplicationLoggingState loggingState,
        TestApplicationOptions testApplicationOptions,
        IUnhandledExceptionsHandler unhandledExceptionsHandler,
        DateTimeOffset createBuilderStart)
    {
        ApplicationStateGuard.Ensure(TestFramework is not null);
        BuildContext context = await SetupCommonServicesAsync(
            loggingState,
            testApplicationOptions,
            unhandledExceptionsHandler,
            createBuilderStart).ConfigureAwait(false);
    
        return await BuildHostAsync(context).ConfigureAwait(false);
    }

Copilot AI requested review from Copilot and removed request for Copilot May 13, 2026 19:29
Copilot AI linked an issue May 13, 2026 that may be closed by this pull request
5 tasks
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot May 13, 2026 19:46
Copilot AI changed the title [WIP] Refactor TestHostBuilder.cs into smaller modules Refactor TestHostBuilder into focused partial modules May 13, 2026
Copilot AI requested a review from Evangelink May 13, 2026 19:47
@Evangelink Evangelink marked this pull request as ready for review May 14, 2026 07:29
Copilot AI review requested due to automatic review settings May 14, 2026 07:29
@Evangelink Evangelink enabled auto-merge (squash) May 14, 2026 07:29
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

This PR refactors the large TestHostBuilder.cs into multiple partial class files to improve readability and reviewability. Behavior, public/internal surface, and host-building flow are preserved; only code organization changes. A new private nested BuildContext class is introduced to thread shared state through the new helper methods.

Changes:

  • Split TestHostBuilder into partials: CommonServices, Modes, Framework, Utilities, while reducing the main file to a thin BuildAsync orchestration entry point.
  • Introduced an internal BuildContext state object (with mutable settable properties for values populated during setup) to pass coordination state between phases.
  • Captured constructor parameters into private fields (_environment, _processHandler, _runtimeFeature) so they can be referenced from the other partial files.
Show a summary per file
File Description
src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs Reduced to class declaration, fields, BuildAsync, and the nested BuildContext type.
src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.CommonServices.cs Extracted SetupCommonServicesAsync covering services, configuration, telemetry, validation, and early-exit handling.
src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Modes.cs Extracted dispatch into Tools/Help/Info/Orchestrator/Controllers/TestHost (server vs console) flows.
src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Framework.cs Extracted BuildTestFrameworkAsync and CreateConsoleTestHost.
src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.Utilities.cs Extracted shared helpers (telemetry metadata, banner display, named-pipe connect, registration helpers, activity completion).

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 0

@Evangelink Evangelink disabled auto-merge May 14, 2026 07:42
@Evangelink Evangelink merged commit 85de570 into main May 14, 2026
25 of 29 checks passed
@Evangelink Evangelink deleted the copilot/refactor-testhostbuilder-into-modules branch May 14, 2026 07:42
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.

🏋️ File Diet: Refactor TestHostBuilder.cs (875 lines) into focused modules

3 participants