セルフテストの更新
[autotest_mk.git] / selftest / unittest / Define.mk
1 # autotest.mk > test_template > Define.mk
2 # 自動テスト用の変数、マクロ定義
3
4 SHELL=/bin/sh
5
6 ######################################################################
7 # テストグループのディレクトリー
8 ######################################################################
9
10 # テストグループとテストの両方で使う変数を定義したファイル
11 DEF_FILE := Define.mk
12
13 # テストのMakefileにインクルードするファイル
14 TEST_MAKEFILE := Test.mk
15
16 ######################################################################
17 # テストのディレクトリー
18 ######################################################################
19
20 # Makefile
21 MAKEFILE ?= Makefile
22
23 # 現在の日時
24 DATE = $(shell date +"%F %T")
25
26 # テストコマンドファイル
27 CMD_FILE ?= cmd
28
29 # テスト説明ファイル
30 DESC_FILE ?= desc.txt
31
32 # テスト想定結果ファイル
33 TEST0_FILE ?= 0.txt
34
35 # テスト結果ファイル
36 TEST1_FILE ?= 1.txt
37
38 # テストの、想定結果と結果の差分ファイル
39 DIFF_FILE ?= diff.txt
40
41 # テストエラーファイル
42 ERR_FILE ?= err.txt
43
44 # テストログファイル
45 LOG_FILE ?= test.log
46
47 # 実行時間ファイル
48 TIME_FILE ?= time.log
49
50 ######################################################################
51 # コマンド
52 ######################################################################
53
54 CP ?= cp
55
56 CAT ?= cat
57
58 MKDIR ?= mkdir
59
60 RM ?= rm -f
61
62 ECHO ?= echo
63
64 TIME ?= /usr/bin/time --quiet
65
66 DIFF ?= diff -c
67
68 DEV_NULL ?= /dev/null
69
70 CHMOD ?= chmod
71
72 ######################################################################
73 # エラー
74 ######################################################################
75
76 # chk_var_null: 引数がNULLの場合、エラー
77 # 用例: $(call chk_var_null,var)
78 define chk_var_null
79     $(if $1,,$(error NULL argument))
80 endef
81
82 # chk_file_ext: 指定されたファイルが実在する場合、エラー
83 # 用例: $(call chk_file_ext,file)
84 define chk_file_ext
85     $(if $(wildcard $1),$(error $1 exists in $(shell pwd)))
86 endef
87
88 # chk_file_notext: 指定されたファイルが実在しない場合、エラー
89 # 用例: $(call chk_file_notext,file)
90 define chk_file_notext
91     if test ! -s $1; then $(error $1 not exists in $(shell pwd)); fi
92 endef
93
94 ######################################################################
95 # マクロ
96 ######################################################################
97
98 # 指定したディレクトリーを作成
99 # 用例: $(call create_dir,name)
100 define create_dir
101     $(call chk_var_null,$1)
102     $(call chk_file_ext,$1)
103     $(MKDIR) $1
104 endef
105
106 # テストごとのMakefileを作成
107 # 用例: $(call create_makefile,file)
108 define create_makefile
109     $(RM) $1
110     $(foreach mkfile,$(DEF_FILE) $(TEST_MAKEFILE),$(ECHO) "include ../$(mkfile)" >>$1; )
111 endef
112
113 # リストで指定したディレクトリーでmakeを実行
114 # 用例: $(call make_tests,list_dir,target)
115 define make_tests
116     $(foreach dir,$1,$(call make_test_each,$(dir),$2))
117 endef
118
119 # 指定したディレクトリーでMakeを実行
120 # 用例: $(call make_test_each,tests,target)
121 define make_test_each
122     $(MAKE) $2 -sC $1;
123
124 endef
125
126 # 引数のファイルをチェックし、内容がない場合は削除
127 # 用例: $(call rm_null,file)
128 define rm_null
129     if test ! -s $1; then $(RM) $1; fi
130 endef
131
132 # 説明ファイルの内容を、引数のファイルに出力。
133 # 用例: $(call desc_log,file_out)
134 define desc_log
135     if test -s $(DESC_FILE); then $(CAT) $(DESC_FILE) >>$1; fi
136 endef
137
138 # テスト実行の経過時間を、ファイルに出力して表示。
139 # 引数は、テスト名、コマンドファイル、出力ファイル
140 # 用例: $(call time_cmd,name,file_cmd,file_out)
141 define time_cmd
142     $(TIME) -f"$1: %E" -o $3 ./$2 >$(DEV_NULL) 2>&1
143 endef
144
145 # テスト実行コマンド。引数は、コマンドファイル、出力ファイル、エラーファイル
146 # ファイルの内容と、CMD_FILE実行の標準出力を、出力ファイルに保存。
147 # エラー発生時は、エラー出力を出力ファイルとエラーファイルに保存。
148 # 用例: $(call exec_cmd,file_cmd,file_out,file_err)
149 define exec_cmd
150     $(CAT) $1 >$2
151     ./$1 >>$2 2>$3
152     if test -s $3; then $(CAT) $3 >>$2; fi
153     $(call rm_null,$3)
154 endef
155
156 # 2つのファイルを比較し、差分ファイルを作成。
157 # 引数は、2ファイルのリスト、差分ファイル
158 # 用例: $(call diff_files,files,file_out)
159 define diff_files
160     $(DIFF) $1 >$2 2>&1
161     $(call rm_null,$2)
162 endef
163
164 # 差分ファイルの内容をログファイルに出力。
165 # 引数は、テスト名、差分ファイル、ログファイル
166 # 用例: $(call test_log,name,file_diff,file_log)
167 define test_log
168     if test ! -s $2; then RES=Success; else RES=Failure; fi; $(ECHO) "$1: Test $$RES $(DATE)" >>$3
169 endef
170
171 # テストごとのファイルをグループファイルに出力
172 # 引数は、テストのリスト、グループファイル、テストファイル
173 # 用例: $(call group_log,files_test_log,file_group_log)
174 define group_log
175     $(foreach target,$1,$(call group_log_each,$(target),$2))
176 endef
177
178 # テストのログファイルをグループログファイルに出力。引数は、テスト、グループログファイル
179 # 用例: $(call group_log_each,file_test_log,file_group_log)
180 define group_log_each
181     if test -s $1; then $(CAT) $1 >>$2; else $(ECHO) $(dir $1)": no log" >>$2; fi
182     echo >>$2;
183
184 endef
185
186 # テストの結果を、グループログファイルを元にレポート。
187 # 引数は、グループログファイル
188 # 用例: $(call group_report,name,file_log,file_report)
189 define group_report
190     $(ECHO) "$1: $(SUCCESS_TEST) / $(ALL_TEST) tests passed. Details in $2" >$3;
191     if test $(FAIL_TEST) -eq 0; then $(ECHO) "$1: All tests are succeded." >>$3; fi
192 endef