Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame^] | 1 | package android |
| 2 | |
| 3 | import ( |
| 4 | "io/ioutil" |
| 5 | "os" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | var packageTests = []struct { |
| 10 | name string |
| 11 | fs map[string][]byte |
| 12 | expectedErrors []string |
| 13 | }{ |
| 14 | // Package default_visibility handling is tested in visibility_test.go |
| 15 | { |
| 16 | name: "package must not accept visibility and name properties", |
| 17 | fs: map[string][]byte{ |
| 18 | "top/Blueprints": []byte(` |
| 19 | package { |
| 20 | name: "package", |
| 21 | visibility: ["//visibility:private"], |
| 22 | }`), |
| 23 | }, |
| 24 | expectedErrors: []string{ |
| 25 | `top/Blueprints:3:10: mutated field name cannot be set in a Blueprint file`, |
| 26 | `top/Blueprints:4:16: unrecognized property "visibility"`, |
| 27 | }, |
| 28 | }, |
| 29 | { |
| 30 | name: "multiple packages in separate directories", |
| 31 | fs: map[string][]byte{ |
| 32 | "top/Blueprints": []byte(` |
| 33 | package { |
| 34 | }`), |
| 35 | "other/Blueprints": []byte(` |
| 36 | package { |
| 37 | }`), |
| 38 | "other/nested/Blueprints": []byte(` |
| 39 | package { |
| 40 | }`), |
| 41 | }, |
| 42 | }, |
| 43 | { |
| 44 | name: "package must not be specified more than once per package", |
| 45 | fs: map[string][]byte{ |
| 46 | "top/Blueprints": []byte(` |
| 47 | package { |
| 48 | default_visibility: ["//visibility:private"], |
| 49 | } |
| 50 | |
| 51 | package { |
| 52 | }`), |
| 53 | }, |
| 54 | expectedErrors: []string{ |
| 55 | `module "//top": package {...} specified multiple times`, |
| 56 | }, |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | func TestPackage(t *testing.T) { |
| 61 | buildDir, err := ioutil.TempDir("", "soong_package_test") |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | defer os.RemoveAll(buildDir) |
| 66 | |
| 67 | for _, test := range packageTests { |
| 68 | t.Run(test.name, func(t *testing.T) { |
| 69 | _, errs := testPackage(buildDir, test.fs) |
| 70 | |
| 71 | expectedErrors := test.expectedErrors |
| 72 | if expectedErrors == nil { |
| 73 | FailIfErrored(t, errs) |
| 74 | } else { |
| 75 | for _, expectedError := range expectedErrors { |
| 76 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 77 | } |
| 78 | if len(errs) > len(expectedErrors) { |
| 79 | t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs)) |
| 80 | for i, expectedError := range expectedErrors { |
| 81 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 82 | } |
| 83 | for i, err := range errs { |
| 84 | t.Errorf("errs[%d] = %s", i, err) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func testPackage(buildDir string, fs map[string][]byte) (*TestContext, []error) { |
| 93 | |
| 94 | // Create a new config per test as visibility information is stored in the config. |
| 95 | config := TestArchConfig(buildDir, nil) |
| 96 | |
| 97 | ctx := NewTestArchContext() |
| 98 | ctx.RegisterModuleType("package", ModuleFactoryAdaptor(PackageFactory)) |
| 99 | ctx.PreArchMutators(registerPackageRenamer) |
| 100 | ctx.Register() |
| 101 | |
| 102 | ctx.MockFileSystem(fs) |
| 103 | |
| 104 | _, errs := ctx.ParseBlueprintsFiles(".") |
| 105 | if len(errs) > 0 { |
| 106 | return ctx, errs |
| 107 | } |
| 108 | |
| 109 | _, errs = ctx.PrepareBuildActions(config) |
| 110 | return ctx, errs |
| 111 | } |