aboutsummaryrefslogtreecommitdiff
path: root/bp2build/python_library_conversion_test.go
blob: a53371d2b0e4a0850c2005ad658a326f6b361dda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package bp2build

import (
	"fmt"
	"strings"
	"testing"

	"android/soong/android"
	"android/soong/python"
)

// TODO(alexmarquez): Should be lifted into a generic Bp2Build file
type PythonLibBp2Build func(ctx android.TopDownMutatorContext)

type pythonLibBp2BuildTestCase struct {
	description          string
	filesystem           map[string]string
	blueprint            string
	expectedBazelTargets []testBazelTarget
	dir                  string
	expectedError        error
}

func convertPythonLibTestCaseToBp2build_Host(tc pythonLibBp2BuildTestCase) Bp2buildTestCase {
	for i := range tc.expectedBazelTargets {
		tc.expectedBazelTargets[i].attrs["target_compatible_with"] = `select({
        "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
        "//conditions:default": [],
    })`
	}

	return convertPythonLibTestCaseToBp2build(tc)
}

func convertPythonLibTestCaseToBp2build(tc pythonLibBp2BuildTestCase) Bp2buildTestCase {
	var bp2BuildTargets []string
	for _, t := range tc.expectedBazelTargets {
		bp2BuildTargets = append(bp2BuildTargets, MakeBazelTarget(t.typ, t.name, t.attrs))
	}
	// Copy the filesystem so that we can change stuff in it later without it
	// affecting the original pythonLibBp2BuildTestCase
	filesystemCopy := make(map[string]string)
	for k, v := range tc.filesystem {
		filesystemCopy[k] = v
	}
	return Bp2buildTestCase{
		Description:          tc.description,
		Filesystem:           filesystemCopy,
		Blueprint:            tc.blueprint,
		ExpectedBazelTargets: bp2BuildTargets,
		Dir:                  tc.dir,
		ExpectedErr:          tc.expectedError,
	}
}

func runPythonLibraryTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) {
	t.Helper()
	testCase := convertPythonLibTestCaseToBp2build(tc)
	testCase.Description = fmt.Sprintf(testCase.Description, "python_library")
	testCase.Blueprint = fmt.Sprintf(testCase.Blueprint, "python_library")
	for name, contents := range testCase.Filesystem {
		if strings.HasSuffix(name, "Android.bp") {
			testCase.Filesystem[name] = fmt.Sprintf(contents, "python_library")
		}
	}
	testCase.ModuleTypeUnderTest = "python_library"
	testCase.ModuleTypeUnderTestFactory = python.PythonLibraryFactory

	RunBp2BuildTestCaseSimple(t, testCase)
}

func runPythonLibraryHostTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) {
	t.Helper()
	testCase := convertPythonLibTestCaseToBp2build_Host(tc)
	testCase.Description = fmt.Sprintf(testCase.Description, "python_library_host")
	testCase.Blueprint = fmt.Sprintf(testCase.Blueprint, "python_library_host")
	for name, contents := range testCase.Filesystem {
		if strings.HasSuffix(name, "Android.bp") {
			testCase.Filesystem[name] = fmt.Sprintf(contents, "python_library_host")
		}
	}
	testCase.ModuleTypeUnderTest = "python_library_host"
	testCase.ModuleTypeUnderTestFactory = python.PythonLibraryHostFactory
	RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
		ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
	},
		testCase)
}

func runPythonLibraryTestCases(t *testing.T, tc pythonLibBp2BuildTestCase) {
	t.Helper()
	runPythonLibraryTestCase(t, tc)
	runPythonLibraryHostTestCase(t, tc)
}

