Skip to content

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

Closed

Description

Overview

The file src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs has grown to 875 lines, making it harder to navigate and maintain. This task involves refactoring it into smaller, more focused files.

Note: The largest file overall is Jsonite.cs (1552 lines), but that is a vendored third-party library. The largest production file written by the team is TestHostBuilder.cs.

Current State

  • File: src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs
  • Size: 875 lines
  • Language: C#
Structural Analysis

The file contains a single TestHostBuilder class with:

  • One massive BuildAsync method (lines 62–579, ~517 lines) divided into clearly commented sections:
    • SETUP COMMON SERVICE USED IN ALL MODES (lines 68–341): service registration, configuration, telemetry, command-line, logging, output devices
    • SELECT AND RUN THE ACTUAL MODE (lines 343–579): dispatches to Tools mode, Test Host Orchestrator, Test Host Controller mode, and Test Host mode
  • ConnectToTestHostProcessMonitorIfAvailableAsync (line 580)
  • AddApplicationTelemetryMetadata (line 625)
  • LogTestHostCreatedAsync (line 666)
  • BuildTestFrameworkAsync (line 691)
  • AddServiceIfNotSkippedAsync (line 823)
  • RegisterAsServiceOrConsumerOrBothAsync (line 838)
  • DisplayBannerIfEnabledAsync (line 859)

The primary problem is the monolithic BuildAsync method which does too much in one place.

Refactoring Strategy

Proposed File Splits

Using C# partial class to split TestHostBuilder across multiple focused files:

  1. TestHostBuilder.cs (keep, reduced)

    • Contents: Class declaration, properties, BuildAsync orchestration (thin dispatcher only)
    • Responsibility: Entry point that delegates to partial methods
  2. TestHostBuilder.CommonServices.cs

    • Contents: The "SETUP COMMON SERVICE USED IN ALL MODES" logic (~270 lines)
    • Responsibility: Service provider setup — registers system services, configuration, telemetry, command-line, logging, output devices
  3. TestHostBuilder.Modes.cs

    • Contents: The "SELECT AND RUN THE ACTUAL MODE" dispatch logic + tools mode, orchestrator mode, controller mode, and test host mode branches
    • Responsibility: Mode selection and host construction per mode
  4. TestHostBuilder.Utilities.cs

    • Contents: ConnectToTestHostProcessMonitorIfAvailableAsync, AddApplicationTelemetryMetadata, LogTestHostCreatedAsync, AddServiceIfNotSkippedAsync, RegisterAsServiceOrConsumerOrBothAsync, DisplayBannerIfEnabledAsync
    • Responsibility: Private helper methods shared across modes
  5. TestHostBuilder.Framework.cs

    • Contents: BuildTestFrameworkAsync and related framework-construction helpers
    • Responsibility: Test framework instantiation and validation

Implementation Guidelines

  1. Preserve Behavior: All existing functionality must work identically after the split
  2. Use partial class: C# partial classes allow splitting a single class across files without changing the public API
  3. Maintain Public API: Keep all public/internal properties and method signatures unchanged
  4. Test After Each Split: Run the test suite after each incremental partial extraction
  5. One File at a Time: Extract one partial file at a time to make review easier

Acceptance Criteria

  • TestHostBuilder is split into focused partial class files
  • Each new file is under 300 lines
  • All tests pass after refactoring
  • No breaking changes to public API
  • BuildAsync in the main file is a clean orchestrator under ~50 lines

Priority: Medium
Effort: Medium
Expected Impact: Improved code navigability, easier testing of individual build phases, reduced merge conflicts

Generated by Daily File Diet · ● 3.3M · ◷

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions