Skip to content

Deduplicate JUnit report filename validation#8949

Merged
Evangelink merged 2 commits into
mainfrom
copilot/duplicate-code-fix-junit-report-generator
Jun 9, 2026
Merged

Deduplicate JUnit report filename validation#8949
Evangelink merged 2 commits into
mainfrom
copilot/duplicate-code-fix-junit-report-generator

Conversation

Copilot AI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

The JUnit report command-line provider had drift-prone copies of the shared report filename/path validation logic, unlike the HTML and TRX providers which already delegate to ReportFileNameValidator. This change aligns JUnit with the shared validation path so future fixes and security-sensitive path checks apply consistently across report generators.

  • Refactor JUnit command-line validation

    • Replace inline --report-junit-filename validation with ReportFileNameValidator.ValidateReportFileNameArgumentAsync(...)
    • Replace inline option cross-validation with ReportFileNameValidator.ValidateReportCommandLineOptionsAsync(...)
  • Remove duplicated path-validation logic

    • Delete the JUnit-local copies of:
      • DirectorySeparators
      • EscapesResultsDirectory
      • IsPathFullyQualified
  • Wire JUnit into shared helpers

    • Link src/Platform/SharedExtensionHelpers/ReportFileNameValidator.cs into Microsoft.Testing.Extensions.JUnitReport.csproj
    • This matches the existing HTML/TRX project structure
  • Add focused JUnit command-line coverage

    • Add JUnitReportGeneratorCommandLineTests
    • Cover the same validation surface already exercised for HTML/TRX:
      • valid relative/absolute paths
      • invalid extension
      • empty filename
      • parent-directory traversal
      • Windows drive-relative paths
      • option dependency/discovery-mode validation

Example of the simplification:

public override Task<ValidationResult> ValidateOptionArgumentsAsync(CommandLineOption commandOption, string[] arguments)
    => commandOption.Name == JUnitReportFileNameOptionName
        ? global::Microsoft.Testing.Extensions.ReportFileNameValidator.ValidateReportFileNameArgumentAsync(
            arguments,
            ".xml",
            ExtensionResources.JUnitReportFileNameMustNotBeEmpty,
            ExtensionResources.JUnitReportFileNameExtensionIsNotXml,
            ExtensionResources.JUnitReportFileNameRelativePathMustStayUnderResultsDirectory)
        : ValidationResult.ValidTask;

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 9, 2026 05:57
Copilot AI changed the title [WIP] Fix duplicate code in JUnitReportGeneratorCommandLine Deduplicate JUnit report filename validation Jun 9, 2026
Copilot AI requested a review from Evangelink June 9, 2026 05:58
@Evangelink Evangelink marked this pull request as ready for review June 9, 2026 06:48
Copilot AI review requested due to automatic review settings June 9, 2026 06:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 aligns the JUnit report extension’s command-line filename/path validation with the shared ReportFileNameValidator helper (already used by HTML/TRX), reducing drift and ensuring future fixes—especially around path safety checks—apply consistently across report generators.

Changes:

  • Refactored JUnitReportGeneratorCommandLine to delegate filename and option cross-validation to Microsoft.Testing.Extensions.ReportFileNameValidator.
  • Linked SharedExtensionHelpers/ReportFileNameValidator.cs into the JUnit report extension project to reuse the shared implementation.
  • Added focused unit tests validating JUnit filename/path rules and option dependency/discovery-mode constraints.
Show a summary per file
File Description
test/UnitTests/Microsoft.Testing.Extensions.UnitTests/Microsoft.Testing.Extensions.UnitTests.csproj Adds a project reference to the JUnit report extension so its command-line provider can be exercised by unit tests.
test/UnitTests/Microsoft.Testing.Extensions.UnitTests/JUnitReportGeneratorCommandLineTests.cs New unit tests covering JUnit report filename validation and command-line option cross-validation behaviors.
src/Platform/Microsoft.Testing.Extensions.JUnitReport/Microsoft.Testing.Extensions.JUnitReport.csproj Links the shared ReportFileNameValidator helper into the JUnit extension to match HTML/TRX structure.
src/Platform/Microsoft.Testing.Extensions.JUnitReport/JUnitReportGeneratorCommandLine.cs Replaces local validation logic with calls into the shared ReportFileNameValidator.

Copilot's findings

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

@Evangelink Evangelink merged commit 11a47c0 into main Jun 9, 2026
15 checks passed
@Evangelink Evangelink deleted the copilot/duplicate-code-fix-junit-report-generator branch June 9, 2026 07:50
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.

[duplicate-code] Duplicate Code: JUnitReportGeneratorCommandLine Duplicates ReportFileNameValidator Logic

3 participants