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 | |
| 14 | func TestPythonLibrary(t *testing.T) { |
| 15 | testPythonLib(t, "python_library", |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 16 | python.PythonLibraryFactory, python.PythonLibraryBp2Build, |
| 17 | func(ctx android.RegistrationContext) {}) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | func TestPythonLibraryHost(t *testing.T) { |
| 21 | testPythonLib(t, "python_library_host", |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 22 | python.PythonLibraryHostFactory, python.PythonLibraryHostBp2Build, |
| 23 | func(ctx android.RegistrationContext) { |
| 24 | ctx.RegisterModuleType("python_library", python.PythonLibraryFactory) |
| 25 | }) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | func testPythonLib(t *testing.T, modType string, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 29 | factory android.ModuleFactory, mutator PythonLibBp2Build, |
| 30 | registration func(ctx android.RegistrationContext)) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 31 | t.Helper() |
| 32 | // Simple |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 33 | runBp2BuildTestCase(t, registration, bp2buildTestCase{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 34 | description: fmt.Sprintf("simple %s converts to a native py_library", modType), |
| 35 | moduleTypeUnderTest: modType, |
| 36 | moduleTypeUnderTestFactory: factory, |
| 37 | moduleTypeUnderTestBp2BuildMutator: mutator, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 38 | filesystem: map[string]string{ |
| 39 | "a.py": "", |
| 40 | "b/c.py": "", |
| 41 | "b/d.py": "", |
| 42 | "b/e.py": "", |
| 43 | "files/data.txt": "", |
| 44 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 45 | blueprint: fmt.Sprintf(`%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 46 | name: "foo", |
| 47 | srcs: ["**/*.py"], |
| 48 | exclude_srcs: ["b/e.py"], |
| 49 | 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] | 50 | libs: ["bar"], |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 51 | 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] | 52 | } |
| 53 | python_library { |
| 54 | name: "bar", |
| 55 | srcs: ["b/e.py"], |
| 56 | bazel_module: { bp2build_available: false }, |
| 57 | }`, modType), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 58 | expectedBazelTargets: []string{`py_library( |
| 59 | name = "foo", |
| 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 | deps = [":bar"], |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 62 | srcs = [ |
| 63 | "a.py", |
| 64 | "b/c.py", |
| 65 | "b/d.py", |
| 66 | ], |
| 67 | srcs_version = "PY3", |
| 68 | )`, |
| 69 | }, |
| 70 | }) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 71 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 72 | // PY2 |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 73 | runBp2BuildTestCaseSimple(t, bp2buildTestCase{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 74 | description: fmt.Sprintf("py2 %s converts to a native py_library", modType), |
| 75 | moduleTypeUnderTest: modType, |
| 76 | moduleTypeUnderTestFactory: factory, |
| 77 | moduleTypeUnderTestBp2BuildMutator: mutator, |
| 78 | blueprint: fmt.Sprintf(`%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 79 | name: "foo", |
| 80 | srcs: ["a.py"], |
| 81 | version: { |
| 82 | py2: { |
| 83 | enabled: true, |
| 84 | }, |
| 85 | py3: { |
| 86 | enabled: false, |
| 87 | }, |
| 88 | }, |
| 89 | |
| 90 | bazel_module: { bp2build_available: true }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 91 | }`, modType), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 92 | expectedBazelTargets: []string{`py_library( |
| 93 | name = "foo", |
| 94 | srcs = ["a.py"], |
| 95 | srcs_version = "PY2", |
| 96 | )`, |
| 97 | }, |
| 98 | }) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 99 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 100 | // PY3 |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 101 | runBp2BuildTestCaseSimple(t, bp2buildTestCase{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 102 | description: fmt.Sprintf("py3 %s converts to a native py_library", modType), |
| 103 | moduleTypeUnderTest: modType, |
| 104 | moduleTypeUnderTestFactory: factory, |
| 105 | moduleTypeUnderTestBp2BuildMutator: mutator, |
| 106 | blueprint: fmt.Sprintf(`%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 107 | name: "foo", |
| 108 | srcs: ["a.py"], |
| 109 | version: { |
| 110 | py2: { |
| 111 | enabled: false, |
| 112 | }, |
| 113 | py3: { |
| 114 | enabled: true, |
| 115 | }, |
| 116 | }, |
| 117 | |
| 118 | bazel_module: { bp2build_available: true }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 119 | }`, modType), |
| 120 | expectedBazelTargets: []string{`py_library( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 121 | name = "foo", |
| 122 | srcs = ["a.py"], |
| 123 | srcs_version = "PY3", |
| 124 | )`, |
| 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 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 128 | // Both |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 129 | runBp2BuildTestCaseSimple(t, bp2buildTestCase{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 130 | description: fmt.Sprintf("py2&3 %s converts to a native py_library", modType), |
| 131 | moduleTypeUnderTest: modType, |
| 132 | moduleTypeUnderTestFactory: factory, |
| 133 | moduleTypeUnderTestBp2BuildMutator: mutator, |
| 134 | blueprint: fmt.Sprintf(`%s { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 135 | name: "foo", |
| 136 | srcs: ["a.py"], |
| 137 | version: { |
| 138 | py2: { |
| 139 | enabled: true, |
| 140 | }, |
| 141 | py3: { |
| 142 | enabled: true, |
| 143 | }, |
| 144 | }, |
| 145 | |
| 146 | bazel_module: { bp2build_available: true }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 147 | }`, modType), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 148 | expectedBazelTargets: []string{ |
| 149 | // srcs_version is PY2ANDPY3 by default. |
| 150 | `py_library( |
| 151 | name = "foo", |
| 152 | srcs = ["a.py"], |
| 153 | )`, |
| 154 | }, |
| 155 | }) |
| 156 | } |