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 15package android 16 17import ( 18 "path/filepath" 19 "testing" 20 21 "android/soong/bazel" 22 "github.com/google/blueprint" 23 "github.com/google/blueprint/pathtools" 24) 25 26type TestBazelPathContext struct{} 27 28func (*TestBazelPathContext) Config() Config { 29 cfg := NullConfig("out", "out/soong") 30 cfg.BazelContext = MockBazelContext{ 31 OutputBaseDir: "out/bazel", 32 } 33 return cfg 34} 35 36func (*TestBazelPathContext) AddNinjaFileDeps(...string) { 37 panic("Unimplemented") 38} 39 40func 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 54func 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 69func 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 84func 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 99func 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} 113 114type TestBazelConversionPathContext struct { 115 TestBazelConversionContext 116 moduleDir string 117 cfg Config 118} 119 120func (ctx *TestBazelConversionPathContext) AddNinjaFileDeps(...string) { 121 panic("Unimplemented") 122} 123 124func (ctx *TestBazelConversionPathContext) GlobWithDeps(string, []string) ([]string, error) { 125 panic("Unimplemented") 126} 127 128func (ctx *TestBazelConversionPathContext) PropertyErrorf(string, string, ...interface{}) { 129 panic("Unimplemented") 130} 131 132func (ctx *TestBazelConversionPathContext) GetDirectDep(string) (blueprint.Module, blueprint.DependencyTag) { 133 panic("Unimplemented") 134} 135 136func (ctx *TestBazelConversionPathContext) ModuleFromName(string) (blueprint.Module, bool) { 137 panic("Unimplemented") 138} 139 140func (ctx *TestBazelConversionPathContext) AddUnconvertedBp2buildDep(string) { 141 panic("Unimplemented") 142} 143 144func (ctx *TestBazelConversionPathContext) AddMissingBp2buildDep(string) { 145 panic("Unimplemented") 146} 147 148func (ctx *TestBazelConversionPathContext) Module() Module { 149 panic("Unimplemented") 150} 151 152func (ctx *TestBazelConversionPathContext) Config() Config { 153 return ctx.cfg 154} 155 156func (ctx *TestBazelConversionPathContext) ModuleDir() string { 157 return ctx.moduleDir 158} 159 160func 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} 184