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" |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 5 | "strings" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 6 | "testing" |
| 7 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 8 | "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] | 9 | "android/soong/python" |
| 10 | ) |
| 11 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 12 | // TODO(alexmarquez): Should be lifted into a generic Bp2Build file |
| 13 | type PythonLibBp2Build func(ctx android.TopDownMutatorContext) |
| 14 | |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 15 | type pythonLibBp2BuildTestCase struct { |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 16 | description string |
| 17 | filesystem map[string]string |
| 18 | blueprint string |
| 19 | expectedBazelTargets []testBazelTarget |
| 20 | dir string |
| 21 | expectedError error |
| 22 | stubbedBuildDefinitions []string |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 25 | func convertPythonLibTestCaseToBp2build_Host(tc pythonLibBp2BuildTestCase) Bp2buildTestCase { |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 26 | for i := range tc.expectedBazelTargets { |
| 27 | tc.expectedBazelTargets[i].attrs["target_compatible_with"] = `select({ |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 28 | "//build/bazel_common_rules/platforms/os:android": ["@platforms//:incompatible"], |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 29 | "//conditions:default": [], |
| 30 | })` |
| 31 | } |
| 32 | |
| 33 | return convertPythonLibTestCaseToBp2build(tc) |
| 34 | } |
| 35 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 36 | func convertPythonLibTestCaseToBp2build(tc pythonLibBp2BuildTestCase) Bp2buildTestCase { |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 37 | var bp2BuildTargets []string |
| 38 | for _, t := range tc.expectedBazelTargets { |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 39 | bp2BuildTargets = append(bp2BuildTargets, MakeBazelTarget(t.typ, t.name, t.attrs)) |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 40 | } |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 41 | // Copy the filesystem so that we can change stuff in it later without it |
| 42 | // affecting the original pythonLibBp2BuildTestCase |
| 43 | filesystemCopy := make(map[string]string) |
| 44 | for k, v := range tc.filesystem { |
| 45 | filesystemCopy[k] = v |
| 46 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 47 | return Bp2buildTestCase{ |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 48 | Description: tc.description, |
| 49 | Filesystem: filesystemCopy, |
| 50 | Blueprint: tc.blueprint, |
| 51 | ExpectedBazelTargets: bp2BuildTargets, |
| 52 | Dir: tc.dir, |
| 53 | ExpectedErr: tc.expectedError, |
| 54 | StubbedBuildDefinitions: tc.stubbedBuildDefinitions, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | 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] | 59 | t.Helper() |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 60 | testCase := convertPythonLibTestCaseToBp2build(tc) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 61 | testCase.Description = fmt.Sprintf(testCase.Description, "python_library") |
| 62 | testCase.Blueprint = fmt.Sprintf(testCase.Blueprint, "python_library") |
| 63 | for name, contents := range testCase.Filesystem { |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 64 | if strings.HasSuffix(name, "Android.bp") { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 65 | testCase.Filesystem[name] = fmt.Sprintf(contents, "python_library") |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 68 | testCase.ModuleTypeUnderTest = "python_library" |
| 69 | testCase.ModuleTypeUnderTestFactory = python.PythonLibraryFactory |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 70 | |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 71 | RunBp2BuildTestCaseSimple(t, testCase) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 74 | func runPythonLibraryHostTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 75 | t.Helper() |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 76 | testCase := convertPythonLibTestCaseToBp2build_Host(tc) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 77 | testCase.Description = fmt.Sprintf(testCase.Description, "python_library_host") |
| 78 | testCase.Blueprint = fmt.Sprintf(testCase.Blueprint, "python_library_host") |
| 79 | for name, contents := range testCase.Filesystem { |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 80 | if strings.HasSuffix(name, "Android.bp") { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 81 | testCase.Filesystem[name] = fmt.Sprintf(contents, "python_library_host") |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 84 | testCase.ModuleTypeUnderTest = "python_library_host" |
| 85 | testCase.ModuleTypeUnderTestFactory = python.PythonLibraryHostFactory |
| 86 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 87 | ctx.RegisterModuleType("python_library", python.PythonLibraryFactory) |
| 88 | }, |
| 89 | testCase) |
| 90 | } |
| 91 | |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 92 | func runPythonLibraryTestCases(t *testing.T, tc pythonLibBp2BuildTestCase) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 93 | t.Helper() |
| 94 | runPythonLibraryTestCase(t, tc) |
| 95 | runPythonLibraryHostTestCase(t, tc) |
| 96 | } |
| 97 | |
| 98 | func TestSimplePythonLib(t *testing.T) { |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 99 | testCases := []pythonLibBp2BuildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 100 | { |
| 101 | description: "simple %s converts to a native py_library", |
| 102 | filesystem: map[string]string{ |
| 103 | "a.py": "", |
| 104 | "b/c.py": "", |
| 105 | "b/d.py": "", |
| 106 | "b/e.py": "", |
| 107 | "files/data.txt": "", |
| 108 | }, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 109 | stubbedBuildDefinitions: []string{"bar"}, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 110 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 111 | name: "foo", |
| 112 | srcs: ["**/*.py"], |
| 113 | exclude_srcs: ["b/e.py"], |
| 114 | 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] | 115 | libs: ["bar"], |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 116 | 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] | 117 | } |
| 118 | python_library { |
| 119 | name: "bar", |
| 120 | srcs: ["b/e.py"], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 121 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 122 | expectedBazelTargets: []testBazelTarget{ |
| 123 | { |
| 124 | typ: "py_library", |
| 125 | name: "foo", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 126 | attrs: AttrNameToString{ |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 127 | "data": `["files/data.txt"]`, |
| 128 | "deps": `[":bar"]`, |
| 129 | "srcs": `[ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 130 | "a.py", |
| 131 | "b/c.py", |
| 132 | "b/d.py", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 133 | ]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 134 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 135 | "imports": `["."]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 136 | }, |
| 137 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 138 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 139 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 140 | { |
| 141 | description: "py2 %s converts to a native py_library", |
| 142 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 143 | name: "foo", |
| 144 | srcs: ["a.py"], |
| 145 | version: { |
| 146 | py2: { |
| 147 | enabled: true, |
| 148 | }, |
| 149 | py3: { |
| 150 | enabled: false, |
| 151 | }, |
| 152 | }, |
| 153 | |
| 154 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 155 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 156 | expectedBazelTargets: []testBazelTarget{ |
| 157 | { |
| 158 | typ: "py_library", |
| 159 | name: "foo", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 160 | attrs: AttrNameToString{ |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 161 | "srcs": `["a.py"]`, |
| 162 | "srcs_version": `"PY2"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 163 | "imports": `["."]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 164 | }, |
| 165 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 166 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 167 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 168 | { |
| 169 | description: "py3 %s converts to a native py_library", |
| 170 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 171 | name: "foo", |
| 172 | srcs: ["a.py"], |
| 173 | version: { |
| 174 | py2: { |
| 175 | enabled: false, |
| 176 | }, |
| 177 | py3: { |
| 178 | enabled: true, |
| 179 | }, |
| 180 | }, |
| 181 | |
| 182 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 183 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 184 | expectedBazelTargets: []testBazelTarget{ |
| 185 | { |
| 186 | typ: "py_library", |
| 187 | name: "foo", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 188 | attrs: AttrNameToString{ |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 189 | "srcs": `["a.py"]`, |
| 190 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 191 | "imports": `["."]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 192 | }, |
| 193 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 194 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 195 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 196 | { |
| 197 | description: "py2&3 %s converts to a native py_library", |
| 198 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 199 | name: "foo", |
| 200 | srcs: ["a.py"], |
| 201 | version: { |
| 202 | py2: { |
| 203 | enabled: true, |
| 204 | }, |
| 205 | py3: { |
| 206 | enabled: true, |
| 207 | }, |
| 208 | }, |
| 209 | |
| 210 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 211 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 212 | expectedBazelTargets: []testBazelTarget{ |
| 213 | { |
| 214 | // srcs_version is PY2ANDPY3 by default. |
| 215 | typ: "py_library", |
| 216 | name: "foo", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 217 | attrs: AttrNameToString{ |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 218 | "srcs": `["a.py"]`, |
| 219 | "imports": `["."]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 220 | }, |
| 221 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 222 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 223 | }, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 224 | { |
| 225 | description: "%s: pkg_path in a subdirectory of the same name converts correctly", |
| 226 | dir: "mylib/subpackage", |
| 227 | filesystem: map[string]string{ |
| 228 | "mylib/subpackage/a.py": "", |
| 229 | "mylib/subpackage/Android.bp": `%s { |
| 230 | name: "foo", |
| 231 | srcs: ["a.py"], |
| 232 | pkg_path: "mylib/subpackage", |
| 233 | |
| 234 | bazel_module: { bp2build_available: true }, |
| 235 | }`, |
| 236 | }, |
| 237 | blueprint: `%s {name: "bar"}`, |
| 238 | expectedBazelTargets: []testBazelTarget{ |
| 239 | { |
| 240 | // srcs_version is PY2ANDPY3 by default. |
| 241 | typ: "py_library", |
| 242 | name: "foo", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 243 | attrs: AttrNameToString{ |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 244 | "srcs": `["a.py"]`, |
| 245 | "imports": `["../.."]`, |
| 246 | "srcs_version": `"PY3"`, |
| 247 | }, |
| 248 | }, |
| 249 | }, |
| 250 | }, |
| 251 | { |
| 252 | description: "%s: pkg_path in a subdirectory of a different name fails", |
| 253 | dir: "mylib/subpackage", |
| 254 | filesystem: map[string]string{ |
| 255 | "mylib/subpackage/a.py": "", |
| 256 | "mylib/subpackage/Android.bp": `%s { |
| 257 | name: "foo", |
| 258 | srcs: ["a.py"], |
| 259 | pkg_path: "mylib/subpackage2", |
| 260 | bazel_module: { bp2build_available: true }, |
| 261 | }`, |
| 262 | }, |
| 263 | blueprint: `%s {name: "bar"}`, |
| 264 | expectedError: fmt.Errorf("Currently, bp2build only supports pkg_paths that are the same as the folders the Android.bp file is in."), |
| 265 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | for _, tc := range testCases { |
| 269 | t.Run(tc.description, func(t *testing.T) { |
| 270 | runPythonLibraryTestCases(t, tc) |
| 271 | }) |
| 272 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 273 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 274 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 275 | func TestPythonArchVariance(t *testing.T) { |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 276 | runPythonLibraryTestCases(t, pythonLibBp2BuildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 277 | 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] | 278 | filesystem: map[string]string{ |
| 279 | "dir/arm.py": "", |
| 280 | "dir/x86.py": "", |
| 281 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 282 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 283 | name: "foo", |
| 284 | arch: { |
| 285 | arm: { |
| 286 | srcs: ["arm.py"], |
| 287 | }, |
| 288 | x86: { |
| 289 | srcs: ["x86.py"], |
| 290 | }, |
| 291 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 292 | }`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 293 | expectedBazelTargets: []testBazelTarget{ |
| 294 | { |
| 295 | typ: "py_library", |
| 296 | name: "foo", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 297 | attrs: AttrNameToString{ |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 298 | "srcs": `select({ |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 299 | "//build/bazel_common_rules/platforms/arch:arm": ["arm.py"], |
| 300 | "//build/bazel_common_rules/platforms/arch:x86": ["x86.py"], |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 301 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 302 | })`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 303 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 304 | "imports": `["."]`, |
Sam Delmerico | 75539d6 | 2022-01-31 14:37:29 +0000 | [diff] [blame] | 305 | }, |
| 306 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 307 | }, |
| 308 | }) |
| 309 | } |
Cole Faust | 53b6209 | 2022-05-12 15:37:02 -0700 | [diff] [blame] | 310 | |
| 311 | func TestPythonLibraryWithProtobufs(t *testing.T) { |
| 312 | runPythonLibraryTestCases(t, pythonLibBp2BuildTestCase{ |
| 313 | description: "test %s protobuf", |
| 314 | filesystem: map[string]string{ |
| 315 | "dir/mylib.py": "", |
| 316 | "dir/myproto.proto": "", |
| 317 | }, |
| 318 | blueprint: `%s { |
| 319 | name: "foo", |
| 320 | srcs: [ |
| 321 | "dir/mylib.py", |
| 322 | "dir/myproto.proto", |
| 323 | ], |
| 324 | }`, |
| 325 | expectedBazelTargets: []testBazelTarget{ |
| 326 | { |
| 327 | typ: "proto_library", |
| 328 | name: "foo_proto", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 329 | attrs: AttrNameToString{ |
Cole Faust | 53b6209 | 2022-05-12 15:37:02 -0700 | [diff] [blame] | 330 | "srcs": `["dir/myproto.proto"]`, |
| 331 | }, |
| 332 | }, |
| 333 | { |
| 334 | typ: "py_proto_library", |
| 335 | name: "foo_py_proto", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 336 | attrs: AttrNameToString{ |
Cole Faust | 53b6209 | 2022-05-12 15:37:02 -0700 | [diff] [blame] | 337 | "deps": `[":foo_proto"]`, |
| 338 | }, |
| 339 | }, |
| 340 | { |
| 341 | typ: "py_library", |
| 342 | name: "foo", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 343 | attrs: AttrNameToString{ |
Cole Faust | 53b6209 | 2022-05-12 15:37:02 -0700 | [diff] [blame] | 344 | "srcs": `["dir/mylib.py"]`, |
| 345 | "srcs_version": `"PY3"`, |
| 346 | "imports": `["."]`, |
| 347 | "deps": `[":foo_py_proto"]`, |
| 348 | }, |
| 349 | }, |
| 350 | }, |
| 351 | }) |
| 352 | } |
Spandan Das | cb84763 | 2023-08-22 19:24:07 +0000 | [diff] [blame] | 353 | |
| 354 | func TestPythonLibraryWithProtobufsAndPkgPath(t *testing.T) { |
| 355 | t.Parallel() |
| 356 | runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{ |
| 357 | Description: "test python_library protobuf with pkg_path", |
| 358 | Filesystem: map[string]string{ |
| 359 | "dir/foo.proto": "", |
| 360 | "dir/bar.proto": "", // bar contains "import dir/foo.proto" |
| 361 | "dir/Android.bp": ` |
| 362 | python_library { |
| 363 | name: "foo", |
| 364 | pkg_path: "dir", |
| 365 | srcs: [ |
| 366 | "foo.proto", |
| 367 | "bar.proto", |
| 368 | ], |
| 369 | bazel_module: {bp2build_available: true}, |
| 370 | }`, |
| 371 | }, |
| 372 | Dir: "dir", |
| 373 | ExpectedBazelTargets: []string{ |
| 374 | MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{ |
| 375 | "import_prefix": `"dir"`, |
| 376 | "strip_import_prefix": `""`, |
| 377 | "srcs": `[ |
| 378 | "foo.proto", |
| 379 | "bar.proto", |
| 380 | ]`, |
| 381 | }), |
| 382 | MakeBazelTarget("py_proto_library", "foo_py_proto", AttrNameToString{ |
| 383 | "deps": `[":foo_proto"]`, |
| 384 | }), |
| 385 | MakeBazelTarget("py_library", "foo", AttrNameToString{ |
| 386 | "srcs_version": `"PY3"`, |
| 387 | "imports": `[".."]`, |
| 388 | "deps": `[":foo_py_proto"]`, |
| 389 | }), |
| 390 | }, |
| 391 | }) |
| 392 | } |