Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | var packageTests = []struct { |
| 8 | name string |
| 9 | fs map[string][]byte |
| 10 | expectedErrors []string |
| 11 | }{ |
| 12 | // Package default_visibility handling is tested in visibility_test.go |
| 13 | { |
| 14 | name: "package must not accept visibility and name properties", |
| 15 | fs: map[string][]byte{ |
| 16 | "top/Blueprints": []byte(` |
| 17 | package { |
| 18 | name: "package", |
| 19 | visibility: ["//visibility:private"], |
| 20 | }`), |
| 21 | }, |
| 22 | expectedErrors: []string{ |
Paul Duffin | a43e13e | 2020-05-01 11:57:12 +0100 | [diff] [blame] | 23 | `top/Blueprints:3:10: unrecognized property "name"`, |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 24 | `top/Blueprints:4:16: unrecognized property "visibility"`, |
| 25 | }, |
| 26 | }, |
| 27 | { |
| 28 | name: "multiple packages in separate directories", |
| 29 | fs: map[string][]byte{ |
| 30 | "top/Blueprints": []byte(` |
| 31 | package { |
| 32 | }`), |
| 33 | "other/Blueprints": []byte(` |
| 34 | package { |
| 35 | }`), |
| 36 | "other/nested/Blueprints": []byte(` |
| 37 | package { |
| 38 | }`), |
| 39 | }, |
| 40 | }, |
| 41 | { |
| 42 | name: "package must not be specified more than once per package", |
| 43 | fs: map[string][]byte{ |
| 44 | "top/Blueprints": []byte(` |
| 45 | package { |
| 46 | default_visibility: ["//visibility:private"], |
Bob Badour | dc5be90 | 2021-03-15 20:00:01 -0700 | [diff] [blame] | 47 | default_applicable_licenses: ["license"], |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | package { |
| 51 | }`), |
| 52 | }, |
| 53 | expectedErrors: []string{ |
Paul Duffin | a43e13e | 2020-05-01 11:57:12 +0100 | [diff] [blame] | 54 | `module "//top" already defined`, |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 55 | }, |
| 56 | }, |
| 57 | } |
| 58 | |
| 59 | func TestPackage(t *testing.T) { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 60 | for _, test := range packageTests { |
| 61 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | fa07821 | 2019-07-02 11:31:37 -0700 | [diff] [blame] | 62 | _, errs := testPackage(test.fs) |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 63 | |
| 64 | expectedErrors := test.expectedErrors |
| 65 | if expectedErrors == nil { |
| 66 | FailIfErrored(t, errs) |
| 67 | } else { |
| 68 | for _, expectedError := range expectedErrors { |
| 69 | FailIfNoMatchingErrors(t, expectedError, errs) |
| 70 | } |
| 71 | if len(errs) > len(expectedErrors) { |
| 72 | t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs)) |
| 73 | for i, expectedError := range expectedErrors { |
| 74 | t.Errorf("expectedErrors[%d] = %s", i, expectedError) |
| 75 | } |
| 76 | for i, err := range errs { |
| 77 | t.Errorf("errs[%d] = %s", i, err) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
Colin Cross | fa07821 | 2019-07-02 11:31:37 -0700 | [diff] [blame] | 85 | func testPackage(fs map[string][]byte) (*TestContext, []error) { |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 86 | |
| 87 | // Create a new config per test as visibility information is stored in the config. |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 88 | config := TestArchConfig(buildDir, nil, "", fs) |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 89 | |
| 90 | ctx := NewTestArchContext() |
Paul Duffin | c132742 | 2020-01-14 12:15:29 +0000 | [diff] [blame] | 91 | RegisterPackageBuildComponents(ctx) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 92 | ctx.Register(config) |
Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 93 | |
| 94 | _, errs := ctx.ParseBlueprintsFiles(".") |
| 95 | if len(errs) > 0 { |
| 96 | return ctx, errs |
| 97 | } |
| 98 | |
| 99 | _, errs = ctx.PrepareBuildActions(config) |
| 100 | return ctx, errs |
| 101 | } |