blob: 7a909a6f9d2b2651cc7e98ec9e5645e4fd3e1277 [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
Paul Duffin8bd28652021-03-03 00:42:36 +000011 fs MockFS
Bob Badour37af0462021-01-07 03:34:31 +000012 expectedErrors []string
13}{
14 {
15 name: "license_kind must not accept licenses property",
16 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020017 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000018 license_kind {
19 name: "top_license",
20 licenses: ["other_license"],
21 }`),
22 },
23 expectedErrors: []string{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020024 `top/Android.bp:4:14: unrecognized property "licenses"`,
Bob Badour37af0462021-01-07 03:34:31 +000025 },
26 },
27 {
28 name: "bad license_kind",
29 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020030 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000031 license_kind {
32 name: "top_notice",
33 conditions: ["notice"],
34 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020035 "other/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000036 mock_license {
37 name: "other_notice",
38 license_kinds: ["notice"],
39 }`),
40 },
41 expectedErrors: []string{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020042 `other/Android.bp:2:5: "other_notice" depends on undefined module "notice"`,
Bob Badour37af0462021-01-07 03:34:31 +000043 },
44 },
45 {
46 name: "good license kind",
47 fs: map[string][]byte{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020048 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000049 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 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020058 "other/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000059 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{
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020068 "top/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000069 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 }`),
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +020088 "other/Android.bp": []byte(`
Bob Badour37af0462021-01-07 03:34:31 +000089 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) {
Paul Duffin30ac3e72021-03-20 00:36:14 +0000100 GroupFixturePreparers(
101 prepareForLicenseTest,
102 FixtureRegisterWithContext(func(ctx RegistrationContext) {
103 ctx.RegisterModuleType("mock_license", newMockLicenseModule)
104 }),
105 test.fs.AddToFixture(),
106 ).
107 ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
Paul Duffin8bd28652021-03-03 00:42:36 +0000108 RunTest(t)
Bob Badour37af0462021-01-07 03:34:31 +0000109 })
110 }
111}
112
Bob Badour37af0462021-01-07 03:34:31 +0000113type mockLicenseProperties struct {
114 License_kinds []string
115}
116
117type mockLicenseModule struct {
118 ModuleBase
119 DefaultableModuleBase
120
121 properties mockLicenseProperties
122}
123
124func newMockLicenseModule() Module {
125 m := &mockLicenseModule{}
126 m.AddProperties(&m.properties)
127 InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon)
128 InitDefaultableModule(m)
129 return m
130}
131
132type licensekindTag struct {
133 blueprint.BaseDependencyTag
134}
135
136func (j *mockLicenseModule) DepsMutator(ctx BottomUpMutatorContext) {
137 m, ok := ctx.Module().(Module)
138 if !ok {
139 return
140 }
141 ctx.AddDependency(m, licensekindTag{}, j.properties.License_kinds...)
142}
143
144func (p *mockLicenseModule) GenerateAndroidBuildActions(ModuleContext) {
145}