-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.mk
180 lines (155 loc) · 4.21 KB
/
tests.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Make Tests
# Konrad Talik <[email protected]>
#/ In order to `make tests', set up following variables:
#/ TEXEC target executable
#/
#/ Optional parameters (with default values)
#/ TDDIR=tests tests default dir
#/ TPARAMS="[INPUT_FILE]" executable params for all tests
#/ TINPUT=TDDIR input files root directory
#/ TOUTPUT=TDDIR expected outputs root directory
#/ TACTUAL=TDDIR actual outputs root directory
#/ TIN=.in test input extension (start with dot if present)
#/ TOUT=.out expected outputs extension
#/ TACT=.act actual outputs extension
#/
#/ Available TPARAMS aliases with their meanings (see README.md):
#/ [INPUT_FILE] full test input file name
#/ [FILE_STEM] test input/output file stem (name without extension)
###
# Useful variables
###
MAKEFILE_PATH=$(abspath $(lastword $(MAKEFILE_LIST)))
###
# Default constans
###
# maketests is using bash
SHELL=/bin/bash
# Tests default dir
ifeq ($(origin TDDIR), undefined)
TDDIR=tests
endif
ifeq ($(origin TPARAMS), undefined)
TPARAMS="[INPUT_FILE]"
endif
ifeq ($(origin TINPUT), undefined)
TINPUT=$(TDDIR)
endif
ifeq ($(origin TOUTPUT), undefined)
TOUTPUT=$(TDDIR)
endif
ifeq ($(origin TACTUAL), undefined)
TACTUAL=$(TDDIR)
endif
ifeq ($(origin TIN), undefined)
TIN=.in
endif
ifeq ($(origin TOUT), undefined)
TOUT=.out
endif
ifeq ($(origin TACT), undefined)
TACT=.act
endif
INPUTS=$(wildcard $(TINPUT)/*$(TIN))
ACTUALS=$(patsubst %$(TIN), %$(TACT), $(INPUTS))
DIFF=git diff --no-index --no-prefix --ignore-space-change --word-diff=plain
###
# Recipes
###
.PHONY: tests_help
tests_help:
@grep "^#/" < $(MAKEFILE_PATH) | cut -c4-; exit
# Make Tests
.PHONY: tests
tests:
@printf "Tests: params check...\n"
$(MAKE) --silent tests_params_check
@printf "Tests: start.\n"
$(MAKE) init_tests
$(MAKE) --silent run_tests
@printf "Tests: DONE.\n"
$(MAKE) --silent sumup_tests
$(MAKE) --silent tests_status
# Params check
.PHONY: tests_params_check
tests_params_check:
ifeq ($(origin TEXEC), undefined)
$(error TEXEC: Please provide executable filename/path!)
endif
# Init tests
.PHONY: init_tests
init_tests:
@echo "Tests: init..."
for act in $(ACTUALS) ; do \
rm -f $$act; \
rm -f "$$act.failed"; \
done
# Run tests
.PHONY: run_tests
run_tests:
@echo "Tests: running..."
@for act in $(ACTUALS) ; do \
$(MAKE) --silent $$act ; \
done
# Print summary with exit code
.PHONY: sumup_tests
sumup_tests:
@echo "Tests summary..."
@for act in $(ACTUALS) ; do \
$(MAKE) --silent compare_test \
act="$$act" \
out="$${act/$(TACT)/$(TOUT)}" ;\
done
@echo
# Return tests status code
.PHONY: tests_status
tests_status:
@echo "Tests: exiting with tests status..."
@for act in $(ACTUALS) ; do \
if [ -a $$act.failed ] ; then exit 2 ; fi ; \
done
# Test exec with input and compare actuals
.PHONY: %$(TACT)
%$(TACT): %$(TIN) %$(TOUT)
$(MAKE) --silent run_test \
params="$(subst [INPUT_FILE], $*$(TIN), $(subst [FILE_STEM], $*, $(TPARAMS)))"\
act=$@
$(MAKE) --silent compare_test act="$@" out="$(word 2, $^)" verbose=true
# Test exec with params and create actuals
#
# Parameters:
# params executable parameters for run_test
# act actual output filename
# verbose flag: print run_test description
#
.PHONY: run_test
run_test:
@if [ -n $(verbose) ] ; then \
printf "Test: $(TEXEC) $(params)"; \
fi;
$(shell $(TEXEC) $(params) > $(act))
# Compare expected outputs and actuals
#
# Parameters:
# verbose flag: print statuses as PASSED or FAILED
# if ommited, status will have format: . or x respectively
# silent flag: do not print any output
# fail_details flag: print FAILED tests details
.PHONY: compare_test
compare_test:
@if [ "$$($(DIFF) $(out) $(act))" == "" ] ; then\
if [ $(verbose) ] ; then \
printf " ...\e[0;32mPASSED\e[0m.\n"; \
$(DIFF) $(out) $(act); \
elif [ ! $(silent) ] ; then \
printf "\e[0;32m.\e[0m"; \
fi; \
else \
if [ "$(verbose)" -o "$(fail_details)" ] ; then \
printf " ...\e[0;31mFAILED\e[0m:\n"; \
$(DIFF) $(out) $(act); \
elif [ ! $(silent) ] ; then \
printf "\e[0;31mx\e[0m"; \
touch $(act).failed; \
fi; \
fi;