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