blob: 767b64e82dd6b13bdd8305bbb0c421bba82bb82c [file] [log] [blame]
Bob Badour37af0462021-01-07 03:34:31 +00001package android
2
3import (
4 "testing"
5
6 "github.com/google/blueprint"
7)
8
9var licenseKindTests = []struct {
10 name string
11 fs map[string][]byte
12 expectedErrors []string
13}{
14 {
15 name: "license_kind must not accept licenses property",
16 fs: map[string][]byte{
17 "top/Blueprints": []byte(`
18 license_kind {
19 name: "top_license",
20 licenses: ["other_license"],
21 }`),
22 },
23 expectedErrors: []string{
24 `top/Blueprints:4:14: unrecognized property "licenses"`,
25 },
26 },
27 {
28 name: "bad license_kind",
29 fs: map[string][]byte{
30 "top/Blueprints": []byte(`
31 license_kind {
32 name: "top_notice",
33 conditions: ["notice"],
34 }`),
35 "other/Blueprints": []byte(`
36 mock_license {
37 name: "other_notice",
38 license_kinds: ["notice"],
39 }`),
40 },
41 expectedErrors: []string{
42 `other/Blueprints:2:5: "other_notice" depends on undefined module "notice"`,
43 },
44 },
45 {
46 name: "good license kind",
47 fs: map[string][]byte{
48 "top/Blueprints": []byte(`
49 license_kind {
50 name: "top_by_exception_only",
51 conditions: ["by_exception_only"],
52 }
53
54 mock_license {
55 name: "top_proprietary",
56 license_kinds: ["top_by_exception_only"],
57 }`),
58 "other/Blueprints": []byte(`
59 mock_license {
60 name: "other_proprietary",
61 license_kinds: ["top_proprietary"],
62 }`),
63 },
64 },
65 {
66 name: "multiple license kinds",
67 fs: map[string][]byte{
68 "top/Blueprints": []byte(`
69 license_kind {
70 name: "top_notice",
71 conditions: ["notice"],
72 }
73
74 license_kind {
75 name: "top_by_exception_only",
76 conditions: ["by_exception_only"],
77 }
78
79 mock_license {
80 name: "top_allowed_as_notice",
81 license_kinds: ["top_notice"],
82 }
83
84 mock_license {
85 name: "top_proprietary",
86 license_kinds: ["top_by_exception_only"],
87 }`),
88 "other/Blueprints": []byte(`
89 mock_license {
90 name: "other_rule",
91 license_kinds: ["top_by_exception_only"],
92 }`),
93 },
94 },
95}
96
97func TestLicenseKind(t *testing.T) {
98 for _, test := range licenseKindTests {
99 t.Run(test.name, func(t *testing.T) {
100 _, errs := testLicenseKind(test.fs)
101
102 expectedErrors := test.expectedErrors
103 if expectedErrors == nil {
104 FailIfErrored(t, errs)
105 } else {
106 for _, expectedError := range expectedErrors {
107 FailIfNoMatchingErrors(t, expectedError, errs)
108 }
109 if len(errs) > len(expectedErrors) {
110 t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs))
111 for i, expectedError := range expectedErrors {
112 t.Errorf("expectedErrors[%d] = %s", i, expectedError)
113 }
114 for i, err := range errs {
115 t.Errorf("errs[%d] = %s", i, err)
116 }
117 }
118 }
119 })
120 }
121}
122
123func testLicenseKind(fs map[string][]byte) (*TestContext, []error) {
124
125 // Create a new config per test as license_kind information is stored in the config.
126 config := TestArchConfig(buildDir, nil, "", fs)
127
128 ctx := NewTestArchContext(config)
129 RegisterLicenseKindBuildComponents(ctx)
130 ctx.RegisterModuleType("mock_license", newMockLicenseModule)
131 ctx.Register()
132
133 _, errs := ctx.ParseBlueprintsFiles(".")
134 if len(errs) > 0 {
135 return ctx, errs
136 }
137
138 _, errs = ctx.PrepareBuildActions(config)
139 return ctx, errs
140}
141
142type mockLicenseProperties struct {
143 License_kinds []string
144}
145
146type mockLicenseModule struct {
147 ModuleBase
148 DefaultableModuleBase
149
150 properties mockLicenseProperties
151}
152
153func newMockLicenseModule() Module {
154 m := &mockLicenseModule{}
155 m.AddProperties(&m.properties)
156 InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
157 InitDefaultableModule(m)
158 return m
159}
160
161type licensekindTag struct {
162 blueprint.BaseDependencyTag
163}
164
165func (j *mockLicenseModule) DepsMutator(ctx BottomUpMutatorContext) {
166 m, ok := ctx.Module().(Module)
167 if !ok {
168 return
169 }
170 ctx.AddDependency(m, licensekindTag{}, j.properties.License_kinds...)
171}
172
173func (p *mockLicenseModule) GenerateAndroidBuildActions(ModuleContext) {
174}