Skip to content

[duplicate-code] TrxReportGeneratorCommandLine should extend ReportGeneratorCommandLineBase #9169

Description

@Evangelink

🔍 Duplicate Code Detected: TRX Report Command Line Validation Pattern

Analysis of commit 6d6e32d

Assignee: @copilot

Summary

TrxReportGeneratorCommandLine manually implements two validation methods (ValidateOptionArgumentsAsync and ValidateCommandLineOptionsAsync) by directly calling ReportFileNameValidator helpers. The base class ReportGeneratorCommandLineBase was specifically designed to encapsulate this exact pattern, and three other report generators (CTRF, HTML, JUnit) already extend it — but TRX does not.

Duplication Details

Pattern: Report validation logic implemented inline instead of via base class

  • Severity: Medium
  • Occurrences: 4 implementations — 1 inline (TRX), 3 using base class (CTRF, HTML, JUnit)
  • Locations:
    • src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxCommandLine.cs (lines 31–46) — inline duplicate
    • src/Platform/SharedExtensionHelpers/ReportGeneratorCommandLineBase.cs — base class with identical abstracted logic
    • src/Platform/Microsoft.Testing.Extensions.CtrfReport/CtrfReportGeneratorCommandLine.cs — correct pattern
    • src/Platform/Microsoft.Testing.Extensions.HtmlReport/HtmlReportGeneratorCommandLine.cs — correct pattern
    • src/Platform/Microsoft.Testing.Extensions.JUnitReport/JUnitReportGeneratorCommandLine.cs — correct pattern
  • Code Sample (current TRX inline implementation):
// TrxCommandLine.cs - manual duplication
public override Task<ValidationResult> ValidateOptionArgumentsAsync(CommandLineOption commandOption, string[] arguments)
    => commandOption.Name == TrxReportFileNameOptionName
        ? ReportFileNameValidator.ValidateReportFileNameArgumentAsync(
            arguments,
            ".trx",
            ExtensionResources.TrxReportFileNameMustNotBeEmpty,
            ExtensionResources.TrxReportFileNameExtensionIsNotTrx,
            ExtensionResources.TrxReportFileNameRelativePathMustStayUnderResultsDirectory)
        : ValidationResult.ValidTask;

public override Task<ValidationResult> ValidateCommandLineOptionsAsync(ICommandLineOptions commandLineOptions)
    => ReportFileNameValidator.ValidateReportCommandLineOptionsAsync(
        commandLineOptions,
        TrxReportOptionName,
        TrxReportFileNameOptionName,
        ExtensionResources.TrxReportFileNameRequiresTrxReport,
        ExtensionResources.TrxReportIsNotValidForDiscovery,
        PlatformCommandLineProvider.DiscoverTestsOptionKey);
  • Code Sample (correct CTRF pattern):
// CtrfReportGeneratorCommandLine.cs - extends base class
internal sealed class CtrfReportGeneratorCommandLine : ReportGeneratorCommandLineBase
{
    public CtrfReportGeneratorCommandLine()
        : base(
            nameof(CtrfReportGeneratorCommandLine),
            ExtensionResources.CtrfReportGeneratorDisplayName,
            ExtensionResources.CtrfReportGeneratorDescription,
            CtrfReportOptionName,
            CtrfReportFileNameOptionName,
            /* ... resource strings ... */
            ".json",
            /* ... more resource strings ... */)
    { }
}

Impact Analysis

  • Maintainability: Any future change to the shared ReportFileNameValidator calling convention or new validation logic in ReportGeneratorCommandLineBase must be manually replicated in TRX as well. Other report generators benefit automatically.
  • Bug Risk: A fix applied to validation logic via the base class silently misses TRX. This is especially risky for security- or correctness-related validation changes.
  • Code Bloat: 17 extra lines in TrxCommandLine.cs that add no unique logic.

Refactoring Recommendations

  1. Extend ReportGeneratorCommandLineBase in TrxReportGeneratorCommandLine
    • Change TrxReportGeneratorCommandLine to extend ReportGeneratorCommandLineBase instead of CommandLineOptionsProviderBase
    • Move the hardcoded strings (.trx, resource keys) to constructor arguments
    • Remove the ValidateOptionArgumentsAsync and ValidateCommandLineOptionsAsync override methods entirely
    • Target file: src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxCommandLine.cs
    • Estimated effort: < 1 hour
    • Benefits: Consistent validation behavior with all other report generators; future validation improvements automatically apply to TRX

Implementation Checklist

  • Review duplication findings
  • Change TrxReportGeneratorCommandLine to extend ReportGeneratorCommandLineBase
  • Remove redundant ValidateOptionArgumentsAsync and ValidateCommandLineOptionsAsync overrides
  • Verify existing TRX tests still pass
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 5
  • Detection Method: Semantic code analysis — base class pattern mismatch
  • Commit: 6d6e32d
  • Analysis Date: 2026-06-16

🤖 Automated content by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Duplicate Code Detector workflow. · 951.6 AIC · ⌖ 25.8 AIC · [◷]( · )

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/duplicate-code-detector.md@main
  • expires on Jun 18, 2026, 6:16 AM UTC

Metadata

Metadata

Labels

type/automationCreated or maintained by an agentic workflow.type/tech-debtCode health, refactoring, simplification.

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions