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 com.android.tools.lint.checks.infrastructure.TestMode
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import org.junit.runners.JUnit4
25 
26 @RunWith(JUnit4::class)
27 class IdeaSuppressionDetectorTest :
28     AbstractLintDetectorTest(
29         useDetector = IdeaSuppressionDetector(),
30         useIssues = listOf(IdeaSuppressionDetector.ISSUE),
31     ) {
32 
33     @Test
Detection of IDEA-specific suppression in Java sourcesnull34     fun `Detection of IDEA-specific suppression in Java sources`() {
35         val input =
36             java(
37                 "src/androidx/IdeaSuppressionJava.java",
38                 """
39                 public class IdeaSuppressionJava {
40 
41                     // Call to a deprecated method with an inline suppression.
42                     public void callDeprecatedMethod() {
43                         //noinspection deprecation
44                         deprecatedMethod();
45 
46                         notDeprecatedMethod();
47                     }
48 
49                     @Deprecated
50                     public void deprecatedMethod() {}
51 
52                     public void notDeprecatedMethod() {}
53                 }
54             """
55                     .trimIndent()
56             )
57 
58         val expected =
59             """
60 src/androidx/IdeaSuppressionJava.java:5: Error: Uses IntelliJ-specific suppression, should use @SuppressWarnings("deprecation") [IdeaSuppression]
61         //noinspection deprecation
62         ~~~~~~~~~~~~~~~~~~~~~~~~~~
63 1 errors, 0 warnings
64         """
65                 .trimIndent()
66 
67         lint()
68             .files(*stubs, input)
69             .allowDuplicates()
70             .skipTestModes(TestMode.SUPPRESSIBLE)
71             .run()
72             .expect(expected)
73     }
74 
75     @Test
Detection of IDEA-specific suppression in Kotlin sourcesnull76     fun `Detection of IDEA-specific suppression in Kotlin sources`() {
77         val input =
78             kotlin(
79                 "src/androidx/IdeaSuppressionKotlin.kt",
80                 """
81                 class IdeaSuppressionKotlin {
82 
83                     // Call to a deprecated method with an inline suppression.
84                     fun callDeprecatedMethod() {
85                         //noinspection deprecation
86                         deprecatedMethod()
87 
88                         notDeprecatedMethod()
89                     }
90 
91                     @Deprecated("Replaced with {@link #notDeprecatedMethod()}")
92                     fun deprecatedMethod() {}
93 
94                     fun notDeprecatedMethod() {}
95                 }
96             """
97                     .trimIndent()
98             )
99 
100         val expected =
101             """
102 src/androidx/IdeaSuppressionKotlin.kt:5: Error: Uses IntelliJ-specific suppression, should use @SuppressWarnings("deprecation") [IdeaSuppression]
103         //noinspection deprecation
104         ~~~~~~~~~~~~~~~~~~~~~~~~~~
105 1 errors, 0 warnings
106         """
107                 .trimIndent()
108 
109         lint()
110             .files(*stubs, input)
111             .allowDuplicates()
112             .skipTestModes(TestMode.SUPPRESSIBLE)
113             .run()
114             .expect(expected)
115     }
116 }
117