Repository navigation
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 isTestHostBuilder.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
BuildAsyncmethod (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 devicesSELECT 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:
-
TestHostBuilder.cs(keep, reduced)- Contents: Class declaration, properties,
BuildAsyncorchestration (thin dispatcher only) - Responsibility: Entry point that delegates to partial methods
- Contents: Class declaration, properties,
-
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
-
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
-
TestHostBuilder.Utilities.cs- Contents:
ConnectToTestHostProcessMonitorIfAvailableAsync,AddApplicationTelemetryMetadata,LogTestHostCreatedAsync,AddServiceIfNotSkippedAsync,RegisterAsServiceOrConsumerOrBothAsync,DisplayBannerIfEnabledAsync - Responsibility: Private helper methods shared across modes
- Contents:
-
TestHostBuilder.Framework.cs- Contents:
BuildTestFrameworkAsyncand related framework-construction helpers - Responsibility: Test framework instantiation and validation
- Contents:
Implementation Guidelines
- Preserve Behavior: All existing functionality must work identically after the split
- Use
partial class: C# partial classes allow splitting a single class across files without changing the public API - Maintain Public API: Keep all public/internal properties and method signatures unchanged
- Test After Each Split: Run the test suite after each incremental
partialextraction - One File at a Time: Extract one partial file at a time to make review easier
Acceptance Criteria
-
TestHostBuilderis split into focusedpartial classfiles - Each new file is under 300 lines
- All tests pass after refactoring
- No breaking changes to public API
-
BuildAsyncin 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 · ◷
Activity