.PHONY: clean dev pip_dev lint test test_unit
.PHONY: docker_unit_lint down_unit_lint ci_unit_lint ci_unit_lint_clean
.PHONY: up down
.PHONY: clean_int pip_int test_int
.PHONY: docker_test_integration down_test_integration ci_test_integration ci_test_integration_clean

# Used by `docker compose` dev targets that use multiple yml files.
COMPOSE_DEV_FILES = -f docker-compose.yml -f docker-compose-dev.yml

clean:
	rm -rf env

dev: env dev.py
	env/bin/python3.11 dev.py

env pip_dev:
	python3.11 -m venv env
	env/bin/pip install --upgrade pip setuptools
	env/bin/pip install -r requirements.txt
	env/bin/pip install -r requirements-dev.txt

lint:
	env/bin/ruff check grass/ tests/
	env/bin/ruff format grass/ tests/ --check

fmt:
	env/bin/ruff check grass/ tests/ --fix
	env/bin/ruff format grass/ tests/

test_unit:
	env/bin/py.test tests/unit \
		--cov grass tests/unit \
		--cov-report xml \
		--junitxml=pyunit.xml

docker_unit_lint:
	docker compose up \
		--build \
		--exit-code-from unit-lint \
		--abort-on-container-exit \
		--remove-orphans \
		unit-lint

down_unit_lint:
	docker compose down unit-lint --remove-orphans

ci_unit_lint: ci_unit_lint_clean
	mkdir -p build
	chmod a+w build
	make docker_unit_lint

ci_unit_lint_clean: down_unit_lint
	rm -rf build

up:
	docker compose $(COMPOSE_DEV_FILES) up --build \
		--detach \
		--remove-orphans \
		ows-grass

down:
	docker compose $(COMPOSE_DEV_FILES) down

clean_int:
	rm -rf tests/integration/venv

pip_int:
	python3.11 -m venv tests/integration/venv
	tests/integration/venv/bin/pip install --upgrade pip setuptools
	tests/integration/venv/bin/pip install -r requirements-int.txt

test_int: pip_int
	tests/integration/venv/bin/py.test -s -vv \
		tests/integration/api_contracts/ \
		--tavern-global-cfg tests/integration/api_contracts/services/*.yaml \
		tests/integration/api_contracts/constants/*.yaml

docker_test_integration:
	docker compose up \
		--build \
		--exit-code-from test-integration \
		--abort-on-container-exit \
		--remove-orphans \
		test-integration

down_test_integration: down

ci_test_integration: down_test_integration
	make docker_test_integration

ci_test_integration_clean: down_test_integration
