2021-03-08 13:32:28 +01:00
|
|
|
package bp2build
|
|
|
|
|
|
|
|
import (
|
2021-05-21 09:37:00 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-08-26 22:13:29 +02:00
|
|
|
"android/soong/android"
|
2021-03-08 13:32:28 +01:00
|
|
|
"android/soong/python"
|
|
|
|
)
|
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc Bp2buildTestCase) {
|
2021-11-08 18:56:31 +01:00
|
|
|
t.Helper()
|
2022-06-21 21:28:33 +02:00
|
|
|
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
|
2021-08-26 22:13:29 +02:00
|
|
|
ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
|
|
|
|
ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory)
|
|
|
|
}, tc)
|
|
|
|
}
|
|
|
|
|
2021-05-21 09:37:00 +02:00
|
|
|
func TestPythonBinaryHostSimple(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
|
|
|
|
Description: "simple python_binary_host converts to a native py_binary",
|
|
|
|
ModuleTypeUnderTest: "python_binary_host",
|
|
|
|
ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
|
|
|
|
Filesystem: map[string]string{
|
2021-05-21 09:37:00 +02:00
|
|
|
"a.py": "",
|
|
|
|
"b/c.py": "",
|
|
|
|
"b/d.py": "",
|
|
|
|
"b/e.py": "",
|
|
|
|
"files/data.txt": "",
|
|
|
|
},
|
2022-06-21 21:28:33 +02:00
|
|
|
Blueprint: `python_binary_host {
|
2021-03-08 13:32:28 +01:00
|
|
|
name: "foo",
|
|
|
|
main: "a.py",
|
2021-04-08 16:40:57 +02:00
|
|
|
srcs: ["**/*.py"],
|
|
|
|
exclude_srcs: ["b/e.py"],
|
|
|
|
data: ["files/data.txt",],
|
2021-08-26 22:13:29 +02:00
|
|
|
libs: ["bar"],
|
2021-03-08 13:32:28 +01:00
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}
|
2021-08-26 22:13:29 +02:00
|
|
|
python_library_host {
|
|
|
|
name: "bar",
|
|
|
|
srcs: ["b/e.py"],
|
2021-11-01 20:32:43 +01:00
|
|
|
bazel_module: { bp2build_available: false },
|
2021-08-26 22:13:29 +02:00
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTarget("py_binary", "foo", AttrNameToString{
|
2022-06-02 21:11:12 +02:00
|
|
|
"data": `["files/data.txt"]`,
|
|
|
|
"deps": `[":bar"]`,
|
|
|
|
"main": `"a.py"`,
|
|
|
|
"imports": `["."]`,
|
2021-11-08 18:56:31 +01:00
|
|
|
"srcs": `[
|
2021-03-08 13:32:28 +01:00
|
|
|
"a.py",
|
|
|
|
"b/c.py",
|
|
|
|
"b/d.py",
|
2021-11-08 18:56:31 +01:00
|
|
|
]`,
|
2022-01-31 15:37:29 +01:00
|
|
|
"target_compatible_with": `select({
|
|
|
|
"//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
|
|
|
|
"//conditions:default": [],
|
|
|
|
})`,
|
2021-11-08 18:56:31 +01:00
|
|
|
}),
|
2021-03-08 13:32:28 +01:00
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonBinaryHostPy2(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
runBp2BuildTestCaseSimple(t, Bp2buildTestCase{
|
|
|
|
Description: "py2 python_binary_host",
|
|
|
|
ModuleTypeUnderTest: "python_binary_host",
|
|
|
|
ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
|
|
|
|
Blueprint: `python_binary_host {
|
2021-03-08 13:32:28 +01:00
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.py"],
|
|
|
|
version: {
|
|
|
|
py2: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
py3: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}
|
|
|
|
`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTarget("py_binary", "foo", AttrNameToString{
|
2021-11-08 18:56:31 +01:00
|
|
|
"python_version": `"PY2"`,
|
2022-06-02 21:11:12 +02:00
|
|
|
"imports": `["."]`,
|
2021-11-08 18:56:31 +01:00
|
|
|
"srcs": `["a.py"]`,
|
2022-01-31 15:37:29 +01:00
|
|
|
"target_compatible_with": `select({
|
|
|
|
"//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
|
|
|
|
"//conditions:default": [],
|
|
|
|
})`,
|
2021-11-08 18:56:31 +01:00
|
|
|
}),
|
2021-03-08 13:32:28 +01:00
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonBinaryHostPy3(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
runBp2BuildTestCaseSimple(t, Bp2buildTestCase{
|
|
|
|
Description: "py3 python_binary_host",
|
|
|
|
ModuleTypeUnderTest: "python_binary_host",
|
|
|
|
ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
|
|
|
|
Blueprint: `python_binary_host {
|
2021-03-08 13:32:28 +01:00
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.py"],
|
|
|
|
version: {
|
|
|
|
py2: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
py3: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}
|
|
|
|
`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2021-05-21 09:37:00 +02:00
|
|
|
// python_version is PY3 by default.
|
2022-06-21 21:28:33 +02:00
|
|
|
makeBazelTarget("py_binary", "foo", AttrNameToString{
|
2022-06-02 21:11:12 +02:00
|
|
|
"imports": `["."]`,
|
|
|
|
"srcs": `["a.py"]`,
|
2022-01-31 15:37:29 +01:00
|
|
|
"target_compatible_with": `select({
|
|
|
|
"//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
|
|
|
|
"//conditions:default": [],
|
|
|
|
})`,
|
2021-11-08 18:56:31 +01:00
|
|
|
}),
|
2021-03-08 13:32:28 +01:00
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
2021-03-08 13:32:28 +01:00
|
|
|
}
|
2021-09-17 22:30:21 +02:00
|
|
|
|
|
|
|
func TestPythonBinaryHostArchVariance(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
runBp2BuildTestCaseSimple(t, Bp2buildTestCase{
|
|
|
|
Description: "test arch variants",
|
|
|
|
ModuleTypeUnderTest: "python_binary_host",
|
|
|
|
ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
|
|
|
|
Filesystem: map[string]string{
|
2021-09-17 22:30:21 +02:00
|
|
|
"dir/arm.py": "",
|
|
|
|
"dir/x86.py": "",
|
|
|
|
},
|
2022-06-21 21:28:33 +02:00
|
|
|
Blueprint: `python_binary_host {
|
2021-09-17 22:30:21 +02:00
|
|
|
name: "foo-arm",
|
|
|
|
arch: {
|
|
|
|
arm: {
|
|
|
|
srcs: ["arm.py"],
|
|
|
|
},
|
|
|
|
x86: {
|
|
|
|
srcs: ["x86.py"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
makeBazelTarget("py_binary", "foo-arm", AttrNameToString{
|
2022-06-02 21:11:12 +02:00
|
|
|
"imports": `["."]`,
|
2021-11-08 18:56:31 +01:00
|
|
|
"srcs": `select({
|
2021-09-17 22:30:21 +02:00
|
|
|
"//build/bazel/platforms/arch:arm": ["arm.py"],
|
|
|
|
"//build/bazel/platforms/arch:x86": ["x86.py"],
|
|
|
|
"//conditions:default": [],
|
2022-01-31 15:37:29 +01:00
|
|
|
})`,
|
|
|
|
"target_compatible_with": `select({
|
|
|
|
"//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
|
|
|
|
"//conditions:default": [],
|
2021-11-08 18:56:31 +01:00
|
|
|
})`,
|
|
|
|
}),
|
2021-09-17 22:30:21 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|