Make the module tests depend on the auth tests
[freeradius.git] / src / tests / modules / test.mk
1 >#
2 #  Add the module tests to the overall dependencies
3 #
4
5 TESTS.MODULES_FILES :=
6
7 # If module requires test server, make sure TEST_SERVER of <MODULE>_TEST_SERVER variables are defined
8 # If TEST_SERVER is defined, define <MODULE>_TEST_SERVER for all modules that have CHECK_MODULE_TEST_CAN_BE_RUN
9 define CHECK_MODULE_TEST_CAN_BE_RUN
10   ifndef ${1}_require_test_server
11     tests.modules: ${1}.test
12   else
13     ifdef TEST_SERVER
14       tests.modules: ${1}.test
15       export $(shell echo ${1} | tr a-z A-Z)_TEST_SERVER := $(TEST_SERVER)
16     endif
17     ifdef $(shell echo ${1} | tr a-z A-Z)_TEST_SERVER
18       tests.modules: ${1}.test
19     endif
20   endif
21 endef
22 $(foreach x,$(TEST_BUILT) $(TEST_SUBBUILT),$(eval $(call CHECK_MODULE_TEST_CAN_BE_RUN,$x)))
23
24 ######################################################################
25 #
26 #  And now more makefile magic to automatically run the tests
27 #  for each module.
28 #
29
30 define DEFAULT_ATTRS
31 ifeq "$(wildcard ${1}.attrs)"
32 ${1}.attrs
33 else
34 src/tests/modules/default-input.attrs
35 endif
36 endef
37
38 #
39 #  Files in the output dir depend on the unit tests
40 #
41 #       src/tests/$(MODULE_DIR)/FOO.unlang      unlang for the test
42 #       src/tests/$(MODULE_DIR)/FOO.attrs       input RADIUS and output filter
43 #       build/tests/$(MODULE_DIR)/FOO.out       updated if the test succeeds
44 #       build/tests/$(MODULE_DIR)/FOO.log       debug output for the test
45 #
46 #  If the test fails, then look for ERROR in the input.  No error
47 #  means it's unexpected, so we die.
48 #
49 #  Otherwise, check the log file for a parse error which matches the
50 #  ERROR line in the input.
51 #
52 $(BUILD_DIR)/tests/modules/%: src/tests/modules/%.unlang $(BUILD_DIR)/tests/modules/%.attrs $(TESTBINDIR)/unittest | build.raddb
53         @mkdir -p $(dir $@)
54         @echo MODULE-TEST $(lastword $(subst /, ,$(dir $@))) $(basename $(notdir $@))
55         @if ! MODULE_TEST_DIR=$(dir $<) MODULE_TEST_UNLANG=$< $(TESTBIN)/unittest -D share -d src/tests/modules/ -i $@.attrs -f $@.attrs -xxx > $@.log 2>&1; then \
56                 if ! grep ERROR $< 2>&1 > /dev/null; then \
57                         cat $@.log; \
58                         echo "# $@.log"; \
59                         echo MODULE_TEST_DIR=$(dir $<) MODULE_TEST_UNLANG=$< $(TESTBIN)/unittest -D share -d src/tests/modules/ -i $@.attrs -f $@.attrs -xx; \
60                         exit 1; \
61                 fi; \
62                 FOUND=$$(grep ^$< $@.log | head -1 | sed 's/:.*//;s/.*\[//;s/\].*//'); \
63                 EXPECTED=$$(grep -n ERROR $< | sed 's/:.*//'); \
64                 if [ "$$EXPECTED" != "$$FOUND" ]; then \
65                         cat $@.log; \
66                         echo "# $@.log"; \
67                         echo MODULE_TEST_DIR=$(dir $<) MODULE_TEST_UNLANG=$< $(TESTBIN)/unittest -D share -d src/tests/modules/ -i $@.attrs -f $@.attrs -xx; \
68                         exit 1; \
69                 fi \
70         fi
71         @touch $@
72
73 #
74 #  Sometimes we have a default input.  So use that.  Otherwise, use
75 #  the input specific to the test.
76 #
77 MODULE_UNLANG           := $(wildcard src/tests/modules/*/*.unlang src/tests/modules/*/*/*.unlang)
78 MODULE_ATTRS_REQUIRES   := $(patsubst %.unlang,%.attrs,$(MODULE_UNLANG))
79 MODULE_ATTRS_EXISTS     := $(wildcard src/tests/modules/*/*.attrs src/tests/modules/*/*/*.attrs)
80 MODULE_ATTRS_NEEDS      := $(filter-out $(MODULE_ATTRS_EXISTS),$(MODULE_ATTRS_REQUIRES))
81
82 MODULE_CONF_REQUIRES    := $(patsubst %.unlang,%.conf,$(MODULE_UNLANG))
83 MODULE_CONF_EXISTS      := $(wildcard src/tests/modules/*/*.conf src/tests/modules/*/*/*.attrs)
84 MODULE_CONF_NEEDS       := $(filter-out $(MODULE_CONF_EXISTS),$(MODULE_CONF_REQUIRES))
85
86 #
87 #  The complete list of tests which are to be run
88 #
89 MODULE_TESTS            := $(patsubst src/tests/modules/%/all.mk,%,$(wildcard src/tests/modules/*/all.mk))
90
91
92 #
93 #  Target-specific rules
94 #
95 define MODULE_COPY_FILE
96 $(BUILD_DIR)/${1}: src/${1}
97         @mkdir -p $$(@D)
98         @cp $$< $$@
99
100 endef
101
102 #
103 #  Default rules
104 #
105 define MODULE_COPY_ATTR
106 $(BUILD_DIR)/${1}: src/tests/modules/default-input.attrs
107         mkdir -p $$(@D)
108         @cp $$< $$@
109 endef
110
111 #
112 #  FIXME: get this working
113 #
114 define MODULE_COPY_CONF
115 $(BUILD_DIR)/${1}: src/tests/modules/${2}/module.conf
116         @mkdir -p $$(@D)
117         @cp $$< $$@
118 endef
119
120 define MODULE_FILE_TARGET
121 $(BUILD_DIR)/${1}: src/${1}.unlang $(BUILD_DIR)/${1}.attrs
122
123 endef
124
125 define MODULE_TEST_TARGET
126 ${1}.test: $(patsubst %.unlang,%,$(subst src,$(BUILD_DIR),$(filter src/tests/modules/${1}/%,$(MODULE_UNLANG))))
127
128 TESTS.MODULES_FILES += $(patsubst %.unlang,%,$(subst src,$(BUILD_DIR),$(filter src/tests/modules/${1}/%,$(MODULE_UNLANG))))
129 endef
130
131 #
132 #  Create the rules from the list of input files
133 #
134 $(foreach x,$(MODULE_ATTRS_EXISTS),$(eval $(call MODULE_COPY_FILE,$(subst src/,,$x))))
135 $(foreach x,$(MODULE_CONF_EXISTS),$(eval $(call MODULE_COPY_FILE,$(subst src/,,$x))))
136
137 $(foreach x,$(MODULE_ATTRS_NEEDS),$(eval $(call MODULE_COPY_ATTR,$(subst src/,,$x))))
138 # FIXME: copy src/tests/modules/*/module.conf to the right place, too
139
140 $(foreach x,$(MODULE_UNLANG),$(eval $(call MODULE_FILE_TARGET,$(patsubst %.unlang,%,$(subst src/,,$x)))))
141 $(foreach x,$(MODULE_TESTS),$(eval $(call MODULE_TEST_TARGET,$x)))
142
143 $(TESTS.MODULES_FILES): $(TESTS.AUTH_FILES)
144
145 .PHONY: clean.modules.test
146 clean.modules.test:
147         @rm -rf $(BUILD_DIR)/tests/modules/
148
149 #
150 #  For each file, look for precursor test.
151 #  Ensure that each test depends on its precursors.
152 #
153 -include $(BUILD_DIR)/tests/modules/depends.mk
154
155 $(BUILD_DIR)/tests/modules/depends.mk: $(MODULE_UNLANG) | $(BUILD_DIR)/tests/modules
156         @rm -f $@
157         @for x in $^; do \
158                 y=`grep PRE $$x | awk '{ print $$3 }'`; \
159                 if [ "$$y" != "" ]; then \
160                         z=`echo $$x | sed 's,src/,$(BUILD_DIR)/', | sed 's/.unlang//'`; \
161                         d=$$(basename $$(dirname $$x)); \
162                         echo "$$z: $(BUILD_DIR)/tests/modules/$$d/$$y" >> $@; \
163                         echo "" >> $@; \
164                 fi \
165         done