• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 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
15package proto_test
16
17import (
18	"testing"
19
20	"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
21)
22
23var testArgs = bazel_testing.Args{
24	WorkspaceSuffix: `
25load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
26
27http_archive(
28    name = "com_google_protobuf",
29    sha256 = "75be42bd736f4df6d702a0e4e4d30de9ee40eac024c4b845d17ae4cc831fe4ae",
30    strip_prefix = "protobuf-21.7",
31    # latest available in BCR, as of 2022-09-30
32    urls = [
33        "https://github.com/protocolbuffers/protobuf/archive/v21.7.tar.gz",
34        "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v21.7.tar.gz",
35    ],
36)
37
38load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
39
40protobuf_deps()
41
42http_archive(
43    name = "rules_proto",
44    sha256 = "4d421d51f9ecfe9bf96ab23b55c6f2b809cbaf0eea24952683e397decfbd0dd0",
45    strip_prefix = "rules_proto-f6b8d89b90a7956f6782a4a3609b2f0eee3ce965",
46    # master, as of 2020-01-06
47    urls = [
48        "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/f6b8d89b90a7956f6782a4a3609b2f0eee3ce965.tar.gz",
49        "https://github.com/bazelbuild/rules_proto/archive/f6b8d89b90a7956f6782a4a3609b2f0eee3ce965.tar.gz",
50    ],
51)
52`,
53	Main: `
54-- BUILD.bazel --
55load("@rules_proto//proto:defs.bzl", "proto_library")
56load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
57load("@io_bazel_rules_go//go:def.bzl", "go_binary")
58
59proto_library(
60    name = "cross_proto",
61    srcs = ["cross.proto"],
62)
63
64go_proto_library(
65    name = "cross_go_proto",
66    importpath = "github.com/bazelbuild/rules_go/tests/core/cross",
67    protos = [":cross_proto"],
68)
69
70go_binary(
71    name = "use_bin",
72    srcs = ["use.go"],
73    deps = [":cross_go_proto"],
74    goos = "linux",
75    goarch = "386",
76)
77
78go_binary(
79    name = "use_shared",
80    srcs = ["use.go"],
81    deps = [":cross_go_proto"],
82    linkmode = "c-shared",
83)
84
85-- cross.proto --
86syntax = "proto3";
87
88package cross;
89
90option go_package = "github.com/bazelbuild/rules_go/tests/core/cross";
91
92message Foo {
93  int64 x = 1;
94}
95
96-- use.go --
97package main
98
99import _ "github.com/bazelbuild/rules_go/tests/core/cross"
100
101func main() {}
102`,
103}
104
105func TestMain(m *testing.M) {
106	bazel_testing.TestMain(m, testArgs)
107}
108
109func TestCmdLine(t *testing.T) {
110	args := []string{
111		"build",
112		"--platforms=@io_bazel_rules_go//go/toolchain:linux_386",
113		":cross_go_proto",
114	}
115	if err := bazel_testing.RunBazel(args...); err != nil {
116		t.Fatal(err)
117	}
118}
119
120func TestTargets(t *testing.T) {
121	for _, target := range []string{"//:use_bin", "//:use_shared"} {
122		if err := bazel_testing.RunBazel("build", target); err != nil {
123			t.Errorf("building target %s: %v", target, err)
124		}
125	}
126}
127