blob: ebd241d119a3b0da40ad6c8d5506c10145d4bca7 [file] [log] [blame]
Spandan Dasea2abba2023-06-14 21:30:38 +00001// Copyright 2023 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 "github.com/google/blueprint/bootstrap"
21
22 "android/soong/android"
23)
24
25func runGoTests(t *testing.T, tc Bp2buildTestCase) {
26 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
27 tCtx := ctx.(*android.TestContext)
28 bootstrap.RegisterGoModuleTypes(tCtx.Context.Context) // android.TestContext --> android.Context --> blueprint.Context
29 }, tc)
30}
31
32func TestConvertGoPackage(t *testing.T) {
33 bp := `
34bootstrap_go_package {
35 name: "foo",
36 pkgPath: "android/foo",
37 deps: [
38 "bar",
39 ],
40 srcs: [
41 "foo1.go",
42 "foo2.go",
43 ],
44 linux: {
45 srcs: [
46 "foo_linux.go",
47 ],
Spandan Das682e7862023-06-22 22:22:11 +000048 testSrcs: [
49 "foo_linux_test.go",
50 ],
Spandan Dasea2abba2023-06-14 21:30:38 +000051 },
52 darwin: {
53 srcs: [
54 "foo_darwin.go",
55 ],
Spandan Das682e7862023-06-22 22:22:11 +000056 testSrcs: [
57 "foo_darwin_test.go",
58 ],
Spandan Dasea2abba2023-06-14 21:30:38 +000059 },
60 testSrcs: [
61 "foo1_test.go",
62 "foo2_test.go",
63 ],
64}
65`
66 depBp := `
67bootstrap_go_package {
68 name: "bar",
69}
70`
71 t.Parallel()
72 runGoTests(t, Bp2buildTestCase{
73 Description: "Convert bootstrap_go_package to go_library",
74 ModuleTypeUnderTest: "bootrstap_go_package",
75 Blueprint: bp,
76 Filesystem: map[string]string{
77 "bar/Android.bp": depBp, // Put dep in Android.bp to reduce boilerplate in ExpectedBazelTargets
78 },
79 ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_library", "foo",
80 AttrNameToString{
81 "deps": `["//bar:bar"]`,
82 "importpath": `"android/foo"`,
83 "srcs": `[
Spandan Das0a8a2752023-06-21 01:50:33 +000084 "foo1.go",
85 "foo2.go",
Spandan Dasea2abba2023-06-14 21:30:38 +000086 ] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +000087 "//build/bazel_common_rules/platforms/os:darwin": ["foo_darwin.go"],
88 "//build/bazel_common_rules/platforms/os:linux_glibc": ["foo_linux.go"],
Spandan Dasea2abba2023-06-14 21:30:38 +000089 "//conditions:default": [],
90 })`,
91 },
92 android.HostSupported,
Spandan Das682e7862023-06-22 22:22:11 +000093 ),
94 makeBazelTargetHostOrDevice("go_test", "foo-test",
95 AttrNameToString{
96 "embed": `[":foo"]`,
97 "srcs": `[
98 "foo1_test.go",
99 "foo2_test.go",
100 ] + select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +0000101 "//build/bazel_common_rules/platforms/os:darwin": ["foo_darwin_test.go"],
102 "//build/bazel_common_rules/platforms/os:linux_glibc": ["foo_linux_test.go"],
Spandan Das682e7862023-06-22 22:22:11 +0000103 "//conditions:default": [],
104 })`,
105 },
106 android.HostSupported,
107 )},
Spandan Dasea2abba2023-06-14 21:30:38 +0000108 })
109}
Spandan Dasde623292023-06-14 21:30:38 +0000110
111func TestConvertGoBinaryWithTransitiveDeps(t *testing.T) {
112 bp := `
113blueprint_go_binary {
114 name: "foo",
115 srcs: ["main.go"],
116 deps: ["bar"],
117}
118`
119 depBp := `
120bootstrap_go_package {
121 name: "bar",
122 deps: ["baz"],
123}
124bootstrap_go_package {
125 name: "baz",
126}
127`
128 t.Parallel()
129 runGoTests(t, Bp2buildTestCase{
130 Description: "Convert blueprint_go_binary to go_binary",
131 Blueprint: bp,
132 Filesystem: map[string]string{
133 "bar/Android.bp": depBp, // Put dep in Android.bp to reduce boilerplate in ExpectedBazelTargets
134 },
135 ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_binary", "foo",
136 AttrNameToString{
137 "deps": `[
138 "//bar:bar",
139 "//bar:baz",
140 ]`,
Spandan Das0a8a2752023-06-21 01:50:33 +0000141 "srcs": `["main.go"]`,
142 },
143 android.HostSupported,
144 )},
145 })
146}
147
Spandan Das682e7862023-06-22 22:22:11 +0000148func TestConvertGoBinaryWithTestSrcs(t *testing.T) {
149 bp := `
150blueprint_go_binary {
151 name: "foo",
152 srcs: ["main.go"],
153 testSrcs: ["main_test.go"],
154}
155`
156 t.Parallel()
157 runGoTests(t, Bp2buildTestCase{
158 Description: "Convert blueprint_go_binary with testSrcs",
159 Blueprint: bp,
160 ExpectedBazelTargets: []string{
161 makeBazelTargetHostOrDevice("go_binary", "foo",
162 AttrNameToString{
163 "deps": `[]`,
164 "embed": `[":foo-source"]`,
165 },
166 android.HostSupported,
167 ),
168 makeBazelTargetHostOrDevice("go_source", "foo-source",
169 AttrNameToString{
170 "deps": `[]`,
171 "srcs": `["main.go"]`,
172 },
173 android.HostSupported,
174 ),
175 makeBazelTargetHostOrDevice("go_test", "foo-test",
176 AttrNameToString{
177 "embed": `[":foo-source"]`,
178 "srcs": `["main_test.go"]`,
179 },
180 android.HostSupported,
181 ),
182 },
183 })
184}
185
Spandan Das0a8a2752023-06-21 01:50:33 +0000186func TestConvertGoBinaryWithSrcInDifferentPackage(t *testing.T) {
187 bp := `
188blueprint_go_binary {
189 name: "foo",
190 srcs: ["subdir/main.go"],
191}
192`
193 t.Parallel()
194 runGoTests(t, Bp2buildTestCase{
195 Description: "Convert blueprint_go_binary with src in different package",
196 Blueprint: bp,
197 Filesystem: map[string]string{
198 "subdir/Android.bp": "",
199 },
200 ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_binary", "foo",
201 AttrNameToString{
202 "deps": `[]`,
203 "srcs": `["//subdir:main.go"]`,
Spandan Dasde623292023-06-14 21:30:38 +0000204 },
205 android.HostSupported,
206 )},
207 })
208}