python_binary_conversion_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package bp2build
  2. import (
  3. "testing"
  4. "android/soong/android"
  5. "android/soong/python"
  6. )
  7. func runBp2BuildTestCaseWithPythonLibraries(t *testing.T, tc Bp2buildTestCase) {
  8. t.Helper()
  9. RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
  10. ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
  11. ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory)
  12. }, tc)
  13. }
  14. func TestPythonBinaryHostSimple(t *testing.T) {
  15. runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
  16. Description: "simple python_binary_host converts to a native py_binary",
  17. ModuleTypeUnderTest: "python_binary_host",
  18. ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
  19. Filesystem: map[string]string{
  20. "a.py": "",
  21. "b/c.py": "",
  22. "b/d.py": "",
  23. "b/e.py": "",
  24. "files/data.txt": "",
  25. },
  26. Blueprint: `python_binary_host {
  27. name: "foo",
  28. main: "a.py",
  29. srcs: ["**/*.py"],
  30. exclude_srcs: ["b/e.py"],
  31. data: ["files/data.txt",],
  32. libs: ["bar"],
  33. bazel_module: { bp2build_available: true },
  34. }
  35. python_library_host {
  36. name: "bar",
  37. srcs: ["b/e.py"],
  38. bazel_module: { bp2build_available: false },
  39. }`,
  40. ExpectedBazelTargets: []string{
  41. MakeBazelTarget("py_binary", "foo", AttrNameToString{
  42. "data": `["files/data.txt"]`,
  43. "deps": `[":bar"]`,
  44. "main": `"a.py"`,
  45. "imports": `["."]`,
  46. "srcs": `[
  47. "a.py",
  48. "b/c.py",
  49. "b/d.py",
  50. ]`,
  51. "target_compatible_with": `select({
  52. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  53. "//conditions:default": [],
  54. })`,
  55. }),
  56. },
  57. })
  58. }
  59. func TestPythonBinaryHostPy2(t *testing.T) {
  60. RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
  61. Description: "py2 python_binary_host",
  62. ModuleTypeUnderTest: "python_binary_host",
  63. ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
  64. Blueprint: `python_binary_host {
  65. name: "foo",
  66. srcs: ["a.py"],
  67. version: {
  68. py2: {
  69. enabled: true,
  70. },
  71. py3: {
  72. enabled: false,
  73. },
  74. },
  75. bazel_module: { bp2build_available: true },
  76. }
  77. `,
  78. ExpectedBazelTargets: []string{
  79. MakeBazelTarget("py_binary", "foo", AttrNameToString{
  80. "python_version": `"PY2"`,
  81. "imports": `["."]`,
  82. "srcs": `["a.py"]`,
  83. "target_compatible_with": `select({
  84. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  85. "//conditions:default": [],
  86. })`,
  87. }),
  88. },
  89. })
  90. }
  91. func TestPythonBinaryHostPy3(t *testing.T) {
  92. RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
  93. Description: "py3 python_binary_host",
  94. ModuleTypeUnderTest: "python_binary_host",
  95. ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
  96. Blueprint: `python_binary_host {
  97. name: "foo",
  98. srcs: ["a.py"],
  99. version: {
  100. py2: {
  101. enabled: false,
  102. },
  103. py3: {
  104. enabled: true,
  105. },
  106. },
  107. bazel_module: { bp2build_available: true },
  108. }
  109. `,
  110. ExpectedBazelTargets: []string{
  111. // python_version is PY3 by default.
  112. MakeBazelTarget("py_binary", "foo", AttrNameToString{
  113. "imports": `["."]`,
  114. "srcs": `["a.py"]`,
  115. "target_compatible_with": `select({
  116. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  117. "//conditions:default": [],
  118. })`,
  119. }),
  120. },
  121. })
  122. }
  123. func TestPythonBinaryHostArchVariance(t *testing.T) {
  124. RunBp2BuildTestCaseSimple(t, Bp2buildTestCase{
  125. Description: "test arch variants",
  126. ModuleTypeUnderTest: "python_binary_host",
  127. ModuleTypeUnderTestFactory: python.PythonBinaryHostFactory,
  128. Filesystem: map[string]string{
  129. "dir/arm.py": "",
  130. "dir/x86.py": "",
  131. },
  132. Blueprint: `python_binary_host {
  133. name: "foo-arm",
  134. arch: {
  135. arm: {
  136. srcs: ["arm.py"],
  137. },
  138. x86: {
  139. srcs: ["x86.py"],
  140. },
  141. },
  142. }`,
  143. ExpectedBazelTargets: []string{
  144. MakeBazelTarget("py_binary", "foo-arm", AttrNameToString{
  145. "imports": `["."]`,
  146. "srcs": `select({
  147. "//build/bazel/platforms/arch:arm": ["arm.py"],
  148. "//build/bazel/platforms/arch:x86": ["x86.py"],
  149. "//conditions:default": [],
  150. })`,
  151. "target_compatible_with": `select({
  152. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  153. "//conditions:default": [],
  154. })`,
  155. }),
  156. },
  157. })
  158. }