func TestSimplePythonLib(t *testing.T) {
	testCases := []pythonLibBp2BuildTestCase{
		{
			description: "simple %s converts to a native py_library",
			filesystem: map[string]string{
				"a.py":           "",
				"b/c.py":         "",
				"b/d.py":         "",
				"b/e.py":         "",
				"files/data.txt": "",
			},
			blueprint: `%s {
    name: "foo",
    srcs: ["**/*.py"],
    exclude_srcs: ["b/e.py"],
    data: ["files/data.txt",],
    libs: ["bar"],
    bazel_module: { bp2build_available: true },
}
    python_library {
      name: "bar",
      srcs: ["b/e.py"],
      bazel_module: { bp2build_available: false },
    }`,
			expectedBazelTargets: []testBazelTarget{
				{
					typ:  "py_library",
					name: "foo",
					attrs: AttrNameToString{
						"data": `["files/data.txt"]`,
						"deps": `[":bar"]`,
						"srcs": `[
        "a.py",
        "b/c.py",
        "b/d.py",
    ]`,
						"srcs_version": `"PY3"`,
						"imports":      `["."]`,
					},
				},
			},
		},
		{
			description: "py2 %s converts to a native py_library",
			blueprint: `%s {
    name: "foo",
    srcs: ["a.py"],
    version: {
        py2: {
            enabled: true,
        },
        py3: {
            enabled: false,
        },
    },

    bazel_module: { bp2build_available: true },
}`,
			expectedBazelTargets: []testBazelTarget{
				{
					typ:  "py_library",
					name: "foo",
					attrs: AttrNameToString{
						"srcs":         `["a.py"]`,
						"srcs_version": `"PY2"`,
						"imports":      `["."]`,
					},
				},
			},
		},
		{
			description: "py3 %s converts to a native py_library",
			blueprint: `%s {
    name: "foo",
    srcs: ["a.py"],
    version: {
        py2: {
            enabled: false,
        },
        py3: {
            enabled: true,
        },
    },

    bazel_module: { bp2build_available: true },
}`,
			expectedBazelTargets: []testBazelTarget{
				{
					typ:  "py_library",
					name: "foo",
					attrs: AttrNameToString{
						"srcs":         `["a.py"]`,
						"srcs_version": `"PY3"`,
						"imports":      `["."]`,
					},
				},
			},
		},
		{
			description: "py2&3 %s converts to a native py_library",
			blueprint: `%s {
    name: "foo",
    srcs: ["a.py"],
    version: {
        py2: {
            enabled: true,
        },
        py3: {
            enabled: true,
        },
    },

    bazel_module: { bp2build_available: true },
}`,
			expectedBazelTargets: []testBazelTarget{
				{
					// srcs_version is PY2ANDPY3 by default.
					typ:  "py_library",
					name: "foo",
					attrs: AttrNameToString{
						"srcs":    `["a.py"]`,
						"imports": `["."]`,
					},
				},
			},
		},
		{
			description: "%s: pkg_path in a subdirectory of the same name converts correctly",
			dir:         "mylib/subpackage",
			filesystem: map[string]string{
				"mylib/subpackage/a.py": "",
				"mylib/subpackage/Android.bp": `%s {
				name: "foo",
				srcs: ["a.py"],
				pkg_path: "mylib/subpackage",

				bazel_module: { bp2build_available: true },
			}`,
			},
			blueprint: `%s {name: "bar"}`,
			expectedBazelTargets: []testBazelTarget{
				{
					// srcs_version is PY2ANDPY3 by default.
					typ:  "py_library",
					name: "foo",
					attrs: AttrNameToString{
						"srcs":         `["a.py"]`,
						"imports":      `["../.."]`,
						"srcs_version": `"PY3"`,
					},
				},
			},
		},
		{
			description: "%s: pkg_path in a subdirectory of a different name fails",
			dir:         "mylib/subpackage",
			filesystem: map[string]string{
				"mylib/subpackage/a.py": "",
				"mylib/subpackage/Android.bp": `%s {
				name: "foo",
				srcs: ["a.py"],
				pkg_path: "mylib/subpackage2",
				bazel_module: { bp2build_available: true },
			}`,
			},
			blueprint:     `%s {name: "bar"}`,
			expectedError: fmt.Errorf("Currently, bp2build only supports pkg_paths that are the same as the folders the Android.bp file is in."),
		},
	}

	for _, tc := range testCases {
		t.Run(tc.description, func(t *testing.T) {
			runPythonLibraryTestCases(t, tc)
		})
	}
}

func TestPythonArchVariance(t *testing.T) {
	runPythonLibraryTestCases(t, pythonLibBp2BuildTestCase{
		description: "test %s arch variants",
		filesystem: map[string]string{
			"dir/arm.py": "",
			"dir/x86.py": "",
		},
		blueprint: `%s {
					 name: "foo",
					 arch: {
						 arm: {
							 srcs: ["arm.py"],
						 },
						 x86: {
							 srcs: ["x86.py"],
						 },
					},
				 }`,
		expectedBazelTargets: []testBazelTarget{
			{
				typ:  "py_library",
				name: "foo",
				attrs: AttrNameToString{
					"srcs": `select({
        "//build/bazel/platforms/arch:arm": ["arm.py"],
        "//build/bazel/platforms/arch:x86": ["x86.py"],
        "//conditions:default": [],
    })`,
					"srcs_version": `"PY3"`,
					"imports":      `["."]`,
				},
			},
		},
	})
}

func TestPythonLibraryWithProtobufs(t *testing.T) {
	runPythonLibraryTestCases(t, pythonLibBp2BuildTestCase{
		description: "test %s protobuf",
		filesystem: map[string]string{
			"dir/mylib.py":      "",
			"dir/myproto.proto": "",
		},
		blueprint: `%s {
					 name: "foo",
					 srcs: [
						"dir/mylib.py",
						"dir/myproto.proto",
					 ],
				 }`,
		expectedBazelTargets: []testBazelTarget{
			{
				typ:  "proto_library",
				name: "foo_proto",
				attrs: AttrNameToString{
					"srcs": `["dir/myproto.proto"]`,
				},
			},
			{
				typ:  "py_proto_library",
				name: "foo_py_proto",
				attrs: AttrNameToString{
					"deps": `[":foo_proto"]`,
				},
			},
			{
				typ:  "py_library",
				name: "foo",
				attrs: AttrNameToString{
					"srcs":         `["dir/mylib.py"]`,
					"srcs_version": `"PY3"`,
					"imports":      `["."]`,
					"deps":         `[":foo_py_proto"]`,
				},
			},
		},
	})
}