blob: 7bedf7120638404a02565fcb001eef8a0907cf54 [file] [log] [blame]
Jingwen Chen13b9b422021-03-08 07:32:28 -05001package bp2build
2
3import (
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02004 "testing"
5
Jingwen Chen13b9b422021-03-08 07:32:28 -05006 "android/soong/android"
7 "android/soong/python"
Jingwen Chen13b9b422021-03-08 07:32:28 -05008)
9
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020010func runPythonTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040011 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020012 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
13}
14
15func TestPythonBinaryHostSimple(t *testing.T) {
16 runPythonTestCase(t, bp2buildTestCase{
17 description: "simple python_binary_host converts to a native py_binary",
18 moduleTypeUnderTest: "python_binary_host",
19 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
20 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
21 filesystem: map[string]string{
22 "a.py": "",
23 "b/c.py": "",
24 "b/d.py": "",
25 "b/e.py": "",
26 "files/data.txt": "",
27 },
28 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050029 name: "foo",
30 main: "a.py",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000031 srcs: ["**/*.py"],
32 exclude_srcs: ["b/e.py"],
33 data: ["files/data.txt",],
Jingwen Chen13b9b422021-03-08 07:32:28 -050034 bazel_module: { bp2build_available: true },
35}
36`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020037 expectedBazelTargets: []string{`py_binary(
Jingwen Chen13b9b422021-03-08 07:32:28 -050038 name = "foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000039 data = ["files/data.txt"],
Jingwen Chen13b9b422021-03-08 07:32:28 -050040 main = "a.py",
41 srcs = [
42 "a.py",
43 "b/c.py",
44 "b/d.py",
45 ],
46)`,
Jingwen Chen13b9b422021-03-08 07:32:28 -050047 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020048 })
49}
50
51func TestPythonBinaryHostPy2(t *testing.T) {
52 runPythonTestCase(t, bp2buildTestCase{
53 description: "py2 python_binary_host",
54 moduleTypeUnderTest: "python_binary_host",
55 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
56 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
57 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050058 name: "foo",
59 srcs: ["a.py"],
60 version: {
61 py2: {
62 enabled: true,
63 },
64 py3: {
65 enabled: false,
66 },
67 },
68
69 bazel_module: { bp2build_available: true },
70}
71`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020072 expectedBazelTargets: []string{`py_binary(
Jingwen Chen13b9b422021-03-08 07:32:28 -050073 name = "foo",
74 python_version = "PY2",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000075 srcs = ["a.py"],
Jingwen Chen13b9b422021-03-08 07:32:28 -050076)`,
Jingwen Chen13b9b422021-03-08 07:32:28 -050077 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020078 })
79}
80
81func TestPythonBinaryHostPy3(t *testing.T) {
82 runPythonTestCase(t, bp2buildTestCase{
83 description: "py3 python_binary_host",
84 moduleTypeUnderTest: "python_binary_host",
85 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
86 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
87 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050088 name: "foo",
89 srcs: ["a.py"],
90 version: {
91 py2: {
92 enabled: false,
93 },
94 py3: {
95 enabled: true,
96 },
97 },
98
99 bazel_module: { bp2build_available: true },
100}
101`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200102 expectedBazelTargets: []string{
103 // python_version is PY3 by default.
104 `py_binary(
Jingwen Chen13b9b422021-03-08 07:32:28 -0500105 name = "foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000106 srcs = ["a.py"],
Jingwen Chen13b9b422021-03-08 07:32:28 -0500107)`,
Jingwen Chen13b9b422021-03-08 07:32:28 -0500108 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200109 })
Jingwen Chen13b9b422021-03-08 07:32:28 -0500110}