-
Notifications
You must be signed in to change notification settings - Fork 289
Analyzer suggestion for duplicate test methods #6973
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
base: main
Are you sure you want to change the base?
Analyzer suggestion for duplicate test methods #6973
Conversation
| namespace MSTest.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// MSTEST0033: <inheritdoc cref="Resources.DoNotDuplicateTestMethodTitle"/>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// MSTEST0033: <inheritdoc cref="Resources.DoNotDuplicateTestMethodTitle"/>. | |
| /// MSTEST0059: <inheritdoc cref="Resources.DoNotDuplicateTestMethodTitle"/>. |
| context => CollectTestMethod(context, testClassAttributeSymbol, testMethodAttributeSymbol, testMethodsFound), | ||
| SymbolKind.Method); | ||
|
|
||
| compilationContext.RegisterCompilationEndAction( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of being compilation end, can it instead of a symbol start/end analyzer? You first RegisterSymbolStartAction for named types, then RegisterOperationBlockAction to analyze the individual methods, and then RegisterSymbolEndAction to do the final analysis of the collected methods?
| private static SyntaxNode? GetMethodBody(SyntaxNode methodNode) | ||
| { | ||
| // Try block body first | ||
| System.Reflection.PropertyInfo? bodyProperty = methodNode.GetType().GetProperty("Body"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using reflection here doesn't seem like a good idea. This also actually might fail for VB which the analyzer claims to support
| for (int i = 0; i < methods.Count; i++) | ||
| { | ||
| for (int j = i + 1; j < methods.Count; j++) | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like very extensive computations. Probably there is something we can do better here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CyrusNajmabadi if you can suggest something here please. This PR attempts to implement #6813
This pull request fixes #6813
This new analyzer implementation allows similar unit test methods with different names within the same test class to be detected as duplicates.
NB: Because this is a new analyzer feature, I declared a new DIagnosticId value of
MSTEST0059asDoNotDuplicateTestMethodRuleId