blob: 9b7748f104630220c98db1e934efcf5e7f292e27 [file] [log] [blame]
Jingwen Chen537242c2022-08-24 11:53:27 +00001// 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 targets []testBazelTarget
30}
31
32func registerCcTestModuleTypes(ctx android.RegistrationContext) {
33 cc.RegisterCCBuildComponents(ctx)
34 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
35 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
36 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
37 ctx.RegisterModuleType("cc_test_library", cc.TestLibraryFactory)
38 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
39}
40
41func runCcTestTestCase(t *testing.T, testCase ccTestBp2buildTestCase) {
42 t.Helper()
43 moduleTypeUnderTest := "cc_test"
44
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 ModuleTypeUnderTest: moduleTypeUnderTest,
51 ModuleTypeUnderTestFactory: cc.TestFactory,
52 Description: description,
53 Blueprint: testCase.blueprint,
54 })
55 })
56}
57
58func TestBasicCcTest(t *testing.T) {
59 runCcTestTestCase(t, ccTestBp2buildTestCase{
60 description: "basic cc_test with commonly used attributes",
61 blueprint: `
62cc_test {
63 name: "mytest",
64 host_supported: true,
65 srcs: ["test.cpp"],
66 target: {
67 android: {
68 srcs: ["android.cpp"],
69 shared_libs: ["foolib"],
70 },
71 linux: {
72 srcs: ["linux.cpp"],
73 },
74 host: {
75 static_libs: ["hostlib"],
76 },
77 },
78 data: [":data_mod", "file.txt"],
79 data_bins: [":cc_bin"],
80 data_libs: [":cc_lib"],
81 cflags: ["-Wall"],
82}
83` + simpleModuleDoNotConvertBp2build("cc_library", "foolib") +
84 simpleModuleDoNotConvertBp2build("cc_library_static", "hostlib") +
85 simpleModuleDoNotConvertBp2build("genrule", "data_mod") +
86 simpleModuleDoNotConvertBp2build("cc_binary", "cc_bin") +
87 simpleModuleDoNotConvertBp2build("cc_test_library", "cc_lib"),
88 targets: []testBazelTarget{
89 {"cc_test", "mytest", AttrNameToString{
90 "copts": `["-Wall"]`,
91 "data": `[
92 ":data_mod",
93 "file.txt",
94 ":cc_bin",
95 ":cc_lib",
96 ]`,
97 "deps": `select({
98 "//build/bazel/platforms/os:darwin": [":hostlib"],
99 "//build/bazel/platforms/os:linux": [":hostlib"],
100 "//build/bazel/platforms/os:linux_bionic": [":hostlib"],
101 "//build/bazel/platforms/os:linux_musl": [":hostlib"],
102 "//build/bazel/platforms/os:windows": [":hostlib"],
103 "//conditions:default": [],
104 })`,
105 "gtest": "True",
106 "isolated": "True",
107 "local_includes": `["."]`,
108 "dynamic_deps": `select({
109 "//build/bazel/platforms/os:android": [":foolib"],
110 "//conditions:default": [],
111 })`,
112 "srcs": `["test.cpp"] + select({
113 "//build/bazel/platforms/os:android": [
114 "linux.cpp",
115 "android.cpp",
116 ],
117 "//build/bazel/platforms/os:linux": ["linux.cpp"],
118 "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"],
119 "//build/bazel/platforms/os:linux_musl": ["linux.cpp"],
120 "//conditions:default": [],
121 })`,
122 },
123 },
124 },
125 })
126}
127
128func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
129 runCcTestTestCase(t, ccTestBp2buildTestCase{
130 description: "cc test with disabled gtest and isolated props",
131 blueprint: `
132cc_test {
133 name: "mytest",
134 host_supported: true,
135 srcs: ["test.cpp"],
136 gtest: false,
137 isolated: false,
138}
139`,
140 targets: []testBazelTarget{
141 {"cc_test", "mytest", AttrNameToString{
142 "gtest": "False",
143 "isolated": "False",
144 "local_includes": `["."]`,
145 "srcs": `["test.cpp"]`,
146 },
147 },
148 },
149 })
150}