#! /usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

export PYBUILD_NAME=locust
export SETUPTOOLS_SCM_PRETEND_VERSION=$(DEB_VERSION_UPSTREAM)

# tests excluded from the build and the autopkgtest:
# test_stuff, test_verify_false_succeeds_with_bad_cert,
#   test_verify_true_fails_with_bad_cert, test_locustfile_from_url,
#   test_json_file, test_json_schema, test_pytest_style_locustfile,
#   test_request_name - need network access
# test_constructor - expects localhost to also resolve to an IPv6 address,
#   which is not the case on every builder
# test_run_autostart_with_multiple_locustfiles, test_autostart_w_run_time -
#   racy shutdown while a web request is in flight; the former can leave
#   locust running on port 8089, failing the web UI tests that follow
# test_default_wait_time - timing sensitive, flaky on loaded builders
PYTEST_ARGS = -v -k 'not test_stuff and not test_verify_false_succeeds_with_bad_cert \
	and not test_verify_true_fails_with_bad_cert and not test_locustfile_from_url \
	and not test_json_file and not test_json_schema and not test_pytest_style_locustfile \
	and not test_request_name and not test_constructor \
	and not test_run_autostart_with_multiple_locustfiles and not test_autostart_w_run_time \
	and not test_default_wait_time'

# the tests are not installed: run them from symlinks to the installed
# package, with locust/test copied next to them
ifdef PYBUILD_AUTOPKGTEST
# empty, not unset: pybuild would set them to a dead proxy, which also
# catches the requests the tests make to 127.0.0.1
export http_proxy =
export https_proxy =
export PYBUILD_BEFORE_TEST = mkdir -p {build_dir} && cp -as /usr/lib/python3/dist-packages/locust {build_dir}/ && cp -a {dir}/locust/test {build_dir}/locust/
export PYBUILD_TEST_ARGS = $(PYTEST_ARGS) locust/test
endif

%:
	dh $@ --buildsystem=pybuild

execute_before_dh_auto_build:
	# prebuilt web UI from the webui component tarball
	cp -a webui/locust/webui/dist locust/webui/
	python3 debian/remove-azure-ad.py locust/webui/dist/assets/src-*.js

override_dh_auto_test:
	http_proxy= https_proxy= PATH=$(PATH):$(CURDIR)/debian PYBUILD_SYSTEM=custom PYBUILD_TEST_ARGS="PYTHONPATH={build_dir} {interpreter} -m pytest $(PYTEST_ARGS)" dh_auto_test

execute_before_dh_installman:
	# COLUMNS keeps argparse from wrapping the help text, and the URLs in it;
	# the package is still in the versioned dist-packages before dh_python3
	COLUMNS=1000 PYTHONPATH=$(CURDIR)/debian/python3-locust/usr/lib/python$(shell py3versions -dv)/dist-packages \
		help2man --no-info --version-string=$(DEB_VERSION_UPSTREAM) \
		--name="developer-friendly load testing framework" \
		--output=debian/locust.1 debian/python3-locust/usr/bin/locust

override_dh_installexamples:
	dh_installexamples -X.pyc

override_dh_clean:
	rm -rf locust/_version.py locust/webui/dist web_test_*.csv locust_output.json debian/locust.1
	dh_clean
