diff options
Diffstat (limited to 'tests/legacy/examples')
68 files changed, 1417 insertions, 0 deletions
diff --git a/tests/legacy/examples/bin/BUILD.bazel b/tests/legacy/examples/bin/BUILD.bazel new file mode 100644 index 00000000..67a791e7 --- /dev/null +++ b/tests/legacy/examples/bin/BUILD.bazel @@ -0,0 +1,17 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary") + +package( + default_visibility = ["//visibility:public"], +) + +go_binary( + name = "bin", + srcs = ["bin.go"], + x_defs = { + "main.buildTime": "2016/05/19 09:10am", + }, + deps = [ + "//tests/legacy/examples/lib:go_default_library", + "//tests/legacy/examples/vendor/github.com/user/vendored:go_default_library", + ], +) diff --git a/tests/legacy/examples/bin/bin.go b/tests/legacy/examples/bin/bin.go new file mode 100644 index 00000000..6abac002 --- /dev/null +++ b/tests/legacy/examples/bin/bin.go @@ -0,0 +1,34 @@ +/* Copyright 2016 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + + "github.com/bazelbuild/rules_go/examples/lib" + "github.com/user/vendored" +) + +var buildTime string + +func main() { + fmt.Println("meaning: ", lib.Meaning()) + fmt.Println("vendored: ", vendored.Vendored()) + fmt.Println("build time: ", buildTime) + + lib.AddTwoNumbers() + lib.SubTwoNumbers() +} diff --git a/tests/legacy/examples/bindata/BUILD.bazel b/tests/legacy/examples/bindata/BUILD.bazel new file mode 100644 index 00000000..b4d58a26 --- /dev/null +++ b/tests/legacy/examples/bindata/BUILD.bazel @@ -0,0 +1,22 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("@io_bazel_rules_go//extras:bindata.bzl", "bindata") + +bindata( + name = "data", + srcs = glob(["data/*"]), + package = "bindata", + strip_external = True, +) + +go_library( + name = "go_default_library", + srcs = [":data"], + importpath = "github.com/bazelbuild/rules_go/examples/bindata", +) + +go_test( + name = "go_default_test", + size = "small", + srcs = ["bindata_test.go"], + embed = [":go_default_library"], +) diff --git a/tests/legacy/examples/bindata/bindata_test.go b/tests/legacy/examples/bindata/bindata_test.go new file mode 100644 index 00000000..f3eeb1c4 --- /dev/null +++ b/tests/legacy/examples/bindata/bindata_test.go @@ -0,0 +1,33 @@ +/* Copyright 2016 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package bindata + +import ( + "testing" +) + +func TestMessage(t *testing.T) { + data, err := Asset("data/message.txt") + if err != nil { + // Asset was not found. + t.Errorf("Asset was not embeded: %s", err) + } + got := string(data) + const expected = "Some embedded data" + if got != expected { + t.Errorf("Got embedded asset %q\nexpected %q", got, expected) + } +} diff --git a/tests/legacy/examples/bindata/data/message.txt b/tests/legacy/examples/bindata/data/message.txt new file mode 100644 index 00000000..44ae74f4 --- /dev/null +++ b/tests/legacy/examples/bindata/data/message.txt @@ -0,0 +1 @@ +Some embedded data
\ No newline at end of file diff --git a/tests/legacy/examples/cgo/BUILD.bazel b/tests/legacy/examples/cgo/BUILD.bazel new file mode 100644 index 00000000..901f6d8d --- /dev/null +++ b/tests/legacy/examples/cgo/BUILD.bazel @@ -0,0 +1,51 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +package( + default_visibility = ["//visibility:public"], +) + +go_library( + name = "go_default_library", + srcs = [ + "export_example.go", + "generated.go", + "import_example.go", + "pure_go.go", + "use_exported.c", + "use_exported.h", + ], + cdeps = ["//tests/legacy/examples/cgo/cc_dependency:version"], + cgo = True, + clinkopts = ["-lm"], + copts = ["-DDEFINED_IN_COPTS=1"], + importpath = "github.com/bazelbuild/rules_go/examples/cgo", + visibility = ["//visibility:public"], + deps = [":sub"], +) + +# TODO(#746) exclude because gazelle does not recognise the rule below +# gazelle:exclude sub + +go_library( + name = "sub", + srcs = ["sub/floor.go"], + cgo = True, + clinkopts = ["-lm"], + importpath = "github.com/bazelbuild/rules_go/examples/cgo/sub", + visibility = ["//visibility:private"], +) + +go_test( + name = "cgo_lib_test", + size = "small", + srcs = ["cgo_lib_test.go"], + embed = [":go_default_library"], +) + +genrule( + name = "generate_go_src", + srcs = ["generated.go.tpl"], + outs = ["generated.go"], + cmd = "cp -f $< $@", + visibility = ["//visibility:private"], +) diff --git a/tests/legacy/examples/cgo/cc_dependency/BUILD.bazel b/tests/legacy/examples/cgo/cc_dependency/BUILD.bazel new file mode 100644 index 00000000..f0eee7dd --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/BUILD.bazel @@ -0,0 +1,29 @@ +cc_library( + name = "version", + srcs = ["cxx_version.cc"], + hdrs = ["version.h"], + linkopts = ["-ldl"], + visibility = ["//tests/legacy/examples/cgo:__pkg__"], + # TODO(yugui) Support darwin too and remove this workaround. + # See also comments in cxx_version.cc. + deps = select({ + "@platforms//os:macos": [], + "//conditions:default": [":c_version_import"], + }), +) + +cc_binary( + name = "c_version_so", + srcs = [ + "c_version.c", + "c_version.h", + ], + linkshared = True, +) + +cc_import( + name = "c_version_import", + hdrs = ["c_version.h"], + shared_library = ":c_version_so", + tags = ["manual"], +) diff --git a/tests/legacy/examples/cgo/cc_dependency/c_version.c b/tests/legacy/examples/cgo/cc_dependency/c_version.c new file mode 100644 index 00000000..5672898e --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/c_version.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +#include "tests/legacy/examples/cgo/cc_dependency/c_version.h" + +void PrintCVersion() { +#ifdef __STDC__ +# ifdef __STDC_VERSION__ + printf("C version: %ld\n", __STDC_VERSION__); +# else + printf("C version: C89\n"); +# endif +#else + printf("C version: maybe K&R\n"); +#endif +} diff --git a/tests/legacy/examples/cgo/cc_dependency/c_version.h b/tests/legacy/examples/cgo/cc_dependency/c_version.h new file mode 100644 index 00000000..5ff655cf --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/c_version.h @@ -0,0 +1,5 @@ +#ifdef __cplusplus +extern "C" void PrintCVersion(); +#else +void PrintCVersion(); +#endif diff --git a/tests/legacy/examples/cgo/cc_dependency/cxx_version.cc b/tests/legacy/examples/cgo/cc_dependency/cxx_version.cc new file mode 100644 index 00000000..43fb427e --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/cxx_version.cc @@ -0,0 +1,30 @@ +#include <dlfcn.h> +#include <iostream> + +#include "tests/legacy/examples/cgo/cc_dependency/version.h" + +// TODO(yugui) Support Darwin too once Bazel allows it. +// +// Bazel passes two or more -Wl,-rpath to $(CC) when it links a binary with +// shared libraries prebuilt outside of Bazel (i.e. when "srcs" attribute of +// the dependency cc_library contains ".so" files). +// Unfortunately tools/cpp/osx_cc_wrapper.sh, which is $(CC) on Darwin, expects +// only one -Wl,-rpath. So the binary fails to resolve the shared libraries +// at runtime. +#ifndef __APPLE_CC__ +# include "tests/legacy/examples/cgo/cc_dependency/c_version.h" +#endif + +extern "C" void PrintCXXVersion() { +#ifndef __APPLE_CC__ + PrintCVersion(); +#endif + void* ptr = dlsym(RTLD_DEFAULT, "PrintCXXVersion"); + if (ptr) { + std::cout + << "function ptr: " << std::hex << ptr << std::dec << std::endl; + } else { + std::cout << dlerror() << std::endl; + } + std::cout << "C++ version: " << __cplusplus << std::endl; +} diff --git a/tests/legacy/examples/cgo/cc_dependency/version.h b/tests/legacy/examples/cgo/cc_dependency/version.h new file mode 100644 index 00000000..025656b3 --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/version.h @@ -0,0 +1,10 @@ +#ifndef BAZEL_RULES_GO_EXAMPLES_CGO_CC_DEPENDENCY_VERSION_H_ +#define BAZEL_RULES_GO_EXAMPLES_CGO_CC_DEPENDENCY_VERSION_H_ + +#ifdef __cplusplus +extern "C" void PrintCXXVersion(); +#else +void PrintCXXVersion(); +#endif + +#endif // BAZEL_RULES_GO_EXAMPLES_CGO_CC_DEPENDENCY_VERSION_H_ diff --git a/tests/legacy/examples/cgo/cgo_lib_test.go b/tests/legacy/examples/cgo/cgo_lib_test.go new file mode 100644 index 00000000..a486da6a --- /dev/null +++ b/tests/legacy/examples/cgo/cgo_lib_test.go @@ -0,0 +1,15 @@ +package cgo + +import ( + "math" + "testing" +) + +func TestNsqrt(t *testing.T) { + for _, n := range []int{1, 2, 10, 100, 1000} { + got, want := Nsqrt(n), int(math.Floor(math.Sqrt(float64(n)))) + if got != want { + t.Errorf("Nsqrt(n) = %d; want %d", got, want) + } + } +} diff --git a/tests/legacy/examples/cgo/example_command/BUILD.bazel b/tests/legacy/examples/cgo/example_command/BUILD.bazel new file mode 100644 index 00000000..db480ab7 --- /dev/null +++ b/tests/legacy/examples/cgo/example_command/BUILD.bazel @@ -0,0 +1,26 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary") +load(":generate_test.bzl", "generate_script") + +package( + default_visibility = ["//visibility:public"], +) + +go_binary( + name = "example_command", + srcs = ["main.go"], + deps = [ + "//tests/legacy/examples/cgo:go_default_library", + ], +) + +generate_script( + name = "example_command_script", + binary = ":example_command", +) + +sh_test( + name = "example_command_test", + size = "small", + srcs = [":example_command_script"], + data = [":example_command"], +) diff --git a/tests/legacy/examples/cgo/example_command/generate_test.bzl b/tests/legacy/examples/cgo/example_command/generate_test.bzl new file mode 100644 index 00000000..c3c991df --- /dev/null +++ b/tests/legacy/examples/cgo/example_command/generate_test.bzl @@ -0,0 +1,17 @@ +def _generate_script_impl(ctx): + script_file = ctx.actions.declare_file(ctx.label.name + ".bash") + ctx.actions.write(output = script_file, is_executable = True, content = """ +{0} +""".format(ctx.file.binary.short_path)) + return struct( + files = depset([script_file]), + ) + +generate_script = rule( + _generate_script_impl, + attrs = { + "binary": attr.label( + allow_single_file = True, + ), + }, +) diff --git a/tests/legacy/examples/cgo/example_command/main.go b/tests/legacy/examples/cgo/example_command/main.go new file mode 100644 index 00000000..8f0bfd6a --- /dev/null +++ b/tests/legacy/examples/cgo/example_command/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + + "github.com/bazelbuild/rules_go/examples/cgo" +) + +func main() { + fmt.Println("floor(sqrt(10)) = ", cgo.Nsqrt(10)) + cgo.PrintGoVersion() + cgo.PrintCXXVersion() +} diff --git a/tests/legacy/examples/cgo/export_example.go b/tests/legacy/examples/cgo/export_example.go new file mode 100644 index 00000000..874340c7 --- /dev/null +++ b/tests/legacy/examples/cgo/export_example.go @@ -0,0 +1,11 @@ +package cgo + +import ( + "C" + "runtime" +) + +//export goVersion +func goVersion() string { + return runtime.Version() +} diff --git a/tests/legacy/examples/cgo/generated.go.tpl b/tests/legacy/examples/cgo/generated.go.tpl new file mode 100644 index 00000000..ba80e040 --- /dev/null +++ b/tests/legacy/examples/cgo/generated.go.tpl @@ -0,0 +1,13 @@ +package cgo + +import ( + //#cgo LDFLAGS: -lm + //#include <math.h> + "C" + "math" +) + +// Ncbrt returns the cube root of n. +func Ncbrt(n int) int { + return int(math.Floor(float64(C.cbrt(C.double(n))))) +} diff --git a/tests/legacy/examples/cgo/import_example.go b/tests/legacy/examples/cgo/import_example.go new file mode 100644 index 00000000..a25a6971 --- /dev/null +++ b/tests/legacy/examples/cgo/import_example.go @@ -0,0 +1,29 @@ +package cgo + +import ( + //#cgo LDFLAGS: -lm -lversion -lc_version -L${SRCDIR}/cc_dependency + //#cgo CPPFLAGS: -I${SRCDIR}/../.. + //#include <math.h> + //#include "use_exported.h" + //#include "cc_dependency/version.h" + "C" + + "github.com/bazelbuild/rules_go/examples/cgo/sub" +) + +// Nsqrt returns the square root of n. +func Nsqrt(n int) int { + return int(sub.Floor(float64(C.sqrt(C.double(n))))) +} + +func PrintGoVersion() { + C.PrintGoVersion() +} + +func printCXXVersion() { + C.PrintCXXVersion() +} + +func ReturnDefined() int { + return int(C.DEFINED_IN_COPTS) +} diff --git a/tests/legacy/examples/cgo/pure_go.go b/tests/legacy/examples/cgo/pure_go.go new file mode 100644 index 00000000..531517dd --- /dev/null +++ b/tests/legacy/examples/cgo/pure_go.go @@ -0,0 +1,5 @@ +package cgo + +func PrintCXXVersion() { + printCXXVersion() +} diff --git a/tests/legacy/examples/cgo/skip_go_library/BUILD.bazel b/tests/legacy/examples/cgo/skip_go_library/BUILD.bazel new file mode 100644 index 00000000..69a99596 --- /dev/null +++ b/tests/legacy/examples/cgo/skip_go_library/BUILD.bazel @@ -0,0 +1,13 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +package(default_visibility = ["//visibility:public"]) + +go_library( + name = "go_default_library", + srcs = [ + "cgo_foo.go", + "types.go", + ], + cgo = True, + importpath = "github.com/bazelbuild/rules_go/examples/cgo/skip_go_library", +) diff --git a/tests/legacy/examples/cgo/skip_go_library/cgo_foo.go b/tests/legacy/examples/cgo/skip_go_library/cgo_foo.go new file mode 100644 index 00000000..42b1dbaa --- /dev/null +++ b/tests/legacy/examples/cgo/skip_go_library/cgo_foo.go @@ -0,0 +1,8 @@ +package skip_go_library + +/* +#include <stdio.h> +*/ +import "C" + +var _ = Type{} diff --git a/tests/legacy/examples/cgo/skip_go_library/types.go b/tests/legacy/examples/cgo/skip_go_library/types.go new file mode 100644 index 00000000..1f3510ec --- /dev/null +++ b/tests/legacy/examples/cgo/skip_go_library/types.go @@ -0,0 +1,3 @@ +package skip_go_library + +type Type struct{} diff --git a/tests/legacy/examples/cgo/sub/floor.go b/tests/legacy/examples/cgo/sub/floor.go new file mode 100644 index 00000000..315b5c8f --- /dev/null +++ b/tests/legacy/examples/cgo/sub/floor.go @@ -0,0 +1,13 @@ +package sub + +import ( + //#cgo LDFLAGS: -lm + //#include <math.h> + "C" +) + +// Floor calculates floor of the given number +// with the implementation in the standard C library. +func Floor(f float64) float64 { + return float64(C.floor(C.double(f))) +} diff --git a/tests/legacy/examples/cgo/use_exported.c b/tests/legacy/examples/cgo/use_exported.c new file mode 100644 index 00000000..0a6d01e7 --- /dev/null +++ b/tests/legacy/examples/cgo/use_exported.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +#include "_cgo_export.h" + +void PrintGoVersion() { + GoString version = goVersion(); + printf("Go version: %.*s\n", (int)version.n, version.p); +} diff --git a/tests/legacy/examples/cgo/use_exported.h b/tests/legacy/examples/cgo/use_exported.h new file mode 100644 index 00000000..3c8f8414 --- /dev/null +++ b/tests/legacy/examples/cgo/use_exported.h @@ -0,0 +1,6 @@ +#ifndef BAZEL_RULES_GO_EXAMPLES_CGO_USE_EXPORTED_H_ +#define BAZEL_RULES_GO_EXAMPLES_CGO_USE_EXPORTED_H_ + +void PrintGoVersion(); + +#endif // BAZEL_RULES_GO_EXAMPLES_CGO_USE_EXPORTED_H_ diff --git a/tests/legacy/examples/external/BUILD.bazel b/tests/legacy/examples/external/BUILD.bazel new file mode 100644 index 00000000..a56ec6e5 --- /dev/null +++ b/tests/legacy/examples/external/BUILD.bazel @@ -0,0 +1,11 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary") + +go_binary( + name = "record_log", + srcs = [ + "main.go", + ], + deps = [ + "@com_github_golang_glog//:go_default_library", + ], +) diff --git a/tests/legacy/examples/external/main.go b/tests/legacy/examples/external/main.go new file mode 100644 index 00000000..03ed27e7 --- /dev/null +++ b/tests/legacy/examples/external/main.go @@ -0,0 +1,31 @@ +/* Copyright 2016 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Command record_log just records a log with glog to +// show an example of linking with a external dependency. +package main + +import ( + "flag" + + "github.com/golang/glog" +) + +func main() { + flag.Parse() + defer glog.Flush() + + glog.Info("Hello") +} diff --git a/tests/legacy/examples/lib/BUILD.bazel b/tests/legacy/examples/lib/BUILD.bazel new file mode 100644 index 00000000..9b9249d4 --- /dev/null +++ b/tests/legacy/examples/lib/BUILD.bazel @@ -0,0 +1,46 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +package( + default_visibility = ["//visibility:public"], +) + +go_library( + name = "go_default_library", + srcs = [ + "add_amd64.s", + "add_arm64.s", + "asm.go", + "lib.go", + "sub_amd64.s", + "sub_arm64.s", + ], + importpath = "github.com/bazelbuild/rules_go/examples/lib", + deps = ["//tests/legacy/examples/lib/deep:go_default_library"], +) + +go_test( + name = "lib_test", + size = "small", + srcs = [ + "lib_test.go", + ], + embed = [":go_default_library"], +) + +go_test( + name = "lib_external_test", + size = "small", + srcs = [ + "lib_x_test.go", + ], + args = [ + "-lib_test.buildtime", + "'05/19/2016 10:30am'", + ], + x_defs = { + "github.com/bazelbuild/rules_go/examples/lib.buildTime": "05/19/2016 10:30am", + }, + deps = [ + ":go_default_library", + ], +) diff --git a/tests/legacy/examples/lib/add_amd64.s b/tests/legacy/examples/lib/add_amd64.s new file mode 100644 index 00000000..72f8298c --- /dev/null +++ b/tests/legacy/examples/lib/add_amd64.s @@ -0,0 +1,8 @@ +#include "textflag.h" + +TEXT ·add(SB),NOSPLIT,$0 + MOVQ x+0(FP), BX + MOVQ y+8(FP), BP + ADDQ BP, BX + MOVQ BX, ret+16(FP) + RET diff --git a/tests/legacy/examples/lib/add_arm64.s b/tests/legacy/examples/lib/add_arm64.s new file mode 100644 index 00000000..7ade97d3 --- /dev/null +++ b/tests/legacy/examples/lib/add_arm64.s @@ -0,0 +1,20 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +TEXT ·add(SB), $0 + MOVD x+0(FP), R0 + MOVD y+8(FP), R1 + ADD R0, R1, R0 + MOVD R0, ret+16(FP) + RET diff --git a/tests/legacy/examples/lib/asm.go b/tests/legacy/examples/lib/asm.go new file mode 100644 index 00000000..a65f29d5 --- /dev/null +++ b/tests/legacy/examples/lib/asm.go @@ -0,0 +1,14 @@ +package lib + +import "fmt" + +func add(x, y int64) int64 +func sub(x, y int64) int64 + +func AddTwoNumbers() { + fmt.Println("2 + 3 =", add(2, 3)) +} + +func SubTwoNumbers() { + fmt.Println("2 - 3 =", sub(2, 3)) +}
\ No newline at end of file diff --git a/tests/legacy/examples/lib/deep/BUILD.bazel b/tests/legacy/examples/lib/deep/BUILD.bazel new file mode 100644 index 00000000..e827a6a8 --- /dev/null +++ b/tests/legacy/examples/lib/deep/BUILD.bazel @@ -0,0 +1,12 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +package(default_visibility = ["//tests/legacy/examples/lib:__pkg__"]) + +go_library( + name = "go_default_library", + srcs = [ + "doc.go", + "thought.go", + ], + importpath = "github.com/bazelbuild/rules_go/examples/lib/deep", +) diff --git a/tests/legacy/examples/lib/deep/doc.go b/tests/legacy/examples/lib/deep/doc.go new file mode 100644 index 00000000..f4b4b2d2 --- /dev/null +++ b/tests/legacy/examples/lib/deep/doc.go @@ -0,0 +1,3 @@ +// Package deep provides an emulator of a computer which calculates +// answer to the ultimate question of Life, the Universe, and Everything. +package deep diff --git a/tests/legacy/examples/lib/deep/thought.go b/tests/legacy/examples/lib/deep/thought.go new file mode 100644 index 00000000..a4d0d728 --- /dev/null +++ b/tests/legacy/examples/lib/deep/thought.go @@ -0,0 +1,6 @@ +package deep + +// Thought emulates Deep Thought. +func Thought() int { + return 42 +} diff --git a/tests/legacy/examples/lib/lib.go b/tests/legacy/examples/lib/lib.go new file mode 100644 index 00000000..03a932c2 --- /dev/null +++ b/tests/legacy/examples/lib/lib.go @@ -0,0 +1,41 @@ +/* Copyright 2016 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lib + +import ( + "reflect" + + "github.com/bazelbuild/rules_go/examples/lib/deep" +) + +var buildTime string + +// Meaning returns the meaning of Life, the Universe and Everything. +func Meaning() int { + return deep.Thought() +} + +type dummy struct{} + +// PkgPath returns the package importpath of this package. +func PkgPath() string { + return reflect.TypeOf(dummy{}).PkgPath() +} + +// BuildTime returns the buildTime which should be replaced with -X flag. +func BuildTime() string { + return buildTime +} diff --git a/tests/legacy/examples/lib/lib_test.go b/tests/legacy/examples/lib/lib_test.go new file mode 100644 index 00000000..494d854a --- /dev/null +++ b/tests/legacy/examples/lib/lib_test.go @@ -0,0 +1,30 @@ +/* Copyright 2016 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lib + +import ( + "testing" +) + +func TestMeaning(t *testing.T) { + if m := Meaning(); m != 42 { + t.Errorf("got %d, want 42", m) + } +} + +func TestAddTwoNumbers(t *testing.T) { + AddTwoNumbers() +} diff --git a/tests/legacy/examples/lib/lib_x_test.go b/tests/legacy/examples/lib/lib_x_test.go new file mode 100644 index 00000000..0256cb75 --- /dev/null +++ b/tests/legacy/examples/lib/lib_x_test.go @@ -0,0 +1,52 @@ +/* Copyright 2016 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lib_test + +import ( + "flag" + "os" + "testing" + + "github.com/bazelbuild/rules_go/examples/lib" +) + +var ( + buildTimeWant = flag.String("lib_test.buildtime", "", "expected value in TestBuildTime") + wasTestMainCalled = false +) + +func TestLibraryPkgPath(t *testing.T) { + if got, want := lib.PkgPath(), "github.com/bazelbuild/rules_go/examples/lib"; got != want { + t.Errorf("lib.PkgPath() = %q; want %q", got, want) + } +} + +func TestBuildTime(t *testing.T) { + if got, want := lib.BuildTime(), *buildTimeWant; got != want { + t.Errorf("buildTime = %q; want %q", got, want) + } +} + +func TestMainCalled(t *testing.T) { + if !wasTestMainCalled { + t.Errorf("TestMain was not called") + } +} + +func TestMain(m *testing.M) { + wasTestMainCalled = true + os.Exit(m.Run()) +} diff --git a/tests/legacy/examples/lib/sub_amd64.s b/tests/legacy/examples/lib/sub_amd64.s new file mode 100644 index 00000000..78694846 --- /dev/null +++ b/tests/legacy/examples/lib/sub_amd64.s @@ -0,0 +1,8 @@ +#include "textflag.h" + +TEXT ·sub(SB),NOSPLIT,$0 + MOVQ x+0(FP), BX + MOVQ y+8(FP), BP + SUBQ BP, BX + MOVQ BX, ret+16(FP) + RET diff --git a/tests/legacy/examples/lib/sub_arm64.s b/tests/legacy/examples/lib/sub_arm64.s new file mode 100644 index 00000000..0af72ce8 --- /dev/null +++ b/tests/legacy/examples/lib/sub_arm64.s @@ -0,0 +1,20 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +TEXT ·sub(SB), $0 + MOVD x+0(FP), R0 + MOVD y+8(FP), R1 + SUB R0, R1, R0 + MOVD R0, ret+16(FP) + RET diff --git a/tests/legacy/examples/monobuild/cmd/BUILD.bazel b/tests/legacy/examples/monobuild/cmd/BUILD.bazel new file mode 100644 index 00000000..4d48d506 --- /dev/null +++ b/tests/legacy/examples/monobuild/cmd/BUILD.bazel @@ -0,0 +1,14 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary") + +package( + default_visibility = ["//visibility:public"], +) + +go_binary( + name = "mycmd", + srcs = ["mycmd.go"], + deps = [ + "//tests/legacy/examples/monobuild/lib1:go_default_library", + "//tests/legacy/examples/monobuild/lib2:go_default_library", + ], +) diff --git a/tests/legacy/examples/monobuild/cmd/mycmd.go b/tests/legacy/examples/monobuild/cmd/mycmd.go new file mode 100644 index 00000000..1e4ce255 --- /dev/null +++ b/tests/legacy/examples/monobuild/cmd/mycmd.go @@ -0,0 +1,28 @@ +/* Copyright 2017 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + + "full/import/path/to/lib1" + "totally/different/import/for/lib2" +) + +func main() { + fmt.Println("lib1: ", lib1.Name()) + fmt.Println("lib2: ", lib2.Description()) +} diff --git a/tests/legacy/examples/monobuild/lib1/BUILD.bazel b/tests/legacy/examples/monobuild/lib1/BUILD.bazel new file mode 100644 index 00000000..e460ddd4 --- /dev/null +++ b/tests/legacy/examples/monobuild/lib1/BUILD.bazel @@ -0,0 +1,11 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +package(default_visibility = ["//tests/legacy/examples/monobuild/cmd:__pkg__"]) + +go_library( + name = "go_default_library", + srcs = [ + "lib1.go", + ], + importpath = "full/import/path/to/lib1", +) diff --git a/tests/legacy/examples/monobuild/lib1/lib1.go b/tests/legacy/examples/monobuild/lib1/lib1.go new file mode 100644 index 00000000..a99ac29b --- /dev/null +++ b/tests/legacy/examples/monobuild/lib1/lib1.go @@ -0,0 +1,20 @@ +/* Copyright 2017 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lib1 + +func Name() string { + return "I am lib 1" +} diff --git a/tests/legacy/examples/monobuild/lib2/BUILD.bazel b/tests/legacy/examples/monobuild/lib2/BUILD.bazel new file mode 100644 index 00000000..f73f7da9 --- /dev/null +++ b/tests/legacy/examples/monobuild/lib2/BUILD.bazel @@ -0,0 +1,11 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +package(default_visibility = ["//tests/legacy/examples/monobuild/cmd:__pkg__"]) + +go_library( + name = "go_default_library", + srcs = [ + "lib2.go", + ], + importpath = "totally/different/import/for/lib2", +) diff --git a/tests/legacy/examples/monobuild/lib2/lib2.go b/tests/legacy/examples/monobuild/lib2/lib2.go new file mode 100644 index 00000000..836b365f --- /dev/null +++ b/tests/legacy/examples/monobuild/lib2/lib2.go @@ -0,0 +1,20 @@ +/* Copyright 2017 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lib2 + +func Description() string { + return "I am lib 2" +} diff --git a/tests/legacy/examples/proto/BUILD.bazel b/tests/legacy/examples/proto/BUILD.bazel new file mode 100644 index 00000000..703a53a4 --- /dev/null +++ b/tests/legacy/examples/proto/BUILD.bazel @@ -0,0 +1,34 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["gs.go"], + importpath = "github.com/bazelbuild/rules_go/examples/proto", + visibility = ["//visibility:public"], + deps = [ + "//tests/legacy/examples/proto/gostyle:gostyle_go_proto", + "//tests/legacy/examples/proto/lib:lib_go_proto", + ], +) + +go_test( + name = "proto_test", + size = "small", + srcs = ["proto_test.go"], + pure = "off", + deps = [ + "//tests/legacy/examples/proto/embed:go_default_library", + "//tests/legacy/examples/proto/lib:lib_go_proto", + ], +) + +go_test( + name = "proto_pure_test", + size = "small", + srcs = ["proto_test.go"], + pure = "on", + deps = [ + "//tests/legacy/examples/proto/embed:go_default_library", + "//tests/legacy/examples/proto/lib:lib_go_proto", + ], +) diff --git a/tests/legacy/examples/proto/dep/BUILD.bazel b/tests/legacy/examples/proto/dep/BUILD.bazel new file mode 100644 index 00000000..13e00b87 --- /dev/null +++ b/tests/legacy/examples/proto/dep/BUILD.bazel @@ -0,0 +1,18 @@ +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_library") + +proto_library( + name = "useful_proto", + srcs = ["useful.proto"], + visibility = ["//visibility:public"], + deps = [ + "@com_google_protobuf//:duration_proto", + ], +) + +go_proto_library( + name = "useful_go_proto", + importpath = "github.com/bazelbuild/rules_go/examples/proto/dep", + proto = ":useful_proto", + visibility = ["//visibility:public"], +) diff --git a/tests/legacy/examples/proto/dep/useful.proto b/tests/legacy/examples/proto/dep/useful.proto new file mode 100644 index 00000000..030046e3 --- /dev/null +++ b/tests/legacy/examples/proto/dep/useful.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package very.useful; + +import "google/protobuf/duration.proto"; + +message VeryUseful { + string very = 1; + int32 useful = 2; + google.protobuf.Duration timing = 3; +} diff --git a/tests/legacy/examples/proto/embed/BUILD.bazel b/tests/legacy/examples/proto/embed/BUILD.bazel new file mode 100644 index 00000000..c273fa54 --- /dev/null +++ b/tests/legacy/examples/proto/embed/BUILD.bazel @@ -0,0 +1,28 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_library") + +proto_library( + name = "embed_proto", + srcs = ["embed.proto"], + visibility = ["//visibility:public"], +) + +go_proto_library( + name = "embed_go_proto", + compilers = [ + "@io_bazel_rules_go//proto:go_proto", + "@io_bazel_rules_go//proto:go_proto_validate", + ], + # Note that if you forget the importpath everything will break horribly. + importpath = "github.com/bazelbuild/rules_go/examples/proto/embed", + proto = ":embed_proto", +) + +go_library( + name = "go_default_library", + srcs = ["embed.go"], + embed = [":embed_go_proto"], + importpath = "github.com/bazelbuild/rules_go/examples/proto/embed", + visibility = ["//visibility:public"], +) diff --git a/tests/legacy/examples/proto/embed/embed.go b/tests/legacy/examples/proto/embed/embed.go new file mode 100644 index 00000000..b063a06b --- /dev/null +++ b/tests/legacy/examples/proto/embed/embed.go @@ -0,0 +1,7 @@ +package embed + +func OtherThing() *EmbedExample { + return &EmbedExample{ + A: 42, + } +} diff --git a/tests/legacy/examples/proto/embed/embed.proto b/tests/legacy/examples/proto/embed/embed.proto new file mode 100644 index 00000000..085fd8fe --- /dev/null +++ b/tests/legacy/examples/proto/embed/embed.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package embed; + +option go_package = "github.com/bazelbuild/rules_go/examples/proto/embed"; + +message EmbedExample { + int32 a = 2; +} diff --git a/tests/legacy/examples/proto/gogo/BUILD.bazel b/tests/legacy/examples/proto/gogo/BUILD.bazel new file mode 100644 index 00000000..b228ca5f --- /dev/null +++ b/tests/legacy/examples/proto/gogo/BUILD.bazel @@ -0,0 +1,29 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_library") + +proto_library( + name = "values_proto", + srcs = ["values.proto"], + deps = [ + "@com_google_protobuf//:duration_proto", + "@gogo_special_proto//github.com/gogo/protobuf/gogoproto", + ], +) + +go_proto_library( + name = "values_go_proto", + compilers = [ + "@io_bazel_rules_go//proto:gogofaster_proto", + ], + importpath = "github.com/bazelbuild/rules_go/examples/proto/gogo", + proto = ":values_proto", +) + +go_test( + name = "gogo_test", + srcs = ["gogo_test.go"], + deps = [ + ":values_go_proto", + ], +) diff --git a/tests/legacy/examples/proto/gogo/gogo_test.go b/tests/legacy/examples/proto/gogo/gogo_test.go new file mode 100644 index 00000000..b6fb3eec --- /dev/null +++ b/tests/legacy/examples/proto/gogo/gogo_test.go @@ -0,0 +1,25 @@ +package gogo_test + +import ( + "testing" + + "github.com/bazelbuild/rules_go/examples/proto/gogo" +) + +func TestGoString(t *testing.T) { + p := gogo.Value{Item: 20} + got := p.GoString() + expect := "&gogo.Value{Item: 20,\n}" + if got != expect { + t.Errorf("got %q, expect %q", got, expect) + } +} + +func TestSize(t *testing.T) { + p := gogo.Value{Item: 20} + got := p.Size() + expect := 2 + if got != expect { + t.Errorf("got %v, expect %v", got, expect) + } +} diff --git a/tests/legacy/examples/proto/gogo/values.proto b/tests/legacy/examples/proto/gogo/values.proto new file mode 100644 index 00000000..df542977 --- /dev/null +++ b/tests/legacy/examples/proto/gogo/values.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package gogo; + +import "google/protobuf/duration.proto"; +import "github.com/gogo/protobuf/gogoproto/gogo.proto"; + +option go_package = "github.com/bazelbuild/rules_go/examples/proto/gogo"; +option (gogoproto.gostring_all) = true; + +message Value { + int32 item = 1; + google.protobuf.Duration timing = 3; +} diff --git a/tests/legacy/examples/proto/gostyle/BUILD.bazel b/tests/legacy/examples/proto/gostyle/BUILD.bazel new file mode 100644 index 00000000..1b7d5ff3 --- /dev/null +++ b/tests/legacy/examples/proto/gostyle/BUILD.bazel @@ -0,0 +1,25 @@ +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_library") + +genrule( + name = "copy", + srcs = ["gostyle.proto"], + outs = ["gostyle.gen.proto"], + cmd = "sed -e 's/ano.proto/any.proto/' $< > $@", +) + +proto_library( + name = "gostyle_proto", + srcs = [":copy"], + visibility = ["//visibility:public"], + deps = [ + "@com_google_protobuf//:any_proto", + ], +) + +go_proto_library( + name = "gostyle_go_proto", + importpath = "github.com/bazelbuild/rules_go/examples/proto/gostyle", + proto = ":gostyle_proto", + visibility = ["//visibility:public"], +) diff --git a/tests/legacy/examples/proto/gostyle/gostyle.proto b/tests/legacy/examples/proto/gostyle/gostyle.proto new file mode 100644 index 00000000..8b95d399 --- /dev/null +++ b/tests/legacy/examples/proto/gostyle/gostyle.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +package gostyle; + +option go_package = "github.com/bazelbuild/rules_go/examples/proto/gostyle"; + +import "google/protobuf/ano.proto"; + +message GoStyleObject { + int32 go_not_bazel = 2; + google.protobuf.Any anything = 3; +} diff --git a/tests/legacy/examples/proto/grpc/BUILD.bazel b/tests/legacy/examples/proto/grpc/BUILD.bazel new file mode 100644 index 00000000..671dec57 --- /dev/null +++ b/tests/legacy/examples/proto/grpc/BUILD.bazel @@ -0,0 +1,44 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary") +load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library", "go_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_library") + +proto_library( + name = "my_svc_proto", + srcs = ["my_svc.proto"], + deps = [ + "//tests/legacy/examples/proto/lib:lib_proto", + "@com_google_protobuf//:any_proto", + "@com_google_protobuf//:empty_proto", + ], +) + +go_proto_library( + name = "not_grpc", + importpath = "github.com/bazelbuild/rules_go/examples/proto/grpc/my_svc_proto", + proto = ":my_svc_proto", + deps = [ + "//tests/legacy/examples/proto/lib:lib_go_proto", + ], +) + +go_grpc_library( + name = "my_svc_go_proto", + importpath = "github.com/bazelbuild/rules_go/examples/proto/grpc/my_svc_proto", + proto = ":my_svc_proto", + deps = [ + "//tests/legacy/examples/proto/lib:lib_go_proto", + ], +) + +go_binary( + name = "test_grpc", + srcs = ["main.go"], + deps = [ + ":my_svc_go_proto", + "//tests/legacy/examples/proto/lib:lib_go_proto", + "@io_bazel_rules_go//proto/wkt:any_go_proto", + "@io_bazel_rules_go//proto/wkt:empty_go_proto", + "@org_golang_google_grpc//:go_default_library", + "@org_golang_x_net//context:go_default_library", + ], +) diff --git a/tests/legacy/examples/proto/grpc/main.go b/tests/legacy/examples/proto/grpc/main.go new file mode 100644 index 00000000..b62c2319 --- /dev/null +++ b/tests/legacy/examples/proto/grpc/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "log" + "net" + + pb "github.com/bazelbuild/rules_go/examples/proto/grpc/my_svc_proto" + lpb "github.com/bazelbuild/rules_go/examples/proto/lib/lib_proto" + apb "github.com/golang/protobuf/ptypes/any" + epb "github.com/golang/protobuf/ptypes/empty" + "golang.org/x/net/context" + "google.golang.org/grpc" +) + +type server struct{} + +func (s *server) Get(ctx context.Context, req *pb.GetRequest) (*epb.Empty, error) { + return &epb.Empty{}, nil +} + +func (s *server) Put(ctx context.Context, req *apb.Any) (*lpb.LibObject, error) { + return &lpb.LibObject{}, nil +} + +func main() { + lis, err := net.Listen("tcp", ":8080") + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + s := grpc.NewServer() + pb.RegisterMyServiceServer(s, &server{}) + s.Serve(lis) +} diff --git a/tests/legacy/examples/proto/grpc/my_svc.proto b/tests/legacy/examples/proto/grpc/my_svc.proto new file mode 100644 index 00000000..16344c12 --- /dev/null +++ b/tests/legacy/examples/proto/grpc/my_svc.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; + +package examples.svc; + +option go_package = "github.com/bazelbuild/rules_go/examples/proto/grpc/my_svc_proto"; + +import "tests/legacy/examples/proto/lib/lib.proto"; +import "google/protobuf/any.proto"; +import "google/protobuf/empty.proto"; + +message GetRequest { + examples.lib.LibObject obj = 1; +} + +service MyService { + rpc Get(GetRequest) returns (google.protobuf.Empty); + rpc Put(google.protobuf.Any) returns (examples.lib.LibObject); +} diff --git a/tests/legacy/examples/proto/gs.go b/tests/legacy/examples/proto/gs.go new file mode 100644 index 00000000..c52dcba8 --- /dev/null +++ b/tests/legacy/examples/proto/gs.go @@ -0,0 +1,22 @@ +package proto + +import ( + "fmt" + + "github.com/bazelbuild/rules_go/examples/proto/gostyle" + lib_proto "github.com/bazelbuild/rules_go/examples/proto/lib/lib_proto" +) + +func DoGoStyle(g *gostyle.GoStyleObject) error { + if g == nil { + return fmt.Errorf("got nil") + } + return nil +} + +func DoMultiProtos(a *lib_proto.LibObject, b *lib_proto.LibObject2) error { + if a == nil || b == nil { + return fmt.Errorf("got nil") + } + return nil +} diff --git a/tests/legacy/examples/proto/lib/BUILD.bazel b/tests/legacy/examples/proto/lib/BUILD.bazel new file mode 100644 index 00000000..61215267 --- /dev/null +++ b/tests/legacy/examples/proto/lib/BUILD.bazel @@ -0,0 +1,24 @@ +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") +load("@rules_proto//proto:defs.bzl", "proto_library") + +proto_library( + name = "lib_proto", + srcs = [ + "lib.proto", + "lib2.proto", + ], + visibility = ["//visibility:public"], + deps = [ + "//tests/legacy/examples/proto/dep:useful_proto", + ], +) + +go_proto_library( + name = "lib_go_proto", + importpath = "github.com/bazelbuild/rules_go/examples/proto/lib/lib_proto", + proto = ":lib_proto", + visibility = ["//visibility:public"], + deps = [ + "//tests/legacy/examples/proto/dep:useful_go_proto", + ], +) diff --git a/tests/legacy/examples/proto/lib/lib.proto b/tests/legacy/examples/proto/lib/lib.proto new file mode 100644 index 00000000..b591f453 --- /dev/null +++ b/tests/legacy/examples/proto/lib/lib.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +package examples.lib; + +option go_package = "github.com/bazelbuild/rules_go/examples/proto/lib/lib_proto"; + +import "tests/legacy/examples/proto/dep/useful.proto"; + +message LibObject { + very.useful.VeryUseful very_useful = 1; + int32 are_you_sure = 2; +} diff --git a/tests/legacy/examples/proto/lib/lib2.proto b/tests/legacy/examples/proto/lib/lib2.proto new file mode 100644 index 00000000..5c3d16d6 --- /dev/null +++ b/tests/legacy/examples/proto/lib/lib2.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package examples.lib; + +option go_package = "github.com/bazelbuild/rules_go/examples/proto/lib/lib_proto"; + +import "tests/legacy/examples/proto/dep/useful.proto"; +import "tests/legacy/examples/proto/lib/lib.proto"; + +message LibObject2 { + very.useful.VeryUseful very_useful = 1; + int32 are_you_sure = 2; + LibObject lib_object = 4; +} diff --git a/tests/legacy/examples/proto/proto_test.go b/tests/legacy/examples/proto/proto_test.go new file mode 100644 index 00000000..68ba8348 --- /dev/null +++ b/tests/legacy/examples/proto/proto_test.go @@ -0,0 +1,43 @@ +/* Copyright 2017 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package proto + +import ( + "testing" + + embed "github.com/bazelbuild/rules_go/examples/proto/embed" + lib_proto "github.com/bazelbuild/rules_go/examples/proto/lib/lib_proto" +) + +func TestProto(t *testing.T) { + p := lib_proto.LibObject{AreYouSure: 20} + sure := p.GetAreYouSure() + if sure != 20 { + t.Errorf("got %d, want 20", sure) + } +} + +func TestEmbed(t *testing.T) { + if embed.OtherThing().A != 42 { + t.Errorf("Unable to call method from embedded go files") + } +} + +func TestValidate(t *testing.T) { + if err := embed.OtherThing().Validate(); err != nil { + t.Errorf("Proto did not pass validation") + } +} diff --git a/tests/legacy/examples/stamped_bin/BUILD.bazel b/tests/legacy/examples/stamped_bin/BUILD.bazel new file mode 100644 index 00000000..4607f53d --- /dev/null +++ b/tests/legacy/examples/stamped_bin/BUILD.bazel @@ -0,0 +1,6 @@ +load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test") + +go_bazel_test( + name = "stamped_test", + srcs = ["stamped_test.go"], +) diff --git a/tests/legacy/examples/stamped_bin/stamped_test.go b/tests/legacy/examples/stamped_bin/stamped_test.go new file mode 100644 index 00000000..3c9fad92 --- /dev/null +++ b/tests/legacy/examples/stamped_bin/stamped_test.go @@ -0,0 +1,122 @@ +// Copyright 2019 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package no_prefix_test + +import ( + "testing" + + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" +) + +const mainFiles = ` +-- BUILD.bazel -- +load("@io_bazel_rules_go//go:def.bzl", "go_test", "go_library") + +go_library( + name = "stamp", + srcs = ["stamp.go"], + importpath = "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp", + visibility = ["//visibility:public"], + x_defs = { + "XdefBuildTimestamp": "{BUILD_TIMESTAMP}", + }, +) + +go_test( + name = "stamp_with_x_defs", + size = "small", + srcs = ["stamped_bin_test.go"], + x_defs = { + "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.BUILD_TIMESTAMP": "{BUILD_TIMESTAMP}", + "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.PassIfEmpty": "", + "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.XdefInvalid": "{Undefined_Var}", # undefined should leave the var alone + "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.Multiple": "{BUILD_TIMESTAMP}{BUILD_TIMESTAMP}", + }, + deps = [":stamp"], +) + +-- stamp.go -- +package stamp + +var BUILD_TIMESTAMP = "fail" + +// an xdef should set this to "" +var PassIfEmpty = "fail" + +// an xdef should set this to nonempty +var XdefBuildTimestamp = "" + +// an xdef with a missing key should leave this alone +var XdefInvalid = "pass" + +// an xdef with multiple keys +var Multiple = "fail" + +-- stamped_bin_test.go -- +package stamped_bin_test + +import ( + "testing" + + "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp" +) + +func TestStampedBin(t *testing.T) { + // If we use an x_def when linking to override BUILD_TIMESTAMP but fail to + // pass through the workspace status value, it'll be set to empty string - + // overridden but still wrong. Check for that case too. + if stamp.BUILD_TIMESTAMP == "fail" || stamp.BUILD_TIMESTAMP == "" { + t.Errorf("Expected timestamp to have been modified, got %s.", stamp.BUILD_TIMESTAMP) + } + if stamp.XdefBuildTimestamp == "" { + t.Errorf("Expected XdefBuildTimestamp to have been modified, got %s.", stamp.XdefBuildTimestamp) + } + if stamp.PassIfEmpty != "" { + t.Errorf("Expected PassIfEmpty to have been set to '', got %s.", stamp.PassIfEmpty) + } + if stamp.XdefInvalid != "pass" { + t.Errorf("Expected XdefInvalid to have been left alone, got %s.", stamp.XdefInvalid) + } + if stamp.Multiple != stamp.BUILD_TIMESTAMP + stamp.BUILD_TIMESTAMP { + t.Errorf("Expected Multiple to have two BUILD_TIMESTAMP, got %s.", stamp.Multiple) + } +} + +` + +func TestMain(m *testing.M) { + bazel_testing.TestMain(m, bazel_testing.Args{ + Main: mainFiles, + }) +} + +func TestBuild(t *testing.T) { + if err := bazel_testing.RunBazel("test", "--stamp", ":all"); err != nil { + t.Fatal(err) + } +} + +func TestBuildWithoutStamp(t *testing.T) { + if err := bazel_testing.RunBazel("test", "--nostamp", ":all"); err != nil { + if eErr, ok := err.(*bazel_testing.StderrExitError); ok { + if eErr.Err.ExitCode() == 3 { // 3 is TEST_FAILED bazel exit code + return + } + t.Fatalf("expected tests to have failed (instead got exit code %d)", eErr.Err.ExitCode()) + } + t.Fatal("expected bazel_testing.StderrExitError") + } + t.Fatal("expected error") +} diff --git a/tests/legacy/examples/vendor/github.com/user/vendored/BUILD.bazel b/tests/legacy/examples/vendor/github.com/user/vendored/BUILD.bazel new file mode 100644 index 00000000..f1c69c5f --- /dev/null +++ b/tests/legacy/examples/vendor/github.com/user/vendored/BUILD.bazel @@ -0,0 +1,13 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +package( + default_visibility = ["//visibility:public"], +) + +go_library( + name = "go_default_library", + srcs = [ + "vendored.go", + ], + importpath = "github.com/user/vendored", +) diff --git a/tests/legacy/examples/vendor/github.com/user/vendored/vendored.go b/tests/legacy/examples/vendor/github.com/user/vendored/vendored.go new file mode 100644 index 00000000..81341a9a --- /dev/null +++ b/tests/legacy/examples/vendor/github.com/user/vendored/vendored.go @@ -0,0 +1,21 @@ +/* Copyright 2016 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package vendored + +// Vendored returns a string. +func Vendored() string { + return "I was vendored" +} |