bazel_paths_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2022 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package android
  15. import (
  16. "path/filepath"
  17. "testing"
  18. "android/soong/bazel"
  19. "github.com/google/blueprint"
  20. "github.com/google/blueprint/pathtools"
  21. )
  22. type TestBazelPathContext struct{}
  23. func (*TestBazelPathContext) Config() Config {
  24. cfg := NullConfig("out", "out/soong")
  25. cfg.BazelContext = MockBazelContext{
  26. OutputBaseDir: "out/bazel",
  27. }
  28. return cfg
  29. }
  30. func (*TestBazelPathContext) AddNinjaFileDeps(...string) {
  31. panic("Unimplemented")
  32. }
  33. func TestPathForBazelOut(t *testing.T) {
  34. ctx := &TestBazelPathContext{}
  35. out := PathForBazelOut(ctx, "foo/bar/baz/boq.txt")
  36. expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt")
  37. if out.String() != expectedPath {
  38. t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
  39. }
  40. expectedRelPath := "foo/bar/baz/boq.txt"
  41. if out.Rel() != expectedRelPath {
  42. t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
  43. }
  44. }
  45. func TestPathForBazelOutRelative(t *testing.T) {
  46. ctx := &TestBazelPathContext{}
  47. out := PathForBazelOutRelative(ctx, "foo/bar", "foo/bar/baz/boq.txt")
  48. expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt")
  49. if out.String() != expectedPath {
  50. t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
  51. }
  52. expectedRelPath := "baz/boq.txt"
  53. if out.Rel() != expectedRelPath {
  54. t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
  55. }
  56. }
  57. func TestPathForBazelOutRelativeUnderBinFolder(t *testing.T) {
  58. ctx := &TestBazelPathContext{}
  59. out := PathForBazelOutRelative(ctx, "foo/bar", "bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt")
  60. expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt")
  61. if out.String() != expectedPath {
  62. t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
  63. }
  64. expectedRelPath := "baz/boq.txt"
  65. if out.Rel() != expectedRelPath {
  66. t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
  67. }
  68. }
  69. func TestPathForBazelOutOutsideOfExecroot(t *testing.T) {
  70. ctx := &TestBazelPathContext{}
  71. out := PathForBazelOut(ctx, "../bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar")
  72. expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar")
  73. if out.String() != expectedPath {
  74. t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
  75. }
  76. expectedRelPath := "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar"
  77. if out.Rel() != expectedRelPath {
  78. t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
  79. }
  80. }
  81. func TestPathForBazelOutRelativeWithParentDirectoryRoot(t *testing.T) {
  82. ctx := &TestBazelPathContext{}
  83. out := PathForBazelOutRelative(ctx, "../bazel_tools", "../bazel_tools/foo/bar/baz.sh")
  84. expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/foo/bar/baz.sh")
  85. if out.String() != expectedPath {
  86. t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
  87. }
  88. expectedRelPath := "foo/bar/baz.sh"
  89. if out.Rel() != expectedRelPath {
  90. t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
  91. }
  92. }
  93. type TestBazelConversionPathContext struct {
  94. TestBazelConversionContext
  95. moduleDir string
  96. cfg Config
  97. }
  98. func (ctx *TestBazelConversionPathContext) AddNinjaFileDeps(...string) {
  99. panic("Unimplemented")
  100. }
  101. func (ctx *TestBazelConversionPathContext) GlobWithDeps(string, []string) ([]string, error) {
  102. panic("Unimplemented")
  103. }
  104. func (ctx *TestBazelConversionPathContext) PropertyErrorf(string, string, ...interface{}) {
  105. panic("Unimplemented")
  106. }
  107. func (ctx *TestBazelConversionPathContext) GetDirectDep(string) (blueprint.Module, blueprint.DependencyTag) {
  108. panic("Unimplemented")
  109. }
  110. func (ctx *TestBazelConversionPathContext) ModuleFromName(string) (blueprint.Module, bool) {
  111. panic("Unimplemented")
  112. }
  113. func (ctx *TestBazelConversionPathContext) AddUnconvertedBp2buildDep(string) {
  114. panic("Unimplemented")
  115. }
  116. func (ctx *TestBazelConversionPathContext) AddMissingBp2buildDep(string) {
  117. panic("Unimplemented")
  118. }
  119. func (ctx *TestBazelConversionPathContext) Module() Module {
  120. panic("Unimplemented")
  121. }
  122. func (ctx *TestBazelConversionPathContext) Config() Config {
  123. return ctx.cfg
  124. }
  125. func (ctx *TestBazelConversionPathContext) ModuleDir() string {
  126. return ctx.moduleDir
  127. }
  128. func TestTransformSubpackagePath(t *testing.T) {
  129. cfg := NullConfig("out", "out/soong")
  130. cfg.fs = pathtools.MockFs(map[string][]byte{
  131. "x/Android.bp": nil,
  132. "x/y/Android.bp": nil,
  133. })
  134. var ctx BazelConversionPathContext = &TestBazelConversionPathContext{
  135. moduleDir: "x",
  136. cfg: cfg,
  137. }
  138. pairs := map[string]string{
  139. "y/a.c": "//x/y:a.c",
  140. "./y/a.c": "//x/y:a.c",
  141. "z/b.c": "z/b.c",
  142. "./z/b.c": "z/b.c",
  143. }
  144. for in, out := range pairs {
  145. actual := transformSubpackagePath(ctx, bazel.Label{Label: in}).Label
  146. if actual != out {
  147. t.Errorf("expected:\n%v\nactual:\n%v", out, actual)
  148. }
  149. }
  150. }