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 ( |
| 18 | "path/filepath" |
| 19 | "testing" |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 20 | |
| 21 | "android/soong/bazel" |
Liz Kammer | c86e094 | 2023-08-11 16:15:12 -0400 | [diff] [blame^] | 22 | |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 23 | "github.com/google/blueprint" |
| 24 | "github.com/google/blueprint/pathtools" |
Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | type TestBazelPathContext struct{} |
| 28 | |
| 29 | func (*TestBazelPathContext) Config() Config { |
| 30 | cfg := NullConfig("out", "out/soong") |
| 31 | cfg.BazelContext = MockBazelContext{ |
| 32 | OutputBaseDir: "out/bazel", |
| 33 | } |
| 34 | return cfg |
| 35 | } |
| 36 | |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 37 | func (*TestBazelPathContext) AddNinjaFileDeps(...string) { |
Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 38 | panic("Unimplemented") |
| 39 | } |
| 40 | |
| 41 | func TestPathForBazelOut(t *testing.T) { |
| 42 | ctx := &TestBazelPathContext{} |
| 43 | out := PathForBazelOut(ctx, "foo/bar/baz/boq.txt") |
| 44 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt") |
| 45 | if out.String() != expectedPath { |
| 46 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) |
| 47 | } |
| 48 | |
| 49 | expectedRelPath := "foo/bar/baz/boq.txt" |
| 50 | if out.Rel() != expectedRelPath { |
| 51 | t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestPathForBazelOutRelative(t *testing.T) { |
| 56 | ctx := &TestBazelPathContext{} |
| 57 | out := PathForBazelOutRelative(ctx, "foo/bar", "foo/bar/baz/boq.txt") |
| 58 | |
| 59 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt") |
| 60 | if out.String() != expectedPath { |
| 61 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) |
| 62 | } |
| 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 | |
| 70 | func TestPathForBazelOutRelativeUnderBinFolder(t *testing.T) { |
| 71 | ctx := &TestBazelPathContext{} |
| 72 | out := PathForBazelOutRelative(ctx, "foo/bar", "bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt") |
| 73 | |
| 74 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt") |
| 75 | if out.String() != expectedPath { |
| 76 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) |
| 77 | } |
| 78 | |
| 79 | expectedRelPath := "baz/boq.txt" |
| 80 | if out.Rel() != expectedRelPath { |
| 81 | t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestPathForBazelOutOutsideOfExecroot(t *testing.T) { |
| 86 | ctx := &TestBazelPathContext{} |
| 87 | out := PathForBazelOut(ctx, "../bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar") |
| 88 | |
| 89 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar") |
| 90 | if out.String() != expectedPath { |
| 91 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) |
| 92 | } |
| 93 | |
| 94 | expectedRelPath := "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar" |
| 95 | if out.Rel() != expectedRelPath { |
| 96 | t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestPathForBazelOutRelativeWithParentDirectoryRoot(t *testing.T) { |
| 101 | ctx := &TestBazelPathContext{} |
| 102 | out := PathForBazelOutRelative(ctx, "../bazel_tools", "../bazel_tools/foo/bar/baz.sh") |
| 103 | |
| 104 | expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/foo/bar/baz.sh") |
| 105 | if out.String() != expectedPath { |
| 106 | t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String()) |
| 107 | } |
| 108 | |
| 109 | expectedRelPath := "foo/bar/baz.sh" |
| 110 | if out.Rel() != expectedRelPath { |
| 111 | t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) |
| 112 | } |
| 113 | } |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 114 | |
| 115 | type TestBazelConversionPathContext struct { |
| 116 | TestBazelConversionContext |
| 117 | moduleDir string |
| 118 | cfg Config |
| 119 | } |
| 120 | |
| 121 | func (ctx *TestBazelConversionPathContext) AddNinjaFileDeps(...string) { |
| 122 | panic("Unimplemented") |
| 123 | } |
| 124 | |
| 125 | func (ctx *TestBazelConversionPathContext) GlobWithDeps(string, []string) ([]string, error) { |
| 126 | panic("Unimplemented") |
| 127 | } |
| 128 | |
| 129 | func (ctx *TestBazelConversionPathContext) PropertyErrorf(string, string, ...interface{}) { |
| 130 | panic("Unimplemented") |
| 131 | } |
| 132 | |
| 133 | func (ctx *TestBazelConversionPathContext) GetDirectDep(string) (blueprint.Module, blueprint.DependencyTag) { |
| 134 | panic("Unimplemented") |
| 135 | } |
| 136 | |
| 137 | func (ctx *TestBazelConversionPathContext) ModuleFromName(string) (blueprint.Module, bool) { |
| 138 | panic("Unimplemented") |
| 139 | } |
| 140 | |
| 141 | func (ctx *TestBazelConversionPathContext) AddUnconvertedBp2buildDep(string) { |
| 142 | panic("Unimplemented") |
| 143 | } |
| 144 | |
| 145 | func (ctx *TestBazelConversionPathContext) AddMissingBp2buildDep(string) { |
| 146 | panic("Unimplemented") |
| 147 | } |
| 148 | |
| 149 | func (ctx *TestBazelConversionPathContext) Module() Module { |
| 150 | panic("Unimplemented") |
| 151 | } |
| 152 | |
| 153 | func (ctx *TestBazelConversionPathContext) Config() Config { |
| 154 | return ctx.cfg |
| 155 | } |
| 156 | |
| 157 | func (ctx *TestBazelConversionPathContext) ModuleDir() string { |
| 158 | return ctx.moduleDir |
| 159 | } |
| 160 | |
Liz Kammer | c86e094 | 2023-08-11 16:15:12 -0400 | [diff] [blame^] | 161 | func (ctx *TestBazelConversionPathContext) ModuleName() string { |
| 162 | panic("Unimplemented") |
| 163 | } |
| 164 | |
| 165 | func (ctx *TestBazelConversionPathContext) ModuleType() string { |
| 166 | panic("Unimplemented") |
| 167 | } |
| 168 | |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 169 | func TestTransformSubpackagePath(t *testing.T) { |
| 170 | cfg := NullConfig("out", "out/soong") |
| 171 | cfg.fs = pathtools.MockFs(map[string][]byte{ |
| 172 | "x/Android.bp": nil, |
| 173 | "x/y/Android.bp": nil, |
| 174 | }) |
| 175 | |
| 176 | var ctx BazelConversionPathContext = &TestBazelConversionPathContext{ |
| 177 | moduleDir: "x", |
| 178 | cfg: cfg, |
| 179 | } |
| 180 | pairs := map[string]string{ |
| 181 | "y/a.c": "//x/y:a.c", |
| 182 | "./y/a.c": "//x/y:a.c", |
| 183 | "z/b.c": "z/b.c", |
| 184 | "./z/b.c": "z/b.c", |
| 185 | } |
| 186 | for in, out := range pairs { |
Spandan Das | 0a8a275 | 2023-06-21 01:50:33 +0000 | [diff] [blame] | 187 | actual := transformSubpackagePath(ctx.Config(), ctx.ModuleDir(), bazel.Label{Label: in}).Label |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 188 | if actual != out { |
| 189 | t.Errorf("expected:\n%v\nactual:\n%v", out, actual) |
| 190 | } |
| 191 | } |
| 192 | } |