-
Notifications
You must be signed in to change notification settings - Fork 71
Fixes false positives with public methods which are not allowed #82
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
Conversation
…d in xunit: static and abstract PublicMethodShouldBeMarkedAsTest greedily issues warning for 'public static' and 'public abstract' methods although they can not be used as a test method in xunit framework. Introduced code changes verify if public method is abstract or is static and if detected skip further checks.
|
@4creators, |
|
PTAL @marcind |
| <Prerequisites> | ||
| <Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0.26208.0,16.0)" DisplayName="Visual Studio core editor" /> | ||
| </Prerequisites> | ||
| </PackageManifest> |
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.
Please do not reformat files. The entire file looks changed because of spacing differences.
|
|
||
| var method = (IMethodSymbol)member; | ||
| if (method.MethodKind != MethodKind.Ordinary) | ||
| if (method.MethodKind != MethodKind.Ordinary || method.IsStatic || method.IsAbstract) |
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.
xUnit.net does support public static test methods.
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.
Yeah, I have tested initially and xunit failed to detect that but using xunit.analyzers.tests I got it as You said. My wrong
…anges in vsix manifest
| <PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011"> | ||
| <Metadata> | ||
| <Identity Id="xunit.analyzers..d09124d1-9c5e-40de-b1de-35120196c3e0" Version="1.0" Language="en-US" Publisher="Marcin Dobosz"/> | ||
| <Identity Id="xunit.analyzers..d09124d1-9c5e-40de-b1de-35120196c3e0" Version="1.0.1" Language="en-US" Publisher="Marcin Dobosz"/> |
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.
Why is this file changed at all? Please undo.
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.
Thought that xunit has semantic versioning. Change in error/warning detection syntax changes behaviour so I have incremented build version change.
Obviously will change it as requested.
|
Addressed all review remarks |
|
|
||
| var method = (IMethodSymbol)member; | ||
| if (method.MethodKind != MethodKind.Ordinary) | ||
| if (method.MethodKind != MethodKind.Ordinary || method.IsAbstract) |
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.
Is this ever true? We returned earlier if type.IsAbstract was true, so logically this must be a concrete type and none of its members can be abstract.
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.
Maybe the best fix would be to remove the type.IsAbstract check above and keep this as-is.
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.
Sorry for late replay, I was stuck with other work for a while.
This way it is set it can skip diagnostics in partially invalid code during code editing i.e. concrete class with abstract method defined or skip analysis in abstract class for public concrete methods.
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.
OK, it's fine to leave this as-is then, but can you add a comment about it?
| { | ||
| var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
| @"public abstract class TestClass { | ||
| [Xunit.Fact] public void TestMethod() { } |
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.
Nit: Rename to AbstractMethod, since it isn't a test method and it doesn't raise an error.
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.
Will do
| }"); | ||
|
|
||
| Assert.Empty(diagnostics); | ||
| } |
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.
Would be good to also add this test:
[Fact]
public async void DoesNotFindErrorForPublicAbstractMethodMarkedWithFact()
{
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"public abstract class TestClass {
[Xunit.Fact] public void TestMethod() { }
[Xunit.Fact] public abstract void AbstractTestMethod();
}");
Assert.Empty(diagnostics);
}I can't test right now but that should work fine at runtime, since the [Fact] attribute is inherited on overriding members.
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.
OK it makes sense
|
PTAL |
|
After you leave a comment LGTM |
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.
👍 once the requested comment has been made
|
OK All feedback addressed 😄 |
|
Thanks for the submission |
Bump test project dependencies - Castle.Core, Moq, and xUnit versions were all out of date - hold xunit.runner.visualstudio version back in .NET SDK test projects - can be moved to latest version after we stop testing on netcoreapp2.1 - add missing xunit.analyzers reference to System.Web.Razor.Test project - move all test projects to .NET v4.6.2 (a supported framework) - remove `netstandard` reference in System.Net.Http.Formatting.Test; not needed w/ new TFM & updated references - further separate build of Microsoft.TestCommon project when invoked from NetCore.Test project - special case `RestorePackages` for this case - add System.Net.Http references to avoid conflicting versions e.g. src/ and test/ TFMs differ React to changed xUnit APIs - adjust Microsoft.TestCommon code - nit: use `Array.Empty<byte>()` in `TranscodingStreamTests` - `TranscodingStream` `internal`s can be `private` instead Resolve xUnit issues new analyzers find - address xUnit2000 warnings - pass expected values to `Assert.Equal(...)` as correct (left) argument - make generic method types explicit to avoid `error CS0121: The call is ambiguous between the following methods or properties: ...` - note: cannot remove unnecessary xUnit1013 suppression - related bug (xunit/xunit#1466) apparently not fixed in 1.0.0 analyzers package - was xunit/xunit.analyzers#82 fix (in 2017) insufficient? React to new Moq changes - avoid `ProtectedMock\`1.ThrowIfPublicMethod(...)` `throw`ing - use new overloads introduced in the Moq 4.11.0 release - adjust to Moq hiding the `ObjectProxy` type better - update `ControllerContext` mocking to avoid NREs - setting `HttpContext.User` on the `Object` left `get` value `null` (did nothing) - nit: use `SetupGet(...)` for another property
as test methods in xunit: "public static" and "public abstract"
PublicMethodShouldBeMarkedAsTest greedily issues warning for 'public static' and 'public abstract'
methods although they can not be used as a test method in xunit framework. Introduced code changes
verify if public method is abstract or is static and if detected skip further checks.
Fixes xunit/xunit#1466