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