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