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{ |
| 46 | "data": `["files/data.txt"]`, |
| 47 | "deps": `[":bar"]`, |
| 48 | "main": `"a.py"`, |
| 49 | "srcs": `[ |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 50 | "a.py", |
| 51 | "b/c.py", |
| 52 | "b/d.py", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 53 | ]`, |
| 54 | }), |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 55 | }, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 56 | }) |
| 57 | } |
| 58 | |
| 59 | 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] | 60 | runBp2BuildTestCaseSimple(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 61 | description: "py2 python_binary_host", |
| 62 | moduleTypeUnderTest: "python_binary_host", |
| 63 | moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 64 | blueprint: `python_binary_host { |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 65 | name: "foo", |
| 66 | srcs: ["a.py"], |
| 67 | version: { |
| 68 | py2: { |
| 69 | enabled: true, |
| 70 | }, |
| 71 | py3: { |
| 72 | enabled: false, |
| 73 | }, |
| 74 | }, |
| 75 | |
| 76 | bazel_module: { bp2build_available: true }, |
| 77 | } |
| 78 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 79 | expectedBazelTargets: []string{ |
| 80 | makeBazelTarget("py_binary", "foo", attrNameToString{ |
| 81 | "python_version": `"PY2"`, |
| 82 | "srcs": `["a.py"]`, |
| 83 | }), |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 84 | }, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 85 | }) |
| 86 | } |
| 87 | |
| 88 | 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] | 89 | runBp2BuildTestCaseSimple(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 90 | description: "py3 python_binary_host", |
| 91 | moduleTypeUnderTest: "python_binary_host", |
| 92 | moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 93 | blueprint: `python_binary_host { |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 94 | name: "foo", |
| 95 | srcs: ["a.py"], |
| 96 | version: { |
| 97 | py2: { |
| 98 | enabled: false, |
| 99 | }, |
| 100 | py3: { |
| 101 | enabled: true, |
| 102 | }, |
| 103 | }, |
| 104 | |
| 105 | bazel_module: { bp2build_available: true }, |
| 106 | } |
| 107 | `, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 108 | expectedBazelTargets: []string{ |
| 109 | // python_version is PY3 by default. |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 110 | makeBazelTarget("py_binary", "foo", attrNameToString{ |
| 111 | "srcs": `["a.py"]`, |
| 112 | }), |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 113 | }, |
Lukacs T. Berki | c1cc3b9 | 2021-05-21 09:37:00 +0200 | [diff] [blame] | 114 | }) |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 115 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 116 | |
| 117 | func TestPythonBinaryHostArchVariance(t *testing.T) { |
| 118 | runBp2BuildTestCaseSimple(t, bp2buildTestCase{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 119 | description: "test arch variants", |
| 120 | moduleTypeUnderTest: "python_binary_host", |
| 121 | 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] | 122 | filesystem: map[string]string{ |
| 123 | "dir/arm.py": "", |
| 124 | "dir/x86.py": "", |
| 125 | }, |
| 126 | blueprint: `python_binary_host { |
| 127 | name: "foo-arm", |
| 128 | arch: { |
| 129 | arm: { |
| 130 | srcs: ["arm.py"], |
| 131 | }, |
| 132 | x86: { |
| 133 | srcs: ["x86.py"], |
| 134 | }, |
| 135 | }, |
| 136 | }`, |
| 137 | expectedBazelTargets: []string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 138 | makeBazelTarget("py_binary", "foo-arm", attrNameToString{ |
| 139 | "srcs": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 140 | "//build/bazel/platforms/arch:arm": ["arm.py"], |
| 141 | "//build/bazel/platforms/arch:x86": ["x86.py"], |
| 142 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 143 | })`, |
| 144 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 145 | }, |
| 146 | }) |
| 147 | } |