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

Rule S2699: Detect custom method with specific attribute as assertion #3634

Closed
egil opened this issue Sep 26, 2020 · 6 comments · Fixed by #3635
Closed

Rule S2699: Detect custom method with specific attribute as assertion #3634

egil opened this issue Sep 26, 2020 · 6 comments · Fixed by #3635
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@egil
Copy link

egil commented Sep 26, 2020

Description

Rule 2699 should recognize domain specific assertion methods as assertion methods as well.

In my particular case, my Blazor testing library bUnit has a semantic HTML comparer exposed as the as the assertion method MarkupMatches(...) which is not recognized as a assertion method by SonarLint.

A simple suggestion would be to add the word MATCHES to the assertion method list. But that is a quick-fix for this particular case, and will likely exclude other domain specific assertion methods in the future, so perhaps an attribute is a safer approach, for those that want to be compatible with SonarLint?

Repro steps

Simple example from the documentation link above:

// arrange
using var ctx = new TestContext();

// act
var cut = ctx.RenderComponent<Heading>();

// assert
cut.MarkupMatches(@"<h3 id=""heading-1337"" required>
                      Heading text
                      <small class=""mark text-muted"">Secondary text</small>
                    </h3>");

Expected behavior

MarkupMatches to be recognized as an assertion method

Actual behavior

MarkupMatches is not recognized as an assertion method.

Known workarounds

Create another extension method that uses one of the keywords that is recognized as assertion statements.

Related information

Discussed here: https://community.sonarsource.com/t/how-to-mark-custom-methods-as-assertion-methods-in-c/31437

Not sure about these, maybe @domn1995 can help out, since he reported the problem originally: bUnit-dev/bUnit#216

  • C#/VB.NET Plugins Version
  • Visual Studio Version
  • MSBuild / dotnet version
  • If running through the Scanner for MSBuild, its version
  • Operating System
@pavel-mikula-sonarsource
Copy link
Contributor

Hi @egil,

Thank you for taking the time to report this. We had a short discussion and we'll introduce some kind of attribute to enable marking methods as assertions.

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be. labels Sep 28, 2020
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title 2699: allow assert method to be named anything S2699: Detect custom method with specific attribute as assertion Sep 28, 2020
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 8.14 milestone Sep 28, 2020
@egil
Copy link
Author

egil commented Sep 30, 2020

Great, thanks. @andrei-epure-sonarsource / @pavel-mikula-sonarsource :
Please make it so that I can just create the attribute locally in the right namespace in my own library, so I do not have to take an unnecessary dependency on a 3rd party package.

@pavel-mikula-sonarsource
Copy link
Contributor

@egil The proposal only checks the attribute name. It can be in any namespace of any assembly.

@pavel-mikula-sonarsource
Copy link
Contributor

The update was merged to master and will be released with next Analyzer for C# 8.14 to SonarCloud and to the next release of SonarQube 8.6

@egil You can get your codebase ready according to new S2699 specification.

@egil
Copy link
Author

egil commented Oct 5, 2020

Thanks, ill add that to my todo list :)

@egil
Copy link
Author

egil commented Oct 7, 2020

Hi @pavel-mikula-sonarsource, can I get you to give this PR a very quick look and see if I am doing it right: bUnit-dev/bUnit#229

@costin-zaharia-sonarsource costin-zaharia-sonarsource changed the title S2699: Detect custom method with specific attribute as assertion Rule S2699: Detect custom method with specific attribute as assertion Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants