1 |
578 |
markom |
# This file contains support code for the Tcl test suite. It is
|
2 |
|
|
# normally sourced by the individual files in the test suite before
|
3 |
|
|
# they run their tests. This improved approach to testing was designed
|
4 |
|
|
# and initially implemented by Mary Ann May-Pumphrey of Sun Microsystems.
|
5 |
|
|
#
|
6 |
|
|
# Copyright (c) 1990-1994 The Regents of the University of California.
|
7 |
|
|
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
|
8 |
|
|
#
|
9 |
|
|
# See the file "license.terms" for information on usage and redistribution
|
10 |
|
|
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
11 |
|
|
#
|
12 |
|
|
# SCCS: @(#) defs 1.44 96/10/08 17:26:58
|
13 |
|
|
|
14 |
|
|
if ![info exists VERBOSE] {
|
15 |
|
|
set VERBOSE 0
|
16 |
|
|
}
|
17 |
|
|
if ![info exists TESTS] {
|
18 |
|
|
set TESTS {}
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
# If tests are being run as root, issue a warning message and set a
|
22 |
|
|
# variable to prevent some tests from running at all.
|
23 |
|
|
|
24 |
|
|
set user {}
|
25 |
|
|
if {$tcl_platform(platform) == "unix"} {
|
26 |
|
|
catch {set user [exec whoami]}
|
27 |
|
|
if {$user == ""} {
|
28 |
|
|
catch {regexp {^[^(]*\(([^)]*)\)} [exec id] dummy user}
|
29 |
|
|
}
|
30 |
|
|
if {$user == ""} {set user root}
|
31 |
|
|
if {$user == "root"} {
|
32 |
|
|
puts stdout "Warning: you're executing as root. I'll have to"
|
33 |
|
|
puts stdout "skip some of the tests, since they'll fail as root."
|
34 |
|
|
}
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
# Some of the tests don't work on some system configurations due to
|
38 |
|
|
# differences in word length, file system configuration, etc. In order
|
39 |
|
|
# to prevent false alarms, these tests are generally only run in the
|
40 |
|
|
# master development directory for Tcl. The presence of a file
|
41 |
|
|
# "doAllTests" in this directory is used to indicate that the non-portable
|
42 |
|
|
# tests should be run.
|
43 |
|
|
|
44 |
|
|
set doNonPortableTests [file exists doAllTests]
|
45 |
|
|
|
46 |
|
|
# If there is no "memory" command (because memory debugging isn't
|
47 |
|
|
# enabled), generate a dummy command that does nothing.
|
48 |
|
|
|
49 |
|
|
if {[info commands memory] == ""} {
|
50 |
|
|
proc memory args {}
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
# Check configuration information that will determine which tests
|
54 |
|
|
# to run. To do this, create an array testConfig. Each element
|
55 |
|
|
# has a 0 or 1 value, and the following elements are defined:
|
56 |
|
|
# unixOnly - 1 means this is a UNIX platform, so it's OK
|
57 |
|
|
# to run tests that only work under UNIX.
|
58 |
|
|
# macOnly - 1 means this is a Mac platform, so it's OK
|
59 |
|
|
# to run tests that only work on Macs.
|
60 |
|
|
# pcOnly - 1 means this is a PC platform, so it's OK to
|
61 |
|
|
# run tests that only work on PCs.
|
62 |
|
|
# unixOrPc - 1 means this is a UNIX or PC platform.
|
63 |
|
|
# macOrPc - 1 means this is a Mac or PC platform.
|
64 |
|
|
# macOrUnix - 1 means this is a Mac or UNIX platform.
|
65 |
|
|
# nonPortable - 1 means this the tests are being running in
|
66 |
|
|
# the master Tcl/Tk development environment;
|
67 |
|
|
# Some tests are inherently non-portable because
|
68 |
|
|
# they depend on things like word length, file system
|
69 |
|
|
# configuration, window manager, etc. These tests
|
70 |
|
|
# are only run in the main Tcl development directory
|
71 |
|
|
# where the configuration is well known. The presence
|
72 |
|
|
# of the file "doAllTests" in this directory indicates
|
73 |
|
|
# that it is safe to run non-portable tests.
|
74 |
|
|
# tempNotPc - The inverse of pcOnly. This flag is used to
|
75 |
|
|
# temporarily disable a test.
|
76 |
|
|
# nonBlockFiles - 1 means this platform supports setting files into
|
77 |
|
|
# nonblocking mode.
|
78 |
|
|
# asyncPipeClose- 1 means this platform supports async flush and
|
79 |
|
|
# async close on a pipe.
|
80 |
|
|
# unixExecs - 1 means this machine has commands such as 'cat',
|
81 |
|
|
# 'echo' etc available.
|
82 |
|
|
|
83 |
|
|
catch {unset testConfig}
|
84 |
|
|
if {$tcl_platform(platform) == "unix"} {
|
85 |
|
|
set testConfig(unixOnly) 1
|
86 |
|
|
set testConfig(tempNotPc) 1
|
87 |
|
|
} else {
|
88 |
|
|
set testConfig(unixOnly) 0
|
89 |
|
|
}
|
90 |
|
|
if {$tcl_platform(platform) == "macintosh"} {
|
91 |
|
|
set testConfig(tempNotPc) 1
|
92 |
|
|
set testConfig(macOnly) 1
|
93 |
|
|
} else {
|
94 |
|
|
set testConfig(macOnly) 0
|
95 |
|
|
}
|
96 |
|
|
if {$tcl_platform(platform) == "windows"} {
|
97 |
|
|
set testConfig(pcOnly) 1
|
98 |
|
|
} else {
|
99 |
|
|
set testConfig(pcOnly) 0
|
100 |
|
|
}
|
101 |
|
|
set testConfig(unixOrPc) [expr $testConfig(unixOnly) || $testConfig(pcOnly)]
|
102 |
|
|
set testConfig(macOrPc) [expr $testConfig(macOnly) || $testConfig(pcOnly)]
|
103 |
|
|
set testConfig(macOrUnix) [expr $testConfig(macOnly) || $testConfig(unixOnly)]
|
104 |
|
|
set testConfig(nonPortable) [file exists doAllTests]
|
105 |
|
|
|
106 |
|
|
set f [open defs r]
|
107 |
|
|
if {[expr [catch {fconfigure $f -blocking off}]] == 0} {
|
108 |
|
|
set testConfig(nonBlockFiles) 1
|
109 |
|
|
} else {
|
110 |
|
|
set testConfig(nonBlockFiles) 0
|
111 |
|
|
}
|
112 |
|
|
close $f
|
113 |
|
|
|
114 |
|
|
# Test for SCO Unix - cannot run async flushing tests because a potential
|
115 |
|
|
# problem with select is apparently interfering. (Mark Diekhans).
|
116 |
|
|
|
117 |
|
|
if {$tcl_platform(platform) == "unix"} {
|
118 |
|
|
if {[catch {exec uname -X | fgrep {Release = 3.2v}}] == 0} {
|
119 |
|
|
set testConfig(asyncPipeClose) 0
|
120 |
|
|
} else {
|
121 |
|
|
set testConfig(asyncPipeClose) 1
|
122 |
|
|
}
|
123 |
|
|
} else {
|
124 |
|
|
set testConfig(asyncPipeClose) 1
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
# Test to see if execed commands such as cat, echo, rm and so forth are
|
128 |
|
|
# present on this machine.
|
129 |
|
|
|
130 |
|
|
set testConfig(unixExecs) 1
|
131 |
|
|
if {$tcl_platform(platform) == "macintosh"} {
|
132 |
|
|
set testConfig(unixExecs) 0
|
133 |
|
|
}
|
134 |
|
|
if {($testConfig(unixExecs) == 1) && ($tcl_platform(platform) == "windows")} {
|
135 |
|
|
if {[catch {exec cat defs}] == 1} {
|
136 |
|
|
set testConfig(unixExecs) 0
|
137 |
|
|
}
|
138 |
|
|
if {($testConfig(unixExecs) == 1) && ([catch {exec echo hello}] == 1)} {
|
139 |
|
|
set testConfig(unixExecs) 0
|
140 |
|
|
}
|
141 |
|
|
if {($testConfig(unixExecs) == 1) && \
|
142 |
|
|
([catch {exec sh -c echo hello}] == 1)} {
|
143 |
|
|
set testConfig(unixExecs) 0
|
144 |
|
|
}
|
145 |
|
|
if {($testConfig(unixExecs) == 1) && ([catch {exec wc defs}] == 1)} {
|
146 |
|
|
set testConfig(unixExecs) 0
|
147 |
|
|
}
|
148 |
|
|
if {$testConfig(unixExecs) == 1} {
|
149 |
|
|
exec echo hello > removeMe
|
150 |
|
|
if {[catch {exec rm removeMe}] == 1} {
|
151 |
|
|
set testConfig(unixExecs) 0
|
152 |
|
|
}
|
153 |
|
|
}
|
154 |
|
|
if {($testConfig(unixExecs) == 1) && ([catch {exec sleep 1}] == 1)} {
|
155 |
|
|
set testConfig(unixExecs) 0
|
156 |
|
|
}
|
157 |
|
|
if {($testConfig(unixExecs) == 1) && \
|
158 |
|
|
([catch {exec fgrep unixExecs defs}] == 1)} {
|
159 |
|
|
set testConfig(unixExecs) 0
|
160 |
|
|
}
|
161 |
|
|
if {($testConfig(unixExecs) == 1) && ([catch {exec ps}] == 1)} {
|
162 |
|
|
set testConfig(unixExecs) 0
|
163 |
|
|
}
|
164 |
|
|
if {($testConfig(unixExecs) == 1) && \
|
165 |
|
|
([catch {exec echo abc > removeMe}] == 0) && \
|
166 |
|
|
([catch {exec chmod 644 removeMe}] == 1) && \
|
167 |
|
|
([catch {exec rm removeMe}] == 0)} {
|
168 |
|
|
set testConfig(unixExecs) 0
|
169 |
|
|
} else {
|
170 |
|
|
catch {exec rm -f removeMe}
|
171 |
|
|
}
|
172 |
|
|
if {($testConfig(unixExecs) == 1) && \
|
173 |
|
|
([catch {exec mkdir removeMe}] == 1)} {
|
174 |
|
|
set testConfig(unixExecs) 0
|
175 |
|
|
} else {
|
176 |
|
|
catch {exec rm -r removeMe}
|
177 |
|
|
}
|
178 |
|
|
if {$testConfig(unixExecs) == 0} {
|
179 |
|
|
puts stdout "Warning: Unix-style executables are not available, so"
|
180 |
|
|
puts stdout "some tests will be skipped."
|
181 |
|
|
}
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
proc print_verbose {name description script code answer} {
|
185 |
|
|
puts stdout "\n"
|
186 |
|
|
puts stdout "==== $name $description"
|
187 |
|
|
puts stdout "==== Contents of test case:"
|
188 |
|
|
puts stdout "$script"
|
189 |
|
|
if {$code != 0} {
|
190 |
|
|
if {$code == 1} {
|
191 |
|
|
puts stdout "==== Test generated error:"
|
192 |
|
|
puts stdout $answer
|
193 |
|
|
} elseif {$code == 2} {
|
194 |
|
|
puts stdout "==== Test generated return exception; result was:"
|
195 |
|
|
puts stdout $answer
|
196 |
|
|
} elseif {$code == 3} {
|
197 |
|
|
puts stdout "==== Test generated break exception"
|
198 |
|
|
} elseif {$code == 4} {
|
199 |
|
|
puts stdout "==== Test generated continue exception"
|
200 |
|
|
} else {
|
201 |
|
|
puts stdout "==== Test generated exception $code; message was:"
|
202 |
|
|
puts stdout $answer
|
203 |
|
|
}
|
204 |
|
|
} else {
|
205 |
|
|
puts stdout "==== Result was:"
|
206 |
|
|
puts stdout "$answer"
|
207 |
|
|
}
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
# test --
|
211 |
|
|
# This procedure runs a test and prints an error message if the
|
212 |
|
|
# test fails. If VERBOSE has been set, it also prints a message
|
213 |
|
|
# even if the test succeeds. The test will be skipped if it
|
214 |
|
|
# doesn't match the TESTS variable, or if one of the elements
|
215 |
|
|
# of "constraints" turns out not to be true.
|
216 |
|
|
#
|
217 |
|
|
# Arguments:
|
218 |
|
|
# name - Name of test, in the form foo-1.2.
|
219 |
|
|
# description - Short textual description of the test, to
|
220 |
|
|
# help humans understand what it does.
|
221 |
|
|
# constraints - A list of one or more keywords, each of
|
222 |
|
|
# which must be the name of an element in
|
223 |
|
|
# the array "testConfig". If any of these
|
224 |
|
|
# elements is zero, the test is skipped.
|
225 |
|
|
# This argument may be omitted.
|
226 |
|
|
# script - Script to run to carry out the test. It must
|
227 |
|
|
# return a result that can be checked for
|
228 |
|
|
# correctness.
|
229 |
|
|
# answer - Expected result from script.
|
230 |
|
|
|
231 |
|
|
proc test {name description script answer args} {
|
232 |
|
|
global VERBOSE TESTS testConfig
|
233 |
|
|
if {[string compare $TESTS ""] != 0} then {
|
234 |
|
|
set ok 0
|
235 |
|
|
foreach test $TESTS {
|
236 |
|
|
if [string match $test $name] then {
|
237 |
|
|
set ok 1
|
238 |
|
|
break
|
239 |
|
|
}
|
240 |
|
|
}
|
241 |
|
|
if !$ok then return
|
242 |
|
|
}
|
243 |
|
|
set i [llength $args]
|
244 |
|
|
if {$i == 0} {
|
245 |
|
|
# Empty body
|
246 |
|
|
} elseif {$i == 1} {
|
247 |
|
|
# "constraints" argument exists; shuffle arguments down, then
|
248 |
|
|
# make sure that the constraints are satisfied.
|
249 |
|
|
|
250 |
|
|
set constraints $script
|
251 |
|
|
set script $answer
|
252 |
|
|
set answer [lindex $args 0]
|
253 |
|
|
foreach constraint $constraints {
|
254 |
|
|
if {![info exists testConfig($constraint)]
|
255 |
|
|
|| !$testConfig($constraint)} {
|
256 |
|
|
return
|
257 |
|
|
}
|
258 |
|
|
}
|
259 |
|
|
} else {
|
260 |
|
|
error "wrong # args: must be \"test name description ?constraints? script answer\""
|
261 |
|
|
}
|
262 |
|
|
memory tag $name
|
263 |
|
|
set code [catch {uplevel $script} result]
|
264 |
|
|
if {$code != 0} {
|
265 |
|
|
print_verbose $name $description $script \
|
266 |
|
|
$code $result
|
267 |
|
|
} elseif {[string compare $result $answer] == 0} then {
|
268 |
|
|
if $VERBOSE then {
|
269 |
|
|
if {$VERBOSE > 0} {
|
270 |
|
|
print_verbose $name $description $script \
|
271 |
|
|
$code $result
|
272 |
|
|
}
|
273 |
|
|
puts stdout "++++ $name PASSED"
|
274 |
|
|
}
|
275 |
|
|
} else {
|
276 |
|
|
print_verbose $name $description $script \
|
277 |
|
|
$code $result
|
278 |
|
|
puts stdout "---- Result should have been:"
|
279 |
|
|
puts stdout "$answer"
|
280 |
|
|
puts stdout "---- $name FAILED"
|
281 |
|
|
}
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
proc dotests {file args} {
|
285 |
|
|
global TESTS
|
286 |
|
|
set savedTests $TESTS
|
287 |
|
|
set TESTS $args
|
288 |
|
|
source $file
|
289 |
|
|
set TESTS $savedTests
|
290 |
|
|
}
|
291 |
|
|
|
292 |
|
|
proc normalizeMsg {msg} {
|
293 |
|
|
regsub "\n$" [string tolower $msg] "" msg
|
294 |
|
|
regsub -all "\n\n" $msg "\n" msg
|
295 |
|
|
regsub -all "\n\}" $msg "\}" msg
|
296 |
|
|
return $msg
|
297 |
|
|
}
|
298 |
|
|
|
299 |
|
|
proc makeFile {contents name} {
|
300 |
|
|
set fd [open $name w]
|
301 |
|
|
fconfigure $fd -translation lf
|
302 |
|
|
if {[string index $contents [expr [string length $contents] - 1]] == "\n"} {
|
303 |
|
|
puts -nonewline $fd $contents
|
304 |
|
|
} else {
|
305 |
|
|
puts $fd $contents
|
306 |
|
|
}
|
307 |
|
|
close $fd
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
proc removeFile {name} {
|
311 |
|
|
file delete $name
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
proc makeDirectory {name} {
|
315 |
|
|
file mkdir $name
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
proc removeDirectory {name} {
|
319 |
|
|
file delete -force $name
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
proc viewFile {name} {
|
323 |
|
|
global tcl_platform testConfig
|
324 |
|
|
if {($tcl_platform(platform) == "macintosh") || \
|
325 |
|
|
($testConfig(unixExecs) == 0)} {
|
326 |
|
|
set f [open $name]
|
327 |
|
|
set data [read -nonewline $f]
|
328 |
|
|
close $f
|
329 |
|
|
return $data
|
330 |
|
|
} else {
|
331 |
|
|
exec cat $name
|
332 |
|
|
}
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
# Locate tcltest executable
|
336 |
|
|
|
337 |
|
|
set tcltest [list [info nameofexecutable]]
|
338 |
|
|
if {$tcltest == "{}"} {
|
339 |
|
|
set tcltest {}
|
340 |
|
|
puts "Unable to find tcltest executable, multiple process tests will fail."
|
341 |
|
|
}
|
342 |
|
|
|
343 |
|
|
|