blob: e5b05566716e2ef93b69f6fca5847420c404a46d [file] [log] [blame]
Paul Duffine2453c72019-05-31 14:00:04 +01001package android
2
3import (
Paul Duffine2453c72019-05-31 14:00:04 +01004 "testing"
5)
6
7var 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{
23 `top/Blueprints:3:10: mutated field name cannot be set in a Blueprint file`,
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"],
47 }
48
49 package {
50 }`),
51 },
52 expectedErrors: []string{
53 `module "//top": package {...} specified multiple times`,
54 },
55 },
56}
57
58func TestPackage(t *testing.T) {
Paul Duffine2453c72019-05-31 14:00:04 +010059 for _, test := range packageTests {
60 t.Run(test.name, func(t *testing.T) {
Colin Crossfa078212019-07-02 11:31:37 -070061 _, errs := testPackage(test.fs)
Paul Duffine2453c72019-05-31 14:00:04 +010062
63 expectedErrors := test.expectedErrors
64 if expectedErrors == nil {
65 FailIfErrored(t, errs)
66 } else {
67 for _, expectedError := range expectedErrors {
68 FailIfNoMatchingErrors(t, expectedError, errs)
69 }
70 if len(errs) > len(expectedErrors) {
71 t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs))
72 for i, expectedError := range expectedErrors {
73 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
74 }
75 for i, err := range errs {
76 t.Errorf("errs[%d] = %s", i, err)
77 }
78 }
79 }
80 })
81 }
82}
83
Colin Crossfa078212019-07-02 11:31:37 -070084func testPackage(fs map[string][]byte) (*TestContext, []error) {
Paul Duffine2453c72019-05-31 14:00:04 +010085
86 // Create a new config per test as visibility information is stored in the config.
87 config := TestArchConfig(buildDir, nil)
88
89 ctx := NewTestArchContext()
90 ctx.RegisterModuleType("package", ModuleFactoryAdaptor(PackageFactory))
91 ctx.PreArchMutators(registerPackageRenamer)
92 ctx.Register()
93
94 ctx.MockFileSystem(fs)
95
96 _, errs := ctx.ParseBlueprintsFiles(".")
97 if len(errs) > 0 {
98 return ctx, errs
99 }
100
101 _, errs = ctx.PrepareBuildActions(config)
102 return ctx, errs
103}