Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 python |
| 16 | |
| 17 | import ( |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 18 | "fmt" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 19 | "os" |
| 20 | "path/filepath" |
Ronald Braunstein | c4cd7a1 | 2024-04-16 16:39:48 -0700 | [diff] [blame^] | 21 | "strings" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 22 | "testing" |
| 23 | |
| 24 | "android/soong/android" |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 25 | "android/soong/cc" |
Ronald Braunstein | c4cd7a1 | 2024-04-16 16:39:48 -0700 | [diff] [blame^] | 26 | |
| 27 | "github.com/google/blueprint" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 30 | type pyModule struct { |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 31 | name string |
| 32 | actualVersion string |
| 33 | pyRunfiles []string |
| 34 | srcsZip string |
| 35 | depsSrcsZips []string |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | var ( |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 39 | buildNamePrefix = "soong_python_test" |
| 40 | // We allow maching almost anything before the actual variant so that the os/arch variant |
| 41 | // is matched. |
| 42 | moduleVariantErrTemplate = `%s: module %q variant "[a-zA-Z0-9_]*%s": ` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 43 | pkgPathErrTemplate = moduleVariantErrTemplate + |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 44 | "pkg_path: %q must be a relative path contained in par file." |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 45 | badIdentifierErrTemplate = moduleVariantErrTemplate + |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 46 | "srcs: the path %q contains invalid subpath %q." |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 47 | dupRunfileErrTemplate = moduleVariantErrTemplate + |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 48 | "found two files to be placed at the same location within zip %q." + |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 49 | " First file: in module %s at path %q." + |
| 50 | " Second file: in module %s at path %q." |
| 51 | noSrcFileErr = moduleVariantErrTemplate + "doesn't have any source files!" |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 52 | badSrcFileExtErr = moduleVariantErrTemplate + "srcs: found non (.py|.proto) file: %q!" |
| 53 | badDataFileExtErr = moduleVariantErrTemplate + "data: found (.py|.proto) file: %q!" |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 54 | bpFile = "Android.bp" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 55 | |
| 56 | data = []struct { |
| 57 | desc string |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 58 | mockFiles android.MockFS |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 59 | |
| 60 | errors []string |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 61 | expectedBinaries []pyModule |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 62 | }{ |
| 63 | { |
| 64 | desc: "module without any src files", |
| 65 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 66 | filepath.Join("dir", bpFile): []byte( |
| 67 | `python_library_host { |
| 68 | name: "lib1", |
| 69 | }`, |
| 70 | ), |
| 71 | }, |
| 72 | errors: []string{ |
| 73 | fmt.Sprintf(noSrcFileErr, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 74 | "dir/Android.bp:1:1", "lib1", "PY3"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 75 | }, |
| 76 | }, |
| 77 | { |
| 78 | desc: "module with bad src file ext", |
| 79 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 80 | filepath.Join("dir", bpFile): []byte( |
| 81 | `python_library_host { |
| 82 | name: "lib1", |
| 83 | srcs: [ |
| 84 | "file1.exe", |
| 85 | ], |
| 86 | }`, |
| 87 | ), |
| 88 | "dir/file1.exe": nil, |
| 89 | }, |
| 90 | errors: []string{ |
| 91 | fmt.Sprintf(badSrcFileExtErr, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 92 | "dir/Android.bp:3:11", "lib1", "PY3", "dir/file1.exe"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 93 | }, |
| 94 | }, |
| 95 | { |
| 96 | desc: "module with bad data file ext", |
| 97 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 98 | filepath.Join("dir", bpFile): []byte( |
| 99 | `python_library_host { |
| 100 | name: "lib1", |
| 101 | srcs: [ |
| 102 | "file1.py", |
| 103 | ], |
| 104 | data: [ |
| 105 | "file2.py", |
| 106 | ], |
| 107 | }`, |
| 108 | ), |
| 109 | "dir/file1.py": nil, |
| 110 | "dir/file2.py": nil, |
| 111 | }, |
| 112 | errors: []string{ |
| 113 | fmt.Sprintf(badDataFileExtErr, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 114 | "dir/Android.bp:6:11", "lib1", "PY3", "dir/file2.py"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 115 | }, |
| 116 | }, |
| 117 | { |
| 118 | desc: "module with bad pkg_path format", |
| 119 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 120 | filepath.Join("dir", bpFile): []byte( |
| 121 | `python_library_host { |
| 122 | name: "lib1", |
| 123 | pkg_path: "a/c/../../", |
| 124 | srcs: [ |
| 125 | "file1.py", |
| 126 | ], |
| 127 | } |
| 128 | |
| 129 | python_library_host { |
| 130 | name: "lib2", |
| 131 | pkg_path: "a/c/../../../", |
| 132 | srcs: [ |
| 133 | "file1.py", |
| 134 | ], |
| 135 | } |
| 136 | |
| 137 | python_library_host { |
| 138 | name: "lib3", |
| 139 | pkg_path: "/a/c/../../", |
| 140 | srcs: [ |
| 141 | "file1.py", |
| 142 | ], |
| 143 | }`, |
| 144 | ), |
| 145 | "dir/file1.py": nil, |
| 146 | }, |
| 147 | errors: []string{ |
| 148 | fmt.Sprintf(pkgPathErrTemplate, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 149 | "dir/Android.bp:11:15", "lib2", "PY3", "a/c/../../../"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 150 | fmt.Sprintf(pkgPathErrTemplate, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 151 | "dir/Android.bp:19:15", "lib3", "PY3", "/a/c/../../"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 152 | }, |
| 153 | }, |
| 154 | { |
| 155 | desc: "module with bad runfile src path format", |
| 156 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 157 | filepath.Join("dir", bpFile): []byte( |
| 158 | `python_library_host { |
| 159 | name: "lib1", |
| 160 | pkg_path: "a/b/c/", |
| 161 | srcs: [ |
| 162 | ".file1.py", |
| 163 | "123/file1.py", |
| 164 | "-e/f/file1.py", |
| 165 | ], |
| 166 | }`, |
| 167 | ), |
| 168 | "dir/.file1.py": nil, |
| 169 | "dir/123/file1.py": nil, |
| 170 | "dir/-e/f/file1.py": nil, |
| 171 | }, |
| 172 | errors: []string{ |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 173 | fmt.Sprintf(badIdentifierErrTemplate, "dir/Android.bp:4:11", |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 174 | "lib1", "PY3", "a/b/c/-e/f/file1.py", "-e"), |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 175 | fmt.Sprintf(badIdentifierErrTemplate, "dir/Android.bp:4:11", |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 176 | "lib1", "PY3", "a/b/c/.file1.py", ".file1"), |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 177 | fmt.Sprintf(badIdentifierErrTemplate, "dir/Android.bp:4:11", |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 178 | "lib1", "PY3", "a/b/c/123/file1.py", "123"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 179 | }, |
| 180 | }, |
| 181 | { |
| 182 | desc: "module with duplicate runfile path", |
| 183 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 184 | filepath.Join("dir", bpFile): []byte( |
| 185 | `python_library_host { |
| 186 | name: "lib1", |
| 187 | pkg_path: "a/b/", |
| 188 | srcs: [ |
| 189 | "c/file1.py", |
| 190 | ], |
| 191 | } |
| 192 | |
| 193 | python_library_host { |
| 194 | name: "lib2", |
| 195 | pkg_path: "a/b/c/", |
| 196 | srcs: [ |
| 197 | "file1.py", |
| 198 | ], |
| 199 | libs: [ |
| 200 | "lib1", |
| 201 | ], |
| 202 | } |
Paul Duffin | c60dd80 | 2021-03-17 23:01:06 +0000 | [diff] [blame] | 203 | |
| 204 | python_binary_host { |
| 205 | name: "bin", |
| 206 | pkg_path: "e/", |
| 207 | srcs: [ |
| 208 | "bin.py", |
| 209 | ], |
| 210 | libs: [ |
| 211 | "lib2", |
| 212 | ], |
| 213 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 214 | `, |
| 215 | ), |
| 216 | "dir/c/file1.py": nil, |
| 217 | "dir/file1.py": nil, |
Paul Duffin | c60dd80 | 2021-03-17 23:01:06 +0000 | [diff] [blame] | 218 | "dir/bin.py": nil, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 219 | }, |
| 220 | errors: []string{ |
Paul Duffin | c60dd80 | 2021-03-17 23:01:06 +0000 | [diff] [blame] | 221 | fmt.Sprintf(dupRunfileErrTemplate, "dir/Android.bp:20:6", |
| 222 | "bin", "PY3", "a/b/c/file1.py", "bin", "dir/file1.py", |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 223 | "lib1", "dir/c/file1.py"), |
| 224 | }, |
| 225 | }, |
| 226 | { |
| 227 | desc: "module for testing dependencies", |
| 228 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 229 | filepath.Join("dir", bpFile): []byte( |
Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 230 | `python_defaults { |
| 231 | name: "default_lib", |
| 232 | srcs: [ |
| 233 | "default.py", |
| 234 | ], |
| 235 | version: { |
| 236 | py2: { |
| 237 | enabled: true, |
| 238 | srcs: [ |
| 239 | "default_py2.py", |
| 240 | ], |
| 241 | }, |
| 242 | py3: { |
| 243 | enabled: false, |
| 244 | srcs: [ |
| 245 | "default_py3.py", |
| 246 | ], |
| 247 | }, |
| 248 | }, |
| 249 | } |
| 250 | |
| 251 | python_library_host { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 252 | name: "lib5", |
| 253 | pkg_path: "a/b/", |
| 254 | srcs: [ |
| 255 | "file1.py", |
| 256 | ], |
| 257 | version: { |
| 258 | py2: { |
| 259 | enabled: true, |
| 260 | }, |
| 261 | py3: { |
| 262 | enabled: true, |
| 263 | }, |
| 264 | }, |
| 265 | } |
| 266 | |
| 267 | python_library_host { |
| 268 | name: "lib6", |
| 269 | pkg_path: "c/d/", |
| 270 | srcs: [ |
| 271 | "file2.py", |
| 272 | ], |
| 273 | libs: [ |
| 274 | "lib5", |
| 275 | ], |
| 276 | } |
| 277 | |
| 278 | python_binary_host { |
| 279 | name: "bin", |
Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 280 | defaults: ["default_lib"], |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 281 | pkg_path: "e/", |
| 282 | srcs: [ |
| 283 | "bin.py", |
| 284 | ], |
| 285 | libs: [ |
| 286 | "lib5", |
| 287 | ], |
| 288 | version: { |
| 289 | py3: { |
| 290 | enabled: true, |
| 291 | srcs: [ |
| 292 | "file4.py", |
| 293 | ], |
| 294 | libs: [ |
| 295 | "lib6", |
| 296 | ], |
| 297 | }, |
| 298 | }, |
| 299 | }`, |
| 300 | ), |
Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 301 | filepath.Join("dir", "default.py"): nil, |
| 302 | filepath.Join("dir", "default_py2.py"): nil, |
| 303 | filepath.Join("dir", "default_py3.py"): nil, |
| 304 | filepath.Join("dir", "file1.py"): nil, |
| 305 | filepath.Join("dir", "file2.py"): nil, |
| 306 | filepath.Join("dir", "bin.py"): nil, |
| 307 | filepath.Join("dir", "file4.py"): nil, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 308 | }, |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 309 | expectedBinaries: []pyModule{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 310 | { |
| 311 | name: "bin", |
| 312 | actualVersion: "PY3", |
| 313 | pyRunfiles: []string{ |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 314 | "e/default.py", |
| 315 | "e/bin.py", |
| 316 | "e/default_py3.py", |
| 317 | "e/file4.py", |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 318 | }, |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 319 | srcsZip: "out/soong/.intermediates/dir/bin/PY3/bin.py.srcszip", |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 320 | }, |
| 321 | }, |
| 322 | }, |
| 323 | } |
| 324 | ) |
| 325 | |
| 326 | func TestPythonModule(t *testing.T) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 327 | for _, d := range data { |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 328 | if d.desc != "module with duplicate runfile path" { |
| 329 | continue |
| 330 | } |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 331 | d.mockFiles[filepath.Join("common", bpFile)] = []byte(` |
| 332 | python_library { |
| 333 | name: "py3-stdlib", |
| 334 | host_supported: true, |
| 335 | } |
| 336 | cc_binary { |
| 337 | name: "py3-launcher", |
| 338 | host_supported: true, |
| 339 | } |
| 340 | `) |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 341 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 342 | t.Run(d.desc, func(t *testing.T) { |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 343 | result := android.GroupFixturePreparers( |
| 344 | android.PrepareForTestWithDefaults, |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 345 | android.PrepareForTestWithArchMutator, |
| 346 | android.PrepareForTestWithAllowMissingDependencies, |
| 347 | cc.PrepareForTestWithCcDefaultModules, |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 348 | PrepareForTestWithPythonBuildComponents, |
| 349 | d.mockFiles.AddToFixture(), |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 350 | ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(d.errors)). |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 351 | RunTest(t) |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 352 | |
| 353 | if len(result.Errs) > 0 { |
| 354 | return |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 355 | } |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 356 | |
| 357 | for _, e := range d.expectedBinaries { |
| 358 | t.Run(e.name, func(t *testing.T) { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 359 | expectModule(t, result.TestContext, e.name, e.actualVersion, e.srcsZip, e.pyRunfiles) |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 360 | }) |
| 361 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 362 | }) |
| 363 | } |
| 364 | } |
| 365 | |
Ronald Braunstein | c4cd7a1 | 2024-04-16 16:39:48 -0700 | [diff] [blame^] | 366 | func TestTestOnlyProvider(t *testing.T) { |
| 367 | t.Parallel() |
| 368 | ctx := android.GroupFixturePreparers( |
| 369 | PrepareForTestWithPythonBuildComponents, |
| 370 | android.PrepareForTestWithAllowMissingDependencies, |
| 371 | ).RunTestWithBp(t, ` |
| 372 | // These should be test-only |
| 373 | python_library { name: "py-lib-test", test_only: true } |
| 374 | python_library { name: "py-lib-test-host", test_only: true, host_supported: true } |
| 375 | python_test { name: "py-test", srcs: ["py-test.py"] } |
| 376 | python_test_host { name: "py-test-host", srcs: ["py-test-host.py"] } |
| 377 | python_binary_host { name: "py-bin-test", srcs: ["py-bin-test.py"] } |
| 378 | |
| 379 | // These should not be. |
| 380 | python_library { name: "py-lib" } |
| 381 | python_binary_host { name: "py-bin", srcs: ["py-bin.py"] } |
| 382 | `) |
| 383 | |
| 384 | // Visit all modules and ensure only the ones that should |
| 385 | // marked as test-only are marked as test-only. |
| 386 | |
| 387 | actualTestOnly := []string{} |
| 388 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 389 | if provider, ok := android.OtherModuleProvider(ctx.TestContext.OtherModuleProviderAdaptor(), m, android.TestOnlyProviderKey); ok { |
| 390 | if provider.TestOnly { |
| 391 | actualTestOnly = append(actualTestOnly, m.Name()) |
| 392 | } |
| 393 | } |
| 394 | }) |
| 395 | expectedTestOnlyModules := []string{ |
| 396 | "py-lib-test", |
| 397 | "py-lib-test-host", |
| 398 | "py-test", |
| 399 | "py-test-host", |
| 400 | } |
| 401 | |
| 402 | notEqual, left, right := android.ListSetDifference(expectedTestOnlyModules, actualTestOnly) |
| 403 | if notEqual { |
| 404 | t.Errorf("test-only: Expected but not found: %v, Found but not expected: %v", left, right) |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | // Don't allow setting test-only on things that are always tests or never tests. |
| 409 | func TestInvalidTestOnlyTargets(t *testing.T) { |
| 410 | testCases := []string{ |
| 411 | ` python_test { name: "py-test", test_only: true, srcs: ["py-test.py"] } `, |
| 412 | ` python_test_host { name: "py-test-host", test_only: true, srcs: ["py-test-host.py"] } `, |
| 413 | ` python_defaults { name: "py-defaults", test_only: true, srcs: ["foo.py"] } `, |
| 414 | } |
| 415 | |
| 416 | for i, bp := range testCases { |
| 417 | ctx := android.GroupFixturePreparers( |
| 418 | PrepareForTestWithPythonBuildComponents, |
| 419 | android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
| 420 | |
| 421 | ctx.RegisterModuleType("python_defaults", DefaultsFactory) |
| 422 | }), |
| 423 | android.PrepareForTestWithAllowMissingDependencies). |
| 424 | ExtendWithErrorHandler(android.FixtureIgnoreErrors). |
| 425 | RunTestWithBp(t, bp) |
| 426 | if len(ctx.Errs) != 1 { |
| 427 | t.Errorf("Expected err setting test_only in testcase #%d: %d errs", i, len(ctx.Errs)) |
| 428 | continue |
| 429 | } |
| 430 | if !strings.Contains(ctx.Errs[0].Error(), "unrecognized property \"test_only\"") { |
| 431 | t.Errorf("ERR: %s bad bp: %s", ctx.Errs[0], bp) |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 436 | func expectModule(t *testing.T, ctx *android.TestContext, name, variant, expectedSrcsZip string, expectedPyRunfiles []string) { |
Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 437 | module := ctx.ModuleForTests(name, variant) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 438 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 439 | base, baseOk := module.Module().(*PythonLibraryModule) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 440 | if !baseOk { |
| 441 | t.Fatalf("%s is not Python module!", name) |
| 442 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 443 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 444 | actualPyRunfiles := []string{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 445 | for _, path := range base.srcsPathMappings { |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 446 | actualPyRunfiles = append(actualPyRunfiles, path.dest) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 449 | android.AssertDeepEquals(t, "pyRunfiles", expectedPyRunfiles, actualPyRunfiles) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 450 | |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 451 | android.AssertPathRelativeToTopEquals(t, "srcsZip", expectedSrcsZip, base.srcsZip) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 454 | func TestMain(m *testing.M) { |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 455 | os.Exit(m.Run()) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 456 | } |