# the magic tests file is fully self-contained, and can be used without
# the rest of the magic make framework

default:
	@test -d reference && $(MAKE) -s check || $(MAKE) -s firstrun

firstrun:
	@echo 'there is no reference folder, I will just show the help'
	@echo 'if you have tests ready to populate the initial folder, just run:'
	@echo '  $$ make sync # now :) '
	@echo ''
	@echo 'PROJECT=$(PROJECT)'
	@echo 'TEXT_EXT=$(TEST_EXT)'
	@echo 'RUN_TEXT=$(RUN_TEST)'
	@echo
	$(MAKE) help

help:
	@echo 'HELP:'
	@echo 'make check     - runs all test; with diffs for differing tests'
	@echo 'make update    - run all tests - shows color coded summary'
	@echo 'make testname  - run single test and show its output'
	@echo 'make sync      - synchronise references with outputs'
	@echo 'make clean     - remove outputs'
	@echo 'make help      - show this help'


DIFF=$(shell which colordiff > /dev/null && echo -n colordiff || echo -n diff )

# this diff command is what the entire test suite relies on, a diff
# between an expected reference folder and the newly created output folder
# we rely both on the output provided by diff, as well as the return codes
# of the diff command.

DIFFCOMMAND=$(DIFF) -Nsu reference output


output/%: %$(TEST_EXT) $(RUN_DEP) Makefile
	@$(PRE_TEST) || true
	@$(RUN_TEST) $< > output/`echo $<|sed 's/\$(TEST_EXT)//'` ; 
	@$(POST_TEST) || true

%: %$(TEST_EXT)
	@$(MAKE) -s output/$@
	@cat output/$@


TEXT_GREEN=tput setaf 2;tput bold
TEXT_NORMAL=tput sgr0
TEXT_RED=tput setaf 1;tput bold

update: *$(TEST_EXT) $(RUN_DEP)
	@echo "Running tests: "
	@rm -rf output > /dev/null 2>&1 || true
	@mkdir output > /dev/null 2>&1 || true
	@mkdir reference > /dev/null 2>&1 || true
	@for a in `for a in *$(TEST_EXT);do echo $$a |sed s/\$(TEST_EXT)$$//;done`;do \
		$(MAKE) output/$$a > /dev/null; \
		$(DIFF) reference/$$a output/$$a > /dev/null && \
		   ($(TEXT_GREEN); echo -n $$a" "; $(TEXT_NORMAL)) ||\
		   ($(TEXT_RED)  ; echo -n $$a" "; $(TEXT_NORMAL)) ;\
	done ; echo ; echo

check: update
	@$(DIFFCOMMAND) | grep -v identical || true ; 
	@$(DIFFCOMMAND) > /dev/null && \
		( echo all `ls -1 reference | wc -l` tests succeed; \
		$(SUCCEED_COMMAND) \
		) \
	  || ( \
		$(TEXT_RED); echo "$$((`ls -1 reference | wc -l` - `$(DIFFCOMMAND) | grep identical | wc -l`))/`ls -1 reference | wc -l` tests failed:" ; $(TEXT_NORMAL); \
		$(DIFFCOMMAND) | grep ' -Nsu' | sed 's/.*\//  /';\
		$(FAIL_COMMAND)  )
clean:
	rm -rf output

sync: update
	cp output/* reference


POST_TEST?=true  # commands to run after test (cleanup)
PRE_TEST?=true   # commands to run before test (paranoid cleanup)
RUN_DEP=         # default empty
