Skip to content

Refactor fixture signature code fixers to shared base class#8875

Merged
Evangelink merged 3 commits into
mainfrom
copilot/duplicate-code-fixers
Jun 7, 2026
Merged

Refactor fixture signature code fixers to shared base class#8875
Evangelink merged 3 commits into
mainfrom
copilot/duplicate-code-fixers

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 6, 2026

Six fixture-method code fixers in MSTest.Analyzers.CodeFixes had near-identical implementations, differing only by diagnostic ID and signature flags. This change centralizes shared behavior to remove lock-step duplication and reduce maintenance risk.

  • What changed

    • Added FixtureMethodSignatureFixerBase in src/Analyzers/MSTest.Analyzers.CodeFixes/Helpers/.
    • Moved common code-fix flow into the base:
      • FixableDiagnosticIds
      • GetFixAllProvider()
      • RegisterCodeFixesAsync(...)
    • Kept per-rule variability as abstract members:
      • DiagnosticRuleId
      • IsParameterLess
      • ShouldBeStatic
  • Refactored fixers

    • Updated these concrete providers to inherit from the base and only declare rule-specific values:
      • AssemblyCleanupShouldBeValidFixer
      • AssemblyInitializeShouldBeValidFixer
      • ClassCleanupShouldBeValidFixer
      • ClassInitializeShouldBeValidFixer
      • TestCleanupShouldBeValidFixer
      • TestInitializeShouldBeValidFixer
  • MEF/export behavior

    • Preserved [ExportCodeFixProvider(...)] and [Shared] on each concrete fixer type.
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(TestCleanupShouldBeValidFixer))]
[Shared]
public sealed class TestCleanupShouldBeValidFixer : FixtureMethodSignatureFixerBase
{
    protected override string DiagnosticRuleId => DiagnosticIds.TestCleanupShouldBeValidRuleId;
    protected override bool IsParameterLess => true;
    protected override bool ShouldBeStatic => false;
}

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 6, 2026 05:49
Copilot AI changed the title [WIP] Refactor duplicate code in MSTest.Analyzers.CodeFixes Refactor fixture signature code fixers to shared base class Jun 6, 2026
Copilot AI requested a review from Evangelink June 6, 2026 05:49
@Evangelink Evangelink marked this pull request as ready for review June 6, 2026 14:05
Copilot AI review requested due to automatic review settings June 6, 2026 14:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 refactors the MSTest fixture-method signature code fix providers in MSTest.Analyzers.CodeFixes by extracting the shared “fix signature” code-fix workflow into a new base class, reducing duplication across the six near-identical fixers.

Changes:

  • Added FixtureMethodSignatureFixerBase to centralize FixableDiagnosticIds, GetFixAllProvider(), and RegisterCodeFixesAsync(...).
  • Updated the six fixture signature fixers to inherit from the base class and provide only rule-specific configuration (DiagnosticRuleId, IsParameterLess, ShouldBeStatic).
  • Preserved MEF export behavior ([ExportCodeFixProvider] and [Shared]) on each concrete fixer type.
Show a summary per file
File Description
src/Analyzers/MSTest.Analyzers.CodeFixes/Helpers/FixtureMethodSignatureFixerBase.cs New shared base implementing the common code-fix registration and “fix signature” action creation.
src/Analyzers/MSTest.Analyzers.CodeFixes/TestInitializeShouldBeValidFixer.cs Refactored to inherit from the shared base and provide rule-specific flags.
src/Analyzers/MSTest.Analyzers.CodeFixes/TestCleanupShouldBeValidFixer.cs Refactored to inherit from the shared base and provide rule-specific flags.
src/Analyzers/MSTest.Analyzers.CodeFixes/ClassInitializeShouldBeValidFixer.cs Refactored to inherit from the shared base and provide rule-specific flags.
src/Analyzers/MSTest.Analyzers.CodeFixes/ClassCleanupShouldBeValidFixer.cs Refactored to inherit from the shared base and provide rule-specific flags.
src/Analyzers/MSTest.Analyzers.CodeFixes/AssemblyInitializeShouldBeValidFixer.cs Refactored to inherit from the shared base and provide rule-specific flags.
src/Analyzers/MSTest.Analyzers.CodeFixes/AssemblyCleanupShouldBeValidFixer.cs Refactored to inherit from the shared base and provide rule-specific flags.

Copilot's findings

  • Files reviewed: 7/7 changed files
  • Comments generated: 1

@Evangelink Evangelink enabled auto-merge (squash) June 7, 2026 07:18
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 7, 2026 07:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 7/7 changed files
  • Comments generated: 0 new

@Evangelink Evangelink disabled auto-merge June 7, 2026 11:04
@Evangelink Evangelink merged commit cd39003 into main Jun 7, 2026
34 checks passed
@Evangelink Evangelink deleted the copilot/duplicate-code-fixers branch June 7, 2026 11:04
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: Near-Identical Fixture Method Code Fixers in MSTest.Analyzers.CodeFixes

3 participants