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