.PHONY: vet fmt-check test race browser ci

# ci is the one gate: what a runner executes and what you run before
# pushing are the same definition (amadan's own rule: CI steps
# delegate to make targets, never keep their own copies).
ci: vet fmt-check test race browser

# The example, when there is one, is its own module with a replace
# back here, so ./... does not reach it; each target visits it
# explicitly.
vet:
	go vet ./...
	@if [ -d example ]; then cd example && go vet ./... && go vet -tags browser ./...; fi

fmt-check:
	@out=$$(gofmt -l .); if [ -n "$$out" ]; then echo "gofmt needed:"; echo "$$out"; exit 1; fi

# -count=1 on purpose: these tests build a real SQLite database; a
# cached PASS re-reports one run as though it were every run.
test:
	go test ./... -count=1
	@if [ -d example ]; then cd example && go test ./... -count=1; fi

# -race needs cgo; everything else runs with the default toolchain, so
# this target sets CGO_ENABLED for its own command only.
race:
	CGO_ENABLED=1 go test ./... -race -count=1
	@if [ -d example ]; then cd example && CGO_ENABLED=1 go test ./... -race -count=1; fi

# The browser drive, via rastrillo's harness. It fails, not skips,
# without a browser (rastrillo's rule: a skip is not a pass). It runs in
# each of the module and the example that has a browser test file, so
# an empty package does not fail the gate; the example's drives the
# starter pages.
browser:
	@ran=; for dir in . example; do \
		found=; for f in $$dir/*_browser_test.go $$dir/browser_test.go; do \
			if [ -f "$$f" ]; then found=1; fi; \
		done; \
		if [ -n "$$found" ]; then \
			(cd $$dir && go test -tags browser -run TestBrowser -count=1 .) || exit 1; \
			ran=1; \
		fi; \
	done; \
	if [ -z "$$ran" ]; then echo "browser: no browser test file yet, nothing to run"; fi
