.PHONY: clean dev dev_env \
	docker_down integration_stack \
	pip_lock_versions \
	start_db sync_docker \
	test lint test_integration test_unit \
	jenkins_env integration_job unit_lint_job \
	drop_unit_db drop_integration_db

clean:
	rm -rf env \
		tests/integration/env \
		.coverage \
		.pytest_cache \
		.cache \
		coverage.xml \
		pyunit.xml
	find . | grep -E "__pycache__|\.pyc|\.pyo$\" | xargs rm -rf

env:
	python3.8 -m venv env
	env/bin/python -m pip install -q -U pip setuptools wheel
	env/bin/python -m pip install -q -r requirements.txt

dev: env
	env/bin/python dev.py

dev_env: env
	if [ ! -f env/bin/py.test ]; then env/bin/python -m pip install -q -r requirements.txt -r requirements-dev.txt; fi

docker_down:
	docker-compose down --remove-orphans

integration_stack:
	if [ -f tests/integration/requirements.txt ]; then env/bin/python -m pip install -q -I -r tests/integration/requirements.txt; fi
	env/bin/python -m pip install -q -i https://pypi.theorchard.io/pypi/ qa-testing-utils
	docker-compose up -d abacus-adjustment-abacus-adjustment
	env/bin/patience --services http://localhost:6652/hello


lint: dev_env
	env/bin/python -m flake8 abacus_adjustment/ tests/ application.py dev.py

pip_lock_versions: clean
	python3.8 -m venv env
	env/bin/python -m pip install -q -U pip setuptools wheel
	env/bin/python -m pip install -q -r requirements-to-freeze.txt
	echo "-i https://pypi.theorchard.io/pypi/" > requirements.txt
	env/bin/python -m pip freeze >> requirements.txt

start_db:
	docker-compose up -d abacus-adjustment-mysql
	docker-compose up --exit-code-from abacus-adjustment-liquibase-runner abacus-adjustment-liquibase-runner

sync_docker:
	`aws ecr get-login --no-include-email --region us-east-1`
	docker-compose pull -q

test: dev_env
	env/bin/python -m pytest \
		tests/unit \
		tests/functional \
		--cov abacus_adjustment \
		--cov-report term-missing

test_integration: dev_env
	env/bin/python -m pytest tests/integration \
		--cov-report xml \
		--junitxml=pyunit.xml

test_unit: dev_env
	env/bin/python -m pytest tests \
		--ignore=tests/integration \
		--cov abacus_adjustment tests/ \
		--cov-report xml \
		--junitxml=pyunit.xml

drop_integration_db:
	docker volume rm -f ows-abacus-adjustment_dbdata
	docker volume rm -f ows-abacus-adjustment-integration-tests_dbdata

drop_unit_db:
	docker volume rm -f ows-abacus-adjustment_dbdata
	# Prevent PR and Unit-lint jobs from err'ing when attempting to delete
	# a db that's in use in a concurrent job.
	docker volume rm -f ows-abacus-adjustment-pull-request_dbdata || true
	docker volume rm -f ows-abacus-adjustment-unit-lint_dbdata || true

jenkins_env: docker_down clean sync_docker

integration_job: jenkins_env drop_integration_db start_db dev_env integration_stack test_integration

unit_lint_job: jenkins_env drop_unit_db start_db dev_env lint test_unit
