blob: bed719cbb5b912a1b1f471ecdf1a51f296108771 [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 (
Spandan Das03010662023-08-15 22:06:41 +000018 "fmt"
Cole Faust9a06d252022-06-03 16:00:11 -070019 "path/filepath"
20 "testing"
Usta Shresthad5580312022-09-23 16:46:38 -040021
22 "android/soong/bazel"
Liz Kammerc86e0942023-08-11 16:15:12 -040023
Usta Shresthad5580312022-09-23 16:46:38 -040024 "github.com/google/blueprint"
25 "github.com/google/blueprint/pathtools"
Cole Faust9a06d252022-06-03 16:00:11 -070026)
27
28type TestBazelPathContext struct{}
29
30func (*TestBazelPathContext) Config() Config {
31 cfg := NullConfig("out", "out/soong")
32 cfg.BazelContext = MockBazelContext{
33 OutputBaseDir: "out/bazel",
34 }
35 return cfg
36}
37
Usta Shresthad5580312022-09-23 16:46:38 -040038func (*TestBazelPathContext) AddNinjaFileDeps(...string) {
Cole Faust9a06d252022-06-03 16:00:11 -070039 panic("Unimplemented")
40}
41
42func TestPathForBazelOut(t *testing.T) {
43 ctx := &TestBazelPathContext{}
44 out := PathForBazelOut(ctx, "foo/bar/baz/boq.txt")
45 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt")
46 if out.String() != expectedPath {
47 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
48 }
49
50 expectedRelPath := "foo/bar/baz/boq.txt"
51 if out.Rel() != expectedRelPath {
52 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
53 }
54}
55
56func TestPathForBazelOutRelative(t *testing.T) {
57 ctx := &TestBazelPathContext{}
58 out := PathForBazelOutRelative(ctx, "foo/bar", "foo/bar/baz/boq.txt")
59
60 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt")
61 if out.String() != expectedPath {
62 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
63 }
64
65 expectedRelPath := "baz/boq.txt"
66 if out.Rel() != expectedRelPath {
67 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
68 }
69}
70
71func TestPathForBazelOutRelativeUnderBinFolder(t *testing.T) {
72 ctx := &TestBazelPathContext{}
73 out := PathForBazelOutRelative(ctx, "foo/bar", "bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt")
74
75 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt")
76 if out.String() != expectedPath {
77 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
78 }
79
80 expectedRelPath := "baz/boq.txt"
81 if out.Rel() != expectedRelPath {
82 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
83 }
84}
85
86func TestPathForBazelOutOutsideOfExecroot(t *testing.T) {
87 ctx := &TestBazelPathContext{}
88 out := PathForBazelOut(ctx, "../bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar")
89
90 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar")
91 if out.String() != expectedPath {
92 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
93 }
94
95 expectedRelPath := "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar"
96 if out.Rel() != expectedRelPath {
97 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
98 }
99}
100
101func TestPathForBazelOutRelativeWithParentDirectoryRoot(t *testing.T) {
102 ctx := &TestBazelPathContext{}
103 out := PathForBazelOutRelative(ctx, "../bazel_tools", "../bazel_tools/foo/bar/baz.sh")
104
105 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/foo/bar/baz.sh")
106 if out.String() != expectedPath {
107 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
108 }
109
110 expectedRelPath := "foo/bar/baz.sh"
111 if out.Rel() != expectedRelPath {
112 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
113 }
114}
Usta Shresthad5580312022-09-23 16:46:38 -0400115
116type TestBazelConversionPathContext struct {
117 TestBazelConversionContext
Spandan Das03010662023-08-15 22:06:41 +0000118 moduleDir string
119 cfg Config
120 mockGlobResults *[]string
Usta Shresthad5580312022-09-23 16:46:38 -0400121}
122
123func (ctx *TestBazelConversionPathContext) AddNinjaFileDeps(...string) {
124 panic("Unimplemented")
125}
126
127func (ctx *TestBazelConversionPathContext) GlobWithDeps(string, []string) ([]string, error) {
Spandan Das03010662023-08-15 22:06:41 +0000128 if ctx.mockGlobResults == nil {
129 return []string{}, fmt.Errorf("Set mock glob results first")
130 }
131 return *ctx.mockGlobResults, nil
Usta Shresthad5580312022-09-23 16:46:38 -0400132}
133
134func (ctx *TestBazelConversionPathContext) PropertyErrorf(string, string, ...interface{}) {
135 panic("Unimplemented")
136}
137
138func (ctx *TestBazelConversionPathContext) GetDirectDep(string) (blueprint.Module, blueprint.DependencyTag) {
139 panic("Unimplemented")
140}
141
142func (ctx *TestBazelConversionPathContext) ModuleFromName(string) (blueprint.Module, bool) {
143 panic("Unimplemented")
144}
145
146func (ctx *TestBazelConversionPathContext) AddUnconvertedBp2buildDep(string) {
147 panic("Unimplemented")
148}
149
150func (ctx *TestBazelConversionPathContext) AddMissingBp2buildDep(string) {
151 panic("Unimplemented")
152}
153
154func (ctx *TestBazelConversionPathContext) Module() Module {
155 panic("Unimplemented")
156}
157
158func (ctx *TestBazelConversionPathContext) Config() Config {
159 return ctx.cfg
160}
161
162func (ctx *TestBazelConversionPathContext) ModuleDir() string {
163 return ctx.moduleDir
164}
165
Liz Kammerc86e0942023-08-11 16:15:12 -0400166func (ctx *TestBazelConversionPathContext) ModuleName() string {
167 panic("Unimplemented")
168}
169
170func (ctx *TestBazelConversionPathContext) ModuleType() string {
171 panic("Unimplemented")
172}
173
Usta Shresthad5580312022-09-23 16:46:38 -0400174func TestTransformSubpackagePath(t *testing.T) {
175 cfg := NullConfig("out", "out/soong")
176 cfg.fs = pathtools.MockFs(map[string][]byte{
177 "x/Android.bp": nil,
178 "x/y/Android.bp": nil,
179 })
180
181 var ctx BazelConversionPathContext = &TestBazelConversionPathContext{
182 moduleDir: "x",
183 cfg: cfg,
184 }
185 pairs := map[string]string{
186 "y/a.c": "//x/y:a.c",
187 "./y/a.c": "//x/y:a.c",
188 "z/b.c": "z/b.c",
189 "./z/b.c": "z/b.c",
190 }
191 for in, out := range pairs {
Spandan Das0a8a2752023-06-21 01:50:33 +0000192 actual := transformSubpackagePath(ctx.Config(), ctx.ModuleDir(), bazel.Label{Label: in}).Label
Usta Shresthad5580312022-09-23 16:46:38 -0400193 if actual != out {
194 t.Errorf("expected:\n%v\nactual:\n%v", out, actual)
195 }
196 }
197}
Spandan Das03010662023-08-15 22:06:41 +0000198
199// Check that the files in a specific directory are returned with labels that respect package boundaries
200// Since the test uses a mock for GlobWithDeps, the params passed to BazelLabelForSrcPatternExcludes are no-ops
201func TestBazelLabelForSrcPatternExcludes(t *testing.T) {
202 cfg := NullConfig("out", "out/soong")
203 cfg.fs = pathtools.MockFs(map[string][]byte{
204 "x/Android.bp": nil,
205 "x/y/Android.bp": nil,
206 // .proto files
207 "foo.proto": nil,
208 "x/bar.proto": nil,
209 "x/baz.proto": nil,
210 "x/y/qux.proto": nil,
211 })
212
213 var ctx BazelConversionPathContext = &TestBazelConversionPathContext{
214 cfg: cfg,
215 }
216
217 // Root dir
218 ctx.(*TestBazelConversionPathContext).mockGlobResults = &[]string{"foo.proto", "x/bar.proto", "x/baz.proto", "x/y/qux.proto"}
219 actualLabelsFromRoot := BazelLabelForSrcPatternExcludes(ctx, ".", "**/*.proto", []string{})
220 expectedLabelsAsString := []string{"foo.proto", "//x:bar.proto", "//x:baz.proto", "//x/y:qux.proto"}
221 for i, actual := range actualLabelsFromRoot.Includes {
222 AssertStringEquals(t, "Error in finding src labels relative to root directory", expectedLabelsAsString[i], actual.Label)
223 }
224
225 // x dir
226 ctx.(*TestBazelConversionPathContext).mockGlobResults = &[]string{"x/bar.proto", "x/baz.proto", "x/y/qux.proto"}
227 actualLabelsFromRoot = BazelLabelForSrcPatternExcludes(ctx, "x", "**/*.proto", []string{})
228 expectedLabelsAsString = []string{"bar.proto", "baz.proto", "//x/y:qux.proto"}
229 for i, actual := range actualLabelsFromRoot.Includes {
230 AssertStringEquals(t, "Error in finding src labels relative to x directory", expectedLabelsAsString[i], actual.Label)
231 }
232
233 // y dir
234 ctx.(*TestBazelConversionPathContext).mockGlobResults = &[]string{"x/y/qux.proto"}
235 actualLabelsFromRoot = BazelLabelForSrcPatternExcludes(ctx, "x/y", "**/*.proto", []string{})
236 expectedLabelsAsString = []string{"qux.proto"}
237 for i, actual := range actualLabelsFromRoot.Includes {
238 AssertStringEquals(t, "Error in finding src labels relative to x/y directory", expectedLabelsAsString[i], actual.Label)
239 }
240}