Skip to content

Fix codefix of analyzer for flowing cancellation token#6239

Merged
Evangelink merged 6 commits intomainfrom
copilot/fix-6237
Jul 31, 2025
Merged

Fix codefix of analyzer for flowing cancellation token#6239
Evangelink merged 6 commits intomainfrom
copilot/fix-6237

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 30, 2025

The FlowTestContextCancellationTokenFixer was generating invalid code when TestContext was not in scope, causing compilation errors (CS0103). The codefix would generate code like testContext.CancellationTokenSource.Token even when no testContext variable existed.

Changes Made

Custom FixAllProvider Implementation

  • Added FlowTestContextCancellationTokenFixAllProvider using DocumentBasedFixAllProvider pattern
  • Ensures TestContext property is added only once per class when multiple fixes are applied
  • Coordinates multiple diagnostics in the same document efficiently

Enhanced Individual Codefix Logic

  • Modified ApplyFixWithTestContextAsync to ensure TestContext availability before applying fixes
  • For static methods (e.g., ClassCleanup, AssemblyCleanup): adds TestContext testContext parameter
  • For instance methods: adds public TestContext TestContext { get; set; } property when missing
  • Preserves existing TestContext references when already available

Before (broken):

[TestClass]
public class MyTestClass
{
    [ClassCleanup]
    public static async Task ClassCleanup()
    {
        await Task.Delay(1000, testContext.CancellationTokenSource.Token); // CS0103 error
    }
}

After (fixed):

[TestClass]
public class MyTestClass
{
    [ClassCleanup]
    public static async Task ClassCleanup(TestContext testContext)
    {
        await Task.Delay(1000, testContext.CancellationTokenSource.Token); // Valid code
    }
}

The fix ensures generated code always compiles successfully while making minimal necessary changes to add TestContext only when needed.

Fixes #6237.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits July 30, 2025 05:53
…ixer

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix codefix of analyzer for flowing cancellation token Fix codefix of analyzer for flowing cancellation token Jul 30, 2025
Copilot AI requested a review from Evangelink July 30, 2025 06:05
Comment thread src/Analyzers/MSTest.Analyzers.CodeFixes/FlowTestContextCancellationTokenFixer.cs Outdated
Comment thread src/Analyzers/MSTest.Analyzers.CodeFixes/FlowTestContextCancellationTokenFixer.cs Outdated
- Implement missing HasTestContextProperty and AddTestContextProperty methods
- Fix parameter list syntax error in AddTestContextParameterToMethod
- Fix KeyValuePair deconstruction in foreach loop
- Add required using statements for System.Linq and System.Collections.Generic
- Make helper methods internal static for access from FixAllProvider

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI requested a review from Evangelink July 30, 2025 07:59
@Evangelink Evangelink marked this pull request as ready for review July 31, 2025 07:23
@Evangelink Evangelink enabled auto-merge (squash) July 31, 2025 07:24
@Youssef1313
Copy link
Copy Markdown
Member

/backport to rel/3.10

@github-actions
Copy link
Copy Markdown
Contributor

Started backporting to rel/3.10: https://github.com/microsoft/testfx/actions/runs/16642926022

Youssef1313 added a commit that referenced this pull request Jul 31, 2025
…6239 (backport to rel/3.10) (#6259)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Youssef1313 <youssefvictor00@gmail.com>
@Evangelink Evangelink merged commit aaa561e into main Jul 31, 2025
9 checks passed
@Evangelink Evangelink deleted the copilot/fix-6237 branch July 31, 2025 08:25
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.

Fix codefix of analyzer for flowing cancellation token

4 participants