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

CA1515 should ignore test projects #7192

Open
martincostello opened this issue Feb 14, 2024 · 11 comments · Fixed by xunit/xunit.analyzers#182
Open

CA1515 should ignore test projects #7192

martincostello opened this issue Feb 14, 2024 · 11 comments · Fixed by xunit/xunit.analyzers#182
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design False_Positive A diagnostic is reported for non-problematic case

Comments

@martincostello
Copy link
Member

Analyzer

Diagnostic ID: CA1515

Describe the improvement

Test projects often contain infrastructure code, such as fixtures and input for data driven tests, that are used with unit test classes/methods. These types are often public because test frameworks require the test classes and methods to be public, so by extension classes used as fixtures for these classes also need to be public. Even test classes are hit by this rule, with static test classes raising the warning, which changing would break.

Describe suggestions on how to achieve the rule

Test projects, however such determination is made, should be automatically excluded from the rule. Alternatively, exclude any type exposed by the public API surface of any class that contains at least one test method (e.g. an xunit [Theory] or ``Fact], NUnit [Test]` etc).

Additional context

I typically just enable all rules in my projects by default and opt-out of them where necessary, but with .NET 9 preview 1 I have found that the rule generates a lot of noise to the point where I've just unconditionally disabled it everywhere.

Trying to fix things where I've tried so far just leads down a spiralling path of further changes, most of which is either incorrect as it breaks code, such as the test scenario, or just creates suppression noise:

On one project this lead to this sort of chain of events:

  1. Make types internal
  2. CA1812 fires because types activated through DI now appear to be unused
  3. Tests can't compile because types are no longer public
  4. Add internals visible to
  5. xunit fails to work properly because types used for tests are no longer public

CA1515 feels far too noisy to be useful at this stage based on its "anything public if it's not a library" heuristic.

@CollinAlpert
Copy link
Contributor

I agree with this, however this is an xunit specific problem, since NUnit allows test classes to be internal.

@CollinAlpert
Copy link
Contributor

@martincostello This is now implemented in xUnit, meaning xUnit will suppress CA1515 in test classes. Can you verify this works for you?

@martincostello
Copy link
Member Author

Sure, I'll take a look when xunit next gets updated in my repos.

@ChrisTorng
Copy link

I have never seen CA1515. But now it shows on all test classes of MSTest V2(?).
Microsoft.NET.Test.Sdk 17.9.0
MSTest.TestAdapter 3.3.1
MSTest.TestFramework 3.3.1
This problem seems to start from upgrading to VS 17.12.0 Preview 1.0.

@JohanRoss
Copy link

I have got the same issue as @ChrisTorng above here, with MSTest (in Visual Studio 17.12.0 preview 3). So it is now not only limited to xUnit or NUnit.

Image

@Thieum
Copy link

Thieum commented Nov 14, 2024

It should ignore APIControllers, Blazor pages, signalR hub derived classes, attributes derived from ActionFilterAttribute and Function App classes at least in .net8 too afaik.

@Daeymon
Copy link

Daeymon commented Nov 15, 2024

It should ignore APIControllers, Blazor pages, signalR hub derived classes, attributes derived from ActionFilterAttribute and Function App classes at least in .net8 too afaik.

Also all classes or enums which are associated with a Blazor component parameter, which also have to be public.

Image
Image

@Youssef1313 Youssef1313 added Bug The product is not behaving according to its current intended design Area-Microsoft.CodeAnalysis.NetAnalyzers False_Positive A diagnostic is reported for non-problematic case labels Dec 6, 2024
@rcdailey
Copy link

I just want to add that this analysis warning in general is poor since it can't detect usages outside of a project via DI or any other runtime use case. I ended up having to silence this warning in my global .editorconfig via:

[*.cs]
# Consider making public types internal
dotnet_diagnostic.CA1515.severity = none

@Youssef1313
Copy link
Member

This applies to MSTest as well.

@dnperfors
Copy link
Contributor

Even though xunit.analyzers tries to suppress this warning in for test classes, it looks like the refactor suggestion will still perform the refactoring. I think I will have to disable this warning as well 👎

@Youssef1313
Copy link
Member

Even though xunit.analyzers tries to suppress this warning in for test classes, it looks like the refactor suggestion will still perform the refactoring.

Not sure if that's expected. Consider opening an issue on dotnet/roslyn in case it's a bug.

cc @CyrusNajmabadi @sharwell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design False_Positive A diagnostic is reported for non-problematic case
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants