Debian Codegraph @

script/mtreview

raw
1 #!/usr/bin/env tclsh
2 #
3 # MTREVIEW, review test suite log file
4 # Copyright (C) 2019-2022 Xavier Delaruelle
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 ##########################################################################
20
21 proc reportUsage {} {
22 puts "Usage: $::argv0 \[options\] testlogfile
23
24 Review test suite log file
25
26 Options:
27 -h, --help Show this help message and exit
28
29 Examples:
30 $::argv0 modules.log
31 $::argv0 install.log
32 $::argv0 lint.log"
33 }
34
35 proc sgr {sgrcode str} {
36 return "\033\[${sgrcode}m$str\033\[0m"
37 }
38
39 proc reportError {str} {
40 puts "[sgr {1;31} ERROR]: $str"
41 }
42
43 proc diffWithIcdiff {} {
44 if {![info exists ::diff_with_icdiff]} {
45 # use local installation of icdiff, check it operates correctly
46 set ::diff_with_icdiff [expr {[auto_execok ./icdiff] ne\
47 {} && ![catch {exec ./icdiff --version}]}]
48 }
49 return $::diff_with_icdiff
50 }
51
52 proc diffWithDiff {} {
53 if {![info exists ::diff_with_diff]} {
54 set ::diff_with_diff [expr {[auto_execok diff] ne {}}]
55 }
56 return $::diff_with_diff
57 }
58
59 proc getDiffCommand {} {
60 # preferably use icdiff if possible
61 if {[diffWithIcdiff]} {
62 set cmdlist [list ./icdiff --no-headers]
63 # force term size on the different CI environments
64 if {[info exists ::env(GITHUB_ACTIONS)]} {
65 lappend cmdlist --cols=170
66 }
67 } else {
68 set cmdlist [list diff -u]
69 if {![catch {exec diff --color=auto /dev/null /dev/null}]} {
70 lappend cmdlist --color=auto
71 }
72 }
73 return $cmdlist
74 }
75
76 # parse arguments
77 set hintmsg "\n Try '$argv0 --help' for more information."
78 if {$argc != 1} {
79 reportError "Unexpected number of arguments$hintmsg"
80 exit 1
81 }
82 set arg [lindex $argv 0]
83 switch -glob -- $arg {
84 -h - --help {
85 reportUsage
86 exit 0
87 }
88 -* {
89 reportError "Invalid option '$arg'$hintmsg"
90 exit 1
91 }
92 default {
93 set logfile $arg
94 }
95 }
96
97 set fid [open $logfile r]
98
99 set state {}
100 while {[gets $fid line] >= 0} {
101 switch -- $state {
102 recres {
103 ##nagelfar ignore Unknown variable
104 if {$res ne {}} {
105 append res \n
106 } else {
107 # trim first line
108 set line [string range $line [string first ' $line] end]
109 }
110 append res $line
111 # end of obtained output?
112 if {[string range $line end-2 end] eq {'#>}} {
113 set state recexp
114 # clean content
115 set res [string range $res 1 end-3]
116 }
117 }
118 recexp {
119 ##nagelfar ignore Unknown variable
120 if {$exp ne {}} {
121 append exp \n
122 } else {
123 # trim first line
124 set line [string range $line [string first ' $line] end]
125 }
126 append exp $line
127 # end of expected output?
128 if {[string range $line end-2 end] eq {'#>}} {
129 # clean expecting content from regexp special char escaping
130 set trimstart 1
131 set trimend 3
132 if {[string index $exp 1] eq {^}} {
133 incr trimstart
134 }
135 if {[string index $exp end-3] eq {$}} {
136 incr trimend
137 }
138 set exp [string range $exp $trimstart end-$trimend]
139 set exp [regsub -all {\\([\\"'$|{}`* ()!&])} $exp {\1}]
140
141 # diff obtained and expecting output
142 if {![info exists externaldiff]} {
143 set externaldiff [expr {[diffWithIcdiff] || [diffWithDiff]}]
144 }
145 # use an external command to diff output
146 if {$externaldiff} {
147 if {![info exists diffcmdlist]} {
148 set diffcmdlist [getDiffCommand]
149 }
150 set fidres [open mtreview_res w]
151 puts $fidres $res
152 close $fidres
153 set fidexp [open mtreview_exp w]
154 puts $fidexp $exp
155 close $fidexp
156 if {[set errCode [catch {
157 eval exec >@stdout $diffcmdlist mtreview_exp mtreview_res
158 } errMsg]]} {
159 # report trouble of diff command
160 if {[diffWithDiff] && $errCode == 2} {
161 puts "[sgr {1;31} ERROR]: $errMsg"
162 }
163 }
164 file delete mtreview_exp mtreview_res
165 } else {
166 puts [sgr {1;31} $exp]
167 puts --
168 puts [sgr {1;32} $res]
169 }
170
171 # clear state to search for next failed test
172 set state {}
173 }
174 }
175 sumup {
176 if {[string index $line 0] eq "#"} {
177 puts $line
178 }
179 }
180 default {
181 if {![string compare -length 6 $line {FAIL: }]} {
182 if {![info exists failure_found]} {
183 set failure_found 1
184 }
185 set state recres
186 set res {}
187 set exp {}
188 ##nagelfar ignore #3 Unknown variable
189 if {![info exists testfile_printed($testfile)]} {
190 set testfile_printed($testfile) 1
191 puts [sgr {1;34;7} "=== $testfile ==="]
192 }
193 puts [sgr 7 $line]
194 } elseif {![string compare -length 8 $line {Running }]} {
195 set testfile [lindex [split $line] 1]
196 } elseif {[string match {*Summary ===} $line]} {
197 set state sumup
198 puts [sgr {1;33;7} {=== test summary ===}]
199 }
200 }
201 }
202 }
203
204 close $fid
205
206 exit [info exists failure_found]
207
208 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent syntax=tcl: