1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 @file:Suppress("UnstableApiUsage")
18 
19 package androidx.build.lint
20 
21 import org.junit.Test
22 import org.junit.runner.RunWith
23 import org.junit.runners.JUnit4
24 
25 @RunWith(JUnit4::class)
26 class BanRestrictToTestsScopeTest :
27     AbstractLintDetectorTest(
28         useDetector = BanRestrictToTestsScope(),
29         useIssues = listOf(BanRestrictToTestsScope.ISSUE),
30         stubs = arrayOf(Stubs.RestrictTo),
31     ) {
32 
33     @Test
Detection of @RestrictTo(TESTS) usage in Java sourcesnull34     fun `Detection of @RestrictTo(TESTS) usage in Java sources`() {
35         val input =
36             arrayOf(
37                 javaSample("androidx.RestrictToTestsAnnotationUsageJava"),
38             )
39 
40         val expected =
41             """
42 src/androidx/RestrictToTestsAnnotationUsageJava.java:26: Error: Replace @RestrictTo(TESTS) with @VisibleForTesting [UsesRestrictToTestsScope]
43     @RestrictTo(androidx.annotation.RestrictTo.Scope.TESTS)
44     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45 src/androidx/RestrictToTestsAnnotationUsageJava.java:29: Error: Replace @RestrictTo(TESTS) with @VisibleForTesting [UsesRestrictToTestsScope]
46     @RestrictTo(RestrictTo.Scope.TESTS)
47     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48 src/androidx/RestrictToTestsAnnotationUsageJava.java:32: Error: Replace @RestrictTo(TESTS) with @VisibleForTesting [UsesRestrictToTestsScope]
49     @RestrictTo(Scope.TESTS)
50     ~~~~~~~~~~~~~~~~~~~~~~~~
51 src/androidx/RestrictToTestsAnnotationUsageJava.java:35: Error: Replace @RestrictTo(TESTS) with @VisibleForTesting [UsesRestrictToTestsScope]
52     @RestrictTo(TESTS)
53     ~~~~~~~~~~~~~~~~~~
54 src/androidx/RestrictToTestsAnnotationUsageJava.java:38: Error: Replace @RestrictTo(TESTS) with @VisibleForTesting [UsesRestrictToTestsScope]
55     @RestrictTo({Scope.TESTS, Scope.LIBRARY})
56     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57 5 errors, 0 warnings
58         """
59                 .trimIndent()
60 
61         val fixDiffs =
62             """
63 Fix for src/androidx/RestrictToTestsAnnotationUsageJava.java line 26: Replace with `@VisibleForTesting`:
64 @@ -26 +26
65 -     @RestrictTo(androidx.annotation.RestrictTo.Scope.TESTS)
66 +     @androidx.annotation.VisibleForTesting
67 Fix for src/androidx/RestrictToTestsAnnotationUsageJava.java line 29: Replace with `@VisibleForTesting`:
68 @@ -29 +29
69 -     @RestrictTo(RestrictTo.Scope.TESTS)
70 +     @androidx.annotation.VisibleForTesting
71 Fix for src/androidx/RestrictToTestsAnnotationUsageJava.java line 32: Replace with `@VisibleForTesting`:
72 @@ -32 +32
73 -     @RestrictTo(Scope.TESTS)
74 +     @androidx.annotation.VisibleForTesting
75 Fix for src/androidx/RestrictToTestsAnnotationUsageJava.java line 35: Replace with `@VisibleForTesting`:
76 @@ -35 +35
77 -     @RestrictTo(TESTS)
78 +     @androidx.annotation.VisibleForTesting
79         """
80                 .trimIndent()
81 
82         check(*input).expect(expected).expectFixDiffs(fixDiffs)
83     }
84 
85     @Test
Detection of @RestrictTo(TESTS) usage in Kotlin sourcesnull86     fun `Detection of @RestrictTo(TESTS) usage in Kotlin sources`() {
87         val input =
88             arrayOf(
89                 ktSample("androidx.RestrictToTestsAnnotationUsageKotlin"),
90             )
91 
92         val expected =
93             """
94 src/androidx/RestrictToTestsAnnotationUsageKotlin.kt:24: Error: Replace @RestrictTo(TESTS) with @VisibleForTesting [UsesRestrictToTestsScope]
95     @RestrictTo(RestrictTo.Scope.TESTS) fun testMethod() {}
96     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97 src/androidx/RestrictToTestsAnnotationUsageKotlin.kt:26: Error: Replace @RestrictTo(TESTS) with @VisibleForTesting [UsesRestrictToTestsScope]
98     @RestrictTo(RestrictTo.Scope.TESTS, RestrictTo.Scope.LIBRARY) fun testMethodVarArg() {}
99     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100 src/androidx/RestrictToTestsAnnotationUsageKotlin.kt:28: Error: Replace @RestrictTo(TESTS) with @VisibleForTesting [UsesRestrictToTestsScope]
101     @get:RestrictTo(RestrictTo.Scope.TESTS) val testPropertyGet = "test"
102     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103 3 errors, 0 warnings
104         """
105                 .trimIndent()
106 
107         val fixDiffs =
108             """
109 Fix for src/androidx/RestrictToTestsAnnotationUsageKotlin.kt line 24: Replace with `@VisibleForTesting`:
110 @@ -22 +22
111 + import androidx.annotation.VisibleForTesting
112 @@ -24 +25
113 -     @RestrictTo(RestrictTo.Scope.TESTS) fun testMethod() {}
114 +     @VisibleForTesting fun testMethod() {}
115 Fix for src/androidx/RestrictToTestsAnnotationUsageKotlin.kt line 28: Replace with `@get:VisibleForTesting`:
116 @@ -22 +22
117 + import androidx.annotation.VisibleForTesting
118 @@ -28 +29
119 -     @get:RestrictTo(RestrictTo.Scope.TESTS) val testPropertyGet = "test"
120 +     @get:VisibleForTesting val testPropertyGet = "test"
121         """
122                 .trimIndent()
123 
124         check(*input).expect(expected).expectFixDiffs(fixDiffs)
125     }
126 }
127