blob: 75b77a36c2e0570bf4843928345779b456c06980 [file] [log] [blame]
Cole Faust9a06d252022-06-03 16:00:11 -07001// 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"
Usta Shresthad5580312022-09-23 16:46:38 -040020
21 "android/soong/bazel"
Liz Kammerc86e0942023-08-11 16:15:12 -040022
Usta Shresthad5580312022-09-23 16:46:38 -040023 "github.com/google/blueprint"
24 "github.com/google/blueprint/pathtools"
Cole Faust9a06d252022-06-03 16:00:11 -070025)
26
27type TestBazelPathContext struct{}
28
29func (*TestBazelPathContext) Config() Config {
30 cfg := NullConfig("out", "out/soong")
31 cfg.BazelContext = MockBazelContext{
32 OutputBaseDir: "out/bazel",
33 }
34 return cfg
35}
36
Usta Shresthad5580312022-09-23 16:46:38 -040037func (*TestBazelPathContext) AddNinjaFileDeps(...string) {
Cole Faust9a06d252022-06-03 16:00:11 -070038 panic("Unimplemented")
39}
40
41func 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
55func 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
70func 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
85func 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
100func 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 Shresthad5580312022-09-23 16:46:38 -0400114
115type TestBazelConversionPathContext struct {
116 TestBazelConversionContext
117 moduleDir string
118 cfg Config
119}
120
121func (ctx *TestBazelConversionPathContext) AddNinjaFileDeps(...string) {
122 panic("Unimplemented")
123}
124
125func (ctx *TestBazelConversionPathContext) GlobWithDeps(string, []string) ([]string, error) {
126 panic("Unimplemented")
127}
128
129func (ctx *TestBazelConversionPathContext) PropertyErrorf(string, string, ...interface{}) {
130 panic("Unimplemented")
131}
132
133func (ctx *TestBazelConversionPathContext) GetDirectDep(string) (blueprint.Module, blueprint.DependencyTag) {
134 panic("Unimplemented")
135}
136
137func (ctx *TestBazelConversionPathContext) ModuleFromName(string) (blueprint.Module, bool) {
138 panic("Unimplemented")
139}
140
141func (ctx *TestBazelConversionPathContext) AddUnconvertedBp2buildDep(string) {
142 panic("Unimplemented")
143}
144
145func (ctx *TestBazelConversionPathContext) AddMissingBp2buildDep(string) {
146 panic("Unimplemented")
147}
148
149func (ctx *TestBazelConversionPathContext) Module() Module {
150 panic("Unimplemented")
151}
152
153func (ctx *TestBazelConversionPathContext) Config() Config {
154 return ctx.cfg
155}
156
157func (ctx *TestBazelConversionPathContext) ModuleDir() string {
158 return ctx.moduleDir
159}
160
Liz Kammerc86e0942023-08-11 16:15:12 -0400161func (ctx *TestBazelConversionPathContext) ModuleName() string {
162 panic("Unimplemented")
163}
164
165func (ctx *TestBazelConversionPathContext) ModuleType() string {
166 panic("Unimplemented")
167}
168
Usta Shresthad5580312022-09-23 16:46:38 -0400169func 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 Das0a8a2752023-06-21 01:50:33 +0000187 actual := transformSubpackagePath(ctx.Config(), ctx.ModuleDir(), bazel.Label{Label: in}).Label
Usta Shresthad5580312022-09-23 16:46:38 -0400188 if actual != out {
189 t.Errorf("expected:\n%v\nactual:\n%v", out, actual)
190 }
191 }
192}