blob: 236fa63ba7f5853af516e57943f2639758918f42 [file] [log] [blame]
Jaewoong Jung79e6f6b2021-04-21 14:01:55 -07001// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jaewoong Jung11623b62021-04-21 14:16:57 -070018 "strings"
Jaewoong Jung79e6f6b2021-04-21 14:01:55 -070019 "testing"
20
21 "android/soong/android"
22)
23
Cole Faustb765d6b2024-01-04 10:29:27 -080024func TestJavaLintDoesntUseBaselineImplicitly(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -080025 t.Parallel()
Jaewoong Jung11623b62021-04-21 14:16:57 -070026 ctx, _ := testJavaWithFS(t, `
27 java_library {
28 name: "foo",
29 srcs: [
30 "a.java",
31 "b.java",
32 "c.java",
33 ],
34 min_sdk_version: "29",
35 sdk_version: "system_current",
36 }
37 `, map[string][]byte{
38 "lint-baseline.xml": nil,
39 })
40
Colin Cross90607e92025-02-11 14:58:07 -080041 foo := ctx.ModuleForTests(t, "foo", "android_common")
Jaewoong Jung11623b62021-04-21 14:16:57 -070042
Colin Crossf61d03d2023-11-02 16:56:39 -070043 sboxProto := android.RuleBuilderSboxProtoForTests(t, ctx, foo.Output("lint.sbox.textproto"))
Cole Faustb765d6b2024-01-04 10:29:27 -080044 if strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
45 t.Error("Passed --baseline flag when baseline_filename was not set")
Jaewoong Jung11623b62021-04-21 14:16:57 -070046 }
47}
48
49func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -080050 t.Parallel()
Jaewoong Jung11623b62021-04-21 14:16:57 -070051 android.GroupFixturePreparers(
52 PrepareForTestWithJavaDefaultModules,
53 android.PrepareForTestDisallowNonExistentPaths,
54 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern([]string{`source path "mybaseline.xml" does not exist`})).
55 RunTestWithBp(t, `
56 java_library {
57 name: "foo",
58 srcs: [
59 ],
60 min_sdk_version: "29",
61 sdk_version: "system_current",
62 lint: {
63 baseline_filename: "mybaseline.xml",
64 },
65 }
66 `)
67}
68
69func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -080070 t.Parallel()
Jaewoong Jung11623b62021-04-21 14:16:57 -070071 ctx, _ := testJavaWithFS(t, `
72 java_library {
73 name: "foo",
74 srcs: [
75 "a.java",
76 "b.java",
77 "c.java",
78 ],
79 min_sdk_version: "29",
80 sdk_version: "system_current",
81 lint: {
82 error_checks: ["SomeCheck"],
83 baseline_filename: "mybaseline.xml",
84 },
85 }
86 `, map[string][]byte{
87 "mybaseline.xml": nil,
88 })
89
Colin Cross90607e92025-02-11 14:58:07 -080090 foo := ctx.ModuleForTests(t, "foo", "android_common")
Jaewoong Jung11623b62021-04-21 14:16:57 -070091
Colin Crossf61d03d2023-11-02 16:56:39 -070092 sboxProto := android.RuleBuilderSboxProtoForTests(t, ctx, foo.Output("lint.sbox.textproto"))
Jaewoong Jung11623b62021-04-21 14:16:57 -070093 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
94 t.Error("did not use the correct file for baseline")
95 }
96
Cole Faust028b94c2024-01-16 17:17:11 -080097 if !strings.Contains(*sboxProto.Commands[0].Command, "--error_check NewApi") {
98 t.Error("should check NewApi errors")
Jaewoong Jung11623b62021-04-21 14:16:57 -070099 }
100
101 if !strings.Contains(*sboxProto.Commands[0].Command, "--error_check SomeCheck") {
102 t.Error("should combine NewApi errors with SomeCheck errors")
103 }
104}
105
Jaewoong Jung79e6f6b2021-04-21 14:01:55 -0700106func TestJavaLintBypassUpdatableChecks(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800107 t.Parallel()
Jaewoong Jung79e6f6b2021-04-21 14:01:55 -0700108 testCases := []struct {
109 name string
110 bp string
111 error string
112 }{
113 {
114 name: "warning_checks",
115 bp: `
116 java_library {
117 name: "foo",
118 srcs: [
119 "a.java",
120 ],
121 min_sdk_version: "29",
122 sdk_version: "current",
123 lint: {
124 warning_checks: ["NewApi"],
125 },
126 }
127 `,
128 error: "lint.warning_checks: Can't treat \\[NewApi\\] checks as warnings if min_sdk_version is different from sdk_version.",
129 },
130 {
131 name: "disable_checks",
132 bp: `
133 java_library {
134 name: "foo",
135 srcs: [
136 "a.java",
137 ],
138 min_sdk_version: "29",
139 sdk_version: "current",
140 lint: {
141 disabled_checks: ["NewApi"],
142 },
143 }
144 `,
145 error: "lint.disabled_checks: Can't disable \\[NewApi\\] checks if min_sdk_version is different from sdk_version.",
146 },
147 }
148
149 for _, testCase := range testCases {
150 t.Run(testCase.name, func(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800151 t.Parallel()
Jaewoong Jung79e6f6b2021-04-21 14:01:55 -0700152 errorHandler := android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.error)
153 android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules).
154 ExtendWithErrorHandler(errorHandler).
155 RunTestWithBp(t, testCase.bp)
156 })
157 }
158}
Jaewoong Jung48de8832021-04-21 16:17:25 -0700159
Cole Faust24e25c02024-01-19 14:12:17 -0800160func TestJavaLintStrictUpdatabilityLinting(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800161 t.Parallel()
Cole Faust24e25c02024-01-19 14:12:17 -0800162 bp := `
163 java_library {
164 name: "foo",
165 srcs: [
166 "a.java",
167 ],
168 static_libs: ["bar"],
169 min_sdk_version: "29",
170 sdk_version: "current",
171 lint: {
172 strict_updatability_linting: true,
Colin Cross87427352024-09-25 15:41:19 -0700173 baseline_filename: "foo_lint_baseline.xml",
Cole Faust24e25c02024-01-19 14:12:17 -0800174 },
175 }
176
177 java_library {
178 name: "bar",
179 srcs: [
180 "a.java",
181 ],
182 min_sdk_version: "29",
183 sdk_version: "current",
184 lint: {
Colin Cross87427352024-09-25 15:41:19 -0700185 baseline_filename: "bar_lint_baseline.xml",
Cole Faust24e25c02024-01-19 14:12:17 -0800186 }
187 }
188 `
189 fs := android.MockFS{
190 "lint-baseline.xml": nil,
191 }
192
193 result := android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules, fs.AddToFixture()).
194 RunTestWithBp(t, bp)
195
Colin Cross90607e92025-02-11 14:58:07 -0800196 foo := result.ModuleForTests(t, "foo", "android_common")
Colin Cross87427352024-09-25 15:41:19 -0700197 strictUpdatabilityCheck := foo.Output("lint_strict_updatability_check.stamp")
198 if !strings.Contains(strictUpdatabilityCheck.RuleParams.Command,
199 "--disallowed_issues NewApi") {
Cole Faust24e25c02024-01-19 14:12:17 -0800200 t.Error("did not restrict baselining NewApi")
201 }
Colin Cross87427352024-09-25 15:41:19 -0700202 android.AssertStringListContains(t, "strict updatability check baseline inputs", strictUpdatabilityCheck.Inputs.Strings(), "foo_lint_baseline.xml")
203 android.AssertStringListContains(t, "strict updatability check baseline inputs", strictUpdatabilityCheck.Inputs.Strings(), "bar_lint_baseline.xml")
Cole Faust24e25c02024-01-19 14:12:17 -0800204}
Pedro Loureiro18233a22021-06-08 18:11:21 +0000205
206func TestJavaLintDatabaseSelectionFull(t *testing.T) {
Cole Faust69861aa2023-01-31 15:49:07 -0800207 testCases := []struct {
208 sdk_version string
209 expected_file string
210 }{
211 {
212 "current",
213 "api_versions_public.xml",
214 }, {
215 "core_platform",
216 "api_versions_public.xml",
217 }, {
218 "system_current",
219 "api_versions_system.xml",
220 }, {
221 "module_current",
222 "api_versions_module_lib.xml",
223 }, {
224 "system_server_current",
225 "api_versions_system_server.xml",
226 }, {
227 "S",
228 "api_versions_public.xml",
229 }, {
230 "30",
231 "api_versions_public.xml",
232 }, {
233 "10000",
234 "api_versions_public.xml",
235 },
Pedro Loureiro18233a22021-06-08 18:11:21 +0000236 }
237 bp := `
238 java_library {
239 name: "foo",
240 srcs: [
241 "a.java",
242 ],
243 min_sdk_version: "29",
244 sdk_version: "XXX",
245 lint: {
246 strict_updatability_linting: true,
247 },
248 }
249`
250 for _, testCase := range testCases {
Cole Faust69861aa2023-01-31 15:49:07 -0800251 thisBp := strings.Replace(bp, "XXX", testCase.sdk_version, 1)
Pedro Loureiro18233a22021-06-08 18:11:21 +0000252
253 result := android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules, FixtureWithPrebuiltApis(map[string][]string{
254 "30": {"foo"},
255 "10000": {"foo"},
256 })).
257 RunTestWithBp(t, thisBp)
258
Colin Cross90607e92025-02-11 14:58:07 -0800259 foo := result.ModuleForTests(t, "foo", "android_common")
Colin Crossf61d03d2023-11-02 16:56:39 -0700260 sboxProto := android.RuleBuilderSboxProtoForTests(t, result.TestContext, foo.Output("lint.sbox.textproto"))
Cole Faust69861aa2023-01-31 15:49:07 -0800261 if !strings.Contains(*sboxProto.Commands[0].Command, "/"+testCase.expected_file) {
Pedro Loureiro18233a22021-06-08 18:11:21 +0000262 t.Error("did not use full api database for case", testCase)
263 }
264 }
Pedro Loureiro18233a22021-06-08 18:11:21 +0000265}
Cole Faust5d0aaf42024-01-29 13:49:14 -0800266
267func TestCantControlCheckSeverityWithFlags(t *testing.T) {
268 bp := `
269 java_library {
270 name: "foo",
271 srcs: [
272 "a.java",
273 ],
274 min_sdk_version: "29",
275 sdk_version: "current",
276 lint: {
277 flags: ["--disabled", "NewApi"],
278 },
279 }
280 `
281 PrepareForTestWithJavaDefaultModules.
282 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("Don't use --disable, --enable, or --check in the flags field, instead use the dedicated disabled_checks, warning_checks, error_checks, or fatal_checks fields")).
283 RunTestWithBp(t, bp)
284}
Cole Faustc7315282025-01-10 15:37:01 -0800285
286// b/358643466
287func TestNotTestViaDefault(t *testing.T) {
288 bp := `
289 java_defaults {
290 name: "mydefaults",
291 lint: {
292 test: false,
293 },
294 }
295 android_test {
296 name: "foo",
297 srcs: [
298 "a.java",
299 ],
300 min_sdk_version: "29",
301 sdk_version: "current",
302 defaults: ["mydefaults"],
303 }
304 android_test {
305 name: "foo2",
306 srcs: [
307 "a.java",
308 ],
309 min_sdk_version: "29",
310 sdk_version: "current",
311 }
312 `
313 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, bp)
314 ctx := result.TestContext
315
Colin Cross90607e92025-02-11 14:58:07 -0800316 foo := ctx.ModuleForTests(t, "foo", "android_common")
Cole Faustc7315282025-01-10 15:37:01 -0800317 sboxProto := android.RuleBuilderSboxProtoForTests(t, ctx, foo.Output("lint.sbox.textproto"))
318 command := *sboxProto.Commands[0].Command
319
320 if strings.Contains(command, "--test") {
321 t.Fatalf("Expected command to not contain --test")
322 }
323
Colin Cross90607e92025-02-11 14:58:07 -0800324 foo2 := ctx.ModuleForTests(t, "foo2", "android_common")
Cole Faustc7315282025-01-10 15:37:01 -0800325 sboxProto2 := android.RuleBuilderSboxProtoForTests(t, ctx, foo2.Output("lint.sbox.textproto"))
326 command2 := *sboxProto2.Commands[0].Command
327
328 if !strings.Contains(command2, "--test") {
329 t.Fatalf("Expected command to contain --test")
330 }
331}