Skip to content

Refactor duplicated class fixture analyzer validation into shared helper - #9492

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

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

Conversation

Copilot AI commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

ClassInitializeShouldBeValidAnalyzer and ClassCleanupShouldBeValidAnalyzer duplicated near-identical class-fixture AnalyzeSymbol logic, creating drift risk. This change aligns class-level fixture analyzers with the existing instance-fixture helper pattern and centralizes the shared validation path.

  • Shared class-fixture helper

    • Added RegisterClassFixtureAnalyzer(...) to FixtureMethodAnalyzerHelper to standardize analyzer registration for class-level fixture attributes.
    • Added AnalyzeClassFixtureMethod(...) to hold the common class-fixture signature/type validation logic (including inheritance-mode + abstract/sealed checks).
  • Analyzer simplification

    • ClassInitializeShouldBeValidAnalyzer now delegates to the helper with FixtureParameterMode.MustHaveTestContext.
    • ClassCleanupShouldBeValidAnalyzer now delegates to the helper with FixtureParameterMode.OptionalTestContext.
    • Removed duplicated private AnalyzeSymbol methods from both analyzers.
  • Behavior-preserving parameterization

    • The helper keeps the class-specific difference explicit via FixtureParameterMode, while preserving the same diagnostic/fixable behavior.
FixtureMethodAnalyzerHelper.RegisterClassFixtureAnalyzer(
    context,
    WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingClassInitializeAttribute,
    Rule,
    FixtureParameterMode.MustHaveTestContext);

FixtureMethodAnalyzerHelper.RegisterClassFixtureAnalyzer(
    context,
    WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingClassCleanupAttribute,
    Rule,
    FixtureParameterMode.OptionalTestContext);

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 29, 2026 06:13
Copilot AI changed the title [WIP] Refactor duplicate AnalyzeSymbol methods in fixture analyzers Refactor duplicated class fixture analyzer validation into shared helper Jun 29, 2026
@Evangelink
Amaury Levé (Evangelink) marked this pull request as ready for review June 29, 2026 10:08
Copilot AI review requested due to automatic review settings June 29, 2026 10:08
@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) June 29, 2026 10:08
@Evangelink Amaury Levé (Evangelink) added the state/needs-review Awaiting review from the team. label Jun 29, 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 logic by centralizing class-level fixture method validation ([ClassInitialize] / [ClassCleanup]) into FixtureMethodAnalyzerHelper, reducing duplication and drift risk while keeping the behavior differences explicit via FixtureParameterMode.

Changes:

  • Added RegisterClassFixtureAnalyzer(...) and AnalyzeClassFixtureMethod(...) to consolidate class-fixture analyzer registration and validation.
  • Updated ClassInitializeShouldBeValidAnalyzer and ClassCleanupShouldBeValidAnalyzer to delegate to the new helper (removing duplicated AnalyzeSymbol logic).
Show a summary per file
File Description
src/Analyzers/MSTest.Analyzers/Helpers/FixtureMethodAnalyzerHelper.cs Adds shared registration + analysis helper for class-level fixtures.
src/Analyzers/MSTest.Analyzers/ClassInitializeShouldBeValidAnalyzer.cs Simplifies analyzer to call the shared class-fixture helper with MustHaveTestContext.
src/Analyzers/MSTest.Analyzers/ClassCleanupShouldBeValidAnalyzer.cs Simplifies analyzer to call the shared class-fixture helper with OptionalTestContext.

Review details

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

…e-tribble

# Conflicts:
#	src/Analyzers/MSTest.Analyzers/Helpers/FixtureMethodAnalyzerHelper.cs
@Evangelink
Amaury Levé (Evangelink) merged commit 58d1d6a into main Jun 29, 2026
32 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the copilot/duplicate-code-fix-analyzers branch June 29, 2026 13:50
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