BUILD_DIR_NAME ?= web
MODULE_NAME := $(notdir $(shell pwd))
MODULE_DIR := modules/$(MODULE_NAME)
PROJECT_NAME = web

DOCKER_COMPOSE_FLAGS ?= -f docker-compose.yml --log-level ERROR
DOCKER_COMPOSE := cd ../../ && \
	MODULE_DIR=$(MODULE_DIR) \
	BUILD_NUMBER=$(BUILD_NUMBER) \
	BRANCH_NAME=$(BRANCH_NAME) \
	PROJECT_NAME=$(PROJECT_NAME) \
	MOCHA_GREP=${MOCHA_GREP} \
	SHARELATEX_CONFIG=/app/$(MODULE_DIR)/test/acceptance/config/settings.test.coffee \
	docker-compose ${DOCKER_COMPOSE_FLAGS}

DOCKER_COMPOSE_TEST_ACCEPTANCE := \
	export COMPOSE_PROJECT_NAME=acceptance_test_$(BUILD_DIR_NAME)_$(MODULE_NAME) \
	&& $(DOCKER_COMPOSE)

DOCKER_COMPOSE_TEST_UNIT := \
	export COMPOSE_PROJECT_NAME=unit_test_$(BUILD_DIR_NAME)_$(MODULE_NAME) \
	&& $(DOCKER_COMPOSE)

ifeq (,$(wildcard test/unit))
test_unit:

else
test_unit:
	${DOCKER_COMPOSE_TEST_UNIT} run --rm test_unit npm -q run test:unit:run_dir -- ${MOCHA_ARGS} $(MODULE_DIR)/test/unit/src
	${DOCKER_COMPOSE_TEST_UNIT} down

endif

ifeq (,$(wildcard test/acceptance))
test_acceptance:
clean_test_acceptance:

else
test_acceptance:
	$(MAKE) --no-print-directory clean_test_acceptance
	${DOCKER_COMPOSE_TEST_ACCEPTANCE} run --rm test_acceptance npm -q run test:acceptance:run_dir -- ${MOCHA_ARGS} $(MODULE_DIR)/test/acceptance/src
	$(MAKE) --no-print-directory clean_test_acceptance

clean_test_acceptance:
	${DOCKER_COMPOSE_TEST_ACCEPTANCE} down -v -t 0

endif
