Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 16 additions & 26 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ make build # Build all services
make test # Run unit tests
make lint # Run all linters (fmt + YAML)
make fmt # Format Go and YAML code
make check-mocks # Check mock files are up to date
make check-tidy # Check go.mod and MODULE.bazel are tidy
make check-gazelle # Check BUILD.bazel files are up to date
make tidy # Run go mod tidy + bazel mod tidy
make gazelle # Update BUILD.bazel files
make mocks # Generate mock files using mockgen
make integration-test # Run all integration tests (Docker-based)
make e2e-test # Run end-to-end tests
make proto # Regenerate proto files
Expand Down Expand Up @@ -194,41 +196,29 @@ make clean # Clean Bazel cache

**Add gomock for an extension interface:**

Mocks use the Bazel `gomock` rule from `@rules_go//extras:gomock.bzl` and are generated at build time (not checked in). See `extension/storage/mock/` for the canonical example.
Mocks are checked-in files generated by [mockgen](https://github.com/uber-go/mock). Run `make mocks` to regenerate, then `make gazelle` to update BUILD files. See `extension/storage/mock/` for the canonical example.

To add a mock for a new interface file in an existing mock package (e.g., `extension/storage/new_store.go`):

1. Add a `//go:generate` directive to the interface file:
```go
//go:generate mockgen -source=new_store.go -destination=mock/new_store.go -package=mock
//go:generate mockgen -source=new_store.go -destination=mock/new_store_mock.go -package=mock
```
2. Add the file to `exports_files` in the parent `BUILD.bazel` with visibility to the mock package:
```starlark
exports_files(
["new_store.go", ...],
visibility = ["//extension/storage/mock:__pkg__"],
)
```
3. Add a `gomock` rule in the mock `BUILD.bazel`:
```starlark
gomock(
name = "mock_new_store_src",
out = "new_store_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/storage:new_store.go",
source_importpath = "github.com/uber/submitqueue/extension/storage",
)
```
4. Add the rule target to the `go_library` srcs in the same file.
2. Run `make mocks` to generate the mock file.
3. Run `make gazelle` to update `BUILD.bazel` files.
4. Commit the generated mock file.

To create a mock package for a new extension (e.g., `extension/newext/mock/`):

To create a mock package for a new extension (e.g., `extension/queue/mock/`):
1. Add `//go:generate` directives to each interface file (same pattern as above).
2. Create the `mock/` directory: `mkdir extension/newext/mock/`.
3. Run `make mocks` to generate mock files into the new directory.
4. Run `make gazelle` to create the `BUILD.bazel` file automatically.

1. Create `extension/{ext}/mock/BUILD.bazel` with `gomock` rules, a `go_library`, and `# gazelle:ignore`.
2. Add `exports_files` to `extension/{ext}/BUILD.bazel` for each interface file.
3. Follow the same per-interface pattern as above.
For inline mocks (mock in the same package, e.g., `extension/queue/mysql/mock_stores.go`):

Mock `BUILD.bazel` files use `# gazelle:ignore` so `make gazelle` will not update them — they must be maintained manually.
1. Add a `//go:generate` directive with `-package=mypkg` and `-destination=mock_file.go`.
2. Run `make mocks` and `make gazelle`.

**Using mocks in tests:**
```go
Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ define assert_clean
fi
endef

.PHONY: build build-all-linux build-gateway-linux build-orchestrator-linux check-gazelle check-tidy clean clean-proto deps e2e-test fmt gazelle integration-test integration-test-consumer integration-test-extensions integration-test-gateway integration-test-orchestrator license-fix lint lint-fmt lint-license local-clean local-gateway-start local-gateway-stop local-init-schemas local-logs local-orchestrator-start local-orchestrator-stop local-ps local-restart local-start local-stop proto query-deps query-targets run-client-gateway run-client-orchestrator run-queue-admin test test-no-cache tidy tidy-bazel tidy-go help
.PHONY: build build-all-linux build-gateway-linux build-orchestrator-linux check-gazelle check-mocks check-tidy clean clean-proto deps e2e-test fmt gazelle integration-test integration-test-consumer integration-test-extensions integration-test-gateway integration-test-orchestrator license-fix lint lint-fmt lint-license local-clean local-gateway-start local-gateway-stop local-init-schemas local-logs local-orchestrator-start local-orchestrator-stop local-ps local-restart local-start local-stop mocks proto query-deps query-targets run-client-gateway run-client-orchestrator run-queue-admin test test-no-cache tidy tidy-bazel tidy-go help


build: ## Build all services and examples
Expand Down Expand Up @@ -64,6 +64,10 @@ check-gazelle: ## Check BUILD.bazel files are up to date
$(call assert_clean,make gazelle)
@echo "BUILD files are up to date."

check-mocks: mocks ## Check mock files are up to date
$(call assert_clean,make mocks)
@echo "Mock files are up to date."

check-tidy: tidy ## Check that go.mod and MODULE.bazel are tidy
$(call assert_clean,make tidy)
@echo "Module files are up to date."
Expand Down Expand Up @@ -239,6 +243,11 @@ local-stop: ## Stop all services (keep data)
@$(COMPOSE) -f $(COMPOSE_FILE) -p $(LOCAL_PROJECT) down
@echo "Services stopped. Data volumes preserved."

mocks: ## Generate mock files using mockgen
@echo "Generating mocks..."
@$(BAZEL) run @rules_go//go -- generate ./extension/storage/... ./extension/counter/... ./extension/queue/... ./extension/mergechecker/... ./extension/scorer/... ./core/consumer/...
@echo "Mocks generated successfully!"

proto: ## Generate protobuf files from .proto definitions
@echo "Generating protobuf files with protoc..."
@protoc --go_out=gateway/protopb --go_opt=paths=source_relative \
Expand Down
5 changes: 0 additions & 5 deletions core/consumer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")

exports_files(
["controller.go"],
visibility = ["//core/consumer/mock:__pkg__"],
)

go_library(
name = "consumer",
srcs = [
Expand Down
17 changes: 1 addition & 16 deletions core/consumer/mock/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
load("@rules_go//extras:gomock.bzl", "gomock")
load("@rules_go//go:def.bzl", "go_library")

_MOCKGEN = "@org_uber_go_mock//mockgen"

gomock(
name = "mock_controller_src",
out = "controller_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//core/consumer:controller.go",
source_importpath = "github.com/uber/submitqueue/core/consumer",
)

# gazelle:ignore
go_library(
name = "mock",
srcs = [
":mock_controller_src",
],
srcs = ["controller_mock.go"],
importpath = "github.com/uber/submitqueue/core/consumer/mock",
visibility = ["//visibility:public"],
deps = [
Expand Down
207 changes: 207 additions & 0 deletions core/consumer/mock/controller_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions extension/counter/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
load("@rules_go//go:def.bzl", "go_library")

exports_files(
["counter.go"],
visibility = ["//extension/counter/mock:__pkg__"],
)

go_library(
name = "counter",
srcs = ["counter.go"],
Expand Down
2 changes: 1 addition & 1 deletion extension/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package counter

//go:generate mockgen -source=counter.go -destination=mock/counter.go -package=mock
//go:generate mockgen -source=counter.go -destination=mock/counter_mock.go -package=mock

import "context"

Expand Down
22 changes: 2 additions & 20 deletions extension/counter/mock/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
load("@rules_go//extras:gomock.bzl", "gomock")
load("@rules_go//go:def.bzl", "go_library")

_MOCKGEN = "@org_uber_go_mock//mockgen"

gomock(
name = "mock_counter_src",
out = "counter_mock.go",
mockgen_tool = _MOCKGEN,
package = "mock",
source = "//extension/counter:counter.go",
source_importpath = "github.com/uber/submitqueue/extension/counter",
)

# gazelle:ignore
go_library(
name = "mock",
srcs = [
":mock_counter_src",
],
srcs = ["counter_mock.go"],
importpath = "github.com/uber/submitqueue/extension/counter/mock",
visibility = ["//visibility:public"],
deps = [
"//extension/counter",
"@org_uber_go_mock//gomock",
],
deps = ["@org_uber_go_mock//gomock"],
)
Loading