Skip to content

Commit

Permalink
bazel: link validation libraries into test executable (#6)
Browse files Browse the repository at this point in the history
The bazel-built test executable was executing but not succeeding because the
validation code was not getting linked in at compile-time and the test used
lazy duck-typing to validate. This patch

1. makes the test fail loudly if an input doesn't implement the correct interface
2. updates the bazel rule to link in the validation code
3. fixes the test code so it imports the validation code
4. modifies the CI script to also run the tests via bazel
  • Loading branch information
akonradi authored and htuch committed Nov 7, 2017
1 parent 9ac970c commit 23d8d65
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ tests/harness/go/go-harness:
go build -o ./tests/harness/go/go-harness ./tests/harness/go

.PHONY: ci
ci: build tests kitchensink testcases harness
ci: build tests kitchensink testcases harness bazel-harness
12 changes: 9 additions & 3 deletions bazel/go_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ _GO_GOOGLE_PROTOBUF = "go_google_protobuf"
_PROTOBUF_REPO = "@com_github_golang_protobuf"
_WELL_KNOWN_REPO = _PROTOBUF_REPO + "//ptypes/"

_WELL_KNOWN_PTYPES = _PROTOBUF_REPO + "//ptypes:" + _DEFAULT_LIB

def _collect_protos_import(ctx):
"""Collect the list of transitive protos and m_import_path.
Expand Down Expand Up @@ -303,6 +305,8 @@ def go_proto_library(name, srcs = None, deps = None,

if not validate:
protoc_gen_validate = None
else:
outs += [s[:-len(".proto")] + ".pb.validate.go" for s in outs]

_go_proto_library_gen(
name = name + _PROTOS_SUFFIX,
Expand All @@ -320,13 +324,15 @@ def go_proto_library(name, srcs = None, deps = None,
protoc_gen_go = protoc_gen_go,
protoc_gen_validate = protoc_gen_validate,
)
grpc_deps = []
go_lib_deps = []
if has_services:
grpc_deps += [x_net_context, google_grpc]
go_lib_deps += [x_net_context, google_grpc]
if validate:
go_lib_deps += [_WELL_KNOWN_PTYPES]
go_library(
name = name,
srcs = [":" + name + _PROTOS_SUFFIX],
deps = deps + grpc_deps + [golang_protobuf],
deps = deps + go_lib_deps + [golang_protobuf],
testonly = testonly,
visibility = visibility,
**kwargs
Expand Down
1 change: 1 addition & 0 deletions tests/harness/cases/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pgv_go_proto_library(
"wkt_timestamp.proto",
"wkt_wrappers.proto",
],
importpath = "github.com/lyft/protoc-gen-validate/tests/harness/cases/go",
rules_go_repo_only_for_internal_use = "",
deps = [
"@com_github_golang_protobuf//ptypes/any:go_default_library",
Expand Down
4 changes: 4 additions & 0 deletions tests/harness/executor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ go_test(
library = ":go_default_library",
rundir = ".",
visibility = ["//visibility:public"],
deps = [
"//tests/harness/cases:go",
"//tests/harness/go:go-harness",
],
)
10 changes: 4 additions & 6 deletions tests/harness/go/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ func main() {
da := new(ptypes.DynamicAny)
checkErr(ptypes.UnmarshalAny(tc.Message, da))

if msg, ok := da.Message.(interface {
msg := da.Message.(interface {
Validate() error
}); ok {
checkValid(msg.Validate())
} else {
checkValid(nil)
}
})
checkValid(msg.Validate())

}

func checkValid(err error) {
Expand Down

0 comments on commit 23d8d65

Please sign in to comment.