Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 4 | "fmt" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 5 | "testing" |
| 6 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 7 | "android/soong/android" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 8 | "android/soong/python" |
| 9 | ) |
| 10 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 11 | // TODO(alexmarquez): Should be lifted into a generic Bp2Build file |
| 12 | type PythonLibBp2Build func(ctx android.TopDownMutatorContext) |
| 13 | |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 14 | type pythonLibBp2BuildTestCase struct { |
| 15 | description string |
| 16 | filesystem map[string]string |
| 17 | blueprint string |
| 18 | expectedBazelTargets []testBazelTarget |
| 19 | } |
| 20 | |
| 21 | func convertPythonLibTestCaseToBp2build_Host(tc pythonLibBp2BuildTestCase) bp2buildTestCase { |
| 22 | for i := range tc.expectedBazelTargets { |
| 23 | tc.expectedBazelTargets[i].attrs["target_compatible_with"] = `select({ |
| 24 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 25 | "//conditions:default": [], |
| 26 | })` |
| 27 | } |
| 28 | |
| 29 | return convertPythonLibTestCaseToBp2build(tc) |
| 30 | } |
| 31 | |
| 32 | func convertPythonLibTestCaseToBp2build(tc pythonLibBp2BuildTestCase) bp2buildTestCase { |
| 33 | var bp2BuildTargets []string |
| 34 | for _, t := range tc.expectedBazelTargets { |
| 35 | bp2BuildTargets = append(bp2BuildTargets, makeBazelTarget(t.typ, t.name, t.attrs)) |
| 36 | } |
| 37 | return bp2buildTestCase{ |
| 38 | description: tc.description, |
| 39 | filesystem: tc.filesystem, |
| 40 | blueprint: tc.blueprint, |
| 41 | expectedBazelTargets: bp2BuildTargets, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func runPythonLibraryTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 46 | t.Helper() |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 47 | testCase := convertPythonLibTestCaseToBp2build(tc) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 48 | testCase.description = fmt.Sprintf(testCase.description, "python_library") |
| 49 | testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library") |
| 50 | testCase.moduleTypeUnderTest = "python_library" |
| 51 | testCase.moduleTypeUnderTestFactory = python.PythonLibraryFactory |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 52 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 53 | runBp2BuildTestCaseSimple(t, testCase) |
| 54 | } |
| 55 | |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 56 | func runPythonLibraryHostTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 57 | t.Helper() |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 58 | testCase := convertPythonLibTestCaseToBp2build_Host(tc) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 59 | testCase.description = fmt.Sprintf(testCase.description, "python_library_host") |
| 60 | testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library_host") |
| 61 | testCase.moduleTypeUnderTest = "python_library_host" |
| 62 | testCase.moduleTypeUnderTestFactory = python.PythonLibraryHostFactory |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 63 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) { |
| 64 | ctx.RegisterModuleType("python_library", python.PythonLibraryFactory) |
| 65 | }, |
| 66 | testCase) |
| 67 | } |
| 68 | |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 69 | func runPythonLibraryTestCases(t *testing.T, tc pythonLibBp2BuildTestCase) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 70 | t.Helper() |
| 71 | runPythonLibraryTestCase(t, tc) |
| 72 | runPythonLibraryHostTestCase(t, tc) |
| 73 | } |
| 74 | |
| 75 | func TestSimplePythonLib(t *testing.T) { |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 76 | testCases := []pythonLibBp2BuildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 77 | { |
| 78 | description: "simple %s converts to a native py_library", |
| 79 | filesystem: map[string]string{ |
| 80 | "a.py": "", |
| 81 | "b/c.py": "", |
| 82 | "b/d.py": "", |
| 83 | "b/e.py": "", |
| 84 | "files/data.txt": "", |
| 85 | }, |
| 86 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 87 | name: "foo", |
| 88 | srcs: ["**/*.py"], |
| 89 | exclude_srcs: ["b/e.py"], |
| 90 | 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] | 91 | libs: ["bar"], |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 92 | bazel_module: { bp2build_available: true }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 93 | } |
| 94 | python_library { |
| 95 | name: "bar", |
| 96 | srcs: ["b/e.py"], |
| 97 | bazel_module: { bp2build_available: false }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 98 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 99 | expectedBazelTargets: []testBazelTarget{ |
| 100 | { |
| 101 | typ: "py_library", |
| 102 | name: "foo", |
| 103 | attrs: attrNameToString{ |
| 104 | "data": `["files/data.txt"]`, |
| 105 | "deps": `[":bar"]`, |
| 106 | "srcs": `[ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 107 | "a.py", |
| 108 | "b/c.py", |
| 109 | "b/d.py", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 110 | ]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 111 | "srcs_version": `"PY3"`, |
| 112 | }, |
| 113 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 114 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 115 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 116 | { |
| 117 | description: "py2 %s converts to a native py_library", |
| 118 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 119 | name: "foo", |
| 120 | srcs: ["a.py"], |
| 121 | version: { |
| 122 | py2: { |
| 123 | enabled: true, |
| 124 | }, |
| 125 | py3: { |
| 126 | enabled: false, |
| 127 | }, |
| 128 | }, |
| 129 | |
| 130 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 131 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 132 | expectedBazelTargets: []testBazelTarget{ |
| 133 | { |
| 134 | typ: "py_library", |
| 135 | name: "foo", |
| 136 | attrs: attrNameToString{ |
| 137 | "srcs": `["a.py"]`, |
| 138 | "srcs_version": `"PY2"`, |
| 139 | }, |
| 140 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 141 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 142 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 143 | { |
| 144 | description: "py3 %s converts to a native py_library", |
| 145 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 146 | name: "foo", |
| 147 | srcs: ["a.py"], |
| 148 | version: { |
| 149 | py2: { |
| 150 | enabled: false, |
| 151 | }, |
| 152 | py3: { |
| 153 | enabled: true, |
| 154 | }, |
| 155 | }, |
| 156 | |
| 157 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 158 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 159 | expectedBazelTargets: []testBazelTarget{ |
| 160 | { |
| 161 | typ: "py_library", |
| 162 | name: "foo", |
| 163 | attrs: attrNameToString{ |
| 164 | "srcs": `["a.py"]`, |
| 165 | "srcs_version": `"PY3"`, |
| 166 | }, |
| 167 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 168 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 169 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 170 | { |
| 171 | description: "py2&3 %s converts to a native py_library", |
| 172 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 173 | name: "foo", |
| 174 | srcs: ["a.py"], |
| 175 | version: { |
| 176 | py2: { |
| 177 | enabled: true, |
| 178 | }, |
| 179 | py3: { |
| 180 | enabled: true, |
| 181 | }, |
| 182 | }, |
| 183 | |
| 184 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 185 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 186 | expectedBazelTargets: []testBazelTarget{ |
| 187 | { |
| 188 | // srcs_version is PY2ANDPY3 by default. |
| 189 | typ: "py_library", |
| 190 | name: "foo", |
| 191 | attrs: attrNameToString{ |
| 192 | "srcs": `["a.py"]`, |
| 193 | }, |
| 194 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 195 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 196 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | for _, tc := range testCases { |
| 200 | t.Run(tc.description, func(t *testing.T) { |
| 201 | runPythonLibraryTestCases(t, tc) |
| 202 | }) |
| 203 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 204 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 205 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 206 | func TestPythonArchVariance(t *testing.T) { |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 207 | runPythonLibraryTestCases(t, pythonLibBp2BuildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 208 | description: "test %s arch variants", |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 209 | filesystem: map[string]string{ |
| 210 | "dir/arm.py": "", |
| 211 | "dir/x86.py": "", |
| 212 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 213 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 214 | name: "foo", |
| 215 | arch: { |
| 216 | arm: { |
| 217 | srcs: ["arm.py"], |
| 218 | }, |
| 219 | x86: { |
| 220 | srcs: ["x86.py"], |
| 221 | }, |
| 222 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 223 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 224 | expectedBazelTargets: []testBazelTarget{ |
| 225 | { |
| 226 | typ: "py_library", |
| 227 | name: "foo", |
| 228 | attrs: attrNameToString{ |
| 229 | "srcs": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 230 | "//build/bazel/platforms/arch:arm": ["arm.py"], |
| 231 | "//build/bazel/platforms/arch:x86": ["x86.py"], |
| 232 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 233 | })`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 234 | "srcs_version": `"PY3"`, |
| 235 | }, |
| 236 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 237 | }, |
| 238 | }) |
| 239 | } |