feat: Add analyzer PH2162 and code fix for TestTimeouts class accessibility#964
feat: Add analyzer PH2162 and code fix for TestTimeouts class accessibility#964
Conversation
Co-authored-by: ajbarga <99690046+ajbarga@users.noreply.github.com>
Co-authored-by: ajbarga <99690046+ajbarga@users.noreply.github.com>
… tests Co-authored-by: ajbarga <99690046+ajbarga@users.noreply.github.com>
|
Documentation/Diagnostics/PH2159.md
Outdated
|
|
||
| ## Introduction | ||
|
|
||
| Projects commonly define a `TestTimeouts` class that centralizes timeout constants (e.g., `CiAppropriate`, `Integration`). This analyzer enforces that any class named `TestTimeouts` is declared as `internal` and not `static`. Public exposure or use of `static` on this coordination class is discouraged. |
There was a problem hiding this comment.
Perhaps mention how this rule helps avoid conflict with the MSTEST analyzer that says all classes in a test project cannot be public unless it's a [TestClass]
There was a problem hiding this comment.
Added mention of the MSTEST analyzer conflict avoidance in the documentation. The rule now explains how it helps avoid conflicts with the MSTest analyzer that restricts all classes in test projects from being public unless they have the [TestClass] attribute. Implemented in commit 439b4e0.
| AvoidUnusedToString = 2153, | ||
| AvoidUnlicensedPackages = 2155, | ||
| AvoidPkcsPaddingWithRsaEncryption = 2158, | ||
| TestTimeoutsClassShouldBeInternal = 2159, |
There was a problem hiding this comment.
Let's use 2162 instead of 2159. 2159, 2160, and 2161 are claimed in other PRs. Remember to update the PH2159.md file name and contents as well.
There was a problem hiding this comment.
Updated diagnostic ID from PH2159 to PH2162 and renamed documentation file accordingly. All references in DiagnosticId.cs and PH2162.md have been updated. Implemented in commit 439b4e0.
|
Copilot see comments above |
…ation Co-authored-by: bcollamore <57269455+bcollamore@users.noreply.github.com>



This PR implements a new Roslyn analyzer that enforces proper accessibility modifiers for classes named "TestTimeouts" used in MSTest projects.
Problem
Classes named
TestTimeoutsthat contain test timeout constants are often declared with inappropriate accessibility modifiers, which can lead to:internal static[TestClass]attributeSolution
Added
TestTimeoutsClassAccessibilityAnalyzer(PH2162) that detects and provides automatic fixes for:public class TestTimeouts→internal class TestTimeoutspublic sealed class TestTimeouts→internal sealed class TestTimeoutsinternal static class TestTimeouts→internal class TestTimeoutsThe analyzer ignores classes that are already properly declared as
internal(non-static) and only targets classes specifically named "TestTimeouts".Example
Before:
After (with code fix applied):
Changes Made
TestTimeoutsClassShouldBeInternal = 2162The analyzer is enabled by default and includes full test coverage with 6 test cases covering all supported scenarios. All existing tests (369 MSTest-related tests) continue to pass, ensuring no regressions.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.