.PHONY: clean test

PYTEST_ARGS?=--tb=short -v

export UV_CACHE_DIR := $(CURDIR)/.uv-cache

# Build matrix: (python-version, pydantic-version) pairs
# 3.10: all three pydantic versions
# 3.12: pydantic 1.10.26 and 2.12.5
TEST_TARGETS := \
    test-3.10-pydantic1.10.23 \
    test-3.10-pydantic2.8.2 \
    test-3.10-pydantic2.12.5 \
    test-3.12-pydantic1.10.26 \
    test-3.12-pydantic2.12.5

ALL_VENVS := $(patsubst test-%,.venv-%,$(TEST_TARGETS))

# Template for generating venv + test rules for a (pyver, pdver) pair.
define VENV_RULES
.venv-$(1)-pydantic$(2):
	uv venv .venv-$(1)-pydantic$(2) --python=$(1)

.venv-$(1)-pydantic$(2)/bin/activate: .venv-$(1)-pydantic$(2) pyproject.toml
	uv pip install -e . pytest "pydantic==$(2)" \
	    --python=$$(CURDIR)/.venv-$(1)-pydantic$(2)/bin/python
	touch $$(CURDIR)/.venv-$(1)-pydantic$(2)/bin/activate

.PHONY: test-$(1)-pydantic$(2)
test-$(1)-pydantic$(2): .venv-$(1)-pydantic$(2)/bin/activate
	$$(CURDIR)/.venv-$(1)-pydantic$(2)/bin/pytest $$(PYTEST_ARGS)
endef

$(eval $(call VENV_RULES,3.10,1.10.23))
$(eval $(call VENV_RULES,3.10,2.8.2))
$(eval $(call VENV_RULES,3.10,2.12.5))
$(eval $(call VENV_RULES,3.12,1.10.26))
$(eval $(call VENV_RULES,3.12,2.12.5))

# Default venv for ad-hoc use (latest Python, latest pydantic)
VENV_BIN := .venv-3.12-pydantic2.12.5/bin/
PYTEST := $(VENV_BIN)pytest

.PHONY: default-venv
default-venv: .venv-3.12-pydantic2.12.5/bin/activate

test: $(TEST_TARGETS)

clean:
	rm -rf $(ALL_VENVS) .uv-cache __pycache__ .pytest_cache *.egg-info
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
