|
| 1 | +// Copyright 2025 The Bazel Authors. 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 | + |
| 15 | +package trimpath_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" |
| 21 | +) |
| 22 | + |
| 23 | +func TestMain(m *testing.M) { |
| 24 | + bazel_testing.TestMain(m, bazel_testing.Args{ |
| 25 | + Main: ` |
| 26 | +-- BUILD.bazel -- |
| 27 | +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") |
| 28 | +
|
| 29 | +go_library( |
| 30 | + name = "maincgo", |
| 31 | + srcs = ["maincgo.go"], |
| 32 | + cgo = True, |
| 33 | + importpath = "example.com/main_repo/maincgo", |
| 34 | + visibility = ["//visibility:private"], |
| 35 | +) |
| 36 | +
|
| 37 | +go_library( |
| 38 | + name = "main_lib", |
| 39 | + srcs = ["main.go"], |
| 40 | + deps = [":maincgo", "@other_repo//:other", "@other_repo//:othercgo"], |
| 41 | + importpath = "example.com/main_repo/main", |
| 42 | + visibility = ["//visibility:private"], |
| 43 | +) |
| 44 | +
|
| 45 | +go_binary( |
| 46 | + name = "main", |
| 47 | + embed = [":main_lib"], |
| 48 | + visibility = ["//visibility:public"], |
| 49 | +) |
| 50 | +-- main.go -- |
| 51 | +package main |
| 52 | +
|
| 53 | +import "runtime" |
| 54 | +import "fmt" |
| 55 | +import "example.com/main_repo/maincgo" |
| 56 | +import "example.com/other_repo/other" |
| 57 | +import "example.com/other_repo/othercgo" |
| 58 | +
|
| 59 | +func File() string { |
| 60 | + _, file, _, _ := runtime.Caller(0) |
| 61 | + return file |
| 62 | +} |
| 63 | +
|
| 64 | +func main() { |
| 65 | + fmt.Println(File()) |
| 66 | + fmt.Println(maincgo.File()) |
| 67 | + fmt.Println(other.File()) |
| 68 | + fmt.Println(othercgo.File()) |
| 69 | +} |
| 70 | +-- maincgo.go -- |
| 71 | +package maincgo |
| 72 | +
|
| 73 | +import "C" |
| 74 | +import "runtime" |
| 75 | +
|
| 76 | +func File() string { |
| 77 | + _, file, _, _ := runtime.Caller(0) |
| 78 | + return file |
| 79 | +} |
| 80 | +-- other_repo/WORKSPACE -- |
| 81 | +-- other_repo/BUILD.bazel -- |
| 82 | +load("@io_bazel_rules_go//go:def.bzl", "go_library") |
| 83 | +
|
| 84 | +go_library( |
| 85 | + name = "other", |
| 86 | + srcs = ["other.go"], |
| 87 | + importpath = "example.com/other_repo/other", |
| 88 | + visibility = ["//visibility:public"], |
| 89 | +) |
| 90 | +
|
| 91 | +go_library( |
| 92 | + name = "othercgo", |
| 93 | + srcs = ["othercgo.go"], |
| 94 | + cgo = True, |
| 95 | + importpath = "example.com/other_repo/othercgo", |
| 96 | + visibility = ["//visibility:public"], |
| 97 | +) |
| 98 | +-- other_repo/other.go -- |
| 99 | +package other |
| 100 | +
|
| 101 | +import "runtime" |
| 102 | +
|
| 103 | +func File() string { |
| 104 | + _, file, _, _ := runtime.Caller(0) |
| 105 | + return file |
| 106 | +} |
| 107 | +-- other_repo/othercgo.go -- |
| 108 | +package othercgo |
| 109 | +
|
| 110 | +import "C" |
| 111 | +import "runtime" |
| 112 | +
|
| 113 | +func File() string { |
| 114 | + _, file, _, _ := runtime.Caller(0) |
| 115 | + return file |
| 116 | +} |
| 117 | +`, |
| 118 | + WorkspaceSuffix: ` |
| 119 | +local_repository( |
| 120 | + name = "other_repo", |
| 121 | + path = "other_repo", |
| 122 | +) |
| 123 | +`, |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +// These are the expected paths after applying trimpath. |
| 128 | +var expectedDefault = ` |
| 129 | +main.go |
| 130 | +maincgo.go |
| 131 | +external/other_repo/other.go |
| 132 | +external/other_repo/othercgo.go |
| 133 | +` |
| 134 | + |
| 135 | +var expectedSibling = ` |
| 136 | +main.go |
| 137 | +maincgo.go |
| 138 | +../other_repo/other.go |
| 139 | +../other_repo/othercgo.go |
| 140 | +` |
| 141 | + |
| 142 | +func TestTrimpath(t *testing.T) { |
| 143 | + t.Run("default", func(t *testing.T) { |
| 144 | + out, err := bazel_testing.BazelOutput("run", "//:main") |
| 145 | + if err != nil { |
| 146 | + t.Fatal(err) |
| 147 | + } |
| 148 | + outStr := "\n" + string(out) |
| 149 | + if outStr != expectedDefault { |
| 150 | + t.Fatal("actual", outStr, "vs expected", expectedDefault) |
| 151 | + } |
| 152 | + }) |
| 153 | + t.Run("experimental_sibling_repository_layout", func(t *testing.T) { |
| 154 | + out, err := bazel_testing.BazelOutput("run", "--experimental_sibling_repository_layout", "//:main") |
| 155 | + if err != nil { |
| 156 | + t.Fatal(err) |
| 157 | + } |
| 158 | + outStr := "\n" + string(out) |
| 159 | + if outStr != expectedSibling { |
| 160 | + t.Fatal("actual", outStr, "vs expected", expectedSibling) |
| 161 | + } |
| 162 | + }) |
| 163 | +} |
0 commit comments