#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk # DEB_HOST_ARCH
include /usr/share/dpkg/pkg-info.mk # DEB_VERSION

# downloads from fromteamdigitale.github.io, see tests/data/openapi/refs.yaml
test_expr = not test_openapi_parser_parse_remote_ref
# Running these sorts of style checks as a downstream doesn't really make
# sense.
test_expr += and not test_ruff
test_expr += and not test_type_checking_imports_default_to_runtime_imports_for_modular_pydantic_ruff
test_expr += and not test_generate_multi_module_pydantic_ruff_defaults_to_runtime_imports
test_expr += and not test_use_type_checking_imports_for_multi_module_pydantic_ruff
test_expr += and not test_openapi_parser_parse_modular_pydantic_v2_ruff_keeps_runtime_imports
test_expr += and not test_no_use_type_checking_imports
# These run "python3 -m ruff", but Debian's ruff only ships the executable.
test_expr += and not test_compile_check
test_expr += and not docs_python_blocks
test_ignore = --ignore=tests/test_docs_formatting.py

# Skip tests that only matter for upstream maintenance.
test_expr += and not docs_script
test_expr += and not skills
test_expr += and not agent_skill
test_expr += and not install_skill
test_ignore += --ignore=tests/test_ci_workflow.py
test_ignore += --ignore=tests/test_release_draft_workflow.py
test_ignore += --ignore=tests/test_resolve_release_draft_pr_script.py
# Enable these once Debian's python3-grpc-tools version matches
# datamodel-code-generator[protobuf]'s requirements.
test_expr += and not test_main_protobuf
test_expr += and not test_main_asyncapi_protobuf_schema_format
test_expr += and not non_finite_embedded_schemas
test_expr += and not (test_diff_against_supports_every_local_input_type and protobuf)
test_ignore += --ignore=tests/main/protobuf
# Needs httpx2 >= 2.13.
test_expr += and not test_perf_client_calls
# These tests are either very slow or hang forever.
test_expr += and not test_main_watch

ifdef PYBUILD_AUTOPKGTEST
# These run the copied source tree from src/, which lacks the _version.py
# that hatch-vcs only generates at build time.
test_expr += and not test_version_fast_path_uses_embedded_version
test_expr += and not test_perf_startup_measurement_script
endif

ifeq (,$(filter $(DEB_HOST_ARCH),amd64 arm64 loong64 ppc64el riscv64))
# We only have msgspec on 64-bit little-endian architectures.  Otherwise,
# skip tests that require it.
test_expr += and not msgspec
test_expr += and not test_backend_comparisons
test_expr += and not test_main_constraints_on_typing_aliases
test_expr += and not test_main_entry_point_constraints
test_expr += and not test_main_jsonschema_field_type_collision_runtime
test_expr += and not test_main_number_constraints_on_emitted_types
test_expr += and not test_main_openapi_discriminator_reuse_variant_mapping
test_expr += and not test_payload_runtime_alias_fallback
test_expr += and not test_payload_runtime_legacy_alias_policy
test_expr += and not test_payload_runtime_null_aliases
test_expr += and not test_recursive_container_constraints
test_expr += and not test_schema_validator_unique_items_executable_paths_cover_all_data_type_branches
test_expr += and not structural_codec
test_expr += and not test_type_union_constraint_options
test_expr += and not test_xmlschema_pattern_alternatives
endif

# Some tests serve schemas from a local HTTP server; bypass pybuild's
# dummy proxy for those.
export no_proxy = 127.0.0.1,localhost

export PYBUILD_TEST_ARGS= $(test_ignore) -k '$(test_expr)'

%:
	dh $@ --buildsystem=pybuild

B = $(CURDIR)/debian/tmp/usr/bin
M = $(CURDIR)/debian/tmp/usr/share/man/man1

execute_after_dh_auto_install:
ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
	mkdir -pv $(M)
	env PYTHONPATH=$(CURDIR)/src \
		help2man --no-info --version-string="$(DEB_VERSION)" \
		--help-option="--no-color --help" \
		-Idebian/datamodel-codegen.h2m \
		$(B)/datamodel-codegen -o $(M)/datamodel-codegen.1
endif
