Skip to content

Commit 8c0678f

Browse files
committed
Java: Exclude @VisibleForTesting-to-@VisibleForTesting access from VisibleForTestingAbuse alerts
1 parent cce3365 commit 8c0678f

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ predicate isWithinPackage(Expr e, RefType t) {
3333
e.getCompilationUnit().getPackage() = t.getPackage()
3434
}
3535

36+
/**
37+
* Holds if a callable or any of its enclosing callables is annotated with @VisibleForTesting
38+
*/
39+
predicate isWithinVisibleForTestingContext(Callable c) {
40+
c.getAnAnnotation().getType().hasName("VisibleForTesting")
41+
or
42+
isWithinVisibleForTestingContext(c.getEnclosingCallable())
43+
}
44+
3645
/**
3746
* Holds if a nested class is within a static context
3847
*/
@@ -136,6 +145,8 @@ where
136145
) and
137146
// not in a test where use is appropriate
138147
not e.getEnclosingCallable() instanceof LikelyTestMethod and
148+
// not when the accessing method or any enclosing method is @VisibleForTesting (test-to-test communication)
149+
not isWithinVisibleForTestingContext(e.getEnclosingCallable()) and
139150
// also omit our own ql unit test where it is acceptable
140151
not e.getEnclosingCallable()
141152
.getFile()

java/ql/test/query-tests/VisibleForTestingAbuse/VisibleForTestingAbuse.expected

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
| packageone/SourcePackage1.java:9:21:9:32 | Annotated.m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
2-
| packageone/SourcePackage1.java:10:21:10:32 | Annotated.m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
3-
| packageone/SourcePackage1.java:12:18:12:36 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
4-
| packageone/SourcePackage1.java:13:18:13:39 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |
5-
| packageone/SourcePackage1.java:16:31:16:42 | Annotated.m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
6-
| packageone/SourcePackage1.java:17:31:17:42 | Annotated.m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
7-
| packageone/SourcePackage1.java:18:28:18:46 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
8-
| packageone/SourcePackage1.java:19:28:19:49 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |
91
| packageone/SourcePackage.java:9:21:9:32 | Annotated.m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
102
| packageone/SourcePackage.java:10:21:10:32 | Annotated.m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
113
| packageone/SourcePackage.java:16:18:16:36 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |

java/ql/test/query-tests/VisibleForTestingAbuse/packageone/SourcePackage1.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ public class SourcePackage1 extends Annotated {
66
@VisibleForTesting
77
public void f() {
88

9-
String s1 = Annotated.m1; // $ SPURIOUS: Alert
10-
String s2 = Annotated.m2; // $ SPURIOUS: Alert
9+
String s1 = Annotated.m1;
10+
String s2 = Annotated.m2;
1111

12-
int i2 = Annotated.fPublic(); // $ SPURIOUS: Alert
13-
int i3 = Annotated.fProtected(); // $ SPURIOUS: Alert
12+
int i2 = Annotated.fPublic();
13+
int i3 = Annotated.fProtected();
1414

1515
Runnable lambda = () -> {
16-
String lambdaS1 = Annotated.m1; // $ SPURIOUS: Alert
17-
String lambdaS2 = Annotated.m2; // $ SPURIOUS: Alert
18-
int lambdaI2 = Annotated.fPublic(); // $ SPURIOUS: Alert
19-
int lambdaI3 = Annotated.fProtected(); // $ SPURIOUS: Alert
16+
String lambdaS1 = Annotated.m1;
17+
String lambdaS2 = Annotated.m2;
18+
int lambdaI2 = Annotated.fPublic();
19+
int lambdaI3 = Annotated.fProtected();
2020
};
2121
}
2222
}

0 commit comments

Comments
 (0)