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