blob: 7600e3651556eb9fd9fd413e71886f8ecbcab496 [file] [log] [blame]
Jingwen Chen13b9b422021-03-08 07:32:28 -05001package bp2build
2
3import (
4 "android/soong/android"
5 "android/soong/python"
6 "fmt"
7 "strings"
8 "testing"
9)
10
11func TestPythonBinaryHost(t *testing.T) {
12 testCases := []struct {
13 description string
14 moduleTypeUnderTest string
15 moduleTypeUnderTestFactory android.ModuleFactory
16 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
17 blueprint string
18 expectedBazelTargets []string
19 filesystem map[string]string
20 }{
21 {
22 description: "simple python_binary_host converts to a native py_binary",
23 moduleTypeUnderTest: "python_binary_host",
24 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
25 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
26 filesystem: map[string]string{
27 "a.py": "",
28 "b/c.py": "",
29 "b/d.py": "",
30 "b/e.py": "",
31 "files/data.txt": "",
32 },
33 blueprint: `python_binary_host {
34 name: "foo",
35 main: "a.py",
36 srcs: [
37 "**/*.py"
38 ],
39 exclude_srcs: [
40 "b/e.py"
41 ],
42 data: [
43 "files/data.txt",
44 ],
45
46 bazel_module: { bp2build_available: true },
47}
48`,
49 expectedBazelTargets: []string{`py_binary(
50 name = "foo",
51 data = [
52 "files/data.txt",
53 ],
54 main = "a.py",
55 srcs = [
56 "a.py",
57 "b/c.py",
58 "b/d.py",
59 ],
60)`,
61 },
62 },
63 {
64 description: "py2 python_binary_host",
65 moduleTypeUnderTest: "python_binary_host",
66 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
67 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
68 blueprint: `python_binary_host {
69 name: "foo",
70 srcs: ["a.py"],
71 version: {
72 py2: {
73 enabled: true,
74 },
75 py3: {
76 enabled: false,
77 },
78 },
79
80 bazel_module: { bp2build_available: true },
81}
82`,
83 expectedBazelTargets: []string{`py_binary(
84 name = "foo",
85 python_version = "PY2",
86 srcs = [
87 "a.py",
88 ],
89)`,
90 },
91 },
92 {
93 description: "py3 python_binary_host",
94 moduleTypeUnderTest: "python_binary_host",
95 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
96 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
97 blueprint: `python_binary_host {
98 name: "foo",
99 srcs: ["a.py"],
100 version: {
101 py2: {
102 enabled: false,
103 },
104 py3: {
105 enabled: true,
106 },
107 },
108
109 bazel_module: { bp2build_available: true },
110}
111`,
112 expectedBazelTargets: []string{
113 // python_version is PY3 by default.
114 `py_binary(
115 name = "foo",
116 srcs = [
117 "a.py",
118 ],
119)`,
120 },
121 },
122 }
123
124 dir := "."
125 for _, testCase := range testCases {
126 filesystem := make(map[string][]byte)
127 toParse := []string{
128 "Android.bp",
129 }
130 for f, content := range testCase.filesystem {
131 if strings.HasSuffix(f, "Android.bp") {
132 toParse = append(toParse, f)
133 }
134 filesystem[f] = []byte(content)
135 }
136 config := android.TestConfig(buildDir, nil, testCase.blueprint, filesystem)
137 ctx := android.NewTestContext(config)
138
139 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
140 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
141 ctx.RegisterForBazelConversion()
142
143 _, errs := ctx.ParseFileList(dir, toParse)
144 if Errored(t, testCase.description, errs) {
145 continue
146 }
147 _, errs = ctx.ResolveDependencies(config)
148 if Errored(t, testCase.description, errs) {
149 continue
150 }
151
152 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
153 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
154 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
155 fmt.Println(bazelTargets)
156 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
157 } else {
158 for i, target := range bazelTargets {
159 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
160 t.Errorf(
161 "%s: Expected generated Bazel target to be '%s', got '%s'",
162 testCase.description,
163 w,
164 g,
165 )
166 }
167 }
168 }
169 }
170}