• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2022 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bp2build
16
17import (
18	"testing"
19
20	"android/soong/android"
21	"android/soong/genrule"
22)
23
24func registerDependentModules(ctx android.RegistrationContext) {
25	ctx.RegisterModuleType("license", android.LicenseFactory)
26	ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
27}
28
29func TestPackage(t *testing.T) {
30	tests := []struct {
31		description string
32		modules     string
33		fs          map[string]string
34		expected    []ExpectedRuleTarget
35	}{
36		{
37			description: "with default applicable licenses",
38			modules: `
39license {
40  name: "my_license",
41  visibility: [":__subpackages__"],
42  license_kinds: ["SPDX-license-identifier-Apache-2.0"],
43  license_text: ["NOTICE"],
44}
45
46package {
47  default_applicable_licenses: ["my_license"],
48}
49`,
50			expected: []ExpectedRuleTarget{
51				{
52					"package",
53					"",
54					AttrNameToString{
55						"default_package_metadata": `[":my_license"]`,
56						"default_visibility":       `["//visibility:public"]`,
57					},
58					android.HostAndDeviceDefault,
59				},
60				{
61					"android_license",
62					"my_license",
63					AttrNameToString{
64						"license_kinds": `["SPDX-license-identifier-Apache-2.0"]`,
65						"license_text":  `"NOTICE"`,
66						"visibility":    `[":__subpackages__"]`,
67					},
68					android.HostAndDeviceDefault,
69				},
70			},
71		},
72		{
73			description: "package has METADATA file",
74			fs: map[string]string{
75				"METADATA": ``,
76			},
77			modules: `
78license {
79  name: "my_license",
80  visibility: [":__subpackages__"],
81  license_kinds: ["SPDX-license-identifier-Apache-2.0"],
82  license_text: ["NOTICE"],
83}
84
85package {
86  default_applicable_licenses: ["my_license"],
87}
88`,
89			expected: []ExpectedRuleTarget{
90				{
91					"package",
92					"",
93					AttrNameToString{
94						"default_package_metadata": `[
95        ":my_license",
96        ":default_metadata_file",
97    ]`,
98						"default_visibility": `["//visibility:public"]`,
99					},
100					android.HostAndDeviceDefault,
101				},
102				{
103					"android_license",
104					"my_license",
105					AttrNameToString{
106						"license_kinds": `["SPDX-license-identifier-Apache-2.0"]`,
107						"license_text":  `"NOTICE"`,
108						"visibility":    `[":__subpackages__"]`,
109					},
110					android.HostAndDeviceDefault,
111				},
112				{
113					"filegroup",
114					"default_metadata_file",
115					AttrNameToString{
116						"applicable_licenses": `[]`,
117						"srcs":                `["METADATA"]`,
118					},
119					android.HostAndDeviceDefault,
120				},
121			},
122		},
123	}
124	for _, test := range tests {
125		expected := make([]string, 0, len(test.expected))
126		for _, e := range test.expected {
127			expected = append(expected, e.String())
128		}
129		RunBp2BuildTestCase(t, registerDependentModules,
130			Bp2buildTestCase{
131				Description:                test.description,
132				ModuleTypeUnderTest:        "package",
133				ModuleTypeUnderTestFactory: android.PackageFactory,
134				Blueprint:                  test.modules,
135				ExpectedBazelTargets:       expected,
136				Filesystem:                 test.fs,
137			})
138	}
139}
140