Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
bazel: native go-mockgen in Bazel (#60386)
Browse files Browse the repository at this point in the history
Adds a new:
- gazelle generator
- rule + rule targets + catchall target
for generating go-mockgen mocks & testing for their being up-to-date.

Each go_mockgen macro invocation adds targets for generating mocks, copying to the source tree, as well as testing whether the current source tree mocks are up-to-date.

How to use this: `bazel run //dev:go_mockgen` for the catch-all, or `bazel run //some/target:generate_mocks` for an individual package, and `bazel test //some/target:generate_mocks_tests` to test for up-to-date-ness. There is no catch-all for testing

This currently uses a fork of go-mockgen, with an open PR for upstream here: derision-test/go-mockgen#50.

Closes https://github.com/sourcegraph/sourcegraph/issues/60099

## Test plan

Extensive testing during development, including the following cases:
- Deleting a generated file and its entry in a go_library/go_test `srcs` attribute list and then re-running `sg bazel configure`
- Adding a non-existent output directory to mockgen.test.yaml and running the bash one-liner emitted to prepare the workspace for rerunning `sg bazel configure`

The existing config tests a lot of existing paths anyway (creating mocks for a 3rd party library's interface, entries for a given output file in >1 config file etc)
  • Loading branch information
Strum355 authored Feb 16, 2024
1 parent 235781a commit 19d9cfc
Show file tree
Hide file tree
Showing 97 changed files with 1,763 additions and 25 deletions.
5 changes: 3 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ load("@buildifier_prebuilt//:rules.bzl", "buildifier")
# gazelle:js_npm_package_target_name {dirname}_pkg

# Enable: Aspect javascript, standard go
# gazelle:lang js,go,proto
# gazelle:lang js,go,proto,gomockgen

# Enable race detection for all go_test rules.
# gazelle:map_kind go_test go_test //dev:go_defs.bzl
Expand Down Expand Up @@ -212,6 +212,7 @@ gazelle_binary(
# Gazelle-buf does not include the Go plugin by default, so we have to add it
# ourselves.
"@bazel_gazelle//language/go:go_default_library",
"//dev/go-mockgen-gazelle:go-mockgen-gazelle",
# Bundled with aspect-cli, but we're missing out on buf that way
"@aspect_cli//gazelle/js:js",
# Loads the Buf extension
Expand Down Expand Up @@ -335,7 +336,7 @@ exports_files([
# under certain conditions. See //ui/assets/...
"CONTRIBUTING.md",
".swcrc",
])
] + glob(["mockgen*.yaml"]))

# sg msp settings

Expand Down
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ http_archive(
url = "https://github.com/aspect-build/aspect-cli/archive/5.8.20.tar.gz",
)

http_archive(
name = "rules_multirun",
sha256 = "9cd384e42b2da00104f0e18f25e66285aa21f64b573c667638a7a213206885ab",
strip_prefix = "rules_multirun-0.6.1",
url = "https://github.com/keith/rules_multirun/archive/refs/tags/0.6.1.tar.gz",
)

# hermetic_cc_toolchain setup ================================
HERMETIC_CC_TOOLCHAIN_VERSION = "v2.2.1"

Expand Down
12 changes: 12 additions & 0 deletions cmd/cody-gateway/internal/auth/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -44,3 +45,14 @@ go_test(
"@com_github_vektah_gqlparser_v2//gqlerror",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = ["@com_github_gregjones_httpcache//:httpcache"],
)
12 changes: 12 additions & 0 deletions cmd/cody-gateway/internal/dotcom/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("//dev:go_defs.bzl", "go_test")
load("//dev:write_generated_to_source_files.bzl", "write_generated_to_source_files")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//dev:go_mockgen.bzl", "go_mockgen")

go_library(
name = "dotcom",
Expand Down Expand Up @@ -47,3 +48,14 @@ go_test(
embed = [":dotcom"],
deps = ["@com_github_stretchr_testify//assert"],
)

go_mockgen(
name = "generate_mocks",
out = "mocks.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = ["@com_github_khan_genqlient//graphql"],
)
20 changes: 20 additions & 0 deletions cmd/executor/internal/worker/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -64,3 +65,22 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [
"//cmd/executor/internal/util",
"//cmd/executor/internal/worker/cmdlogger",
"//cmd/executor/internal/worker/command",
"//cmd/executor/internal/worker/files",
"//cmd/executor/internal/worker/runner",
"//cmd/executor/internal/worker/runtime",
"//cmd/executor/internal/worker/workspace",
],
)
12 changes: 12 additions & 0 deletions cmd/executor/internal/worker/cmdlogger/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//dev:go_defs.bzl", "go_test")

Expand Down Expand Up @@ -28,3 +29,14 @@ go_test(
"@com_github_sourcegraph_log//logtest",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [":cmdlogger"],
)
15 changes: 15 additions & 0 deletions cmd/executor/internal/worker/command/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -72,3 +73,17 @@ go_test(
"@io_k8s_utils//pointer",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [
":command",
"//cmd/executor/internal/worker/cmdlogger",
],
)
12 changes: 12 additions & 0 deletions cmd/executor/internal/worker/files/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -26,3 +27,14 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [":files"],
)
16 changes: 16 additions & 0 deletions cmd/executor/internal/worker/runner/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -60,3 +61,18 @@ go_test(
"@io_k8s_client_go//testing",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [
"//cmd/executor/internal/worker/cmdlogger",
"//cmd/executor/internal/worker/command",
"//cmd/executor/internal/worker/files",
],
)
18 changes: 18 additions & 0 deletions cmd/executor/internal/worker/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -56,3 +57,20 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [
"//cmd/executor/internal/util",
"//cmd/executor/internal/worker/cmdlogger",
"//cmd/executor/internal/worker/command",
"//cmd/executor/internal/worker/files",
"//cmd/executor/internal/worker/workspace",
],
)
17 changes: 17 additions & 0 deletions cmd/executor/internal/worker/workspace/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -49,3 +50,19 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [
"//cmd/executor/internal/util",
"//cmd/executor/internal/worker/cmdlogger",
"//cmd/executor/internal/worker/command",
"//cmd/executor/internal/worker/files",
],
)
12 changes: 12 additions & 0 deletions cmd/frontend/backend/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -133,3 +134,14 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_temp.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [":backend"],
)
12 changes: 12 additions & 0 deletions cmd/frontend/internal/executorqueue/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -54,3 +55,14 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [":executorqueue"],
)
12 changes: 12 additions & 0 deletions cmd/frontend/internal/executorqueue/queues/batches/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -58,3 +59,14 @@ go_test(
"@in_gopkg_yaml_v2//:yaml_v2",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [":batches"],
)
12 changes: 12 additions & 0 deletions cmd/frontend/internal/httpapi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -141,3 +142,14 @@ go_test(
"@org_golang_google_protobuf//testing/protocmp",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = ["//internal/httpcli"],
)
12 changes: 12 additions & 0 deletions cmd/frontend/internal/httpapi/releasecache/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//dev:go_mockgen.bzl", "go_mockgen")
load("//dev:go_defs.bzl", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

Expand Down Expand Up @@ -58,3 +59,14 @@ go_test(
"@com_github_stretchr_testify//require",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [":releasecache"],
)
15 changes: 14 additions & 1 deletion cmd/gitserver/internal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//dev:go_defs.bzl", "go_test")
load("//dev:go_mockgen.bzl", "go_mockgen")

go_library(
name = "internal",
Expand All @@ -22,7 +23,8 @@ go_library(
],
embedsrcs = ["sg_maintenance.sh"],
importpath = "github.com/sourcegraph/sourcegraph/cmd/gitserver/internal",
visibility = ["//cmd/gitserver:__subpackages__"],
# visibility = ["//cmd/gitserver:__subpackages__"],
visibility = ["//visibility:public"],
deps = [
"//cmd/frontend/envvar",
"//cmd/gitserver/internal/accesslog",
Expand Down Expand Up @@ -149,3 +151,14 @@ go_test(
"@org_golang_x_time//rate",
],
)

go_mockgen(
name = "generate_mocks",
out = "mocks_test.go",
manifests = [
"//:mockgen.yaml",
"//:mockgen.test.yaml",
"//:mockgen.temp.yaml",
],
deps = [":internal"],
)
Loading

0 comments on commit 19d9cfc

Please sign in to comment.