| 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) { | 
| Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 25 | ctx, _ := testJavaWithFS(t, ` | 
|  | 26 | java_library { | 
|  | 27 | name: "foo", | 
|  | 28 | srcs: [ | 
|  | 29 | "a.java", | 
|  | 30 | "b.java", | 
|  | 31 | "c.java", | 
|  | 32 | ], | 
|  | 33 | min_sdk_version: "29", | 
|  | 34 | sdk_version: "system_current", | 
|  | 35 | } | 
|  | 36 | `, map[string][]byte{ | 
|  | 37 | "lint-baseline.xml": nil, | 
|  | 38 | }) | 
|  | 39 |  | 
|  | 40 | foo := ctx.ModuleForTests("foo", "android_common") | 
|  | 41 |  | 
| Colin Cross | f61d03d | 2023-11-02 16:56:39 -0700 | [diff] [blame] | 42 | sboxProto := android.RuleBuilderSboxProtoForTests(t, ctx, foo.Output("lint.sbox.textproto")) | 
| Cole Faust | b765d6b | 2024-01-04 10:29:27 -0800 | [diff] [blame] | 43 | if strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") { | 
|  | 44 | t.Error("Passed --baseline flag when baseline_filename was not set") | 
| Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 45 | } | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) { | 
|  | 49 | android.GroupFixturePreparers( | 
|  | 50 | PrepareForTestWithJavaDefaultModules, | 
|  | 51 | android.PrepareForTestDisallowNonExistentPaths, | 
|  | 52 | ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern([]string{`source path "mybaseline.xml" does not exist`})). | 
|  | 53 | RunTestWithBp(t, ` | 
|  | 54 | java_library { | 
|  | 55 | name: "foo", | 
|  | 56 | srcs: [ | 
|  | 57 | ], | 
|  | 58 | min_sdk_version: "29", | 
|  | 59 | sdk_version: "system_current", | 
|  | 60 | lint: { | 
|  | 61 | baseline_filename: "mybaseline.xml", | 
|  | 62 | }, | 
|  | 63 | } | 
|  | 64 | `) | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | func TestJavaLintUsesCorrectBpConfig(t *testing.T) { | 
|  | 68 | ctx, _ := testJavaWithFS(t, ` | 
|  | 69 | java_library { | 
|  | 70 | name: "foo", | 
|  | 71 | srcs: [ | 
|  | 72 | "a.java", | 
|  | 73 | "b.java", | 
|  | 74 | "c.java", | 
|  | 75 | ], | 
|  | 76 | min_sdk_version: "29", | 
|  | 77 | sdk_version: "system_current", | 
|  | 78 | lint: { | 
|  | 79 | error_checks: ["SomeCheck"], | 
|  | 80 | baseline_filename: "mybaseline.xml", | 
|  | 81 | }, | 
|  | 82 | } | 
|  | 83 | `, map[string][]byte{ | 
|  | 84 | "mybaseline.xml": nil, | 
|  | 85 | }) | 
|  | 86 |  | 
|  | 87 | foo := ctx.ModuleForTests("foo", "android_common") | 
|  | 88 |  | 
| Colin Cross | f61d03d | 2023-11-02 16:56:39 -0700 | [diff] [blame] | 89 | sboxProto := android.RuleBuilderSboxProtoForTests(t, ctx, foo.Output("lint.sbox.textproto")) | 
| Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 90 | if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") { | 
|  | 91 | t.Error("did not use the correct file for baseline") | 
|  | 92 | } | 
|  | 93 |  | 
| Cole Faust | 69861aa | 2023-01-31 15:49:07 -0800 | [diff] [blame] | 94 | if !strings.Contains(*sboxProto.Commands[0].Command, "--warning_check NewApi") { | 
|  | 95 | // TODO(b/268261262): Change this to check for --error_check | 
|  | 96 | t.Error("should check NewApi warnings") | 
| Jaewoong Jung | 11623b6 | 2021-04-21 14:16:57 -0700 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
|  | 99 | if !strings.Contains(*sboxProto.Commands[0].Command, "--error_check SomeCheck") { | 
|  | 100 | t.Error("should combine NewApi errors with SomeCheck errors") | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 |  | 
| Jaewoong Jung | 79e6f6b | 2021-04-21 14:01:55 -0700 | [diff] [blame] | 104 | func TestJavaLintBypassUpdatableChecks(t *testing.T) { | 
|  | 105 | testCases := []struct { | 
|  | 106 | name  string | 
|  | 107 | bp    string | 
|  | 108 | error string | 
|  | 109 | }{ | 
|  | 110 | { | 
|  | 111 | name: "warning_checks", | 
|  | 112 | bp: ` | 
|  | 113 | java_library { | 
|  | 114 | name: "foo", | 
|  | 115 | srcs: [ | 
|  | 116 | "a.java", | 
|  | 117 | ], | 
|  | 118 | min_sdk_version: "29", | 
|  | 119 | sdk_version: "current", | 
|  | 120 | lint: { | 
|  | 121 | warning_checks: ["NewApi"], | 
|  | 122 | }, | 
|  | 123 | } | 
|  | 124 | `, | 
|  | 125 | error: "lint.warning_checks: Can't treat \\[NewApi\\] checks as warnings if min_sdk_version is different from sdk_version.", | 
|  | 126 | }, | 
|  | 127 | { | 
|  | 128 | name: "disable_checks", | 
|  | 129 | bp: ` | 
|  | 130 | java_library { | 
|  | 131 | name: "foo", | 
|  | 132 | srcs: [ | 
|  | 133 | "a.java", | 
|  | 134 | ], | 
|  | 135 | min_sdk_version: "29", | 
|  | 136 | sdk_version: "current", | 
|  | 137 | lint: { | 
|  | 138 | disabled_checks: ["NewApi"], | 
|  | 139 | }, | 
|  | 140 | } | 
|  | 141 | `, | 
|  | 142 | error: "lint.disabled_checks: Can't disable \\[NewApi\\] checks if min_sdk_version is different from sdk_version.", | 
|  | 143 | }, | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | for _, testCase := range testCases { | 
|  | 147 | t.Run(testCase.name, func(t *testing.T) { | 
|  | 148 | errorHandler := android.FixtureExpectsAtLeastOneErrorMatchingPattern(testCase.error) | 
|  | 149 | android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules). | 
|  | 150 | ExtendWithErrorHandler(errorHandler). | 
|  | 151 | RunTestWithBp(t, testCase.bp) | 
|  | 152 | }) | 
|  | 153 | } | 
|  | 154 | } | 
| Jaewoong Jung | 48de883 | 2021-04-21 16:17:25 -0700 | [diff] [blame] | 155 |  | 
| Cole Faust | 1021ccd | 2023-02-26 21:15:25 -0800 | [diff] [blame] | 156 | // TODO(b/193460475): Re-enable this test | 
|  | 157 | //func TestJavaLintStrictUpdatabilityLinting(t *testing.T) { | 
|  | 158 | //	bp := ` | 
|  | 159 | //		java_library { | 
|  | 160 | //			name: "foo", | 
|  | 161 | //			srcs: [ | 
|  | 162 | //				"a.java", | 
|  | 163 | //			], | 
|  | 164 | //			static_libs: ["bar"], | 
|  | 165 | //			min_sdk_version: "29", | 
|  | 166 | //			sdk_version: "current", | 
|  | 167 | //			lint: { | 
|  | 168 | //				strict_updatability_linting: true, | 
|  | 169 | //			}, | 
|  | 170 | //		} | 
|  | 171 | // | 
|  | 172 | //		java_library { | 
|  | 173 | //			name: "bar", | 
|  | 174 | //			srcs: [ | 
|  | 175 | //				"a.java", | 
|  | 176 | //			], | 
|  | 177 | //			min_sdk_version: "29", | 
|  | 178 | //			sdk_version: "current", | 
|  | 179 | //		} | 
|  | 180 | //	` | 
|  | 181 | //	fs := android.MockFS{ | 
|  | 182 | //		"lint-baseline.xml": nil, | 
|  | 183 | //	} | 
|  | 184 | // | 
|  | 185 | //	result := android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules, fs.AddToFixture()). | 
|  | 186 | //		RunTestWithBp(t, bp) | 
|  | 187 | // | 
|  | 188 | //	foo := result.ModuleForTests("foo", "android_common") | 
|  | 189 | //	sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto")) | 
|  | 190 | //	if !strings.Contains(*sboxProto.Commands[0].Command, | 
|  | 191 | //		"--baseline lint-baseline.xml --disallowed_issues NewApi") { | 
|  | 192 | //		t.Error("did not restrict baselining NewApi") | 
|  | 193 | //	} | 
|  | 194 | // | 
|  | 195 | //	bar := result.ModuleForTests("bar", "android_common") | 
|  | 196 | //	sboxProto = android.RuleBuilderSboxProtoForTests(t, bar.Output("lint.sbox.textproto")) | 
|  | 197 | //	if !strings.Contains(*sboxProto.Commands[0].Command, | 
|  | 198 | //		"--baseline lint-baseline.xml --disallowed_issues NewApi") { | 
|  | 199 | //		t.Error("did not restrict baselining NewApi") | 
|  | 200 | //	} | 
|  | 201 | //} | 
| Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 202 |  | 
|  | 203 | func TestJavaLintDatabaseSelectionFull(t *testing.T) { | 
| Cole Faust | 69861aa | 2023-01-31 15:49:07 -0800 | [diff] [blame] | 204 | testCases := []struct { | 
|  | 205 | sdk_version   string | 
|  | 206 | expected_file string | 
|  | 207 | }{ | 
|  | 208 | { | 
|  | 209 | "current", | 
|  | 210 | "api_versions_public.xml", | 
|  | 211 | }, { | 
|  | 212 | "core_platform", | 
|  | 213 | "api_versions_public.xml", | 
|  | 214 | }, { | 
|  | 215 | "system_current", | 
|  | 216 | "api_versions_system.xml", | 
|  | 217 | }, { | 
|  | 218 | "module_current", | 
|  | 219 | "api_versions_module_lib.xml", | 
|  | 220 | }, { | 
|  | 221 | "system_server_current", | 
|  | 222 | "api_versions_system_server.xml", | 
|  | 223 | }, { | 
|  | 224 | "S", | 
|  | 225 | "api_versions_public.xml", | 
|  | 226 | }, { | 
|  | 227 | "30", | 
|  | 228 | "api_versions_public.xml", | 
|  | 229 | }, { | 
|  | 230 | "10000", | 
|  | 231 | "api_versions_public.xml", | 
|  | 232 | }, | 
| Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 233 | } | 
|  | 234 | bp := ` | 
|  | 235 | java_library { | 
|  | 236 | name: "foo", | 
|  | 237 | srcs: [ | 
|  | 238 | "a.java", | 
|  | 239 | ], | 
|  | 240 | min_sdk_version: "29", | 
|  | 241 | sdk_version: "XXX", | 
|  | 242 | lint: { | 
|  | 243 | strict_updatability_linting: true, | 
|  | 244 | }, | 
|  | 245 | } | 
|  | 246 | ` | 
|  | 247 | for _, testCase := range testCases { | 
| Cole Faust | 69861aa | 2023-01-31 15:49:07 -0800 | [diff] [blame] | 248 | thisBp := strings.Replace(bp, "XXX", testCase.sdk_version, 1) | 
| Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 249 |  | 
|  | 250 | result := android.GroupFixturePreparers(PrepareForTestWithJavaDefaultModules, FixtureWithPrebuiltApis(map[string][]string{ | 
|  | 251 | "30":    {"foo"}, | 
|  | 252 | "10000": {"foo"}, | 
|  | 253 | })). | 
|  | 254 | RunTestWithBp(t, thisBp) | 
|  | 255 |  | 
|  | 256 | foo := result.ModuleForTests("foo", "android_common") | 
| Colin Cross | f61d03d | 2023-11-02 16:56:39 -0700 | [diff] [blame] | 257 | sboxProto := android.RuleBuilderSboxProtoForTests(t, result.TestContext, foo.Output("lint.sbox.textproto")) | 
| Cole Faust | 69861aa | 2023-01-31 15:49:07 -0800 | [diff] [blame] | 258 | if !strings.Contains(*sboxProto.Commands[0].Command, "/"+testCase.expected_file) { | 
| Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 259 | t.Error("did not use full api database for case", testCase) | 
|  | 260 | } | 
|  | 261 | } | 
| Pedro Loureiro | 18233a2 | 2021-06-08 18:11:21 +0000 | [diff] [blame] | 262 | } |