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 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 14 | func runPythonLibraryTestCase(t *testing.T, tc bp2buildTestCase) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 15 | t.Helper() |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 16 | testCase := tc |
| 17 | testCase.description = fmt.Sprintf(testCase.description, "python_library") |
| 18 | testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library") |
| 19 | testCase.moduleTypeUnderTest = "python_library" |
| 20 | testCase.moduleTypeUnderTestFactory = python.PythonLibraryFactory |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 21 | runBp2BuildTestCaseSimple(t, testCase) |
| 22 | } |
| 23 | |
| 24 | func runPythonLibraryHostTestCase(t *testing.T, tc bp2buildTestCase) { |
| 25 | t.Helper() |
| 26 | testCase := tc |
| 27 | testCase.description = fmt.Sprintf(testCase.description, "python_library_host") |
| 28 | testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library_host") |
| 29 | testCase.moduleTypeUnderTest = "python_library_host" |
| 30 | testCase.moduleTypeUnderTestFactory = python.PythonLibraryHostFactory |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 31 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) { |
| 32 | ctx.RegisterModuleType("python_library", python.PythonLibraryFactory) |
| 33 | }, |
| 34 | testCase) |
| 35 | } |
| 36 | |
| 37 | func runPythonLibraryTestCases(t *testing.T, tc bp2buildTestCase) { |
| 38 | t.Helper() |
| 39 | runPythonLibraryTestCase(t, tc) |
| 40 | runPythonLibraryHostTestCase(t, tc) |
| 41 | } |
| 42 | |
| 43 | func TestSimplePythonLib(t *testing.T) { |
| 44 | testCases := []bp2buildTestCase{ |
| 45 | { |
| 46 | description: "simple %s converts to a native py_library", |
| 47 | filesystem: map[string]string{ |
| 48 | "a.py": "", |
| 49 | "b/c.py": "", |
| 50 | "b/d.py": "", |
| 51 | "b/e.py": "", |
| 52 | "files/data.txt": "", |
| 53 | }, |
| 54 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 55 | name: "foo", |
| 56 | srcs: ["**/*.py"], |
| 57 | exclude_srcs: ["b/e.py"], |
| 58 | 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] | 59 | libs: ["bar"], |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 60 | 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] | 61 | } |
| 62 | python_library { |
| 63 | name: "bar", |
| 64 | srcs: ["b/e.py"], |
| 65 | bazel_module: { bp2build_available: false }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 66 | }`, |
| 67 | expectedBazelTargets: []string{ |
| 68 | makeBazelTarget("py_library", "foo", attrNameToString{ |
| 69 | "data": `["files/data.txt"]`, |
| 70 | "deps": `[":bar"]`, |
| 71 | "srcs": `[ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 72 | "a.py", |
| 73 | "b/c.py", |
| 74 | "b/d.py", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 75 | ]`, |
| 76 | "srcs_version": `"PY3"`, |
| 77 | }), |
| 78 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 79 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 80 | { |
| 81 | description: "py2 %s converts to a native py_library", |
| 82 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 83 | name: "foo", |
| 84 | srcs: ["a.py"], |
| 85 | version: { |
| 86 | py2: { |
| 87 | enabled: true, |
| 88 | }, |
| 89 | py3: { |
| 90 | enabled: false, |
| 91 | }, |
| 92 | }, |
| 93 | |
| 94 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 95 | }`, |
| 96 | expectedBazelTargets: []string{ |
| 97 | makeBazelTarget("py_library", "foo", attrNameToString{ |
| 98 | "srcs": `["a.py"]`, |
| 99 | "srcs_version": `"PY2"`, |
| 100 | }), |
| 101 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 102 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 103 | { |
| 104 | description: "py3 %s converts to a native py_library", |
| 105 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 106 | name: "foo", |
| 107 | srcs: ["a.py"], |
| 108 | version: { |
| 109 | py2: { |
| 110 | enabled: false, |
| 111 | }, |
| 112 | py3: { |
| 113 | enabled: true, |
| 114 | }, |
| 115 | }, |
| 116 | |
| 117 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 118 | }`, |
| 119 | expectedBazelTargets: []string{ |
| 120 | makeBazelTarget("py_library", "foo", attrNameToString{ |
| 121 | "srcs": `["a.py"]`, |
| 122 | "srcs_version": `"PY3"`, |
| 123 | }), |
| 124 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 125 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 126 | { |
| 127 | description: "py2&3 %s converts to a native py_library", |
| 128 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 129 | name: "foo", |
| 130 | srcs: ["a.py"], |
| 131 | version: { |
| 132 | py2: { |
| 133 | enabled: true, |
| 134 | }, |
| 135 | py3: { |
| 136 | enabled: true, |
| 137 | }, |
| 138 | }, |
| 139 | |
| 140 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 141 | }`, |
| 142 | expectedBazelTargets: []string{ |
| 143 | // srcs_version is PY2ANDPY3 by default. |
| 144 | makeBazelTarget("py_library", "foo", attrNameToString{ |
| 145 | "srcs": `["a.py"]`, |
| 146 | }), |
| 147 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 148 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | for _, tc := range testCases { |
| 152 | t.Run(tc.description, func(t *testing.T) { |
| 153 | runPythonLibraryTestCases(t, tc) |
| 154 | }) |
| 155 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 156 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 157 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 158 | func TestPythonArchVariance(t *testing.T) { |
| 159 | runPythonLibraryTestCases(t, bp2buildTestCase{ |
| 160 | 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] | 161 | filesystem: map[string]string{ |
| 162 | "dir/arm.py": "", |
| 163 | "dir/x86.py": "", |
| 164 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 165 | blueprint: `%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 166 | name: "foo", |
| 167 | arch: { |
| 168 | arm: { |
| 169 | srcs: ["arm.py"], |
| 170 | }, |
| 171 | x86: { |
| 172 | srcs: ["x86.py"], |
| 173 | }, |
| 174 | }, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 175 | }`, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 176 | expectedBazelTargets: []string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 177 | makeBazelTarget("py_library", "foo", attrNameToString{ |
| 178 | "srcs": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 179 | "//build/bazel/platforms/arch:arm": ["arm.py"], |
| 180 | "//build/bazel/platforms/arch:x86": ["x86.py"], |
| 181 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 182 | })`, |
| 183 | "srcs_version": `"PY3"`, |
| 184 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 185 | }, |
| 186 | }) |
| 187 | } |