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 } elseif {[info exists ::env(CIRRUS_CI)]} {
67 lappend cmdlist --cols=150
68 }
69 } else {
70 set cmdlist [list diff -u]
71 if {![catch {exec diff --color=auto /dev/null /dev/null}]} {
72 lappend cmdlist --color=auto
73 }
74 }
75 return $cmdlist
76 }
77
78 # parse arguments
79 set hintmsg "\n Try '$argv0 --help' for more information."
80 if {$argc != 1} {
81 reportError "Unexpected number of arguments$hintmsg"
82 exit 1
83 }
84 set arg [lindex $argv 0]
85 switch -glob -- $arg {
86 -h - --help {
87 reportUsage
88 exit 0
89 }
90 -* {
91 reportError "Invalid option '$arg'$hintmsg"
92 exit 1
93 }
94 default {
95 set logfile $arg
96 }
97 }
98
99 set fid [open $logfile r]
100
101 set state {}
102 while {[gets $fid line] >= 0} {
103 switch -- $state {
104 recres {
105 ##nagelfar ignore Unknown variable
106 if {$res ne {}} {
107 append res \n
108 } else {
109 # trim first line
110 set line [string range $line [string first ' $line] end]
111 }
112 append res $line
113 # end of obtained output?
114 if {[string range $line end-2 end] eq {'#>}} {
115 set state recexp
116 # clean content
117 set res [string range $res 1 end-3]
118 }
119 }
120 recexp {
121 ##nagelfar ignore Unknown variable
122 if {$exp ne {}} {
123 append exp \n
124 } else {
125 # trim first line
126 set line [string range $line [string first ' $line] end]
127 }
128 append exp $line
129 # end of expected output?
130 if {[string range $line end-2 end] eq {'#>}} {
131 # clean expecting content from regexp special char escaping
132 set trimstart 1
133 set trimend 3
134 if {[string index $exp 1] eq {^}} {
135 incr trimstart
136 }
137 if {[string index $exp end-3] eq {$}} {
138 incr trimend
139 }
140 set exp [string range $exp $trimstart end-$trimend]
141 set exp [regsub -all {\\([\\"'$|{}`* ()!&])} $exp {\1}]
142
143 # diff obtained and expecting output
144 if {![info exists externaldiff]} {
145 set externaldiff [expr {[diffWithIcdiff] || [diffWithDiff]}]
146 }
147 # use an external command to diff output
148 if {$externaldiff} {
149 if {![info exists diffcmdlist]} {
150 set diffcmdlist [getDiffCommand]
151 }
152 set fidres [open mtreview_res w]
153 puts $fidres $res
154 close $fidres
155 set fidexp [open mtreview_exp w]
156 puts $fidexp $exp
157 close $fidexp
158 if {[set errCode [catch {
159 eval exec >@stdout $diffcmdlist mtreview_exp mtreview_res
160 } errMsg]]} {
161 # report trouble of diff command
162 if {[diffWithDiff] && $errCode == 2} {
163 puts "[sgr {1;31} ERROR]: $errMsg"
164 }
165 }
166 file delete mtreview_exp mtreview_res
167 } else {
168 puts [sgr {1;31} $exp]
169 puts --
170 puts [sgr {1;32} $res]
171 }
172
173 # clear state to search for next failed test
174 set state {}
175 }
176 }
177 sumup {
178 if {[string index $line 0] eq "#"} {
179 puts $line
180 }
181 }
182 default {
183 if {![string compare -length 6 $line {FAIL: }]} {
184 if {![info exists failure_found]} {
185 set failure_found 1
186 }
187 set state recres
188 set res {}
189 set exp {}
190 ##nagelfar ignore #3 Unknown variable
191 if {![info exists testfile_printed($testfile)]} {
192 set testfile_printed($testfile) 1
193 puts [sgr {1;34;7} "=== $testfile ==="]
194 }
195 puts [sgr 7 $line]
196 } elseif {![string compare -length 8 $line {Running }]} {
197 set testfile [lindex [split $line] 1]
198 } elseif {[string match {*Summary ===} $line]} {
199 set state sumup
200 puts [sgr {1;33;7} {=== test summary ===}]
201 }
202 }
203 }
204 }
205
206 close $fid
207
208 exit [info exists failure_found]
209
210 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent syntax=tcl: