blob: 95bce3c181d139e3df879a437ce252fe433e1bd7 [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) {
11 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
12}
13
14func TestPythonBinaryHostSimple(t *testing.T) {
15 runPythonTestCase(t, bp2buildTestCase{
16 description: "simple python_binary_host converts to a native py_binary",
17 moduleTypeUnderTest: "python_binary_host",
18 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
19 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
20 filesystem: map[string]string{
21 "a.py": "",
22 "b/c.py": "",
23 "b/d.py": "",
24 "b/e.py": "",
25 "files/data.txt": "",
26 },
27 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050028 name: "foo",
29 main: "a.py",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000030 srcs: ["**/*.py"],
31 exclude_srcs: ["b/e.py"],
32 data: ["files/data.txt",],
Jingwen Chen13b9b422021-03-08 07:32:28 -050033 bazel_module: { bp2build_available: true },
34}
35`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020036 expectedBazelTargets: []string{`py_binary(
Jingwen Chen13b9b422021-03-08 07:32:28 -050037 name = "foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000038 data = ["files/data.txt"],
Jingwen Chen13b9b422021-03-08 07:32:28 -050039 main = "a.py",
40 srcs = [
41 "a.py",
42 "b/c.py",
43 "b/d.py",
44 ],
45)`,
Jingwen Chen13b9b422021-03-08 07:32:28 -050046 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020047 })
48}
49
50func TestPythonBinaryHostPy2(t *testing.T) {
51 runPythonTestCase(t, bp2buildTestCase{
52 description: "py2 python_binary_host",
53 moduleTypeUnderTest: "python_binary_host",
54 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
55 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
56 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050057 name: "foo",
58 srcs: ["a.py"],
59 version: {
60 py2: {
61 enabled: true,
62 },
63 py3: {
64 enabled: false,
65 },
66 },
67
68 bazel_module: { bp2build_available: true },
69}
70`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020071 expectedBazelTargets: []string{`py_binary(
Jingwen Chen13b9b422021-03-08 07:32:28 -050072 name = "foo",
73 python_version = "PY2",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000074 srcs = ["a.py"],
Jingwen Chen13b9b422021-03-08 07:32:28 -050075)`,
Jingwen Chen13b9b422021-03-08 07:32:28 -050076 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020077 })
78}
79
80func TestPythonBinaryHostPy3(t *testing.T) {
81 runPythonTestCase(t, bp2buildTestCase{
82 description: "py3 python_binary_host",
83 moduleTypeUnderTest: "python_binary_host",
84 moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
85 moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
86 blueprint: `python_binary_host {
Jingwen Chen13b9b422021-03-08 07:32:28 -050087 name: "foo",
88 srcs: ["a.py"],
89 version: {
90 py2: {
91 enabled: false,
92 },
93 py3: {
94 enabled: true,
95 },
96 },
97
98 bazel_module: { bp2build_available: true },
99}
100`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200101 expectedBazelTargets: []string{
102 // python_version is PY3 by default.
103 `py_binary(
Jingwen Chen13b9b422021-03-08 07:32:28 -0500104 name = "foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000105 srcs = ["a.py"],
Jingwen Chen13b9b422021-03-08 07:32:28 -0500106)`,
Jingwen Chen13b9b422021-03-08 07:32:28 -0500107 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200108 })
Jingwen Chen13b9b422021-03-08 07:32:28 -0500109}