Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 4 | "testing" |
| 5 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 6 | "android/soong/android" |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 7 | "android/soong/python" |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 8 | ) |
| 9 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 10 | func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc bp2buildTestCase) { |
| 11 | t.Helper() |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 12 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) { |
| 13 | ctx.RegisterModuleType("python_library", python.PythonLibraryFactory) |
| 14 | ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory) |
| 15 | }, tc) |
| 16 | } |
| 17 | |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 18 | func TestPythonBinaryHostSimple(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 19 | runBp2BuildTestCaseWithPythonLibraries(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 20 | description: "simple python_binary_host converts to a native py_binary", |
| 21 | moduleTypeUnderTest: "python_binary_host", |
| 22 | moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 23 | filesystem: map[string]string{ |
| 24 | "a.py": "", |
| 25 | "b/c.py": "", |
| 26 | "b/d.py": "", |
| 27 | "b/e.py": "", |
| 28 | "files/data.txt": "", |
| 29 | }, |
| 30 | blueprint: `python_binary_host { |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 31 | name: "foo", |
| 32 | main: "a.py", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 33 | srcs: ["**/*.py"], |
| 34 | exclude_srcs: ["b/e.py"], |
| 35 | data: ["files/data.txt",], |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 36 | libs: ["bar"], |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 37 | bazel_module: { bp2build_available: true }, |
| 38 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 39 | python_library_host { |
| 40 | name: "bar", |
| 41 | srcs: ["b/e.py"], |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 42 | bazel_module: { bp2build_available: false }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 43 | }`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 44 | expectedBazelTargets: []string{ |
| 45 | makeBazelTarget("py_binary", "foo", attrNameToString{ |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame^] | 46 | "data": `["files/data.txt"]`, |
| 47 | "deps": `[":bar"]`, |
| 48 | "main": `"a.py"`, |
| 49 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 50 | "srcs": `[ |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 51 | "a.py", |
| 52 | "b/c.py", |
| 53 | "b/d.py", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 54 | ]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 55 | "target_compatible_with": `select({ |
| 56 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 57 | "//conditions:default": [], |
| 58 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 59 | }), |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 60 | }, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 61 | }) |
| 62 | } |
| 63 | |
| 64 | func TestPythonBinaryHostPy2(t *testing.T) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ce0a07e | 2021-08-23 16:17:32 +0000 | [diff] [blame] | 65 | runBp2BuildTestCaseSimple(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 66 | description: "py2 python_binary_host", |
| 67 | moduleTypeUnderTest: "python_binary_host", |
| 68 | moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 69 | blueprint: `python_binary_host { |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 70 | name: "foo", |
| 71 | srcs: ["a.py"], |
| 72 | version: { |
| 73 | py2: { |
| 74 | enabled: true, |
| 75 | }, |
| 76 | py3: { |
| 77 | enabled: false, |
| 78 | }, |
| 79 | }, |
| 80 | |
| 81 | bazel_module: { bp2build_available: true }, |
| 82 | } |
| 83 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 84 | expectedBazelTargets: []string{ |
| 85 | makeBazelTarget("py_binary", "foo", attrNameToString{ |
| 86 | "python_version": `"PY2"`, |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame^] | 87 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 88 | "srcs": `["a.py"]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 89 | "target_compatible_with": `select({ |
| 90 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 91 | "//conditions:default": [], |
| 92 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 93 | }), |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 94 | }, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 95 | }) |
| 96 | } |
| 97 | |
| 98 | func TestPythonBinaryHostPy3(t *testing.T) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ce0a07e | 2021-08-23 16:17:32 +0000 | [diff] [blame] | 99 | runBp2BuildTestCaseSimple(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 100 | description: "py3 python_binary_host", |
| 101 | moduleTypeUnderTest: "python_binary_host", |
| 102 | moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 103 | blueprint: `python_binary_host { |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 104 | name: "foo", |
| 105 | srcs: ["a.py"], |
| 106 | version: { |
| 107 | py2: { |
| 108 | enabled: false, |
| 109 | }, |
| 110 | py3: { |
| 111 | enabled: true, |
| 112 | }, |
| 113 | }, |
| 114 | |
| 115 | bazel_module: { bp2build_available: true }, |
| 116 | } |
| 117 | `, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 118 | expectedBazelTargets: []string{ |
| 119 | // python_version is PY3 by default. |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 120 | makeBazelTarget("py_binary", "foo", attrNameToString{ |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame^] | 121 | "imports": `["."]`, |
| 122 | "srcs": `["a.py"]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 123 | "target_compatible_with": `select({ |
| 124 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 125 | "//conditions:default": [], |
| 126 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 127 | }), |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 128 | }, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 129 | }) |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 130 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 131 | |
| 132 | func TestPythonBinaryHostArchVariance(t *testing.T) { |
| 133 | runBp2BuildTestCaseSimple(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 134 | description: "test arch variants", |
| 135 | moduleTypeUnderTest: "python_binary_host", |
| 136 | moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 137 | filesystem: map[string]string{ |
| 138 | "dir/arm.py": "", |
| 139 | "dir/x86.py": "", |
| 140 | }, |
| 141 | blueprint: `python_binary_host { |
| 142 | name: "foo-arm", |
| 143 | arch: { |
| 144 | arm: { |
| 145 | srcs: ["arm.py"], |
| 146 | }, |
| 147 | x86: { |
| 148 | srcs: ["x86.py"], |
| 149 | }, |
| 150 | }, |
| 151 | }`, |
| 152 | expectedBazelTargets: []string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 153 | makeBazelTarget("py_binary", "foo-arm", attrNameToString{ |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame^] | 154 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 155 | "srcs": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 156 | "//build/bazel/platforms/arch:arm": ["arm.py"], |
| 157 | "//build/bazel/platforms/arch:x86": ["x86.py"], |
| 158 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 159 | })`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 160 | "target_compatible_with": `select({ |
| 161 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 162 | "//conditions:default": [], |
| 163 | })`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 164 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 165 | }, |
| 166 | }) |
| 167 | } |