.PHONY: pip_dev dev lint test test_unit pip_test_integration test_integration clean clean_test_integration

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

dev: env
	env/bin/python3 dev.py

lint: env
	env/bin/pre-commit run --all-files

test: env
	env/bin/py.test tests/unit/

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

tests/integration/env pip_test_integration:
	python3 -m venv tests/integration/env
	tests/integration/env/bin/pip3 install --upgrade pip
	tests/integration/env/bin/pip3 install -r tests/integration/requirements.txt

test_integration: tests/integration/env
	tests/integration/env/bin/py.test tests/integration $(INTEGRATION_PYTEST_ARGS)

clean:
	rm -rf env

clean_test_integration:
	rm -rf tests/integration/env

create_split_config:
	touch ~/.split

test_integration_qa:
	Environment=qa $(MAKE) test_integration INTEGRATION_PYTEST_ARGS="$(INTEGRATION_PYTEST_ARGS)"

# --- Jenkins build steps ---

# Used by make pull_request_job_docker
pull_request_job:
	SNOWFLAKE_USER=dummyvalue SNOWFLAKE_DATABASE=dummyvalue $(MAKE) clean pip_dev lint test_unit

# The build step for https://pipeline.theorchard.io/job/ows-playlist-unit-lint
unit_lint_job: clean pip_dev create_split_config lint test_unit

# The build step for https://pipeline.theorchard.io/job/ows-playlist-integration-tests
# Exclude the product_ownership (transfer_ownership) suite from CI integration runs.
integration_job: pip_dev
	$(MAKE) test_integration_qa INTEGRATION_PYTEST_ARGS="--ignore=tests/integration/transfer_ownership"


# --- Jenkins docker ---

# The build step for https://pipeline.theorchard.io/job/ows-playlist-pull-request
pull_request_job_docker:
	docker build --target pr_tests -t ows-playlist-pull-request .
# create blank files otherwise docker creates a directory and mounts them if they don't exist
	touch coverage.xml pyunit.xml
	docker run --volume `pwd`/coverage.xml:/var/app/coverage.xml --volume `pwd`/pyunit.xml:/var/app/pyunit.xml ows-playlist-pull-request

# --- Local dev utils ---

docker_dev_build:
#  ensure your key is present at ~/.ssh/snowflake/rsa_key.p8
#  ensure your .env contains SNOWFLAKE_PRIVATE_KEY_PATH=/root/.ssh/snowflake/rsa_key.p8 when building the image
	mkdir -p tmp
	cp ~/.ssh/snowflake/rsa_key.p8 tmp/
	docker build -t ows-playlist .
	rm -rf tmp/

docker_dev_run:
	docker run -p 5000:5000 ows-playlist