#!/usr/bin/make -f

%:
	dh $@ --builddirectory=debian/.build/upstream --buildsystem=golang

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# The tests for the main `failpoint` package are fragile and fail in the
	# Debian build environment, so skip them and test the sub-packages instead.
	#
	# Environment variables for testing:
	# - GO111MODULE=off: Prevent Go from trying to download dependencies.
	# - GOPATH: Help Go find the existing package sources.
	# - GOCACHE: Set a writable build cache for restricted build environments.
	# - FAILPOINT_ENABLE=true: Required by toolexec to enable failpoint injection.
	set -e; \
	export GOPATH=$(CURDIR)/debian/.build/upstream; \
	export GOCACHE=$(CURDIR)/debian/.build/go-cache; \
	export GO111MODULE=off; \
	export FAILPOINT_ENABLE=true; \
	\
	echo "Cleaning failpoint-toolexec cache..."; \
	rm -rf /tmp/failpoint-rewrite; \
	\
	cd debian/.build/upstream && \
	for pkg in \
		github.com/pingcap/failpoint/code \
		github.com/pingcap/failpoint/examples/injectcall \
	; do \
		echo "Testing $$pkg..."; \
		go test -vet=off -v -p 1 -toolexec=$(CURDIR)/debian/.build/upstream/bin/failpoint-toolexec $$pkg; \
	done
endif
