aboutsummaryrefslogtreecommitdiff
path: root/android/package_test.go
blob: f25599cf83c05c0e07f8e5c143b3ef43028ebcbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package android

import (
	"testing"
)

var packageTests = []struct {
	name           string
	fs             map[string][]byte
	expectedErrors []string
}{
	// Package default_visibility handling is tested in visibility_test.go
	{
		name: "package must not accept visibility and name properties",
		fs: map[string][]byte{
			"top/Blueprints": []byte(`
				package {
					name: "package",
					visibility: ["//visibility:private"],
				}`),
		},
		expectedErrors: []string{
			`top/Blueprints:3:10: unrecognized property "name"`,
			`top/Blueprints:4:16: unrecognized property "visibility"`,
		},
	},
	{
		name: "multiple packages in separate directories",
		fs: map[string][]byte{
			"top/Blueprints": []byte(`
				package {
				}`),
			"other/Blueprints": []byte(`
				package {
				}`),
			"other/nested/Blueprints": []byte(`
				package {
				}`),
		},
	},
	{
		name: "package must not be specified more than once per package",
		fs: map[string][]byte{
			"top/Blueprints": []byte(`
				package {
					default_visibility: ["//visibility:private"],
					default_applicable_licenses: ["license"],
				}

        package {
				}`),
		},
		expectedErrors: []string{
			`module "//top" already defined`,
		},
	},
}

func TestPackage(t *testing.T) {
	for _, test := range packageTests {
		t.Run(test.name, func(t *testing.T) {
			_, errs := testPackage(test.fs)

			expectedErrors := test.expectedErrors
			if expectedErrors == nil {
				FailIfErrored(t, errs)
			} else {
				for _, expectedError := range expectedErrors {
					FailIfNoMatchingErrors(t, expectedError, errs)
				}
				if len(errs) > len(expectedErrors) {
					t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs))
					for i, expectedError := range expectedErrors {
						t.Errorf("expectedErrors[%d] = %s", i, expectedError)
					}
					for i, err := range errs {
						t.Errorf("errs[%d] = %s", i, err)
					}
				}
			}
		})
	}
}

func testPackage(fs map[string][]byte) (*TestContext, []error) {

	// Create a new config per test as visibility information is stored in the config.
	config := TestArchConfig(buildDir, nil, "", fs)

	ctx := NewTestArchContext()
	RegisterPackageBuildComponents(ctx)
	ctx.Register(config)

	_, errs := ctx.ParseBlueprintsFiles(".")
	if len(errs) > 0 {
		return ctx, errs
	}

	_, errs = ctx.PrepareBuildActions(config)
	return ctx, errs
}