123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package bp2build
- import (
- "testing"
- "android/soong/android"
- "android/soong/python"
- )
- func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc Bp2buildTestCase) {
- t.Helper()
- RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
- ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
- ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory)
- }, tc)
- }
- func TestPythonBinaryHostSimple(t *testing.T) {
- 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{
- "a.py": "",
- "b/c.py": "",
- "b/d.py": "",
- "b/e.py": "",
- "files/data.txt": "",
- },
- Blueprint: `python_binary_host {
- name: "foo",
- main: "a.py",
- srcs: ["**/*.py"],
- exclude_srcs: ["b/e.py"],
- data: ["files/data.txt",],
- libs: ["bar"],
- bazel_module: { bp2build_available: true },
- }
- python_library_host {
- name: "bar",
- srcs: ["b/e.py"],
- bazel_module: { bp2build_available: false },
- }`,
- ExpectedBazelTargets: []string{
- MakeBazelTarget("py_binary", "foo", AttrNameToString{
- "data": `["files/data.txt"]`,
- "deps": `[":bar"]`,
- "main": `"a.py"`,
- "imports": `["."]`,
- "srcs": `[
- "a.py",
- "b/c.py",
- "b/d.py",
- ]`,
- "target_compatible_with": `select({
- "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
- "//conditions:default": [],
- })`,
- }),
- },
- })
- }
- func TestPythonBinaryHostPy2(t *testing.T) {
- RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
- Description: "py2 python_binary_host",
- ModuleTypeUnderTest: "python_binary_host",
- ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
- Blueprint: `python_binary_host {
- name: "foo",
- srcs: ["a.py"],
- version: {
- py2: {
- enabled: true,
- },
- py3: {
- enabled: false,
- },
- },
- bazel_module: { bp2build_available: true },
- }
- `,
- ExpectedBazelTargets: []string{
- MakeBazelTarget("py_binary", "foo", AttrNameToString{
- "python_version": `"PY2"`,
- "imports": `["."]`,
- "srcs": `["a.py"]`,
- "target_compatible_with": `select({
- "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
- "//conditions:default": [],
- })`,
- }),
- },
- })
- }
- func TestPythonBinaryHostPy3(t *testing.T) {
- RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
- Description: "py3 python_binary_host",
- ModuleTypeUnderTest: "python_binary_host",
- ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
- Blueprint: `python_binary_host {
- name: "foo",
- srcs: ["a.py"],
- version: {
- py2: {
- enabled: false,
- },
- py3: {
- enabled: true,
- },
- },
- bazel_module: { bp2build_available: true },
- }
- `,
- ExpectedBazelTargets: []string{
- // python_version is PY3 by default.
- MakeBazelTarget("py_binary", "foo", AttrNameToString{
- "imports": `["."]`,
- "srcs": `["a.py"]`,
- "target_compatible_with": `select({
- "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
- "//conditions:default": [],
- })`,
- }),
- },
- })
- }
- func TestPythonBinaryHostArchVariance(t *testing.T) {
- RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
- Description: "test arch variants",
- ModuleTypeUnderTest: "python_binary_host",
- ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
- Filesystem: map[string]string{
- "dir/arm.py": "",
- "dir/x86.py": "",
- },
- Blueprint: `python_binary_host {
- name: "foo-arm",
- arch: {
- arm: {
- srcs: ["arm.py"],
- },
- x86: {
- srcs: ["x86.py"],
- },
- },
- }`,
- ExpectedBazelTargets: []string{
- MakeBazelTarget("py_binary", "foo-arm", AttrNameToString{
- "imports": `["."]`,
- "srcs": `select({
- "//build/bazel/platforms/arch:arm": ["arm.py"],
- "//build/bazel/platforms/arch:x86": ["x86.py"],
- "//conditions:default": [],
- })`,
- "target_compatible_with": `select({
- "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
- "//conditions:default": [],
- })`,
- }),
- },
- })
- }
|