Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSTEST0029 code fix #3747

Merged
merged 7 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,10 @@
<data name="TestContextShouldBeValidFix" xml:space="preserve">
<value>Fix test context</value>
</data>
<data name="AddTestMethodAttributeFix" xml:space="preserve">
<value>Add '[TestMethod]'</value>
</data>
<data name="ChangeMethodAccessibilityToPrivateFix" xml:space="preserve">
<value>Change method accessibility to 'private'</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;
using System.Composition;

using Analyzer.Utilities;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Text;

using MSTest.Analyzers.Helpers;

namespace MSTest.Analyzers;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PublicMethodShouldBeTestMethodFixer))]
[Shared]
public sealed class PublicMethodShouldBeTestMethodFixer : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray.Create(DiagnosticIds.PublicMethodShouldBeTestMethodRuleId);

public override FixAllProvider GetFixAllProvider()
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
=> WellKnownFixAllProviders.BatchFixer;

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

Diagnostic diagnostic = context.Diagnostics[0];
TextSpan diagnosticSpan = diagnostic.Location.SourceSpan;

SyntaxToken syntaxToken = root.FindToken(diagnosticSpan.Start);
if (syntaxToken.Parent is null)
{
return;
}

// Find the method declaration identified by the diagnostic.
MethodDeclarationSyntax methodDeclaration = syntaxToken.Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().First();

context.RegisterCodeFix(
CodeAction.Create(
title: CodeFixResources.AddTestMethodAttributeFix,
createChangedDocument: c => AddTestMethodAttributeAsync(context.Document, methodDeclaration, c),
equivalenceKey: nameof(PublicMethodShouldBeTestMethodFixer) + $"_{CodeFixResources.AddTestMethodAttributeFix}"),
diagnostic);

context.RegisterCodeFix(
CodeAction.Create(
title: CodeFixResources.ChangeMethodAccessibilityToPrivateFix,
createChangedDocument: c => ChangeMethodVisibilityAsync(context.Document, methodDeclaration, c),
equivalenceKey: nameof(PublicMethodShouldBeTestMethodFixer) + $"_{CodeFixResources.ChangeMethodAccessibilityToPrivateFix}"),
diagnostic);
}

private static async Task<Document> AddTestMethodAttributeAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
SyntaxNode testMethodAttribute = editor.Generator.Attribute("TestMethod");

// Add the TestMethod attribute to the method.
editor.AddAttribute(methodDeclaration, testMethodAttribute);

// Apply the changes and return the updated document.
return editor.GetChangedDocument();
}

private static async Task<Document> ChangeMethodVisibilityAsync(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
SyntaxNode updatedMethodDeclaration = editor.Generator.WithAccessibility(methodDeclaration, Accessibility.Private);

editor.ReplaceNode(methodDeclaration, updatedMethodDeclaration);

// Apply the changes and return the updated document.
return editor.GetChangedDocument();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">Opravit pořadí skutečných/očekávaných argumentů</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="de" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">Reihenfolge der tatsächlichen/erwarteten Argumente korrigieren</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="es" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">Corregir el orden de los argumentos reales o esperados</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="fr" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">Corriger l’ordre des arguments réels/attendus</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="it" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">Correggi l'ordine degli argomenti effettivi/previsti</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ja" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">実際の引数と予想される引数の順序を修正する</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ko" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">실제/예상 인수 순서 수정</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pl" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">Napraw rzeczywistą/oczekiwaną kolejność argumentów</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pt-BR" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">Corrigir ordem de argumentos real/esperada</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ru" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">Исправить порядок фактических и ожидаемых аргументов</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="tr" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">Fiili/beklenen bağımsız değişken sırasını düzelt</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-Hans" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">修复实际/预期参数顺序</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-Hant" original="../CodeFixResources.resx">
<body>
<trans-unit id="AddTestMethodAttributeFix">
<source>Add '[TestMethod]'</source>
<target state="new">Add '[TestMethod]'</target>
<note />
</trans-unit>
<trans-unit id="ChangeMethodAccessibilityToPrivateFix">
<source>Change method accessibility to 'private'</source>
<target state="new">Change method accessibility to 'private'</target>
<note />
</trans-unit>
<trans-unit id="FixAssertionArgsOrder">
<source>Fix actual/expected arguments order</source>
<target state="translated">修正實際/預期的引數順序</target>
Expand Down
Loading