aboutsummaryrefslogtreecommitdiff
path: root/tests/bcr
diff options
context:
space:
mode:
Diffstat (limited to 'tests/bcr')
-rw-r--r--tests/bcr/.bazelrc1
-rw-r--r--tests/bcr/.bazelversion1
-rw-r--r--tests/bcr/BUILD.bazel55
-rw-r--r--tests/bcr/MODULE.bazel42
-rw-r--r--tests/bcr/WORKSPACE0
-rwxr-xr-xtests/bcr/go_version_test.sh19
-rw-r--r--tests/bcr/lib.go5
-rw-r--r--tests/bcr/main.go11
-rw-r--r--tests/bcr/mockable.go7
-rw-r--r--tests/bcr/mockable_test.go3
-rw-r--r--tests/bcr/other_module/BUILD.bazel1
-rw-r--r--tests/bcr/other_module/MODULE.bazel9
-rw-r--r--tests/bcr/other_module/WORKSPACE0
-rw-r--r--tests/bcr/other_module/bar.txt1
-rw-r--r--tests/bcr/proto/BUILD.bazel51
-rw-r--r--tests/bcr/proto/foo.proto15
-rw-r--r--tests/bcr/proto/foo_grpc_test.go62
-rw-r--r--tests/bcr/proto/foo_proto_test.go16
-rw-r--r--tests/bcr/runfiles/BUILD.bazel9
-rw-r--r--tests/bcr/runfiles/runfiles_test.go67
-rw-r--r--tests/bcr/test.go11
21 files changed, 386 insertions, 0 deletions
diff --git a/tests/bcr/.bazelrc b/tests/bcr/.bazelrc
new file mode 100644
index 00000000..3ce91d27
--- /dev/null
+++ b/tests/bcr/.bazelrc
@@ -0,0 +1 @@
+common --enable_bzlmod
diff --git a/tests/bcr/.bazelversion b/tests/bcr/.bazelversion
new file mode 100644
index 00000000..09b254e9
--- /dev/null
+++ b/tests/bcr/.bazelversion
@@ -0,0 +1 @@
+6.0.0
diff --git a/tests/bcr/BUILD.bazel b/tests/bcr/BUILD.bazel
new file mode 100644
index 00000000..d5c5f6f1
--- /dev/null
+++ b/tests/bcr/BUILD.bazel
@@ -0,0 +1,55 @@
+load("@my_rules_go//extras:gomock.bzl", "gomock")
+load("@my_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
+
+go_library(
+ name = "lib",
+ srcs = ["lib.go"],
+ importpath = "example.com/lib",
+)
+
+go_binary(
+ name = "main",
+ srcs = ["main.go"],
+ deps = [":lib"],
+)
+
+go_test(
+ name = "test",
+ srcs = ["test.go"],
+ embed = [":lib"],
+)
+
+go_library(
+ name = "mockable",
+ srcs = [
+ "mockable.go",
+ ],
+ importpath = "example.com/mockable",
+)
+
+gomock(
+ name = "mocks",
+ out = "mockable_mock.go",
+ library = ":mockable",
+ package = "mockable",
+ source = "mockable.go",
+ visibility = ["//visibility:public"],
+)
+
+go_test(
+ name = "mockable_test",
+ srcs = [
+ "mockable_mock.go",
+ "mockable_test.go",
+ ],
+ embed = [":mockable"],
+ deps = ["@my_rules_go//extras/gomock"],
+)
+
+sh_test(
+ name = "go_version_test",
+ srcs = ["go_version_test.sh"],
+ data = ["@my_rules_go//go"],
+ env = {"GO_TOOL_RLOCATION": "$(rlocationpath @my_rules_go//go)"},
+ deps = ["@bazel_tools//tools/bash/runfiles"],
+)
diff --git a/tests/bcr/MODULE.bazel b/tests/bcr/MODULE.bazel
new file mode 100644
index 00000000..b774feb4
--- /dev/null
+++ b/tests/bcr/MODULE.bazel
@@ -0,0 +1,42 @@
+module(
+ name = "rules_go_bcr_tests",
+ # Test that the default SDK is registered by not registering one from the test module.
+ version = "1.2.3",
+)
+
+# Keep this dep first so that its toolchain takes precedence over the default SDK registered by
+# rules_go.
+bazel_dep(name = "other_module", version = "")
+local_path_override(
+ module_name = "other_module",
+ path = "other_module",
+)
+
+bazel_dep(name = "rules_go", version = "", repo_name = "my_rules_go")
+local_path_override(
+ module_name = "rules_go",
+ path = "../..",
+)
+
+bazel_dep(name = "gazelle", version = "0.26.0")
+bazel_dep(name = "protobuf", version = "3.19.6")
+
+go_sdk = use_extension("@my_rules_go//go:extensions.bzl", "go_sdk")
+go_sdk.download(version = "1.19.5")
+
+# Request an invalid SDK to verify that it isn't fetched since the first tag takes precedence.
+go_sdk.host(version = "3.0.0")
+
+# Bring the default SDK into scope to verify that it exists.
+use_repo(go_sdk, "go_default_sdk")
+
+# Bring the selected host compatible SDK into scope to verify that it exists.
+use_repo(go_sdk, "go_host_compatible_sdk_label")
+
+go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
+go_deps.module(
+ path = "google.golang.org/grpc",
+ sum = "h1:fPVVDxY9w++VjTZsYvXWqEf9Rqar/e+9zYfxKK+W+YU=",
+ version = "v1.50.0",
+)
+use_repo(go_deps, "org_golang_google_grpc")
diff --git a/tests/bcr/WORKSPACE b/tests/bcr/WORKSPACE
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/bcr/WORKSPACE
diff --git a/tests/bcr/go_version_test.sh b/tests/bcr/go_version_test.sh
new file mode 100755
index 00000000..986d91d7
--- /dev/null
+++ b/tests/bcr/go_version_test.sh
@@ -0,0 +1,19 @@
+#!/usr/bin/env bash
+
+# --- begin runfiles.bash initialization v3 ---
+# Copy-pasted from the Bazel Bash runfiles library v3.
+set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
+source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
+ source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
+ source "$0.runfiles/$f" 2>/dev/null || \
+ source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+ source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+ { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
+# --- end runfiles.bash initialization v3 ---
+
+GO_TOOL=$(rlocation "$GO_TOOL_RLOCATION")
+# Set runfiles variables for subprocess.
+runfiles_export_envvars
+# Simulate a bazel run environment.
+export BUILD_WORKING_DIRECTORY=$(pwd)
+[[ "$("$GO_TOOL" version)" =~ ^go ]]
diff --git a/tests/bcr/lib.go b/tests/bcr/lib.go
new file mode 100644
index 00000000..d162287c
--- /dev/null
+++ b/tests/bcr/lib.go
@@ -0,0 +1,5 @@
+package lib
+
+func Name() string {
+ return "bzlmod"
+}
diff --git a/tests/bcr/main.go b/tests/bcr/main.go
new file mode 100644
index 00000000..46c058ef
--- /dev/null
+++ b/tests/bcr/main.go
@@ -0,0 +1,11 @@
+package main
+
+import (
+ "fmt"
+
+ "example.com/lib"
+)
+
+func main() {
+ fmt.Printf("Hello %s!", lib.Name())
+}
diff --git a/tests/bcr/mockable.go b/tests/bcr/mockable.go
new file mode 100644
index 00000000..e310105b
--- /dev/null
+++ b/tests/bcr/mockable.go
@@ -0,0 +1,7 @@
+package mockable
+
+import "net/url"
+
+type Client interface {
+ Connect(addr string) url.URL
+}
diff --git a/tests/bcr/mockable_test.go b/tests/bcr/mockable_test.go
new file mode 100644
index 00000000..7e9cc8a0
--- /dev/null
+++ b/tests/bcr/mockable_test.go
@@ -0,0 +1,3 @@
+package mockable
+
+var _ Client = (*MockClient)(nil)
diff --git a/tests/bcr/other_module/BUILD.bazel b/tests/bcr/other_module/BUILD.bazel
new file mode 100644
index 00000000..65781709
--- /dev/null
+++ b/tests/bcr/other_module/BUILD.bazel
@@ -0,0 +1 @@
+exports_files(["bar.txt"])
diff --git a/tests/bcr/other_module/MODULE.bazel b/tests/bcr/other_module/MODULE.bazel
new file mode 100644
index 00000000..f517abae
--- /dev/null
+++ b/tests/bcr/other_module/MODULE.bazel
@@ -0,0 +1,9 @@
+module(name = "other_module")
+
+bazel_dep(name = "rules_go", version = "")
+
+go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
+
+# Request an invalid SDK to verify that it isn't fetched since the test module registers a toolchain
+# that takes precedence.
+go_sdk.download(version = "3.0.0")
diff --git a/tests/bcr/other_module/WORKSPACE b/tests/bcr/other_module/WORKSPACE
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/bcr/other_module/WORKSPACE
diff --git a/tests/bcr/other_module/bar.txt b/tests/bcr/other_module/bar.txt
new file mode 100644
index 00000000..ce013625
--- /dev/null
+++ b/tests/bcr/other_module/bar.txt
@@ -0,0 +1 @@
+hello
diff --git a/tests/bcr/proto/BUILD.bazel b/tests/bcr/proto/BUILD.bazel
new file mode 100644
index 00000000..49046ffa
--- /dev/null
+++ b/tests/bcr/proto/BUILD.bazel
@@ -0,0 +1,51 @@
+load("@my_rules_go//go:def.bzl", "go_test")
+load("@my_rules_go//proto:def.bzl", "go_grpc_library", "go_proto_library")
+
+proto_library(
+ name = "foo_proto",
+ srcs = ["foo.proto"],
+ deps = [
+ "@protobuf//:empty_proto",
+ ],
+)
+
+go_proto_library(
+ name = "foo_go_proto",
+ importpath = "example.com/foo_proto",
+ proto = ":foo_proto",
+)
+
+go_test(
+ name = "foo_proto_test",
+ srcs = ["foo_proto_test.go"],
+ deps = [":foo_go_proto"],
+)
+
+go_grpc_library(
+ name = "foo_go_grpc",
+ importpath = "example.com/foo_proto",
+ protos = [":foo_proto"],
+)
+
+go_test(
+ name = "foo_grpc_test",
+ srcs = ["foo_grpc_test.go"],
+ deps = [
+ ":foo_go_grpc",
+ "@org_golang_google_grpc//:grpc",
+ "@org_golang_google_grpc//credentials/insecure",
+ ],
+)
+
+go_proto_library(
+ name = "foo_go_proto_gogo",
+ compilers = ["@my_rules_go//proto:gogo_proto"],
+ importpath = "example.com/foo_proto",
+ protos = [":foo_proto"],
+)
+
+go_test(
+ name = "foo_proto_gogo_test",
+ srcs = ["foo_proto_test.go"],
+ deps = [":foo_go_proto_gogo"],
+)
diff --git a/tests/bcr/proto/foo.proto b/tests/bcr/proto/foo.proto
new file mode 100644
index 00000000..71980226
--- /dev/null
+++ b/tests/bcr/proto/foo.proto
@@ -0,0 +1,15 @@
+syntax = "proto3";
+
+package tests.bcr.proto;
+option go_package = "example.com/foo_proto";
+
+import "google/protobuf/empty.proto";
+
+message Foo {
+ int64 value = 1;
+ google.protobuf.Empty empty = 2;
+}
+
+service Fooer {
+ rpc RoundTripFoo (Foo) returns (Foo) {}
+}
diff --git a/tests/bcr/proto/foo_grpc_test.go b/tests/bcr/proto/foo_grpc_test.go
new file mode 100644
index 00000000..4879315f
--- /dev/null
+++ b/tests/bcr/proto/foo_grpc_test.go
@@ -0,0 +1,62 @@
+package grpc_test
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "net"
+ "testing"
+
+ "example.com/foo_proto"
+
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/credentials/insecure"
+)
+
+type fooerServer struct {
+}
+
+func newServer() *fooerServer {
+ return &fooerServer{}
+}
+
+func (*fooerServer) RoundTripFoo(ctx context.Context, foo *foo_proto.Foo) (*foo_proto.Foo, error) {
+ foo.Value += 1
+ return foo, nil
+}
+
+func TestRoundTripFoo(t *testing.T) {
+ // Start the server.
+ address := fmt.Sprintf("localhost:%d", 12345)
+ lis, err := net.Listen("tcp", address)
+ if err != nil {
+ log.Fatalf("failed to listen on %s: %v", address, err)
+ }
+ grpcServer := grpc.NewServer()
+ foo_proto.RegisterFooerServer(grpcServer, newServer())
+ go func() {
+ grpcServer.Serve(lis)
+ }()
+
+ // Start the client.
+ conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
+ if err != nil {
+ log.Fatalf("fail to dial %s: %v", address, err)
+ }
+ defer conn.Close()
+ client := foo_proto.NewFooerClient(conn)
+
+ // Send a message and verify that it is returned correctly.
+ msgIn := &foo_proto.Foo{
+ Value: 42,
+ }
+ msgOut, err := client.RoundTripFoo(context.TODO(), msgIn)
+ if err != nil {
+ log.Fatalf("failed to round-trip message: %v", err)
+ }
+ if msgOut.Value != 43 {
+ log.Fatalf("message did not round-trip correctly: sent %v, got %v", msgIn, msgOut)
+ }
+
+ grpcServer.GracefulStop()
+}
diff --git a/tests/bcr/proto/foo_proto_test.go b/tests/bcr/proto/foo_proto_test.go
new file mode 100644
index 00000000..ff3f4754
--- /dev/null
+++ b/tests/bcr/proto/foo_proto_test.go
@@ -0,0 +1,16 @@
+package proto_test
+
+import (
+ "testing"
+
+ "example.com/foo_proto"
+)
+
+func TestFoo(t *testing.T) {
+ msg := &foo_proto.Foo{
+ Value: 1,
+ }
+ if msg.Value != 1 {
+ t.Fail()
+ }
+}
diff --git a/tests/bcr/runfiles/BUILD.bazel b/tests/bcr/runfiles/BUILD.bazel
new file mode 100644
index 00000000..d86ff26e
--- /dev/null
+++ b/tests/bcr/runfiles/BUILD.bazel
@@ -0,0 +1,9 @@
+load("@my_rules_go//go:def.bzl", "go_test")
+
+go_test(
+ name = "runfiles_test",
+ srcs = ["runfiles_test.go"],
+ args = ["$(rlocationpath @other_module//:bar.txt)"],
+ data = ["@other_module//:bar.txt"],
+ deps = ["@my_rules_go//go/runfiles"],
+)
diff --git a/tests/bcr/runfiles/runfiles_test.go b/tests/bcr/runfiles/runfiles_test.go
new file mode 100644
index 00000000..ea82e976
--- /dev/null
+++ b/tests/bcr/runfiles/runfiles_test.go
@@ -0,0 +1,67 @@
+package runfiles
+
+import (
+ "os"
+ "testing"
+
+ "github.com/bazelbuild/rules_go/go/runfiles"
+)
+
+func TestRunfilesApparent(t *testing.T) {
+ path, err := runfiles.Rlocation("other_module/bar.txt")
+ if err != nil {
+ t.Fatalf("runfiles.Path: %v", err)
+ }
+ assertRunfile(t, path)
+}
+
+func TestRunfilesApparentSourceRepositoryOption(t *testing.T) {
+ r, err := runfiles.New(runfiles.SourceRepo(runfiles.CurrentRepository()))
+ if err != nil {
+ t.Fatalf("runfiles.New: %v", err)
+ }
+ path, err := r.Rlocation("other_module/bar.txt")
+ if err != nil {
+ t.Fatalf("runfiles.Path: %v", err)
+ }
+ assertRunfile(t, path)
+}
+
+func TestRunfilesApparentWithSourceRepository(t *testing.T) {
+ r, err := runfiles.New()
+ if err != nil {
+ t.Fatalf("runfiles.New: %v", err)
+ }
+ r = r.WithSourceRepo(runfiles.CurrentRepository())
+ path, err := r.Rlocation("other_module/bar.txt")
+ if err != nil {
+ t.Fatalf("runfiles.Path: %v", err)
+ }
+ assertRunfile(t, path)
+}
+
+func TestRunfilesFromApparent(t *testing.T) {
+ path, err := runfiles.RlocationFrom("other_module/bar.txt", runfiles.CurrentRepository())
+ if err != nil {
+ t.Fatalf("runfiles.Path: %v", err)
+ }
+ assertRunfile(t, path)
+}
+
+func TestRunfilesCanonical(t *testing.T) {
+ path, err := runfiles.Rlocation(os.Args[1])
+ if err != nil {
+ t.Fatalf("runfiles.Path: %v", err)
+ }
+ assertRunfile(t, path)
+}
+
+func assertRunfile(t *testing.T, path string) {
+ content, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatalf("os.ReadFile: %v", err)
+ }
+ if string(content) != "hello\n" {
+ t.Fatalf("got %q; want %q", content, "hello\n")
+ }
+}
diff --git a/tests/bcr/test.go b/tests/bcr/test.go
new file mode 100644
index 00000000..910c06be
--- /dev/null
+++ b/tests/bcr/test.go
@@ -0,0 +1,11 @@
+package lib
+
+import (
+ "testing"
+)
+
+func TestName(t *testing.T) {
+ if Name() != "bzlmod" {
+ t.Fail()
+ }
+}