| 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 ( | 
|  | 18 | "errors" | 
|  | 19 | "fmt" | 
|  | 20 | "io/ioutil" | 
|  | 21 | "os" | 
|  | 22 | "path/filepath" | 
|  | 23 | "reflect" | 
|  | 24 | "sort" | 
|  | 25 | "strings" | 
|  | 26 | "testing" | 
|  | 27 |  | 
|  | 28 | "android/soong/android" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 29 | ) | 
|  | 30 |  | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 31 | var buildDir string | 
|  | 32 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 33 | type pyModule struct { | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 34 | name          string | 
|  | 35 | actualVersion string | 
|  | 36 | pyRunfiles    []string | 
|  | 37 | srcsZip       string | 
|  | 38 | depsSrcsZips  []string | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 39 | } | 
|  | 40 |  | 
|  | 41 | var ( | 
|  | 42 | buildNamePrefix          = "soong_python_test" | 
|  | 43 | moduleVariantErrTemplate = "%s: module %q variant %q: " | 
|  | 44 | pkgPathErrTemplate       = moduleVariantErrTemplate + | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 45 | "pkg_path: %q must be a relative path contained in par file." | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 46 | badIdentifierErrTemplate = moduleVariantErrTemplate + | 
|  | 47 | "srcs: the path %q contains invalid token %q." | 
|  | 48 | dupRunfileErrTemplate = moduleVariantErrTemplate + | 
| Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 49 | "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] | 50 | " First file: in module %s at path %q." + | 
|  | 51 | " Second file: in module %s at path %q." | 
|  | 52 | noSrcFileErr      = moduleVariantErrTemplate + "doesn't have any source files!" | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 53 | badSrcFileExtErr  = moduleVariantErrTemplate + "srcs: found non (.py|.proto) file: %q!" | 
|  | 54 | badDataFileExtErr = moduleVariantErrTemplate + "data: found (.py|.proto) file: %q!" | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 55 | bpFile            = "Android.bp" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 56 |  | 
|  | 57 | data = []struct { | 
|  | 58 | desc      string | 
|  | 59 | mockFiles map[string][]byte | 
|  | 60 |  | 
|  | 61 | errors           []string | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 62 | expectedBinaries []pyModule | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 63 | }{ | 
|  | 64 | { | 
|  | 65 | desc: "module without any src files", | 
|  | 66 | mockFiles: map[string][]byte{ | 
|  | 67 | bpFile: []byte(`subdirs = ["dir"]`), | 
|  | 68 | filepath.Join("dir", bpFile): []byte( | 
|  | 69 | `python_library_host { | 
|  | 70 | name: "lib1", | 
|  | 71 | }`, | 
|  | 72 | ), | 
|  | 73 | }, | 
|  | 74 | errors: []string{ | 
|  | 75 | fmt.Sprintf(noSrcFileErr, | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 76 | "dir/Android.bp:1:1", "lib1", "PY3"), | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 77 | }, | 
|  | 78 | }, | 
|  | 79 | { | 
|  | 80 | desc: "module with bad src file ext", | 
|  | 81 | mockFiles: map[string][]byte{ | 
|  | 82 | bpFile: []byte(`subdirs = ["dir"]`), | 
|  | 83 | filepath.Join("dir", bpFile): []byte( | 
|  | 84 | `python_library_host { | 
|  | 85 | name: "lib1", | 
|  | 86 | srcs: [ | 
|  | 87 | "file1.exe", | 
|  | 88 | ], | 
|  | 89 | }`, | 
|  | 90 | ), | 
|  | 91 | "dir/file1.exe": nil, | 
|  | 92 | }, | 
|  | 93 | errors: []string{ | 
|  | 94 | fmt.Sprintf(badSrcFileExtErr, | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 95 | "dir/Android.bp:3:11", "lib1", "PY3", "dir/file1.exe"), | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 96 | }, | 
|  | 97 | }, | 
|  | 98 | { | 
|  | 99 | desc: "module with bad data file ext", | 
|  | 100 | mockFiles: map[string][]byte{ | 
|  | 101 | bpFile: []byte(`subdirs = ["dir"]`), | 
|  | 102 | filepath.Join("dir", bpFile): []byte( | 
|  | 103 | `python_library_host { | 
|  | 104 | name: "lib1", | 
|  | 105 | srcs: [ | 
|  | 106 | "file1.py", | 
|  | 107 | ], | 
|  | 108 | data: [ | 
|  | 109 | "file2.py", | 
|  | 110 | ], | 
|  | 111 | }`, | 
|  | 112 | ), | 
|  | 113 | "dir/file1.py": nil, | 
|  | 114 | "dir/file2.py": nil, | 
|  | 115 | }, | 
|  | 116 | errors: []string{ | 
|  | 117 | fmt.Sprintf(badDataFileExtErr, | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 118 | "dir/Android.bp:6:11", "lib1", "PY3", "dir/file2.py"), | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 119 | }, | 
|  | 120 | }, | 
|  | 121 | { | 
|  | 122 | desc: "module with bad pkg_path format", | 
|  | 123 | mockFiles: map[string][]byte{ | 
|  | 124 | bpFile: []byte(`subdirs = ["dir"]`), | 
|  | 125 | filepath.Join("dir", bpFile): []byte( | 
|  | 126 | `python_library_host { | 
|  | 127 | name: "lib1", | 
|  | 128 | pkg_path: "a/c/../../", | 
|  | 129 | srcs: [ | 
|  | 130 | "file1.py", | 
|  | 131 | ], | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | python_library_host { | 
|  | 135 | name: "lib2", | 
|  | 136 | pkg_path: "a/c/../../../", | 
|  | 137 | srcs: [ | 
|  | 138 | "file1.py", | 
|  | 139 | ], | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | python_library_host { | 
|  | 143 | name: "lib3", | 
|  | 144 | pkg_path: "/a/c/../../", | 
|  | 145 | srcs: [ | 
|  | 146 | "file1.py", | 
|  | 147 | ], | 
|  | 148 | }`, | 
|  | 149 | ), | 
|  | 150 | "dir/file1.py": nil, | 
|  | 151 | }, | 
|  | 152 | errors: []string{ | 
|  | 153 | fmt.Sprintf(pkgPathErrTemplate, | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 154 | "dir/Android.bp:11:15", "lib2", "PY3", "a/c/../../../"), | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 155 | fmt.Sprintf(pkgPathErrTemplate, | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 156 | "dir/Android.bp:19:15", "lib3", "PY3", "/a/c/../../"), | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 157 | }, | 
|  | 158 | }, | 
|  | 159 | { | 
|  | 160 | desc: "module with bad runfile src path format", | 
|  | 161 | mockFiles: map[string][]byte{ | 
|  | 162 | bpFile: []byte(`subdirs = ["dir"]`), | 
|  | 163 | filepath.Join("dir", bpFile): []byte( | 
|  | 164 | `python_library_host { | 
|  | 165 | name: "lib1", | 
|  | 166 | pkg_path: "a/b/c/", | 
|  | 167 | srcs: [ | 
|  | 168 | ".file1.py", | 
|  | 169 | "123/file1.py", | 
|  | 170 | "-e/f/file1.py", | 
|  | 171 | ], | 
|  | 172 | }`, | 
|  | 173 | ), | 
|  | 174 | "dir/.file1.py":     nil, | 
|  | 175 | "dir/123/file1.py":  nil, | 
|  | 176 | "dir/-e/f/file1.py": nil, | 
|  | 177 | }, | 
|  | 178 | errors: []string{ | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 179 | fmt.Sprintf(badIdentifierErrTemplate, "dir/Android.bp:4:11", | 
| Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 180 | "lib1", "PY3", "a/b/c/-e/f/file1.py", "-e"), | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 181 | fmt.Sprintf(badIdentifierErrTemplate, "dir/Android.bp:4:11", | 
| Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 182 | "lib1", "PY3", "a/b/c/.file1.py", ".file1"), | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 183 | fmt.Sprintf(badIdentifierErrTemplate, "dir/Android.bp:4:11", | 
| Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 184 | "lib1", "PY3", "a/b/c/123/file1.py", "123"), | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 185 | }, | 
|  | 186 | }, | 
|  | 187 | { | 
|  | 188 | desc: "module with duplicate runfile path", | 
|  | 189 | mockFiles: map[string][]byte{ | 
|  | 190 | bpFile: []byte(`subdirs = ["dir"]`), | 
|  | 191 | filepath.Join("dir", bpFile): []byte( | 
|  | 192 | `python_library_host { | 
|  | 193 | name: "lib1", | 
|  | 194 | pkg_path: "a/b/", | 
|  | 195 | srcs: [ | 
|  | 196 | "c/file1.py", | 
|  | 197 | ], | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | python_library_host { | 
|  | 201 | name: "lib2", | 
|  | 202 | pkg_path: "a/b/c/", | 
|  | 203 | srcs: [ | 
|  | 204 | "file1.py", | 
|  | 205 | ], | 
|  | 206 | libs: [ | 
|  | 207 | "lib1", | 
|  | 208 | ], | 
|  | 209 | } | 
|  | 210 | `, | 
|  | 211 | ), | 
|  | 212 | "dir/c/file1.py": nil, | 
|  | 213 | "dir/file1.py":   nil, | 
|  | 214 | }, | 
|  | 215 | errors: []string{ | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 216 | fmt.Sprintf(dupRunfileErrTemplate, "dir/Android.bp:9:6", | 
| Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 217 | "lib2", "PY3", "a/b/c/file1.py", "lib2", "dir/file1.py", | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 218 | "lib1", "dir/c/file1.py"), | 
|  | 219 | }, | 
|  | 220 | }, | 
|  | 221 | { | 
|  | 222 | desc: "module for testing dependencies", | 
|  | 223 | mockFiles: map[string][]byte{ | 
|  | 224 | bpFile: []byte(`subdirs = ["dir"]`), | 
|  | 225 | filepath.Join("dir", bpFile): []byte( | 
| Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 226 | `python_defaults { | 
|  | 227 | name: "default_lib", | 
|  | 228 | srcs: [ | 
|  | 229 | "default.py", | 
|  | 230 | ], | 
|  | 231 | version: { | 
|  | 232 | py2: { | 
|  | 233 | enabled: true, | 
|  | 234 | srcs: [ | 
|  | 235 | "default_py2.py", | 
|  | 236 | ], | 
|  | 237 | }, | 
|  | 238 | py3: { | 
|  | 239 | enabled: false, | 
|  | 240 | srcs: [ | 
|  | 241 | "default_py3.py", | 
|  | 242 | ], | 
|  | 243 | }, | 
|  | 244 | }, | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | python_library_host { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 248 | name: "lib5", | 
|  | 249 | pkg_path: "a/b/", | 
|  | 250 | srcs: [ | 
|  | 251 | "file1.py", | 
|  | 252 | ], | 
|  | 253 | version: { | 
|  | 254 | py2: { | 
|  | 255 | enabled: true, | 
|  | 256 | }, | 
|  | 257 | py3: { | 
|  | 258 | enabled: true, | 
|  | 259 | }, | 
|  | 260 | }, | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | python_library_host { | 
|  | 264 | name: "lib6", | 
|  | 265 | pkg_path: "c/d/", | 
|  | 266 | srcs: [ | 
|  | 267 | "file2.py", | 
|  | 268 | ], | 
|  | 269 | libs: [ | 
|  | 270 | "lib5", | 
|  | 271 | ], | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | python_binary_host { | 
|  | 275 | name: "bin", | 
| Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 276 | defaults: ["default_lib"], | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 277 | pkg_path: "e/", | 
|  | 278 | srcs: [ | 
|  | 279 | "bin.py", | 
|  | 280 | ], | 
|  | 281 | libs: [ | 
|  | 282 | "lib5", | 
|  | 283 | ], | 
|  | 284 | version: { | 
|  | 285 | py3: { | 
|  | 286 | enabled: true, | 
|  | 287 | srcs: [ | 
|  | 288 | "file4.py", | 
|  | 289 | ], | 
|  | 290 | libs: [ | 
|  | 291 | "lib6", | 
|  | 292 | ], | 
|  | 293 | }, | 
|  | 294 | }, | 
|  | 295 | }`, | 
|  | 296 | ), | 
| Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 297 | filepath.Join("dir", "default.py"):     nil, | 
|  | 298 | filepath.Join("dir", "default_py2.py"): nil, | 
|  | 299 | filepath.Join("dir", "default_py3.py"): nil, | 
|  | 300 | filepath.Join("dir", "file1.py"):       nil, | 
|  | 301 | filepath.Join("dir", "file2.py"):       nil, | 
|  | 302 | filepath.Join("dir", "bin.py"):         nil, | 
|  | 303 | filepath.Join("dir", "file4.py"):       nil, | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 304 | stubTemplateHost: []byte(`PYTHON_BINARY = '%interpreter%' | 
|  | 305 | MAIN_FILE = '%main%'`), | 
|  | 306 | }, | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 307 | expectedBinaries: []pyModule{ | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 308 | { | 
|  | 309 | name:          "bin", | 
|  | 310 | actualVersion: "PY3", | 
|  | 311 | pyRunfiles: []string{ | 
| Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 312 | "e/default.py", | 
|  | 313 | "e/bin.py", | 
|  | 314 | "e/default_py3.py", | 
|  | 315 | "e/file4.py", | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 316 | }, | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 317 | srcsZip: "@prefix@/.intermediates/dir/bin/PY3/bin.py.srcszip", | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 318 | depsSrcsZips: []string{ | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 319 | "@prefix@/.intermediates/dir/lib5/PY3/lib5.py.srcszip", | 
|  | 320 | "@prefix@/.intermediates/dir/lib6/PY3/lib6.py.srcszip", | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 321 | }, | 
|  | 322 | }, | 
|  | 323 | }, | 
|  | 324 | }, | 
|  | 325 | } | 
|  | 326 | ) | 
|  | 327 |  | 
|  | 328 | func TestPythonModule(t *testing.T) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 329 | for _, d := range data { | 
|  | 330 | t.Run(d.desc, func(t *testing.T) { | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 331 | config := android.TestConfig(buildDir, nil, "", d.mockFiles) | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 332 | ctx := android.NewTestContext() | 
|  | 333 | ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { | 
|  | 334 | ctx.BottomUp("version_split", versionSplitMutator()).Parallel() | 
|  | 335 | }) | 
| Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 336 | ctx.RegisterModuleType("python_library_host", PythonLibraryHostFactory) | 
|  | 337 | ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) | 
|  | 338 | ctx.RegisterModuleType("python_defaults", defaultsFactory) | 
| Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 339 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 340 | ctx.Register(config) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 341 | _, testErrs := ctx.ParseBlueprintsFiles(bpFile) | 
| Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 342 | android.FailIfErrored(t, testErrs) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 343 | _, actErrs := ctx.PrepareBuildActions(config) | 
|  | 344 | if len(actErrs) > 0 { | 
|  | 345 | testErrs = append(testErrs, expectErrors(t, actErrs, d.errors)...) | 
|  | 346 | } else { | 
|  | 347 | for _, e := range d.expectedBinaries { | 
|  | 348 | testErrs = append(testErrs, | 
|  | 349 | expectModule(t, ctx, buildDir, e.name, | 
|  | 350 | e.actualVersion, | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 351 | e.srcsZip, | 
|  | 352 | e.pyRunfiles, | 
|  | 353 | e.depsSrcsZips)...) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 354 | } | 
|  | 355 | } | 
| Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 356 | android.FailIfErrored(t, testErrs) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 357 | }) | 
|  | 358 | } | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | func expectErrors(t *testing.T, actErrs []error, expErrs []string) (testErrs []error) { | 
|  | 362 | actErrStrs := []string{} | 
|  | 363 | for _, v := range actErrs { | 
|  | 364 | actErrStrs = append(actErrStrs, v.Error()) | 
|  | 365 | } | 
|  | 366 | sort.Strings(actErrStrs) | 
|  | 367 | if len(actErrStrs) != len(expErrs) { | 
|  | 368 | t.Errorf("got (%d) errors, expected (%d) errors!", len(actErrStrs), len(expErrs)) | 
|  | 369 | for _, v := range actErrStrs { | 
|  | 370 | testErrs = append(testErrs, errors.New(v)) | 
|  | 371 | } | 
|  | 372 | } else { | 
|  | 373 | sort.Strings(expErrs) | 
|  | 374 | for i, v := range actErrStrs { | 
|  | 375 | if v != expErrs[i] { | 
|  | 376 | testErrs = append(testErrs, errors.New(v)) | 
|  | 377 | } | 
|  | 378 | } | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | return | 
|  | 382 | } | 
|  | 383 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 384 | func expectModule(t *testing.T, ctx *android.TestContext, buildDir, name, variant, expectedSrcsZip string, | 
|  | 385 | expectedPyRunfiles, expectedDepsSrcsZips []string) (testErrs []error) { | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 386 | module := ctx.ModuleForTests(name, variant) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 387 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 388 | base, baseOk := module.Module().(*Module) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 389 | if !baseOk { | 
|  | 390 | t.Fatalf("%s is not Python module!", name) | 
|  | 391 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 392 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 393 | actualPyRunfiles := []string{} | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 394 | for _, path := range base.srcsPathMappings { | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 395 | actualPyRunfiles = append(actualPyRunfiles, path.dest) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 396 | } | 
|  | 397 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 398 | if !reflect.DeepEqual(actualPyRunfiles, expectedPyRunfiles) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 399 | testErrs = append(testErrs, errors.New(fmt.Sprintf( | 
|  | 400 | `binary "%s" variant "%s" has unexpected pyRunfiles: %q!`, | 
|  | 401 | base.Name(), | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 402 | base.properties.Actual_version, | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 403 | actualPyRunfiles))) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 404 | } | 
|  | 405 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 406 | if base.srcsZip.String() != strings.Replace(expectedSrcsZip, "@prefix@", buildDir, 1) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 407 | testErrs = append(testErrs, errors.New(fmt.Sprintf( | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 408 | `binary "%s" variant "%s" has unexpected srcsZip: %q!`, | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 409 | base.Name(), | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 410 | base.properties.Actual_version, | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 411 | base.srcsZip))) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 412 | } | 
|  | 413 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 414 | for i, _ := range expectedDepsSrcsZips { | 
|  | 415 | expectedDepsSrcsZips[i] = strings.Replace(expectedDepsSrcsZips[i], "@prefix@", buildDir, 1) | 
|  | 416 | } | 
|  | 417 | if !reflect.DeepEqual(base.depsSrcsZips.Strings(), expectedDepsSrcsZips) { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 418 | testErrs = append(testErrs, errors.New(fmt.Sprintf( | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 419 | `binary "%s" variant "%s" has unexpected depsSrcsZips: %q!`, | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 420 | base.Name(), | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 421 | base.properties.Actual_version, | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 422 | base.depsSrcsZips))) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 423 | } | 
|  | 424 |  | 
|  | 425 | return | 
|  | 426 | } | 
|  | 427 |  | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 428 | func setUp() { | 
|  | 429 | var err error | 
|  | 430 | buildDir, err = ioutil.TempDir("", "soong_python_test") | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 431 | if err != nil { | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 432 | panic(err) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 433 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 434 | } | 
|  | 435 |  | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 436 | func tearDown() { | 
| Nan Zhang | aac67d3 | 2017-06-12 10:49:42 -0700 | [diff] [blame] | 437 | os.RemoveAll(buildDir) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 438 | } | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 439 |  | 
|  | 440 | func TestMain(m *testing.M) { | 
|  | 441 | run := func() int { | 
|  | 442 | setUp() | 
|  | 443 | defer tearDown() | 
|  | 444 |  | 
|  | 445 | return m.Run() | 
|  | 446 | } | 
|  | 447 |  | 
|  | 448 | os.Exit(run()) | 
|  | 449 | } |