Skip to content

Commit

Permalink
Fix makefile to run test/coverage/lint on all modules in the reposito…
Browse files Browse the repository at this point in the history
…ry (#191)

By default, the `go test ./...` includes all packages and sub-packages
within the current _module_. However, in PR #187 we have introduced
another module (`tools`) inside this repository, which is silently
ignored by test / coverage / lint tasks in our makefile. This PR fixes
this by iterating over a predefined list of modules for all these tasks,
making it expandable for future module additions.

Due to this fix, we have run `gofmt -s` on
`tools/cmd/golden-test/main_test.go` to pass linting.
  • Loading branch information
yuxincs authored Feb 2, 2024
1 parent c09916b commit 9b1d75d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
32 changes: 21 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export GOBIN = $(PROJECT_ROOT)/bin

GOLANGCI_LINT_VERSION := $(shell golangci-lint --version 2>/dev/null)

# Directories containing independent Go modules.
MODULE_DIRS = . ./tools

.PHONY: all
all: build lint test

Expand All @@ -17,12 +20,14 @@ build:

.PHONY: test
test:
go test -v -race ./...
@$(foreach mod,$(MODULE_DIRS),(cd $(mod) && go test -race ./...) &&) true

.PHONY: cover
cover:
go test -v -race -coverprofile=cover.out -coverpkg=./... -v ./...
go tool cover -html=cover.out -o cover.html
@$(foreach mod,$(MODULE_DIRS), ( \
cd $(mod) && \
go test -race -coverprofile=cover.out -coverpkg=./... ./... \
&& go tool cover -html=cover.out -o cover.html) &&) true

.PHONY: golden-test
golden-test:
Expand All @@ -39,17 +44,22 @@ ifdef GOLANGCI_LINT_VERSION
else
$(error "golangci-lint not found, please install it from https://golangci-lint.run/usage/install/#local-installation")
endif
@echo "[lint] golangci-lint run"
@golangci-lint run
@$(foreach mod,$(MODULE_DIRS), \
(cd $(mod) && \
echo "[lint] golangci-lint: $(mod)" && \
golangci-lint run --path-prefix $(mod)) &&) true

.PHONY: tidy-lint
tidy-lint:
@echo "[lint] go mod tidy"
@go mod tidy && \
git diff --exit-code -- go.mod go.sum || \
(echo "'go mod tidy' changed files" && false)
@$(foreach mod,$(MODULE_DIRS), \
(cd $(mod) && \
echo "[lint] mod tidy: $(mod)" && \
go mod tidy && \
git diff --exit-code -- go.mod go.sum) &&) true

.PHONY: nilaway-lint
nilaway-lint: build
@echo "[lint] nilaway linting itself"
@$(GOBIN)/nilaway -include-pkgs="go.uber.org/nilaway" ./...
@$(foreach mod,$(MODULE_DIRS), \
(cd $(mod) && \
echo "[lint] nilaway linting itself: $(mod)" && \
$(GOBIN)/nilaway -include-pkgs="go.uber.org/nilaway" ./...) &&) true
16 changes: 8 additions & 8 deletions tools/cmd/golden-test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func TestWriteDiff(t *testing.T) {
var buf bytes.Buffer
base := map[Diagnostic]bool{
// Same in both.
Diagnostic{Posn: "src/file1:10:2", Message: "nil pointer dereference"}: true,
{Posn: "src/file1:10:2", Message: "nil pointer dereference"}: true,
}
test := map[Diagnostic]bool{
// Same in both.
Diagnostic{Posn: "src/file1:10:2", Message: "nil pointer dereference"}: true,
{Posn: "src/file1:10:2", Message: "nil pointer dereference"}: true,
}
branches := [2]*BranchResult{
{Name: "base", ShortSHA: "123456", Result: base},
Expand All @@ -70,19 +70,19 @@ func TestDiff(t *testing.T) {

base := map[Diagnostic]bool{
// Same in both.
Diagnostic{Posn: "src/file1:10:2", Message: "nil pointer dereference"}: true,
{Posn: "src/file1:10:2", Message: "nil pointer dereference"}: true,
// Differs in position.
Diagnostic{Posn: "src/file2:10:2", Message: "nil pointer dereference"}: true,
{Posn: "src/file2:10:2", Message: "nil pointer dereference"}: true,
// Differs in message.
Diagnostic{Posn: "src/file4:10:2", Message: "foo error"}: true,
{Posn: "src/file4:10:2", Message: "foo error"}: true,
}
test := map[Diagnostic]bool{
// Same in both.
Diagnostic{Posn: "src/file1:10:2", Message: "nil pointer dereference"}: true,
{Posn: "src/file1:10:2", Message: "nil pointer dereference"}: true,
// Differs in position.
Diagnostic{Posn: "src/file3:10:2", Message: "nil pointer dereference"}: true,
{Posn: "src/file3:10:2", Message: "nil pointer dereference"}: true,
// Differs in message.
Diagnostic{Posn: "src/file4:10:2", Message: "bar error"}: true,
{Posn: "src/file4:10:2", Message: "bar error"}: true,
}

minuses := Diff(base, test)
Expand Down

0 comments on commit 9b1d75d

Please sign in to comment.