• 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	"fmt"
19	"testing"
20
21	"android/soong/android"
22	"android/soong/cc"
23	"android/soong/genrule"
24)
25
26type ccTestBp2buildTestCase struct {
27	description string
28	blueprint   string
29	filesystem  map[string]string
30	targets     []testBazelTarget
31}
32
33func registerCcTestModuleTypes(ctx android.RegistrationContext) {
34	cc.RegisterCCBuildComponents(ctx)
35	ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
36	ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
37	ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
38	ctx.RegisterModuleType("cc_test_library", cc.TestLibraryFactory)
39	ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
40}
41
42func runCcTestTestCase(t *testing.T, testCase ccTestBp2buildTestCase) {
43	t.Helper()
44	moduleTypeUnderTest := "cc_test"
45	description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
46	t.Run(description, func(t *testing.T) {
47		t.Helper()
48		RunBp2BuildTestCase(t, registerCcTestModuleTypes, Bp2buildTestCase{
49			ExpectedBazelTargets:       generateBazelTargetsForTest(testCase.targets, android.HostAndDeviceSupported),
50			Filesystem:                 testCase.filesystem,
51			ModuleTypeUnderTest:        moduleTypeUnderTest,
52			ModuleTypeUnderTestFactory: cc.TestFactory,
53			Description:                description,
54			Blueprint:                  testCase.blueprint,
55		})
56	})
57}
58
59func TestBasicCcTest(t *testing.T) {
60	runCcTestTestCase(t, ccTestBp2buildTestCase{
61		description: "basic cc_test with commonly used attributes",
62		blueprint: `
63cc_test {
64    name: "mytest",
65    host_supported: true,
66    srcs: ["test.cpp"],
67    target: {
68        android: {
69            srcs: ["android.cpp"],
70            shared_libs: ["foolib"],
71        },
72        linux: {
73            srcs: ["linux.cpp"],
74        },
75        host: {
76            static_libs: ["hostlib"],
77        },
78    },
79    data: [":data_mod", "file.txt"],
80    data_bins: [":cc_bin"],
81    data_libs: [":cc_lib"],
82    cflags: ["-Wall"],
83}
84` + simpleModuleDoNotConvertBp2build("cc_library", "foolib") +
85			simpleModuleDoNotConvertBp2build("cc_library_static", "hostlib") +
86			simpleModuleDoNotConvertBp2build("genrule", "data_mod") +
87			simpleModuleDoNotConvertBp2build("cc_binary", "cc_bin") +
88			simpleModuleDoNotConvertBp2build("cc_test_library", "cc_lib"),
89		targets: []testBazelTarget{
90			{"cc_test", "mytest", AttrNameToString{
91				"copts": `["-Wall"]`,
92				"data": `[
93        ":data_mod",
94        "file.txt",
95        ":cc_bin",
96        ":cc_lib",
97    ]`,
98				"deps": `select({
99        "//build/bazel/platforms/os:darwin": [":hostlib"],
100        "//build/bazel/platforms/os:linux_bionic": [":hostlib"],
101        "//build/bazel/platforms/os:linux_glibc": [":hostlib"],
102        "//build/bazel/platforms/os:linux_musl": [":hostlib"],
103        "//build/bazel/platforms/os:windows": [":hostlib"],
104        "//conditions:default": [],
105    })`,
106				"gtest":          "True",
107				"isolated":       "True",
108				"local_includes": `["."]`,
109				"dynamic_deps": `select({
110        "//build/bazel/platforms/os:android": [":foolib"],
111        "//conditions:default": [],
112    })`,
113				"srcs": `["test.cpp"] + select({
114        "//build/bazel/platforms/os:android": [
115            "linux.cpp",
116            "android.cpp",
117        ],
118        "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"],
119        "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"],
120        "//build/bazel/platforms/os:linux_musl": ["linux.cpp"],
121        "//conditions:default": [],
122    })`,
123			},
124			},
125		},
126	})
127}
128
129func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
130	runCcTestTestCase(t, ccTestBp2buildTestCase{
131		description: "cc test with disabled gtest and isolated props",
132		blueprint: `
133cc_test {
134    name: "mytest",
135    host_supported: true,
136    srcs: ["test.cpp"],
137    gtest: false,
138    isolated: false,
139}
140`,
141		targets: []testBazelTarget{
142			{"cc_test", "mytest", AttrNameToString{
143				"gtest":          "False",
144				"isolated":       "False",
145				"local_includes": `["."]`,
146				"srcs":           `["test.cpp"]`,
147			},
148			},
149		},
150	})
151}
152
153func TestCcTest_TestOptions_Tags(t *testing.T) {
154	runCcTestTestCase(t, ccTestBp2buildTestCase{
155		description: "cc test with test_options.tags converted to tags",
156		blueprint: `
157cc_test {
158    name: "mytest",
159    host_supported: true,
160    srcs: ["test.cpp"],
161    test_options: { tags: ["no-remote"] },
162}
163`,
164		targets: []testBazelTarget{
165			{"cc_test", "mytest", AttrNameToString{
166				"tags":           `["no-remote"]`,
167				"local_includes": `["."]`,
168				"srcs":           `["test.cpp"]`,
169				"gtest":          "True",
170				"isolated":       "True",
171			},
172			},
173		},
174	})
175}
176
177func TestCcTest_TestConfig(t *testing.T) {
178	runCcTestTestCase(t, ccTestBp2buildTestCase{
179		description: "cc test that sets a test_config",
180		filesystem: map[string]string{
181			"test_config.xml": "",
182		},
183		blueprint: `
184cc_test {
185	name: "mytest",
186	srcs: ["test.cpp"],
187	test_config: "test_config.xml",
188}
189`,
190		targets: []testBazelTarget{
191			{"cc_test", "mytest", AttrNameToString{
192				"gtest":                  "True",
193				"isolated":               "True",
194				"local_includes":         `["."]`,
195				"srcs":                   `["test.cpp"]`,
196				"target_compatible_with": `["//build/bazel/platforms/os:android"]`,
197				"test_config":            `"test_config.xml"`,
198			},
199			},
200		},
201	})
202}
203
204func TestCcTest_TestConfigAndroidTestXML(t *testing.T) {
205	runCcTestTestCase(t, ccTestBp2buildTestCase{
206		description: "cc test that defaults to test config AndroidTest.xml",
207		filesystem: map[string]string{
208			"AndroidTest.xml": "",
209		},
210		blueprint: `
211cc_test {
212	name: "mytest",
213	srcs: ["test.cpp"],
214}
215`,
216		targets: []testBazelTarget{
217			{"cc_test", "mytest", AttrNameToString{
218				"gtest":                  "True",
219				"isolated":               "True",
220				"local_includes":         `["."]`,
221				"srcs":                   `["test.cpp"]`,
222				"target_compatible_with": `["//build/bazel/platforms/os:android"]`,
223				"test_config":            `"AndroidTest.xml"`,
224			},
225			},
226		},
227	})
228}
229
230func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
231	runCcTestTestCase(t, ccTestBp2buildTestCase{
232		description: "cc test that sets test config template attributes",
233		filesystem: map[string]string{
234			"test_config_template.xml": "",
235		},
236		blueprint: `
237cc_test {
238	name: "mytest",
239	srcs: ["test.cpp"],
240	test_config_template: "test_config_template.xml",
241	auto_gen_config: true,
242}
243`,
244		targets: []testBazelTarget{
245			{"cc_test", "mytest", AttrNameToString{
246				"auto_generate_test_config": "True",
247				"gtest":                     "True",
248				"isolated":                  "True",
249				"local_includes":            `["."]`,
250				"srcs":                      `["test.cpp"]`,
251				"target_compatible_with":    `["//build/bazel/platforms/os:android"]`,
252				"template_configs": `[
253        "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n        <option name=\"force-root\" value=\"false\" />\\n    </target_preparer>'",
254        "'<option name=\"not-shardable\" value=\"true\" />'",
255    ]`,
256				"template_install_base": `"/data/local/tmp"`,
257				"template_test_config":  `"test_config_template.xml"`,
258			},
259			},
260		},
261	})
262}
263