# Makefile to run all or a subset of tests

# Python and path
PYTHON		:= python
PYTHONPATH  ?= $(CURDIR)/../..
PATH		:= $(CURDIR)/../../scripts:$(PATH)

# Unit tests
utest		:= unit/all.py
utest_r		:= $(patsubst %.py,%.out,$(utest))
# Component tests
ctest		:= component/all.py
ctest_r		:= $(patsubst %.py,%.out,$(ctest))
# System tests
stest		:= system/all.py
stest_r		:= $(patsubst %.py,%.out,$(stest))

default: help

help:
	@printf "Run NetLogger test suite\n\
Usage: make target [target..]\n\
targets:\n\
  unit = run unit tests\n\
  comp = run component tests\n\
  system = run system tests\n\
  all  = run all tests\n\
  all_fast = run all unit and component tests\n\
  clean = clean up in test directories\n\
  help = print this message\n\
Environment variables:\n\
TEST_DEBUG   = 1 or 0 (default is 0)\n\
\nExamples:\n\
  # Run unit tests with debug and verbosity off\n\
  make unit\n\
  # Run all tests with debug and verbosity on\n\
  TEST_DEBUG=1 make all\n";

hdr:
	@printf "# Python interpreter = $(PYTHON)\n\
# Python version = `python -c  'import platform; print platform.python_version()'`\n\
# PYTHONPATH = $(PYTHONPATH) \n";

%.out: %.py
	@PYTHONPATH=$(PYTHONPATH) PATH=$(PATH) \
	$(PYTHON) $<

unit: hdr $(utest_r)

comp: hdr $(ctest_r)
component: comp

system: hdr $(stest_r)

all: hdr $(utest_r) $(ctest_r) $(stest_r)

all_fast: hdr $(utest_r) $(ctest_r)


clean: clean-pipeline clean-pyc

clean-pipeline:
	find system/pipeline -name "test.bp*" -exec rm -f {} \;
	find system/pipeline -name "test.log" -exec rm -f {} \;
	find system/pipeline -name "nl_*.log" -exec rm -f {} \;
	find system/pipeline -name "nl_*.state" -exec rm -f {} \;
	find -L /tmp -name test.sqlite -exec rm -f {} \;

clean-pyc:
	find . -name "*.pyc" -exec rm -f {} \;

.PHONY: help unit comp all clean clean-pipeline clean-pyc
