Skip to content

Commit

Permalink
Add coverage to Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 9, 2024
1 parent d903d68 commit 6c2028d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
working-directory: ./backend

- name: Test code
run: |
set -euo pipefail
go test -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
run: |-
go test -json -v ./...
working-directory: ./backend
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/**/.idea
/**/.env
/**/y.output
/**/coverage.out
/**/coverage.html
/**/profile.out
32 changes: 30 additions & 2 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,38 @@ fmt: ## Format code
test: ## Run tests
printf "${GREEN}Run all tests\n\n${WHITE}"; \
go clean -cache -testcache -i -r; \
go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest; \
go test -race -json -v -coverprofile=coverage.txt ./... 2>&1 | tee /tmp/gotest.log | gotestfmt; \
go test -cover -race -json -v ./... ; \
printf "${GREEN}Done\n"; \

# Directories to exclude from coverage (use | to separate multiple directories)
EXCLUDE_DIRS=backend/pkg/notion

.PHONY: pre-coverage
pre-coverage: ## Generate coverage report for all packages
@echo "mode: count" > coverage.out
@for d in $(shell go list ./...); do \
skip=false; \
for exclude in $(EXCLUDE_DIRS); do \
case $$d in *$$exclude*) skip=true;; esac; \
done; \
if [ "$$skip" = false ]; then \
output=$$(go test -coverprofile=profile.out -covermode=count $$d 2>&1); \
coverage=$$(echo "$$output" | awk '/coverage:/{print $$2}' | sed 's/%//'); \
case "$$output" in \
*\[no\ test\ files\]*) echo "Skipping $$d: no test files";; \
*coverage:\ 0.0*) echo "Skipping $$d: coverage is 0.0%";; \
*) \
echo "$$output"; \
[ -f profile.out ] && tail -n +2 profile.out >> coverage.out && rm profile.out;; \
esac; \
fi \
done

.PHONY: cover
cover: pre-coverage ## Generate report from coverage data
@go tool cover -func=coverage.out
@echo "Coverage report generated at cover"

.PHONY: init
init: ## Init go.mod and go.sum
printf "${GREEN}Init go.mod and go.sum\n\n${WHITE}"; \
Expand Down

0 comments on commit 6c2028d

Please sign in to comment.