blob: ce848e42d947ebcdbb4bf32234a1f5d2bc24cca8 [file] [log] [blame]
Sasha Smundak8bea2672022-08-04 13:31:14 -07001// 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 (
Wei Li2c9e8d62023-05-05 01:07:15 -070018 "testing"
19
Sasha Smundak8bea2672022-08-04 13:31:14 -070020 "android/soong/android"
21 "android/soong/genrule"
Sasha Smundak8bea2672022-08-04 13:31:14 -070022)
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
Wei Li2c9e8d62023-05-05 01:07:15 -070033 fs map[string]string
Sasha Smundak8bea2672022-08-04 13:31:14 -070034 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{
Wei Li2c9e8d62023-05-05 01:07:15 -070055 "default_package_metadata": `[":my_license"]`,
56 "default_visibility": `["//visibility:public"]`,
Sasha Smundak8bea2672022-08-04 13:31:14 -070057 },
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 },
Wei Li2c9e8d62023-05-05 01:07:15 -070072 {
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 },
Sasha Smundak8bea2672022-08-04 13:31:14 -0700123 }
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,
Wei Li2c9e8d62023-05-05 01:07:15 -0700136 Filesystem: test.fs,
Sasha Smundak8bea2672022-08-04 13:31:14 -0700137 })
138 }
139}