Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go_test: ensure external source compilation has data #3848

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _go_test_impl(ctx):
)
external_source = go.library_to_source(go, struct(
srcs = [struct(files = go_srcs)],
data = ctx.attr.data,
embedsrcs = [struct(files = internal_source.embedsrcs)],
deps = internal_archive.direct + [internal_archive],
x_defs = ctx.attr.x_defs,
Expand Down
18 changes: 16 additions & 2 deletions tests/core/go_test/x_defs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,36 @@ go_library(
x_defs = {"Qux": "Qux"},
)

genrule(
name = "data_dep",
outs = ["data_dep.txt"],
cmd = "touch $@",
)

go_library(
name = "x_defs_lib",
srcs = ["x_defs_lib.go"],
data = ["x_defs_lib.go"],
data = [
"data_dep.txt",
"x_defs_lib.go",
],
importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/x_defs_lib",
x_defs = {
"LibGo": "$(rlocationpath x_defs_lib.go)",
"DataDep": "$(rlocationpath :data_dep.txt)",
},
)

go_test(
name = "x_defs_test",
srcs = ["x_defs_test.go"],
data = ["x_defs_test.go"],
data = [
"data_dep.txt",
"x_defs_test.go",
],
x_defs = {
"BinGo": "$(rlocationpath x_defs_test.go)",
fmeum marked this conversation as resolved.
Show resolved Hide resolved
"DataDep": "$(rlocationpath :data_dep.txt)",
},
deps = [
":x_defs_lib",
Expand Down
2 changes: 2 additions & 0 deletions tests/core/go_test/x_defs/x_defs_lib.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package x_defs_lib

var LibGo = "not set"

var DataDep = "not set"
20 changes: 20 additions & 0 deletions tests/core/go_test/x_defs/x_defs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

var BinGo = "not set"

var DataDep = "not set"

func TestLibGoPath(t *testing.T) {
libGoPath, err := runfiles.Rlocation(x_defs_lib.LibGo)
if err != nil {
Expand All @@ -20,6 +22,15 @@ func TestLibGoPath(t *testing.T) {
if err != nil {
t.Fatal(err)
}

dataPath, err := runfiles.Rlocation(x_defs_lib.DataDep)
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(dataPath)
if err != nil {
t.Fatal(err)
}
}

func TestBinGoPath(t *testing.T) {
Expand All @@ -31,4 +42,13 @@ func TestBinGoPath(t *testing.T) {
if err != nil {
t.Fatal(err)
}

dataPath, err := runfiles.Rlocation(DataDep)
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(dataPath)
if err != nil {
t.Fatal(err)
}
}