Debian Codegraph @

ctags/misc/review

raw
1 #!/bin/bash
2 #
3 # review - Review the diff between expected output and actual output in Units
4 #
5 # Copyright (C) 2016 Masatake YAMATO
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 print_help()
22 {
23 echo Usage:
24 printf " %-20s %-30s %s\n" "$0" "help|--help|-h" "show this message"
25 printf " %-20s %-30s %s\n" "$0" "[list] [-b]" "list failed Units and Tmain"
26 help_list
27 printf " %-20s %-30s %s\n" "$0" "inspect [-b]" "inspect difference interactively"
28 help_inspect
29 exit $1
30 }
31
32 is_known_bug()
33 {
34 [[ $1 == *.b ]]
35 return $?
36 }
37
38 do_list ()
39 {
40 local d
41 local n
42 local a
43 local including_known_bugs
44
45 for a in "$@"; do
46 case $a in
47 (-b)
48 including_known_bugs=-b
49 ;;
50 (-*)
51 echo "unknown option: ${a}" 1>&2
52 exit 1
53 ;;
54 (*)
55 echo "unexpected argument ${a}" 1>&2
56 exit 1
57 ;;
58 esac
59 done
60
61 for d in $(find Units -name "DIFF.tmp"); do
62 n=$(dirname "$d")
63 if (( ! is_known_bug $(basename "$n") ) || [[ -n ${including_known_bugs} ]] ) \
64 && [[ -e "$n"/expected.tags ]]; then
65 echo ${n}
66 fi
67 done
68
69 for d in $(find Tmain -name "*-diff.txt"); do
70 n=$(dirname "$d")
71 echo ${n}
72 done | sort -u
73 }
74
75 help_list()
76 {
77 printf " %-20s %-30s %s\n" "" "-b" "list .b (known bug) marked cases"
78 }
79
80 units_inspect_cmd_accept ()
81 {
82 local from=./Units/$1/FILTERED.tmp
83 local to=./Units/$1/expected.tags
84 local r
85
86 if [[ ! -e "${from}" ]]; then
87 echo "./Units/$1/FILTERED.tmp is not found" 1>&2
88 echo "Remove DIFF.tmp?"
89 read -p "[Y/n] "
90 if [[ "$REPLY" == Y ]]; then
91 rm ./Units/$t/DIFF.tmp
92 echo "Removed: DIFF.tmp"
93 r=1
94 else
95 echo "Kept: DIFF.tmp"
96 r=0
97 fi
98 return $r
99 fi
100
101 echo "Do you really want do following shell command?"
102 echo
103 echo " mv \"$from\"" '\'
104 echo " \"$to\" "
105 echo
106 read -p "[Y/n] "
107
108 if [[ "$REPLY" == Y ]]; then
109 mv "$from" "$to"
110 echo "Renamed: $from => $to"
111 r=0
112
113 echo "Remove DIFF.tmp, too?"
114 read -p "[Y/n] "
115 if [[ "$REPLY" == Y ]]; then
116 rm ./Units/$t/DIFF.tmp
117 echo "Removed: DIFF.tmp"
118 else
119 echo "Kept: DIFF.tmp"
120 fi
121 else
122 echo "Aborted"
123 r=1
124 fi
125
126 return $r
127 }
128
129 units_inspect_cmd_skip ()
130 {
131 echo "SKIPPING $1" 1>&2
132 }
133
134 inspect_cmd_show_generic ()
135 {
136 local tcase=$1
137 local file=$2
138 shift 2
139 local f=./Units/${tcase}/${file}
140
141 if [[ -r "$f" ]]; then
142 "$@" "$f"
143 else
144 echo "No ${file}" 1>&2
145 fi
146 echo '---'
147 echo '# ' "$f"
148 }
149
150 units_inspect_cmd_show_diff ()
151 {
152 local cmd=cat
153
154 if which colordiff > /dev/null 2>&1; then
155 cmd=colordiff
156 elif which pygmentize > /dev/null 2>&1; then
157 cmd="pygmentize -l diff"
158 fi
159
160 inspect_cmd_show_generic $1 DIFF.tmp cat | ${cmd}
161 }
162
163 units_inspect_show_args_ctags ()
164 {
165 inspect_cmd_show_generic $1 args.ctags cat
166 }
167
168 pygmentize_or_cat ()
169 {
170 # It seems that pygmentize ignores the empty lines at the head of input.
171 if [ -z "$(head -1 $1)" ]; then
172 cat -n $1
173 elif which pygmentize > /dev/null 2>&1; then
174 pygmentize -O "style=colorful,linenos=1" $1 2>/dev/null || cat -n $1
175 else
176 cat -n $1
177 fi
178 }
179
180 units_inspect_show_input ()
181 {
182 local tcase=$1
183
184 inspect_cmd_show_generic $tcase $(basename ./Units/${tcase}/input.*) pygmentize_or_cat
185 }
186
187 # See https://stackoverflow.com/questions/65533232/bash-extglob-pattern-matching-breaks-when-enclosed-in-a-function
188 shopt -s extglob
189 units_inspect_show_expected_tags ()
190 {
191 local tcase=$1
192
193 (
194 shopt -s extglob
195 inspect_cmd_show_generic $tcase $(basename ./Units/${tcase}/expected.tags?\(-*\)) "cat" "-n"
196 )
197 }
198 shopt -u extglob
199
200 inspect_cmd_quit ()
201 {
202 echo "BYE" 1>&2
203 exit 0
204 }
205
206 inspect_cmd_unknown()
207 {
208 echo "unknown command: $REPLY" 1>&2
209 echo "Just return to show menu" 1>&2
210 }
211
212 help_inspect ()
213 {
214 printf " %-20s %-30s %s\n" "" "-b" "inspect .b (known bug) marked cases"
215 }
216
217 do_units_inspect()
218 {
219 local next=0
220 local r
221 local t=$1
222
223 select r in '<a>ccept' '<s>kip (or <n>)' '<S>hell' '<d>iff' 'show args.<c>tags' 'show <i>nput' '<e>xpected tags' '<q>uit'; do
224 case $r-$REPLY in
225 ("<a>ccept"-*|-a)
226 if units_inspect_cmd_accept "$t"; then
227 next=1
228 fi
229 ;;
230 ("<s>kip (or <n>)"-*|-s|-!|-skip|-n|-next)
231 units_inspect_cmd_skip "$t"
232 next=1
233 ;;
234 ("<S>hell"-*|-S)
235 inspect_cmd_shell Units "$t"
236 ;;
237 ("<d>iff"-*|-d|-=|-diff)
238 units_inspect_cmd_show_diff "$t"
239 ;;
240 ("<q>uit"-*|-q|-quit)
241 inspect_cmd_quit
242 ;;
243 ("show args.<c>tags"-*|-c|-A|-args.ctags)
244 units_inspect_show_args_ctags "$t"
245 ;;
246 ("show <i>nput"-*|-i|-input)
247 units_inspect_show_input "$t"
248 ;;
249 ("<e>xpected tags"-*|-e|-expected.tags)
250 units_inspect_show_expected_tags "$t"
251 ;;
252 (*)
253 inspect_cmd_unknown
254 ;;
255 esac
256 if [[ $next == 1 ]]; then
257 break
258 fi
259 done
260 }
261
262 function inspect_cmd_shell
263 {
264 local prefix=$1
265 local t=$2
266
267 (
268 cd $prefix/$t
269 PS1="review> " bash
270 )
271 }
272
273 function tmain_inspect_run
274 {
275 local t=$1
276 make tmain UNITS=$(basename $t .d)
277 }
278
279 do_tmain_inspect()
280 {
281 local next=0
282 local r
283 local t=$1
284
285 select r in '<n>ext' '<S>hell' '<R>un' '<q>uit'; do
286 case $r-$REPLY in
287 ("<n>ext"-*|-n)
288 next=1
289 ;;
290 ("<S>hell"-*|-S)
291 inspect_cmd_shell Tmain "$t"
292 ;;
293 ("<R>un"-*|-R)
294 tmain_inspect_run "$t"
295 ;;
296 ("<q>uit"-*|-q|-quit)
297 inspect_cmd_quit
298 ;;
299 esac
300 if [[ $next == 1 ]]; then
301 break
302 fi
303 done
304 }
305
306 count ()
307 {
308 echo $#
309 }
310
311 do_inspect ()
312 {
313 local a
314 local t0
315 local t
316 local including_known_bugs
317
318 for a in "$@"; do
319 case $a in
320 (-b)
321 including_known_bugs=-b
322 ;;
323 (-*)
324 echo "unknown option: ${a}" 1>&2
325 exit 1
326 ;;
327 (*)
328 echo "unexpected argument ${a}" 1>&2
329 exit 1
330 ;;
331 esac
332 done
333
334 local T=$(do_list ${including_known_bugs});
335 local total=$(count $T)
336 local i=1
337 for t0 in $T; do
338 PS3="[$i/$total [ $t0 ]]? "
339 i=$(( i + 1 ))
340 t=${t0#Units/}
341 t=${t#Tmain/}
342
343 if [[ "$t0" =~ ^Units ]]; then
344 do_units_inspect "$t"
345 else
346 do_tmain_inspect "$t"
347 fi
348 done
349 }
350
351 main ()
352 {
353 local action
354
355 while [[ $# -gt 0 ]]; do
356 case $1 in
357 (help|--help|-h)
358 print_help 0
359 ;;
360 (list)
361 action=$1
362 shift
363 break
364 ;;
365 (inspect)
366 action=$1
367 shift
368 break
369 ;;
370 (*)
371 print_help 1 1>&2
372 ;;
373 esac
374 done
375
376 if [[ -z "$action" ]]; then
377 action=inspect
378 fi
379
380 if ! [[ -d ./Units ]]; then
381 echo "$0: cannot find ./Units directory" 1>&2
382 exit 1
383 fi
384
385 if ! [[ -d ./Tmain ]]; then
386 echo "$0: cannot find ./Tmain directory" 1>&2
387 exit 1
388 fi
389
390 do_${action} "$@"
391 }
392
393 main "$@"