blob: b047511c71f8263749653ae177418732e994f2aa [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"
20)
21
22type TestBazelPathContext struct{}
23
24func (*TestBazelPathContext) Config() Config {
25 cfg := NullConfig("out", "out/soong")
26 cfg.BazelContext = MockBazelContext{
27 OutputBaseDir: "out/bazel",
28 }
29 return cfg
30}
31
32func (*TestBazelPathContext) AddNinjaFileDeps(deps ...string) {
33 panic("Unimplemented")
34}
35
36func TestPathForBazelOut(t *testing.T) {
37 ctx := &TestBazelPathContext{}
38 out := PathForBazelOut(ctx, "foo/bar/baz/boq.txt")
39 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt")
40 if out.String() != expectedPath {
41 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
42 }
43
44 expectedRelPath := "foo/bar/baz/boq.txt"
45 if out.Rel() != expectedRelPath {
46 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
47 }
48}
49
50func TestPathForBazelOutRelative(t *testing.T) {
51 ctx := &TestBazelPathContext{}
52 out := PathForBazelOutRelative(ctx, "foo/bar", "foo/bar/baz/boq.txt")
53
54 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/foo/bar/baz/boq.txt")
55 if out.String() != expectedPath {
56 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
57 }
58
59 expectedRelPath := "baz/boq.txt"
60 if out.Rel() != expectedRelPath {
61 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
62 }
63}
64
65func TestPathForBazelOutRelativeUnderBinFolder(t *testing.T) {
66 ctx := &TestBazelPathContext{}
67 out := PathForBazelOutRelative(ctx, "foo/bar", "bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt")
68
69 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/__main__/bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/foo/bar/baz/boq.txt")
70 if out.String() != expectedPath {
71 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
72 }
73
74 expectedRelPath := "baz/boq.txt"
75 if out.Rel() != expectedRelPath {
76 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
77 }
78}
79
80func TestPathForBazelOutOutsideOfExecroot(t *testing.T) {
81 ctx := &TestBazelPathContext{}
82 out := PathForBazelOut(ctx, "../bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar")
83
84 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar")
85 if out.String() != expectedPath {
86 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
87 }
88
89 expectedRelPath := "execroot/bazel_tools/linux_x86_64-fastbuild/bin/tools/android/java_base_extras.jar"
90 if out.Rel() != expectedRelPath {
91 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
92 }
93}
94
95func TestPathForBazelOutRelativeWithParentDirectoryRoot(t *testing.T) {
96 ctx := &TestBazelPathContext{}
97 out := PathForBazelOutRelative(ctx, "../bazel_tools", "../bazel_tools/foo/bar/baz.sh")
98
99 expectedPath := filepath.Join(ctx.Config().BazelContext.OutputBase(), "execroot/bazel_tools/foo/bar/baz.sh")
100 if out.String() != expectedPath {
101 t.Errorf("incorrect OutputPath: expected %q, got %q", expectedPath, out.String())
102 }
103
104 expectedRelPath := "foo/bar/baz.sh"
105 if out.Rel() != expectedRelPath {
106 t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel())
107 }
108}