Jaewoong Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 18 | "strings" |
Jaewoong Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
Cole Faust | b765d6b | 2024-01-04 10:29:27 -0800 | [diff] [blame] | 24 | func TestJavaLintDoesntUseBaselineImplicitly(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 25 | t.Parallel() |
Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 26 | 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 Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 41 | foo := ctx.ModuleForTests(t, "foo", "android_common") |
Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 42 | |
Colin Cross | f61d03d | 2023-11-02 16:56:39 -0700 | [diff] [blame] | 43 | sboxProto := android.RuleBuilderSboxProtoForTests(t, ctx, foo.Output("lint.sbox.textproto")) |
Cole Faust | b765d6b | 2024-01-04 10:29:27 -0800 | [diff] [blame] | 44 | if strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") { |
| 45 | t.Error("Passed --baseline flag when baseline_filename was not set") |
Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 50 | t.Parallel() |
Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 51 | 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 | |
| 69 | func TestJavaLintUsesCorrectBpConfig(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 70 | t.Parallel() |
Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 71 | 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 Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 90 | foo := ctx.ModuleForTests(t, "foo", "android_common") |
Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 91 | |
Colin Cross | f61d03d | 2023-11-02 16:56:39 -0700 | [diff] [blame] | 92 | sboxProto := android.RuleBuilderSboxProtoForTests(t, ctx, foo.Output("lint.sbox.textproto")) |
Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 93 | if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") { |
| 94 | t.Error("did not use the correct file for baseline") |
| 95 | } |
| 96 | |
Cole Faust | 028b94c | 2024-01-16 17:17:11 -0800 | [diff] [blame] | 97 | if !strings.Contains(*sboxProto.Commands[0].Command, "--error_check NewApi") { |
| 98 | t.Error("should check NewApi errors") |
Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 99 | } |
| 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 Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 106 | func TestJavaLintBypassUpdatableChecks(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 107 | t.Parallel() |
Jaewoong Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 108 | 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 Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 151 | t.Parallel() |
Jaewoong Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 152 | errorHandler := android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.error) |
| 153 | android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules). |
| 154 | ExtendWithErrorHandler(errorHandler). |
| 155 | RunTestWithBp(t, testCase.bp) |
| 156 | }) |
| 157 | } |
| 158 | } |
Jaewoong Jung | 48de883 | 2021-04-21 16:17:25 -0700 | [diff] [blame] | 159 | |
Cole Faust | 24e25c0 | 2024-01-19 14:12:17 -0800 | [diff] [blame] | 160 | func TestJavaLintStrictUpdatabilityLinting(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 161 | t.Parallel() |
Cole Faust | 24e25c0 | 2024-01-19 14:12:17 -0800 | [diff] [blame] | 162 | 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 Cross | 8742735 | 2024-09-25 15:41:19 -0700 | [diff] [blame] | 173 | baseline_filename: "foo_lint_baseline.xml", |
Cole Faust | 24e25c0 | 2024-01-19 14:12:17 -0800 | [diff] [blame] | 174 | }, |
| 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 Cross | 8742735 | 2024-09-25 15:41:19 -0700 | [diff] [blame] | 185 | baseline_filename: "bar_lint_baseline.xml", |
Cole Faust | 24e25c0 | 2024-01-19 14:12:17 -0800 | [diff] [blame] | 186 | } |
| 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 Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 196 | foo := result.ModuleForTests(t, "foo", "android_common") |
Colin Cross | 8742735 | 2024-09-25 15:41:19 -0700 | [diff] [blame] | 197 | strictUpdatabilityCheck := foo.Output("lint_strict_updatability_check.stamp") |
| 198 | if !strings.Contains(strictUpdatabilityCheck.RuleParams.Command, |
| 199 | "--disallowed_issues NewApi") { |
Cole Faust | 24e25c0 | 2024-01-19 14:12:17 -0800 | [diff] [blame] | 200 | t.Error("did not restrict baselining NewApi") |
| 201 | } |
Colin Cross | 8742735 | 2024-09-25 15:41:19 -0700 | [diff] [blame] | 202 | 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 Faust | 24e25c0 | 2024-01-19 14:12:17 -0800 | [diff] [blame] | 204 | } |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 205 | |
| 206 | func TestJavaLintDatabaseSelectionFull(t *testing.T) { |
Cole Faust | 69861aa | 2023-01-31 15:49:07 -0800 | [diff] [blame] | 207 | 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 Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 236 | } |
| 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 Faust | 69861aa | 2023-01-31 15:49:07 -0800 | [diff] [blame] | 251 | thisBp := strings.Replace(bp, "XXX", testCase.sdk_version, 1) |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 252 | |
| 253 | result := android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules, FixtureWithPrebuiltApis(map[string][]string{ |
| 254 | "30": {"foo"}, |
| 255 | "10000": {"foo"}, |
| 256 | })). |
| 257 | RunTestWithBp(t, thisBp) |
| 258 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 259 | foo := result.ModuleForTests(t, "foo", "android_common") |
Colin Cross | f61d03d | 2023-11-02 16:56:39 -0700 | [diff] [blame] | 260 | sboxProto := android.RuleBuilderSboxProtoForTests(t, result.TestContext, foo.Output("lint.sbox.textproto")) |
Cole Faust | 69861aa | 2023-01-31 15:49:07 -0800 | [diff] [blame] | 261 | if !strings.Contains(*sboxProto.Commands[0].Command, "/"+testCase.expected_file) { |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 262 | t.Error("did not use full api database for case", testCase) |
| 263 | } |
| 264 | } |
Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 265 | } |
Cole Faust | 5d0aaf4 | 2024-01-29 13:49:14 -0800 | [diff] [blame] | 266 | |
| 267 | func 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 Faust | c731528 | 2025-01-10 15:37:01 -0800 | [diff] [blame] | 285 | |
| 286 | // b/358643466 |
| 287 | func 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 Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 316 | foo := ctx.ModuleForTests(t, "foo", "android_common") |
Cole Faust | c731528 | 2025-01-10 15:37:01 -0800 | [diff] [blame] | 317 | 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 Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 324 | foo2 := ctx.ModuleForTests(t, "foo2", "android_common") |
Cole Faust | c731528 | 2025-01-10 15:37:01 -0800 | [diff] [blame] | 325 | 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 | } |