| Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 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 |  | 
|  | 15 | package android | 
|  | 16 |  | 
|  | 17 | import ( | 
| Spandan Das | 0301066 | 2023-08-15 22:06:41 +0000 | [diff] [blame] | 18 | "fmt" | 
| Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 19 | "path/filepath" | 
|  | 20 | "testing" | 
| Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 21 |  | 
|  | 22 | "android/soong/bazel" | 
| Liz Kammer | c86e094 | 2023-08-11 16:15:12 -0400 | [diff] [blame] | 23 |  | 
| Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 24 | "github.com/google/blueprint" | 
|  | 25 | "github.com/google/blueprint/pathtools" | 
| Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 26 | ) | 
|  | 27 |  | 
|  | 28 | type TestBazelPathContext struct{} | 
|  | 29 |  | 
|  | 30 | func (*TestBazelPathContext) Config() Config { | 
|  | 31 | cfg := NullConfig("out", "out/soong") | 
|  | 32 | cfg.BazelContext = MockBazelContext{ | 
|  | 33 | OutputBaseDir: "out/bazel", | 
|  | 34 | } | 
|  | 35 | return cfg | 
|  | 36 | } | 
|  | 37 |  | 
| Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 38 | func (*TestBazelPathContext) AddNinjaFileDeps(...string) { | 
| Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 39 | panic("Unimplemented") | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | func TestPathForBazelOut(t *testing.T) { | 
|  | 43 | ctx := &TestBazelPathContext{} | 
|  | 44 | out := PathForBazelOut(ctx, "foo/bar/baz/boq.txt") | 
|  | 45 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt") | 
|  | 46 | if out.String() != expectedPath { | 
|  | 47 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | expectedRelPath := "foo/bar/baz/boq.txt" | 
|  | 51 | if out.Rel() != expectedRelPath { | 
|  | 52 | t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) | 
|  | 53 | } | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | func TestPathForBazelOutRelative(t *testing.T) { | 
|  | 57 | ctx := &TestBazelPathContext{} | 
|  | 58 | out := PathForBazelOutRelative(ctx, "foo/bar", "foo/bar/baz/boq.txt") | 
|  | 59 |  | 
|  | 60 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt") | 
|  | 61 | if out.String() != expectedPath { | 
|  | 62 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | expectedRelPath := "baz/boq.txt" | 
|  | 66 | if out.Rel() != expectedRelPath { | 
|  | 67 | t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) | 
|  | 68 | } | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | func TestPathForBazelOutRelativeUnderBinFolder(t *testing.T) { | 
|  | 72 | ctx := &TestBazelPathContext{} | 
|  | 73 | out := PathForBazelOutRelative(ctx, "foo/bar", "bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt") | 
|  | 74 |  | 
|  | 75 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt") | 
|  | 76 | if out.String() != expectedPath { | 
|  | 77 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | expectedRelPath := "baz/boq.txt" | 
|  | 81 | if out.Rel() != expectedRelPath { | 
|  | 82 | t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | func TestPathForBazelOutOutsideOfExecroot(t *testing.T) { | 
|  | 87 | ctx := &TestBazelPathContext{} | 
|  | 88 | out := PathForBazelOut(ctx, "../bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar") | 
|  | 89 |  | 
|  | 90 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar") | 
|  | 91 | if out.String() != expectedPath { | 
|  | 92 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | expectedRelPath := "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar" | 
|  | 96 | if out.Rel() != expectedRelPath { | 
|  | 97 | t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) | 
|  | 98 | } | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | func TestPathForBazelOutRelativeWithParentDirectoryRoot(t *testing.T) { | 
|  | 102 | ctx := &TestBazelPathContext{} | 
|  | 103 | out := PathForBazelOutRelative(ctx, "../bazel_tools", "../bazel_tools/foo/bar/baz.sh") | 
|  | 104 |  | 
|  | 105 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/foo/bar/baz.sh") | 
|  | 106 | if out.String() != expectedPath { | 
|  | 107 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | expectedRelPath := "foo/bar/baz.sh" | 
|  | 111 | if out.Rel() != expectedRelPath { | 
|  | 112 | t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) | 
|  | 113 | } | 
|  | 114 | } | 
| Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 115 |  | 
|  | 116 | type TestBazelConversionPathContext struct { | 
|  | 117 | TestBazelConversionContext | 
| Spandan Das | 0301066 | 2023-08-15 22:06:41 +0000 | [diff] [blame] | 118 | moduleDir       string | 
|  | 119 | cfg             Config | 
|  | 120 | mockGlobResults *[]string | 
| Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
|  | 123 | func (ctx *TestBazelConversionPathContext) AddNinjaFileDeps(...string) { | 
|  | 124 | panic("Unimplemented") | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | func (ctx *TestBazelConversionPathContext) GlobWithDeps(string, []string) ([]string, error) { | 
| Spandan Das | 0301066 | 2023-08-15 22:06:41 +0000 | [diff] [blame] | 128 | if ctx.mockGlobResults == nil { | 
|  | 129 | return []string{}, fmt.Errorf("Set mock glob results first") | 
|  | 130 | } | 
|  | 131 | return *ctx.mockGlobResults, nil | 
| Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
|  | 134 | func (ctx *TestBazelConversionPathContext) PropertyErrorf(string, string, ...interface{}) { | 
|  | 135 | panic("Unimplemented") | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | func (ctx *TestBazelConversionPathContext) GetDirectDep(string) (blueprint.Module, blueprint.DependencyTag) { | 
|  | 139 | panic("Unimplemented") | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | func (ctx *TestBazelConversionPathContext) ModuleFromName(string) (blueprint.Module, bool) { | 
|  | 143 | panic("Unimplemented") | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | func (ctx *TestBazelConversionPathContext) AddUnconvertedBp2buildDep(string) { | 
|  | 147 | panic("Unimplemented") | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | func (ctx *TestBazelConversionPathContext) AddMissingBp2buildDep(string) { | 
|  | 151 | panic("Unimplemented") | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | func (ctx *TestBazelConversionPathContext) Module() Module { | 
|  | 155 | panic("Unimplemented") | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | func (ctx *TestBazelConversionPathContext) Config() Config { | 
|  | 159 | return ctx.cfg | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | func (ctx *TestBazelConversionPathContext) ModuleDir() string { | 
|  | 163 | return ctx.moduleDir | 
|  | 164 | } | 
|  | 165 |  | 
| Liz Kammer | c86e094 | 2023-08-11 16:15:12 -0400 | [diff] [blame] | 166 | func (ctx *TestBazelConversionPathContext) ModuleName() string { | 
|  | 167 | panic("Unimplemented") | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | func (ctx *TestBazelConversionPathContext) ModuleType() string { | 
|  | 171 | panic("Unimplemented") | 
|  | 172 | } | 
|  | 173 |  | 
| Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 174 | func TestTransformSubpackagePath(t *testing.T) { | 
|  | 175 | cfg := NullConfig("out", "out/soong") | 
|  | 176 | cfg.fs = pathtools.MockFs(map[string][]byte{ | 
|  | 177 | "x/Android.bp":   nil, | 
|  | 178 | "x/y/Android.bp": nil, | 
|  | 179 | }) | 
|  | 180 |  | 
|  | 181 | var ctx BazelConversionPathContext = &TestBazelConversionPathContext{ | 
|  | 182 | moduleDir: "x", | 
|  | 183 | cfg:       cfg, | 
|  | 184 | } | 
|  | 185 | pairs := map[string]string{ | 
|  | 186 | "y/a.c":   "//x/y:a.c", | 
|  | 187 | "./y/a.c": "//x/y:a.c", | 
|  | 188 | "z/b.c":   "z/b.c", | 
|  | 189 | "./z/b.c": "z/b.c", | 
|  | 190 | } | 
|  | 191 | for in, out := range pairs { | 
| Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 192 | actual := transformSubpackagePath(ctx.Config(), ctx.ModuleDir(), bazel.Label{Label: in}).Label | 
| Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 193 | if actual != out { | 
|  | 194 | t.Errorf("expected:\n%v\nactual:\n%v", out, actual) | 
|  | 195 | } | 
|  | 196 | } | 
|  | 197 | } | 
| Spandan Das | 0301066 | 2023-08-15 22:06:41 +0000 | [diff] [blame] | 198 |  | 
|  | 199 | // Check that the files in a specific directory are returned with labels that respect package boundaries | 
|  | 200 | // Since the test uses a mock for GlobWithDeps, the params passed to BazelLabelForSrcPatternExcludes are no-ops | 
|  | 201 | func TestBazelLabelForSrcPatternExcludes(t *testing.T) { | 
|  | 202 | cfg := NullConfig("out", "out/soong") | 
|  | 203 | cfg.fs = pathtools.MockFs(map[string][]byte{ | 
|  | 204 | "x/Android.bp":   nil, | 
|  | 205 | "x/y/Android.bp": nil, | 
|  | 206 | // .proto files | 
|  | 207 | "foo.proto":     nil, | 
|  | 208 | "x/bar.proto":   nil, | 
|  | 209 | "x/baz.proto":   nil, | 
|  | 210 | "x/y/qux.proto": nil, | 
|  | 211 | }) | 
|  | 212 |  | 
|  | 213 | var ctx BazelConversionPathContext = &TestBazelConversionPathContext{ | 
|  | 214 | cfg: cfg, | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | // Root dir | 
|  | 218 | ctx.(*TestBazelConversionPathContext).mockGlobResults = &[]string{"foo.proto", "x/bar.proto", "x/baz.proto", "x/y/qux.proto"} | 
|  | 219 | actualLabelsFromRoot := BazelLabelForSrcPatternExcludes(ctx, ".", "**/*.proto", []string{}) | 
|  | 220 | expectedLabelsAsString := []string{"foo.proto", "//x:bar.proto", "//x:baz.proto", "//x/y:qux.proto"} | 
|  | 221 | for i, actual := range actualLabelsFromRoot.Includes { | 
|  | 222 | AssertStringEquals(t, "Error in finding src labels relative to root directory", expectedLabelsAsString[i], actual.Label) | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | // x dir | 
|  | 226 | ctx.(*TestBazelConversionPathContext).mockGlobResults = &[]string{"x/bar.proto", "x/baz.proto", "x/y/qux.proto"} | 
|  | 227 | actualLabelsFromRoot = BazelLabelForSrcPatternExcludes(ctx, "x", "**/*.proto", []string{}) | 
|  | 228 | expectedLabelsAsString = []string{"bar.proto", "baz.proto", "//x/y:qux.proto"} | 
|  | 229 | for i, actual := range actualLabelsFromRoot.Includes { | 
|  | 230 | AssertStringEquals(t, "Error in finding src labels relative to x directory", expectedLabelsAsString[i], actual.Label) | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | // y dir | 
|  | 234 | ctx.(*TestBazelConversionPathContext).mockGlobResults = &[]string{"x/y/qux.proto"} | 
|  | 235 | actualLabelsFromRoot = BazelLabelForSrcPatternExcludes(ctx, "x/y", "**/*.proto", []string{}) | 
|  | 236 | expectedLabelsAsString = []string{"qux.proto"} | 
|  | 237 | for i, actual := range actualLabelsFromRoot.Includes { | 
|  | 238 | AssertStringEquals(t, "Error in finding src labels relative to x/y directory", expectedLabelsAsString[i], actual.Label) | 
|  | 239 | } | 
|  | 240 | } |