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 | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 39 | buildNamePrefix = "soong_python_test" |
| 40 | moduleVariantErrTemplate = `%s: module %q variant "[a-zA-Z0-9_]*": ` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 41 | pkgPathErrTemplate = moduleVariantErrTemplate + |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 42 | "pkg_path: %q must be a relative path contained in par file." |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 43 | badIdentifierErrTemplate = moduleVariantErrTemplate + |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 44 | "srcs: the path %q contains invalid subpath %q." |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 45 | dupRunfileErrTemplate = moduleVariantErrTemplate + |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 46 | "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] | 47 | " First file: in module %s at path %q." + |
| 48 | " Second file: in module %s at path %q." |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 49 | badSrcFileExtErr = moduleVariantErrTemplate + `srcs: found non \(.py\|.proto\) file: %q!` |
| 50 | badDataFileExtErr = moduleVariantErrTemplate + `data: found \(.py\) file: %q!` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 51 | bpFile = "Android.bp" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 52 | |
| 53 | data = []struct { |
| 54 | desc string |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 55 | mockFiles android.MockFS |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 56 | |
| 57 | errors []string |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 58 | expectedBinaries []pyModule |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 59 | }{ |
| 60 | { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 61 | desc: "module with bad src file ext", |
| 62 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 63 | filepath.Join("dir", bpFile): []byte( |
| 64 | `python_library_host { |
| 65 | name: "lib1", |
| 66 | srcs: [ |
| 67 | "file1.exe", |
| 68 | ], |
| 69 | }`, |
| 70 | ), |
| 71 | "dir/file1.exe": nil, |
| 72 | }, |
| 73 | errors: []string{ |
| 74 | fmt.Sprintf(badSrcFileExtErr, |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 75 | "dir/Android.bp:3:11", "lib1", "dir/file1.exe"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 76 | }, |
| 77 | }, |
| 78 | { |
| 79 | desc: "module with bad data file ext", |
| 80 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 81 | filepath.Join("dir", bpFile): []byte( |
| 82 | `python_library_host { |
| 83 | name: "lib1", |
| 84 | srcs: [ |
| 85 | "file1.py", |
| 86 | ], |
| 87 | data: [ |
| 88 | "file2.py", |
| 89 | ], |
| 90 | }`, |
| 91 | ), |
| 92 | "dir/file1.py": nil, |
| 93 | "dir/file2.py": nil, |
| 94 | }, |
| 95 | errors: []string{ |
| 96 | fmt.Sprintf(badDataFileExtErr, |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 97 | "dir/Android.bp:6:11", "lib1", "dir/file2.py"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 98 | }, |
| 99 | }, |
| 100 | { |
| 101 | desc: "module with bad pkg_path format", |
| 102 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 103 | filepath.Join("dir", bpFile): []byte( |
| 104 | `python_library_host { |
| 105 | name: "lib1", |
| 106 | pkg_path: "a/c/../../", |
| 107 | srcs: [ |
| 108 | "file1.py", |
| 109 | ], |
| 110 | } |
| 111 | |
| 112 | python_library_host { |
| 113 | name: "lib2", |
| 114 | pkg_path: "a/c/../../../", |
| 115 | srcs: [ |
| 116 | "file1.py", |
| 117 | ], |
| 118 | } |
| 119 | |
| 120 | python_library_host { |
| 121 | name: "lib3", |
| 122 | pkg_path: "/a/c/../../", |
| 123 | srcs: [ |
| 124 | "file1.py", |
| 125 | ], |
| 126 | }`, |
| 127 | ), |
| 128 | "dir/file1.py": nil, |
| 129 | }, |
| 130 | errors: []string{ |
| 131 | fmt.Sprintf(pkgPathErrTemplate, |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 132 | "dir/Android.bp:11:15", "lib2", "a/c/../../../"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 133 | fmt.Sprintf(pkgPathErrTemplate, |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 134 | "dir/Android.bp:19:15", "lib3", "/a/c/../../"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 135 | }, |
| 136 | }, |
| 137 | { |
| 138 | desc: "module with bad runfile src path format", |
| 139 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 140 | filepath.Join("dir", bpFile): []byte( |
| 141 | `python_library_host { |
| 142 | name: "lib1", |
| 143 | pkg_path: "a/b/c/", |
| 144 | srcs: [ |
| 145 | ".file1.py", |
| 146 | "123/file1.py", |
| 147 | "-e/f/file1.py", |
| 148 | ], |
| 149 | }`, |
| 150 | ), |
| 151 | "dir/.file1.py": nil, |
| 152 | "dir/123/file1.py": nil, |
| 153 | "dir/-e/f/file1.py": nil, |
| 154 | }, |
| 155 | errors: []string{ |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 156 | fmt.Sprintf(badIdentifierErrTemplate, "dir/Android.bp:4:11", |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 157 | "lib1", "a/b/c/-e/f/file1.py", "-e"), |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 158 | fmt.Sprintf(badIdentifierErrTemplate, "dir/Android.bp:4:11", |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 159 | "lib1", "a/b/c/.file1.py", ".file1"), |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 160 | fmt.Sprintf(badIdentifierErrTemplate, "dir/Android.bp:4:11", |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 161 | "lib1", "a/b/c/123/file1.py", "123"), |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 162 | }, |
| 163 | }, |
| 164 | { |
| 165 | desc: "module with duplicate runfile path", |
| 166 | mockFiles: map[string][]byte{ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 167 | filepath.Join("dir", bpFile): []byte( |
| 168 | `python_library_host { |
| 169 | name: "lib1", |
| 170 | pkg_path: "a/b/", |
| 171 | srcs: [ |
| 172 | "c/file1.py", |
| 173 | ], |
| 174 | } |
| 175 | |
| 176 | python_library_host { |
| 177 | name: "lib2", |
| 178 | pkg_path: "a/b/c/", |
| 179 | srcs: [ |
| 180 | "file1.py", |
| 181 | ], |
| 182 | libs: [ |
| 183 | "lib1", |
| 184 | ], |
| 185 | } |
Paul Duffin | c60dd80 | 2021-03-17 23:01:06 +0000 | [diff] [blame] | 186 | |
| 187 | python_binary_host { |
| 188 | name: "bin", |
| 189 | pkg_path: "e/", |
| 190 | srcs: [ |
| 191 | "bin.py", |
| 192 | ], |
| 193 | libs: [ |
| 194 | "lib2", |
| 195 | ], |
| 196 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 197 | `, |
| 198 | ), |
| 199 | "dir/c/file1.py": nil, |
| 200 | "dir/file1.py": nil, |
Paul Duffin | c60dd80 | 2021-03-17 23:01:06 +0000 | [diff] [blame] | 201 | "dir/bin.py": nil, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 202 | }, |
| 203 | errors: []string{ |
Paul Duffin | c60dd80 | 2021-03-17 23:01:06 +0000 | [diff] [blame] | 204 | fmt.Sprintf(dupRunfileErrTemplate, "dir/Android.bp:20:6", |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 205 | "bin", "a/b/c/file1.py", "bin", "dir/file1.py", |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 206 | "lib1", "dir/c/file1.py"), |
| 207 | }, |
| 208 | }, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 209 | } |
| 210 | ) |
| 211 | |
| 212 | func TestPythonModule(t *testing.T) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 213 | for _, d := range data { |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 214 | d.mockFiles[filepath.Join("common", bpFile)] = []byte(` |
| 215 | python_library { |
| 216 | name: "py3-stdlib", |
| 217 | host_supported: true, |
| 218 | } |
| 219 | cc_binary { |
| 220 | name: "py3-launcher", |
| 221 | host_supported: true, |
| 222 | } |
| 223 | `) |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 224 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 225 | t.Run(d.desc, func(t *testing.T) { |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 226 | result := android.GroupFixturePreparers( |
| 227 | android.PrepareForTestWithDefaults, |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 228 | android.PrepareForTestWithArchMutator, |
| 229 | android.PrepareForTestWithAllowMissingDependencies, |
| 230 | cc.PrepareForTestWithCcDefaultModules, |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 231 | PrepareForTestWithPythonBuildComponents, |
| 232 | d.mockFiles.AddToFixture(), |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 233 | ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(d.errors)). |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 234 | RunTest(t) |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 235 | |
| 236 | if len(result.Errs) > 0 { |
| 237 | return |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 238 | } |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 239 | |
| 240 | for _, e := range d.expectedBinaries { |
| 241 | t.Run(e.name, func(t *testing.T) { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 242 | expectModule(t, result.TestContext, e.name, e.actualVersion, e.srcsZip, e.pyRunfiles) |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 243 | }) |
| 244 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 245 | }) |
| 246 | } |
| 247 | } |
| 248 | |
Ronald Braunstein | c4cd7a1 | 2024-04-16 16:39:48 -0700 | [diff] [blame] | 249 | func TestTestOnlyProvider(t *testing.T) { |
| 250 | t.Parallel() |
| 251 | ctx := android.GroupFixturePreparers( |
| 252 | PrepareForTestWithPythonBuildComponents, |
| 253 | android.PrepareForTestWithAllowMissingDependencies, |
| 254 | ).RunTestWithBp(t, ` |
| 255 | // These should be test-only |
| 256 | python_library { name: "py-lib-test", test_only: true } |
| 257 | python_library { name: "py-lib-test-host", test_only: true, host_supported: true } |
| 258 | python_test { name: "py-test", srcs: ["py-test.py"] } |
| 259 | python_test_host { name: "py-test-host", srcs: ["py-test-host.py"] } |
| 260 | python_binary_host { name: "py-bin-test", srcs: ["py-bin-test.py"] } |
| 261 | |
| 262 | // These should not be. |
| 263 | python_library { name: "py-lib" } |
| 264 | python_binary_host { name: "py-bin", srcs: ["py-bin.py"] } |
| 265 | `) |
| 266 | |
| 267 | // Visit all modules and ensure only the ones that should |
| 268 | // marked as test-only are marked as test-only. |
| 269 | |
| 270 | actualTestOnly := []string{} |
| 271 | ctx.VisitAllModules(func(m blueprint.Module) { |
| 272 | if provider, ok := android.OtherModuleProvider(ctx.TestContext.OtherModuleProviderAdaptor(), m, android.TestOnlyProviderKey); ok { |
| 273 | if provider.TestOnly { |
| 274 | actualTestOnly = append(actualTestOnly, m.Name()) |
| 275 | } |
| 276 | } |
| 277 | }) |
| 278 | expectedTestOnlyModules := []string{ |
| 279 | "py-lib-test", |
| 280 | "py-lib-test-host", |
| 281 | "py-test", |
| 282 | "py-test-host", |
| 283 | } |
| 284 | |
| 285 | notEqual, left, right := android.ListSetDifference(expectedTestOnlyModules, actualTestOnly) |
| 286 | if notEqual { |
| 287 | t.Errorf("test-only: Expected but not found: %v, Found but not expected: %v", left, right) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Don't allow setting test-only on things that are always tests or never tests. |
| 292 | func TestInvalidTestOnlyTargets(t *testing.T) { |
| 293 | testCases := []string{ |
| 294 | ` python_test { name: "py-test", test_only: true, srcs: ["py-test.py"] } `, |
| 295 | ` python_test_host { name: "py-test-host", test_only: true, srcs: ["py-test-host.py"] } `, |
| 296 | ` python_defaults { name: "py-defaults", test_only: true, srcs: ["foo.py"] } `, |
| 297 | } |
| 298 | |
| 299 | for i, bp := range testCases { |
| 300 | ctx := android.GroupFixturePreparers( |
| 301 | PrepareForTestWithPythonBuildComponents, |
Ronald Braunstein | c4cd7a1 | 2024-04-16 16:39:48 -0700 | [diff] [blame] | 302 | android.PrepareForTestWithAllowMissingDependencies). |
| 303 | ExtendWithErrorHandler(android.FixtureIgnoreErrors). |
| 304 | RunTestWithBp(t, bp) |
| 305 | if len(ctx.Errs) != 1 { |
| 306 | t.Errorf("Expected err setting test_only in testcase #%d: %d errs", i, len(ctx.Errs)) |
| 307 | continue |
| 308 | } |
| 309 | if !strings.Contains(ctx.Errs[0].Error(), "unrecognized property \"test_only\"") { |
| 310 | t.Errorf("ERR: %s bad bp: %s", ctx.Errs[0], bp) |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 315 | func expectModule(t *testing.T, ctx *android.TestContext, name, variant, expectedSrcsZip string, expectedPyRunfiles []string) { |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 316 | module := ctx.ModuleForTests(t, name, variant) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 317 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 318 | base, baseOk := module.Module().(*PythonLibraryModule) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 319 | if !baseOk { |
| 320 | t.Fatalf("%s is not Python module!", name) |
| 321 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 322 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 323 | actualPyRunfiles := []string{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 324 | for _, path := range base.srcsPathMappings { |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 325 | actualPyRunfiles = append(actualPyRunfiles, path.dest) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 328 | android.AssertDeepEquals(t, "pyRunfiles", expectedPyRunfiles, actualPyRunfiles) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 329 | |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 330 | android.AssertPathRelativeToTopEquals(t, "srcsZip", expectedSrcsZip, base.srcsZip) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 333 | func TestMain(m *testing.M) { |
Paul Duffin | 803876aa | 2021-03-17 22:38:23 +0000 | [diff] [blame] | 334 | os.Exit(m.Run()) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 335 | } |