COV_REPORT_DIR := .coverage

define coverage
	@if [ -f "$(1)/go.mod" ]; then \
		echo "===== coverage for $(1) ====="; \
		outdir=$(COV_REPORT_DIR)/$(1); \
		mkdir -p $$outdir; \
		pkgs=$$(go list ./$(1)/... | grep -vE 'mock|config|generated|header|document|internal/pb'); \
		if [ -z "$$pkgs" ]; then \
			echo "no packages to test in $(1)"; exit 0; \
		fi; \
		go test -race -covermode=atomic -coverprofile=$$outdir/coverage.out $$pkgs; \
		go tool cover -html=$$outdir/coverage.out -o $$outdir/coverage.html; \
		echo "html report: $$outdir/coverage.html"; \
	fi
endef

cart-coverage:
	$(call coverage,cart)

loms-coverage:
	$(call coverage,loms)

notifier-coverage:
	$(call coverage,notifier)

comments-coverage:
	$(call coverage,comments)
