#!/usr/bin/make -f

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

PYTHON3_VERSION := $(shell py3versions -d -v)

# Skip tests requiring external network access.
#
# TODO
# The IDNA-related tests require a newer idna release than the one
# currently available in Debian.
#
# The encoding autodetection tests appear to fail due to differences
# in dependency versions available in Debian.
export PYBUILD_TEST_ARGS=\
 --ignore=tests/test_benchmark.py \
 -m "not network" \
 -k 'not test_client_decode_text_using_autodetect \
 and not test_client_decode_text_using_explicit_encoding \
 and not test_logging_redirect_chain \
 and not test_logging_request \
 and not test_response_decode_text_using_autodetect \
'

%:
	dh $@

execute_after_dh_auto_clean:
	rm -rf .pybuild .pytest_cache

override_dh_auto_configure:
	sed -i '/^name = "httpcore2"$$/a version = "$(DEB_VERSION_UPSTREAM)"' src/httpcore2/pyproject.toml
	sed -i '/^name = "httpx2"$$/a version = "$(DEB_VERSION_UPSTREAM)"' src/httpx2/pyproject.toml

override_dh_auto_build:
	PYBUILD_NAME=httpcore2 dh_auto_build --buildsystem=pybuild --sourcedirectory=src/httpcore2
	PYBUILD_NAME=httpx2 dh_auto_build --buildsystem=pybuild --sourcedirectory=src/httpx2

execute_after_dh_auto_build:
	sed -i '/^version = "$(DEB_VERSION_UPSTREAM)"$$/d' src/httpcore2/pyproject.toml
	sed -i '/^version = "$(DEB_VERSION_UPSTREAM)"$$/d' src/httpx2/pyproject.toml

override_dh_auto_install:
	PYBUILD_NAME=httpcore2 dh_auto_install --buildsystem=pybuild --sourcedirectory=src/httpcore2 --destdir=debian/python3-httpcore2
	PYBUILD_NAME=httpx2 dh_auto_install --buildsystem=pybuild --sourcedirectory=src/httpx2 --destdir=debian/python3-httpx2

override_dh_auto_test:
	PYTHONPATH=$(CURDIR)/.pybuild/cpython3_$(PYTHON3_VERSION)_httpcore2/build:$(CURDIR)/.pybuild/cpython3_$(PYTHON3_VERSION)_httpx2/build \
	python3 -m pytest $(PYBUILD_TEST_ARGS)
