2022-06-04 01:00:11 +02:00
|
|
|
// Copyright 2022 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
2023-08-16 00:06:41 +02:00
|
|
|
"fmt"
|
2022-06-04 01:00:11 +02:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2022-09-23 22:46:38 +02:00
|
|
|
|
|
|
|
"android/soong/bazel"
|
2023-08-11 22:15:12 +02:00
|
|
|
|
2022-09-23 22:46:38 +02:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
"github.com/google/blueprint/pathtools"
|
2022-06-04 01:00:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestBazelPathContext struct{}
|
|
|
|
|
|
|
|
func (*TestBazelPathContext) Config() Config {
|
|
|
|
cfg := NullConfig("out", "out/soong")
|
|
|
|
cfg.BazelContext = MockBazelContext{
|
|
|
|
OutputBaseDir: "out/bazel",
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2022-09-23 22:46:38 +02:00
|
|
|
func (*TestBazelPathContext) AddNinjaFileDeps(...string) {
|
2022-06-04 01:00:11 +02:00
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathForBazelOut(t *testing.T) {
|
|
|
|
ctx := &TestBazelPathContext{}
|
|
|
|
out := PathForBazelOut(ctx, "foo/bar/baz/boq.txt")
|
|
|
|
expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt")
|
|
|
|
if out.String() != expectedPath {
|
|
|
|
t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedRelPath := "foo/bar/baz/boq.txt"
|
|
|
|
if out.Rel() != expectedRelPath {
|
|
|
|
t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathForBazelOutRelative(t *testing.T) {
|
|
|
|
ctx := &TestBazelPathContext{}
|
|
|
|
out := PathForBazelOutRelative(ctx, "foo/bar", "foo/bar/baz/boq.txt")
|
|
|
|
|
|
|
|
expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt")
|
|
|
|
if out.String() != expectedPath {
|
|
|
|
t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedRelPath := "baz/boq.txt"
|
|
|
|
if out.Rel() != expectedRelPath {
|
|
|
|
t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathForBazelOutRelativeUnderBinFolder(t *testing.T) {
|
|
|
|
ctx := &TestBazelPathContext{}
|
|
|
|
out := PathForBazelOutRelative(ctx, "foo/bar", "bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt")
|
|
|
|
|
|
|
|
expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt")
|
|
|
|
if out.String() != expectedPath {
|
|
|
|
t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedRelPath := "baz/boq.txt"
|
|
|
|
if out.Rel() != expectedRelPath {
|
|
|
|
t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathForBazelOutOutsideOfExecroot(t *testing.T) {
|
|
|
|
ctx := &TestBazelPathContext{}
|
|
|
|
out := PathForBazelOut(ctx, "../bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar")
|
|
|
|
|
|
|
|
expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar")
|
|
|
|
if out.String() != expectedPath {
|
|
|
|
t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedRelPath := "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar"
|
|
|
|
if out.Rel() != expectedRelPath {
|
|
|
|
t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathForBazelOutRelativeWithParentDirectoryRoot(t *testing.T) {
|
|
|
|
ctx := &TestBazelPathContext{}
|
|
|
|
out := PathForBazelOutRelative(ctx, "../bazel_tools", "../bazel_tools/foo/bar/baz.sh")
|
|
|
|
|
|
|
|
expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/foo/bar/baz.sh")
|
|
|
|
if out.String() != expectedPath {
|
|
|
|
t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedRelPath := "foo/bar/baz.sh"
|
|
|
|
if out.Rel() != expectedRelPath {
|
|
|
|
t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 22:46:38 +02:00
|
|
|
|
|
|
|
type TestBazelConversionPathContext struct {
|
|
|
|
TestBazelConversionContext
|
2023-08-16 00:06:41 +02:00
|
|
|
moduleDir string
|
|
|
|
cfg Config
|
|
|
|
mockGlobResults *[]string
|
2022-09-23 22:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) AddNinjaFileDeps(...string) {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) GlobWithDeps(string, []string) ([]string, error) {
|
2023-08-16 00:06:41 +02:00
|
|
|
if ctx.mockGlobResults == nil {
|
|
|
|
return []string{}, fmt.Errorf("Set mock glob results first")
|
|
|
|
}
|
|
|
|
return *ctx.mockGlobResults, nil
|
2022-09-23 22:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) PropertyErrorf(string, string, ...interface{}) {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) GetDirectDep(string) (blueprint.Module, blueprint.DependencyTag) {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) ModuleFromName(string) (blueprint.Module, bool) {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) AddUnconvertedBp2buildDep(string) {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) AddMissingBp2buildDep(string) {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) Module() Module {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) Config() Config {
|
|
|
|
return ctx.cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) ModuleDir() string {
|
|
|
|
return ctx.moduleDir
|
|
|
|
}
|
|
|
|
|
2023-08-11 22:15:12 +02:00
|
|
|
func (ctx *TestBazelConversionPathContext) ModuleName() string {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *TestBazelConversionPathContext) ModuleType() string {
|
|
|
|
panic("Unimplemented")
|
|
|
|
}
|
|
|
|
|
2022-09-23 22:46:38 +02:00
|
|
|
func TestTransformSubpackagePath(t *testing.T) {
|
|
|
|
cfg := NullConfig("out", "out/soong")
|
|
|
|
cfg.fs = pathtools.MockFs(map[string][]byte{
|
|
|
|
"x/Android.bp": nil,
|
|
|
|
"x/y/Android.bp": nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
var ctx BazelConversionPathContext = &TestBazelConversionPathContext{
|
|
|
|
moduleDir: "x",
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
pairs := map[string]string{
|
|
|
|
"y/a.c": "//x/y:a.c",
|
|
|
|
"./y/a.c": "//x/y:a.c",
|
|
|
|
"z/b.c": "z/b.c",
|
|
|
|
"./z/b.c": "z/b.c",
|
|
|
|
}
|
|
|
|
for in, out := range pairs {
|
2023-06-21 03:50:33 +02:00
|
|
|
actual := transformSubpackagePath(ctx.Config(), ctx.ModuleDir(), bazel.Label{Label: in}).Label
|
2022-09-23 22:46:38 +02:00
|
|
|
if actual != out {
|
|
|
|
t.Errorf("expected:\n%v\nactual:\n%v", out, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 00:06:41 +02:00
|
|
|
|
|
|
|
// Check that the files in a specific directory are returned with labels that respect package boundaries
|
|
|
|
// Since the test uses a mock for GlobWithDeps, the params passed to BazelLabelForSrcPatternExcludes are no-ops
|
|
|
|
func TestBazelLabelForSrcPatternExcludes(t *testing.T) {
|
|
|
|
cfg := NullConfig("out", "out/soong")
|
|
|
|
cfg.fs = pathtools.MockFs(map[string][]byte{
|
|
|
|
"x/Android.bp": nil,
|
|
|
|
"x/y/Android.bp": nil,
|
|
|
|
// .proto files
|
|
|
|
"foo.proto": nil,
|
|
|
|
"x/bar.proto": nil,
|
|
|
|
"x/baz.proto": nil,
|
|
|
|
"x/y/qux.proto": nil,
|
|
|
|
})
|
|
|
|
|
|
|
|
var ctx BazelConversionPathContext = &TestBazelConversionPathContext{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root dir
|
|
|
|
ctx.(*TestBazelConversionPathContext).mockGlobResults = &[]string{"foo.proto", "x/bar.proto", "x/baz.proto", "x/y/qux.proto"}
|
|
|
|
actualLabelsFromRoot := BazelLabelForSrcPatternExcludes(ctx, ".", "**/*.proto", []string{})
|
|
|
|
expectedLabelsAsString := []string{"foo.proto", "//x:bar.proto", "//x:baz.proto", "//x/y:qux.proto"}
|
|
|
|
for i, actual := range actualLabelsFromRoot.Includes {
|
|
|
|
AssertStringEquals(t, "Error in finding src labels relative to root directory", expectedLabelsAsString[i], actual.Label)
|
|
|
|
}
|
|
|
|
|
|
|
|
// x dir
|
|
|
|
ctx.(*TestBazelConversionPathContext).mockGlobResults = &[]string{"x/bar.proto", "x/baz.proto", "x/y/qux.proto"}
|
|
|
|
actualLabelsFromRoot = BazelLabelForSrcPatternExcludes(ctx, "x", "**/*.proto", []string{})
|
|
|
|
expectedLabelsAsString = []string{"bar.proto", "baz.proto", "//x/y:qux.proto"}
|
|
|
|
for i, actual := range actualLabelsFromRoot.Includes {
|
|
|
|
AssertStringEquals(t, "Error in finding src labels relative to x directory", expectedLabelsAsString[i], actual.Label)
|
|
|
|
}
|
|
|
|
|
|
|
|
// y dir
|
|
|
|
ctx.(*TestBazelConversionPathContext).mockGlobResults = &[]string{"x/y/qux.proto"}
|
|
|
|
actualLabelsFromRoot = BazelLabelForSrcPatternExcludes(ctx, "x/y", "**/*.proto", []string{})
|
|
|
|
expectedLabelsAsString = []string{"qux.proto"}
|
|
|
|
for i, actual := range actualLabelsFromRoot.Includes {
|
|
|
|
AssertStringEquals(t, "Error in finding src labels relative to x/y directory", expectedLabelsAsString[i], actual.Label)
|
|
|
|
}
|
|
|
|
}
|