SONARPY-4541 Provide reusable Python test-file heuristic - #2299
SONARPY-4541 Provide reusable Python test-file heuristic#2299erwan-leforestier-sonarsource wants to merge 1 commit into
Conversation
| /** | ||
| * Reports whether the current file is likely to contain test code. | ||
| * @return whether the file is likely test content | ||
| */ | ||
| boolean isLikelyTestFile(); |
There was a problem hiding this comment.
⚠️ Quality: New abstract method on public API SubscriptionContext breaks compat
isLikelyTestFile() is added as a non-default abstract method to the public API interface SubscriptionContext (in the org.sonar.plugins.python.api package). Any external implementation of this interface (e.g. test doubles or third-party integrations relying on the published API) will fail to compile / break binary compatibility. Provide a default implementation (e.g. default boolean isLikelyTestFile() { return false; }) to preserve backward compatibility while SubscriptionContextImpl overrides it.
Make the new method a default method so existing implementers of the public API keep compiling.:
/**
* Reports whether the current file is likely to contain test code.
* @return whether the file is likely test content
*/
default boolean isLikelyTestFile() {
return false;
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
| public Builder testFilePath(String testFilePath) { | ||
| this.testFilePath = Optional.of(testFilePath); | ||
| return this; | ||
| } |
There was a problem hiding this comment.
💡 Bug: Builder.testFilePath(null) throws NPE via Optional.of
testFilePath(String) wraps the argument with Optional.of(testFilePath), which throws NullPointerException if null is passed. The rest of the classifier explicitly supports a null path (the constructor falls back to the PythonFile-based lookup), so this method is inconsistent and brittle for callers other than PythonScanner. Use Optional.ofNullable(testFilePath) to keep the intended null-handling contract.
Avoid NPE and align with the null-tolerant classifier API.:
public Builder testFilePath(String testFilePath) {
this.testFilePath = Optional.ofNullable(testFilePath);
return this;
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
Code Review
|
| Auto-apply | Compact | Unblock |
|
|
|
Was this helpful? React with 👍 / 👎 | Gitar
Summary
Adds a cached, aggressive test-content classifier that Python rules can query from both visitor APIs. The scanner reuses that result when determining rule scope for MAIN-classified files.
Changes
isLikelyTestFile()through visitor and subscription contexts.Functional Validation
Artifact:
SONARPY-4541-fv.zipOnce the file is attached to the PR description, unzip and run:
./run.sh
Expected output: