Skip to content

Refactor TestInitialize/TestCleanup analyzer registration into shared fixture helper - #9485

Merged
Amaury Levé (Evangelink) merged 3 commits into
mainfrom
copilot/duplicate-code-fix-analyzers
Jun 28, 2026
Merged

Amaury Levé (Evangelink) merged 3 commits into
mainfrom
copilot/duplicate-code-fix-analyzers

Conversation

Copilot AI commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

TestInitializeShouldBeValidAnalyzer and TestCleanupShouldBeValidAnalyzer had near-identical Initialize paths, differing only by attribute metadata and rule wiring. This duplicated instance-fixture registration logic and increased drift risk for future changes.

  • What changed

    • Added a shared helper in FixtureMethodAnalyzerHelper:
      • RegisterInstanceFixtureAnalyzer(AnalysisContext context, string fixtureAttributeMetadataName, DiagnosticDescriptor rule)
    • Updated both analyzers to delegate registration to the new helper:
      • TestInitializeShouldBeValidAnalyzer
      • TestCleanupShouldBeValidAnalyzer
    • Kept both public diagnostic descriptors unchanged:
      • TestInitializeShouldBeValidAnalyzer.Rule
      • TestCleanupShouldBeValidAnalyzer.Rule
  • Effect

    • Centralizes the instance-fixture registration pattern in one location.
    • Removes duplicated analyzer boilerplate while preserving analyzer-specific rule identity and attribute target.
internal static void RegisterInstanceFixtureAnalyzer(
    AnalysisContext context,
    string fixtureAttributeMetadataName,
    DiagnosticDescriptor rule)
    => RegisterFixtureMethodSymbolAction(
        context,
        fixtureAttributeMetadataName,
        (symbolContext, symbols) => AnalyzeInstanceFixtureMethod(symbolContext, symbols, rule));

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 28, 2026 06:03
Copilot AI changed the title [WIP] Refactor duplicate test analyzers for cleanup and initialization Refactor TestInitialize/TestCleanup analyzer registration into shared fixture helper Jun 28, 2026
@Evangelink
Amaury Levé (Evangelink) marked this pull request as ready for review June 28, 2026 19:24
Copilot AI review requested due to automatic review settings June 28, 2026 19:24
@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) June 28, 2026 19:24
@Evangelink Amaury Levé (Evangelink) added the state/needs-review Awaiting review from the team. label Jun 28, 2026

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

Refactors MSTest analyzer initialization to centralize the “instance fixture method” registration pattern (used by TestInitialize and TestCleanup) into FixtureMethodAnalyzerHelper, reducing duplicated boilerplate while keeping each analyzer’s diagnostic descriptor intact.

Changes:

  • Added FixtureMethodAnalyzerHelper.RegisterInstanceFixtureAnalyzer(...) to wrap registration + instance-fixture analysis wiring.
  • Updated TestInitializeShouldBeValidAnalyzer and TestCleanupShouldBeValidAnalyzer to call the new helper instead of duplicating registration logic.
Show a summary per file
File Description
src/Analyzers/MSTest.Analyzers/TestInitializeShouldBeValidAnalyzer.cs Switches analyzer initialization to the shared instance-fixture registration helper.
src/Analyzers/MSTest.Analyzers/TestCleanupShouldBeValidAnalyzer.cs Switches analyzer initialization to the shared instance-fixture registration helper.
src/Analyzers/MSTest.Analyzers/Helpers/FixtureMethodAnalyzerHelper.cs Introduces the shared RegisterInstanceFixtureAnalyzer helper that wires instance-fixture symbol analysis.

Review details

  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Low

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state/needs-review Awaiting review from the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants