| 1 |
# Commands covered: exec |
| 2 |
# |
| 3 |
# This file contains a collection of tests for one or more of the Tcl built-in |
| 4 |
# commands. Sourcing this file into Tcl runs the tests and generates output |
| 5 |
# for errors. No output means no errors were found. |
| 6 |
# |
| 7 |
# Copyright (c) 1991-1994 The Regents of the University of California. |
| 8 |
# Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 9 |
# Copyright (c) 1998-1999 by Scriptics Corporation. |
| 10 |
# |
| 11 |
# See the file "license.terms" for information on usage and redistribution of |
| 12 |
# this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
|
| 14 |
# There is no point in running Valgrind on cases where [exec] forks but then |
| 15 |
# fails and the child process doesn't go through full cleanup. |
| 16 |
|
| 17 |
if {"::tcltest" ni [namespace children]} { |
| 18 |
package require tcltest 2.5 |
| 19 |
namespace import -force ::tcltest::* |
| 20 |
} |
| 21 |
source [file join [file dirname [info script]] tcltests.tcl] |
| 22 |
|
| 23 |
# Some skips when running in a macOS CI environment |
| 24 |
testConstraint noosxCI [expr {![info exists ::env(MAC_CI)]}] |
| 25 |
|
| 26 |
# Need a App Exec Alias for testing exec of reparse points, |
| 27 |
# according to MS, winget can only be used on servers with an installed desktop interface, |
| 28 |
# no idea how to check it in GHA programmatically, so simply disable it (todo: rewrite with better check later) |
| 29 |
if {[testConstraint win] && ![info exists ::env(CI)] && |
| 30 |
[info exists ::env(LOCALAPPDATA)]} { |
| 31 |
set wingetPath [file join $::env(LOCALAPPDATA) "Microsoft" "WindowsApps" "winget.exe"] |
| 32 |
if {[file exists $wingetPath]} { |
| 33 |
# Ensure it is on the PATH |
| 34 |
testConstraint haveWinget 1 |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
unset -nocomplain path |
| 39 |
|
| 40 |
# Utilities that are like Bourne shell stalwarts, but cross-platform. |
| 41 |
set path(echo) [makeFile { |
| 42 |
puts -nonewline [lindex $argv 0] |
| 43 |
foreach str [lrange $argv 1 end] { |
| 44 |
puts -nonewline " $str" |
| 45 |
} |
| 46 |
puts {} |
| 47 |
exit |
| 48 |
} echo] |
| 49 |
set path(echo2) [makeFile { |
| 50 |
puts stdout [join $argv] |
| 51 |
puts stderr [lindex $argv 1] |
| 52 |
exit |
| 53 |
} echo2] |
| 54 |
set path(cat) [makeFile { |
| 55 |
if {$argv eq ""} { |
| 56 |
set argv - |
| 57 |
} |
| 58 |
fconfigure stdout -translation binary |
| 59 |
foreach name $argv { |
| 60 |
if {$name eq "-"} { |
| 61 |
set f stdin |
| 62 |
} elseif {[catch {open $name r} f] != 0} { |
| 63 |
puts stderr $f |
| 64 |
continue |
| 65 |
} |
| 66 |
fconfigure $f -translation binary |
| 67 |
while {[eof $f] == 0} { |
| 68 |
puts -nonewline [read $f] |
| 69 |
} |
| 70 |
if {$f ne "stdin"} { |
| 71 |
close $f |
| 72 |
} |
| 73 |
} |
| 74 |
exit |
| 75 |
} cat] |
| 76 |
set path(wc) [makeFile { |
| 77 |
set data [read stdin] |
| 78 |
set lines [regsub -all "\n" $data {} dummy] |
| 79 |
set words [regsub -all "\[^ \t\n]+" $data {} dummy] |
| 80 |
set chars [string length $data] |
| 81 |
puts [format "%8.d%8.d%8.d" $lines $words $chars] |
| 82 |
exit |
| 83 |
} wc] |
| 84 |
set path(sh) [makeFile { |
| 85 |
if {[lindex $argv 0] ne "-c"} { |
| 86 |
error "sh: unexpected arguments $argv" |
| 87 |
} |
| 88 |
set cmd [lindex $argv 1] |
| 89 |
lappend cmd ";" |
| 90 |
set newcmd {} |
| 91 |
foreach arg $cmd { |
| 92 |
if {$arg eq ";"} { |
| 93 |
exec >@stdout 2>@stderr [info nameofexecutable] {*}$newcmd |
| 94 |
set newcmd {} |
| 95 |
continue |
| 96 |
} |
| 97 |
if {$arg eq "1>&2"} { |
| 98 |
set arg >@stderr |
| 99 |
} |
| 100 |
lappend newcmd $arg |
| 101 |
} |
| 102 |
exit |
| 103 |
} sh] |
| 104 |
set path(sh2) [makeFile { |
| 105 |
if {[lindex $argv 0] ne "-c"} { |
| 106 |
error "sh: unexpected arguments $argv" |
| 107 |
} |
| 108 |
set cmd [lindex $argv 1] |
| 109 |
lappend cmd ";" |
| 110 |
set newcmd {} |
| 111 |
foreach arg $cmd { |
| 112 |
if {$arg eq ";"} { |
| 113 |
exec -ignorestderr >@stdout [info nameofexecutable] {*}$newcmd |
| 114 |
set newcmd {} |
| 115 |
continue |
| 116 |
} |
| 117 |
lappend newcmd $arg |
| 118 |
} |
| 119 |
exit |
| 120 |
} sh2] |
| 121 |
set path(sleep) [makeFile { |
| 122 |
after [expr {$argv*1000}] |
| 123 |
exit |
| 124 |
} sleep] |
| 125 |
set path(exit) [makeFile { |
| 126 |
exit $argv |
| 127 |
} exit] |
| 128 |
|
| 129 |
proc readfile filename { |
| 130 |
set f [open $filename] |
| 131 |
set d [read $f] |
| 132 |
close $f |
| 133 |
return [string trimright $d \n] |
| 134 |
} |
| 135 |
|
| 136 |
# ---------------------------------------------------------------------- |
| 137 |
# Basic operations. |
| 138 |
|
| 139 |
test exec-1.1 {basic exec operation} {exec} { |
| 140 |
exec [interpreter] $path(echo) a b c |
| 141 |
} "a b c" |
| 142 |
test exec-1.2 {pipelining} {exec stdio} { |
| 143 |
exec [interpreter] $path(echo) a b c d | [interpreter] $path(cat) | [interpreter] $path(cat) |
| 144 |
} "a b c d" |
| 145 |
test exec-1.3 {pipelining} {exec stdio} { |
| 146 |
set a [exec [interpreter] $path(echo) a b c d | [interpreter] $path(cat) | [interpreter] $path(wc)] |
| 147 |
list [scan $a "%d %d %d" b c d] $b $c |
| 148 |
} {3 1 4} |
| 149 |
set arg {12345678901234567890123456789012345678901234567890} |
| 150 |
set arg "$arg$arg$arg$arg$arg$arg" |
| 151 |
test exec-1.4 {long command lines} {exec} { |
| 152 |
exec [interpreter] $path(echo) $arg |
| 153 |
} $arg |
| 154 |
set arg {} |
| 155 |
|
| 156 |
# I/O redirection: input from Tcl command. |
| 157 |
|
| 158 |
test exec-2.1 {redirecting input from immediate source} {exec stdio} { |
| 159 |
exec [interpreter] $path(cat) << "Sample text" |
| 160 |
} {Sample text} |
| 161 |
test exec-2.2 {redirecting input from immediate source} {exec stdio} { |
| 162 |
exec << "Sample text" [interpreter] $path(cat) | [interpreter] $path(cat) |
| 163 |
} {Sample text} |
| 164 |
test exec-2.3 {redirecting input from immediate source} {exec stdio} { |
| 165 |
exec [interpreter] $path(cat) << "Sample text" | [interpreter] $path(cat) |
| 166 |
} {Sample text} |
| 167 |
test exec-2.4 {redirecting input from immediate source} {exec stdio} { |
| 168 |
exec [interpreter] $path(cat) | [interpreter] $path(cat) << "Sample text" |
| 169 |
} {Sample text} |
| 170 |
test exec-2.5 {redirecting input from immediate source} {exec} { |
| 171 |
exec [interpreter] $path(cat) "<<Joined to arrows" |
| 172 |
} {Joined to arrows} |
| 173 |
test exec-2.6 {redirecting input from immediate source, with UTF} -setup { |
| 174 |
set sysenc [encoding system] |
| 175 |
encoding system iso8859-1 |
| 176 |
proc quotenonascii s { |
| 177 |
regsub -all {\[|\\|\]} $s {\\&} s |
| 178 |
regsub -all "\[\u007f-\uffff\]" $s \ |
| 179 |
{[apply {c {format {\u%04x} [scan $c %c]}} &]} s |
| 180 |
return [subst -novariables $s] |
| 181 |
} |
| 182 |
} -constraints {exec} -body { |
| 183 |
# If this fails, it may give back: "\uC3\uA9\uC3\uA0\uC3\uBC\uC3\uB1" |
| 184 |
# If it does, this means that the UTF -> external conversion did not occur |
| 185 |
# before writing out the temp file. |
| 186 |
quotenonascii [exec [interpreter] $path(cat) << "\uE9\uE0\uFC\uF1"] |
| 187 |
} -cleanup { |
| 188 |
encoding system $sysenc |
| 189 |
rename quotenonascii {} |
| 190 |
} -result {\u00e9\u00e0\u00fc\u00f1} |
| 191 |
|
| 192 |
# I/O redirection: output to file. |
| 193 |
|
| 194 |
set path(gorp.file) [makeFile {} gorp.file] |
| 195 |
file delete $path(gorp.file) |
| 196 |
|
| 197 |
test exec-3.1 {redirecting output to file} {exec} { |
| 198 |
exec [interpreter] $path(echo) "Some simple words" > $path(gorp.file) |
| 199 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 200 |
} "Some simple words" |
| 201 |
test exec-3.2 {redirecting output to file} {exec stdio} { |
| 202 |
exec [interpreter] $path(echo) "More simple words" | >$path(gorp.file) [interpreter] $path(cat) | [interpreter] $path(cat) |
| 203 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 204 |
} "More simple words" |
| 205 |
test exec-3.3 {redirecting output to file} {exec stdio} { |
| 206 |
exec > $path(gorp.file) [interpreter] $path(echo) "Different simple words" | [interpreter] $path(cat) | [interpreter] $path(cat) |
| 207 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 208 |
} "Different simple words" |
| 209 |
test exec-3.4 {redirecting output to file} {exec} { |
| 210 |
exec [interpreter] $path(echo) "Some simple words" >$path(gorp.file) |
| 211 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 212 |
} "Some simple words" |
| 213 |
test exec-3.5 {redirecting output to file} {exec} { |
| 214 |
exec [interpreter] $path(echo) "First line" >$path(gorp.file) |
| 215 |
exec [interpreter] $path(echo) "Second line" >> $path(gorp.file) |
| 216 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 217 |
} "First line\nSecond line" |
| 218 |
test exec-3.6 {redirecting output to file} {exec} { |
| 219 |
exec [interpreter] $path(echo) "First line" >$path(gorp.file) |
| 220 |
exec [interpreter] $path(echo) "Second line" >>$path(gorp.file) |
| 221 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 222 |
} "First line\nSecond line" |
| 223 |
test exec-3.7 {redirecting output to file} {exec} { |
| 224 |
set f [open $path(gorp.file) w] |
| 225 |
puts $f "Line 1" |
| 226 |
flush $f |
| 227 |
exec [interpreter] $path(echo) "More text" >@ $f |
| 228 |
exec [interpreter] $path(echo) >@$f "Even more" |
| 229 |
puts $f "Line 3" |
| 230 |
close $f |
| 231 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 232 |
} "Line 1\nMore text\nEven more\nLine 3" |
| 233 |
|
| 234 |
# I/O redirection: output and stderr to file. |
| 235 |
|
| 236 |
file delete $path(gorp.file) |
| 237 |
|
| 238 |
test exec-4.1 {redirecting output and stderr to file} {exec} { |
| 239 |
exec [interpreter] $path(echo) "test output" >& $path(gorp.file) |
| 240 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 241 |
} "test output" |
| 242 |
test exec-4.2 {redirecting output and stderr to file} {exec} { |
| 243 |
list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" >&$path(gorp.file)] \ |
| 244 |
[exec [interpreter] $path(cat) $path(gorp.file)] |
| 245 |
} {{} {foo bar}} |
| 246 |
test exec-4.3 {redirecting output and stderr to file} {exec} { |
| 247 |
exec [interpreter] $path(echo) "first line" > $path(gorp.file) |
| 248 |
list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" >>&$path(gorp.file)] \ |
| 249 |
[exec [interpreter] $path(cat) $path(gorp.file)] |
| 250 |
} "{} {first line\nfoo bar}" |
| 251 |
test exec-4.4 {redirecting output and stderr to file} {exec} { |
| 252 |
set f [open $path(gorp.file) w] |
| 253 |
puts $f "Line 1" |
| 254 |
flush $f |
| 255 |
exec [interpreter] $path(echo) "More text" >&@ $f |
| 256 |
exec [interpreter] $path(echo) >&@$f "Even more" |
| 257 |
puts $f "Line 3" |
| 258 |
close $f |
| 259 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 260 |
} "Line 1\nMore text\nEven more\nLine 3" |
| 261 |
test exec-4.5 {redirecting output and stderr to file} {exec} { |
| 262 |
set f [open $path(gorp.file) w] |
| 263 |
puts $f "Line 1" |
| 264 |
flush $f |
| 265 |
exec >&@ $f [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" |
| 266 |
exec >&@$f [interpreter] $path(sh) -c "\"$path(echo)\" xyzzy 1>&2" |
| 267 |
puts $f "Line 3" |
| 268 |
close $f |
| 269 |
exec [interpreter] $path(cat) $path(gorp.file) |
| 270 |
} "Line 1\nfoo bar\nxyzzy\nLine 3" |
| 271 |
|
| 272 |
# I/O redirection: input from file. |
| 273 |
|
| 274 |
if {[testConstraint exec]} { |
| 275 |
exec [interpreter] $path(echo) "Just a few thoughts" > $path(gorp.file) |
| 276 |
} |
| 277 |
test exec-5.1 {redirecting input from file} {exec} { |
| 278 |
exec [interpreter] $path(cat) < $path(gorp.file) |
| 279 |
} {Just a few thoughts} |
| 280 |
test exec-5.2 {redirecting input from file} {exec stdio} { |
| 281 |
exec [interpreter] $path(cat) | [interpreter] $path(cat) < $path(gorp.file) |
| 282 |
} {Just a few thoughts} |
| 283 |
test exec-5.3 {redirecting input from file} {exec stdio} { |
| 284 |
exec [interpreter] $path(cat) < $path(gorp.file) | [interpreter] $path(cat) |
| 285 |
} {Just a few thoughts} |
| 286 |
test exec-5.4 {redirecting input from file} {exec stdio} { |
| 287 |
exec < $path(gorp.file) [interpreter] $path(cat) | [interpreter] $path(cat) |
| 288 |
} {Just a few thoughts} |
| 289 |
test exec-5.5 {redirecting input from file} {exec} { |
| 290 |
exec [interpreter] $path(cat) <$path(gorp.file) |
| 291 |
} {Just a few thoughts} |
| 292 |
test exec-5.6 {redirecting input from file} -constraints {exec} -body { |
| 293 |
set f [open $path(gorp.file) r] |
| 294 |
exec [interpreter] $path(cat) <@ $f |
| 295 |
} -cleanup { |
| 296 |
close $f |
| 297 |
} -result {Just a few thoughts} |
| 298 |
test exec-5.7 {redirecting input from file} -constraints {exec} -body { |
| 299 |
set f [open $path(gorp.file) r] |
| 300 |
exec <@$f [interpreter] $path(cat) |
| 301 |
} -cleanup { |
| 302 |
close $f |
| 303 |
} -result {Just a few thoughts} |
| 304 |
|
| 305 |
# I/O redirection: standard error through a pipeline. |
| 306 |
|
| 307 |
test exec-6.1 {redirecting stderr through a pipeline} {exec stdio} { |
| 308 |
exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar" |& [interpreter] $path(cat) |
| 309 |
} "foo bar" |
| 310 |
test exec-6.2 {redirecting stderr through a pipeline} {exec stdio} { |
| 311 |
exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" |& [interpreter] $path(cat) |
| 312 |
} "foo bar" |
| 313 |
test exec-6.3 {redirecting stderr through a pipeline} {exec stdio} { |
| 314 |
exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" \ |
| 315 |
|& [interpreter] $path(sh) -c "\"$path(echo)\" second msg 1>&2 ; \"$path(cat)\"" |& [interpreter] $path(cat) |
| 316 |
} "second msg\nfoo bar" |
| 317 |
|
| 318 |
# I/O redirection: combinations. |
| 319 |
|
| 320 |
set path(gorp.file2) [makeFile {} gorp.file2] |
| 321 |
|
| 322 |
test exec-7.1 {multiple I/O redirections} {exec} { |
| 323 |
exec << "command input" > $path(gorp.file2) [interpreter] $path(cat) < $path(gorp.file) |
| 324 |
exec [interpreter] $path(cat) $path(gorp.file2) |
| 325 |
} {Just a few thoughts} |
| 326 |
test exec-7.2 {multiple I/O redirections} {exec} { |
| 327 |
exec < $path(gorp.file) << "command input" [interpreter] $path(cat) |
| 328 |
} {command input} |
| 329 |
|
| 330 |
# Long input to command and output from command. |
| 331 |
set a "0123456789 xxxxxxxxx abcdefghi ABCDEFGHIJK\n" |
| 332 |
set a [concat $a $a $a $a] |
| 333 |
set a [concat $a $a $a $a] |
| 334 |
set a [concat $a $a $a $a] |
| 335 |
set a [concat $a $a $a $a] |
| 336 |
test exec-8.1 {long input and output} {exec} { |
| 337 |
exec [interpreter] $path(cat) << $a |
| 338 |
} $a |
| 339 |
# More than 20 arguments to exec. |
| 340 |
test exec-8.2 {long input and output} {exec} { |
| 341 |
exec [interpreter] $path(echo) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
| 342 |
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23} |
| 343 |
|
| 344 |
# Commands that return errors. |
| 345 |
|
| 346 |
test exec-9.1 {commands returning errors} {exec notValgrind} { |
| 347 |
set x [catch {exec gorp456} msg] |
| 348 |
list $x [string tolower $msg] [string tolower $errorCode] |
| 349 |
} {1 {couldn't execute "gorp456": no such file or directory} {posix enoent {no such file or directory}}} |
| 350 |
test exec-9.2 {commands returning errors} {exec notValgrind} { |
| 351 |
string tolower [list [catch {exec [interpreter] echo foo | foo123} msg] $msg $errorCode] |
| 352 |
} {1 {couldn't execute "foo123": no such file or directory} {posix enoent {no such file or directory}}} |
| 353 |
test exec-9.3 {commands returning errors} -constraints {exec stdio} -body { |
| 354 |
exec [interpreter] $path(sleep) 1 | [interpreter] $path(exit) 43 | [interpreter] $path(sleep) 1 |
| 355 |
} -returnCodes error -result {child process exited abnormally} |
| 356 |
test exec-9.4 {commands returning errors} -constraints {exec stdio} -body { |
| 357 |
exec [interpreter] $path(exit) 43 | [interpreter] $path(echo) "foo bar" |
| 358 |
} -returnCodes error -result {foo bar |
| 359 |
child process exited abnormally} |
| 360 |
test exec-9.5 {commands returning errors} -constraints {exec stdio notValgrind} -body { |
| 361 |
exec gorp456 | [interpreter] echo a b c |
| 362 |
} -returnCodes error -result {couldn't execute "gorp456": no such file or directory} |
| 363 |
test exec-9.6 {commands returning errors} -constraints {exec} -body { |
| 364 |
exec [interpreter] $path(sh) -c "\"$path(echo)\" error msg 1>&2" |
| 365 |
} -returnCodes error -result {error msg} |
| 366 |
test exec-9.7 {commands returning errors} -constraints {exec stdio nonPortable} -body { |
| 367 |
# This test can fail easily on multiprocessor machines |
| 368 |
exec [interpreter] $path(sh) -c "\"$path(echo)\" error msg 1>&2 ; \"$path(sleep)\" 1" \ |
| 369 |
| [interpreter] $path(sh) -c "\"$path(echo)\" error msg 1>&2 ; \"$path(sleep)\" 1" |
| 370 |
} -returnCodes error -result {error msg |
| 371 |
error msg} |
| 372 |
set path(err) [makeFile {} err] |
| 373 |
test exec-9.8 {commands returning errors} -constraints {exec} -setup { |
| 374 |
set f [open $path(err) w] |
| 375 |
puts $f { |
| 376 |
puts stdout out |
| 377 |
puts stderr err |
| 378 |
} |
| 379 |
close $f |
| 380 |
} -body { |
| 381 |
exec [interpreter] $path(err) |
| 382 |
} -returnCodes error -result {out |
| 383 |
err} |
| 384 |
|
| 385 |
# Errors in executing the Tcl command, as opposed to errors in the processes |
| 386 |
# that are invoked. |
| 387 |
|
| 388 |
test exec-10.1 {errors in exec invocation} -constraints {exec} -body { |
| 389 |
exec |
| 390 |
} -returnCodes error -result {wrong # args: should be "exec ?-option ...? arg ?arg ...?"} |
| 391 |
test exec-10.2 {errors in exec invocation} -constraints {exec} -body { |
| 392 |
exec | cat |
| 393 |
} -returnCodes error -result {illegal use of | or |& in command} |
| 394 |
test exec-10.3 {errors in exec invocation} -constraints {exec} -body { |
| 395 |
exec cat | |
| 396 |
} -returnCodes error -result {illegal use of | or |& in command} |
| 397 |
test exec-10.4 {errors in exec invocation} -constraints {exec} -body { |
| 398 |
exec cat | | cat |
| 399 |
} -returnCodes error -result {illegal use of | or |& in command} |
| 400 |
test exec-10.5 {errors in exec invocation} -constraints {exec} -body { |
| 401 |
exec cat | |& cat |
| 402 |
} -returnCodes error -result {illegal use of | or |& in command} |
| 403 |
test exec-10.6 {errors in exec invocation} -constraints {exec} -body { |
| 404 |
exec cat |& |
| 405 |
} -returnCodes error -result {illegal use of | or |& in command} |
| 406 |
test exec-10.7 {errors in exec invocation} -constraints {exec} -body { |
| 407 |
exec cat < |
| 408 |
} -returnCodes error -result {can't specify "<" as last word in command} |
| 409 |
test exec-10.8 {errors in exec invocation} -constraints {exec} -body { |
| 410 |
exec cat > |
| 411 |
} -returnCodes error -result {can't specify ">" as last word in command} |
| 412 |
test exec-10.9 {errors in exec invocation} -constraints {exec} -body { |
| 413 |
exec cat << |
| 414 |
} -returnCodes error -result {can't specify "<<" as last word in command} |
| 415 |
test exec-10.10 {errors in exec invocation} -constraints {exec} -body { |
| 416 |
exec cat >> |
| 417 |
} -returnCodes error -result {can't specify ">>" as last word in command} |
| 418 |
test exec-10.11 {errors in exec invocation} -constraints {exec} -body { |
| 419 |
exec cat >& |
| 420 |
} -returnCodes error -result {can't specify ">&" as last word in command} |
| 421 |
test exec-10.12 {errors in exec invocation} -constraints {exec} -body { |
| 422 |
exec cat >>& |
| 423 |
} -returnCodes error -result {can't specify ">>&" as last word in command} |
| 424 |
test exec-10.13 {errors in exec invocation} -constraints {exec} -body { |
| 425 |
exec cat >@ |
| 426 |
} -returnCodes error -result {can't specify ">@" as last word in command} |
| 427 |
test exec-10.14 {errors in exec invocation} -constraints {exec} -body { |
| 428 |
exec cat <@ |
| 429 |
} -returnCodes error -result {can't specify "<@" as last word in command} |
| 430 |
test exec-10.15 {errors in exec invocation} -constraints {exec} -body { |
| 431 |
exec cat < a/b/c |
| 432 |
} -returnCodes error -result {couldn't read file "a/b/c": no such file or directory} |
| 433 |
test exec-10.16 {errors in exec invocation} -constraints {exec} -body { |
| 434 |
exec cat << foo > a/b/c |
| 435 |
} -returnCodes error -result {couldn't write file "a/b/c": no such file or directory} |
| 436 |
test exec-10.17 {errors in exec invocation} -constraints {exec} -body { |
| 437 |
exec cat << foo > a/b/c |
| 438 |
} -returnCodes error -result {couldn't write file "a/b/c": no such file or directory} |
| 439 |
set f [open $path(gorp.file) w] |
| 440 |
test exec-10.18 {errors in exec invocation} -constraints {exec} -body { |
| 441 |
exec cat <@ $f |
| 442 |
} -returnCodes error -result "channel \"$f\" wasn't opened for reading" |
| 443 |
close $f |
| 444 |
set f [open $path(gorp.file) r] |
| 445 |
test exec-10.19 {errors in exec invocation} -constraints {exec} -body { |
| 446 |
exec cat >@ $f |
| 447 |
} -returnCodes error -result "channel \"$f\" wasn't opened for writing" |
| 448 |
close $f |
| 449 |
test exec-10.20 {errors in exec invocation} -constraints {exec notValgrind} -body { |
| 450 |
exec ~non_existent_user/foo/bar |
| 451 |
} -returnCodes error -result {user "non_existent_user" doesn't exist} |
| 452 |
test exec-10.21 {errors in exec invocation} -constraints {exec notValgrind} -body { |
| 453 |
exec [interpreter] true | ~xyzzy_bad_user/x | false |
| 454 |
} -returnCodes error -result {user "xyzzy_bad_user" doesn't exist} |
| 455 |
test exec-10.22 {errors in exec invocation} -constraints {exec notValgrind} -body { |
| 456 |
exec echo test > ~non_existent_user/foo/bar |
| 457 |
} -returnCodes error -result {user "non_existent_user" doesn't exist} |
| 458 |
# Commands in background. |
| 459 |
|
| 460 |
test exec-11.1 {commands in background} {exec} { |
| 461 |
set time [time {exec [interpreter] $path(sleep) 2 &}] |
| 462 |
expr {[lindex $time 0] < 1000000} |
| 463 |
} 1 |
| 464 |
test exec-11.2 {commands in background} -constraints {exec} -body { |
| 465 |
exec [interpreter] $path(echo) a &b |
| 466 |
} -result {a &b} |
| 467 |
test exec-11.3 {commands in background} {exec} { |
| 468 |
llength [exec [interpreter] $path(sleep) 1 &] |
| 469 |
} 1 |
| 470 |
test exec-11.4 {commands in background} {exec stdio} { |
| 471 |
llength [exec [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 | [interpreter] $path(sleep) 1 &] |
| 472 |
} 3 |
| 473 |
test exec-11.5 {commands in background} {exec} { |
| 474 |
set f [open $path(gorp.file) w] |
| 475 |
puts $f [list catch [list exec [info nameofexecutable] $path(echo) foo &]] |
| 476 |
close $f |
| 477 |
exec [interpreter] $path(gorp.file) |
| 478 |
} foo |
| 479 |
|
| 480 |
# Make sure that background commands are properly reaped when they |
| 481 |
# eventually die. |
| 482 |
|
| 483 |
if {[testConstraint exec] && [testConstraint nonPortable]} { |
| 484 |
after 1300 |
| 485 |
exec [interpreter] $path(sleep) 1 |
| 486 |
} |
| 487 |
test exec-12.1 {reaping background processes} {exec unix nonPortable} { |
| 488 |
for {set i 0} {$i < 20} {incr i} { |
| 489 |
exec echo foo > /dev/null & |
| 490 |
} |
| 491 |
after 1000 |
| 492 |
catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg |
| 493 |
lindex $msg 0 |
| 494 |
} 0 |
| 495 |
test exec-12.2 {reaping background processes} {exec unix nonPortable} { |
| 496 |
exec sleep 2 | sleep 2 | sleep 2 & |
| 497 |
catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg |
| 498 |
set x [lindex $msg 0] |
| 499 |
after 3000 |
| 500 |
catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg |
| 501 |
list $x [lindex $msg 0] |
| 502 |
} {3 0} |
| 503 |
test exec-12.3 {reaping background processes} {exec unix nonPortable} { |
| 504 |
exec sleep 1000 & |
| 505 |
exec sleep 1000 & |
| 506 |
set x [exec ps | fgrep "sleep" | fgrep -v fgrep] |
| 507 |
set pids {} |
| 508 |
foreach i [split $x \n] { |
| 509 |
lappend pids [lindex $i 0] |
| 510 |
} |
| 511 |
foreach i $pids { |
| 512 |
catch {exec kill -STOP $i} |
| 513 |
} |
| 514 |
catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg |
| 515 |
set x [lindex $msg 0] |
| 516 |
foreach i $pids { |
| 517 |
catch {exec kill -KILL $i} |
| 518 |
} |
| 519 |
catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg |
| 520 |
list $x [lindex $msg 0] |
| 521 |
} {2 0} |
| 522 |
|
| 523 |
# Make sure "errorCode" is set correctly. |
| 524 |
|
| 525 |
test exec-13.1 {setting errorCode variable} {exec} { |
| 526 |
list [catch {exec [interpreter] $path(cat) < a/b/c} msg] [string tolower $errorCode] |
| 527 |
} {1 {posix enoent {no such file or directory}}} |
| 528 |
test exec-13.2 {setting errorCode variable} {exec} { |
| 529 |
list [catch {exec [interpreter] $path(cat) > a/b/c} msg] [string tolower $errorCode] |
| 530 |
} {1 {posix enoent {no such file or directory}}} |
| 531 |
test exec-13.3 {setting errorCode variable} {exec notValgrind} { |
| 532 |
set x [catch {exec _weird_cmd_} msg] |
| 533 |
list $x [string tolower $msg] [lindex $errorCode 0] \ |
| 534 |
[string tolower [lrange $errorCode 2 end]] |
| 535 |
} {1 {couldn't execute "_weird_cmd_": no such file or directory} POSIX {{no such file or directory}}} |
| 536 |
test exec-13.4 {extended exit result codes} -setup { |
| 537 |
set tmp [makeFile {exit 0x00000101} tmpfile.exec-13.4] |
| 538 |
} -constraints {win} -body { |
| 539 |
list [catch {exec [interpreter] $tmp} err] [lreplace $::errorCode 1 1 {}] |
| 540 |
} -cleanup { |
| 541 |
removeFile $tmp |
| 542 |
} -result {1 {CHILDSTATUS {} 257}} |
| 543 |
test exec-13.5 {extended exit result codes: max value} -setup { |
| 544 |
set tmp [makeFile {exit 0x3fffffff} tmpfile.exec-13.5] |
| 545 |
} -constraints {win} -body { |
| 546 |
list [catch {exec [interpreter] $tmp} err] [lreplace $::errorCode 1 1 {}] |
| 547 |
} -cleanup { |
| 548 |
removeFile $tmp |
| 549 |
} -result {1 {CHILDSTATUS {} 1073741823}} |
| 550 |
test exec-13.6 {extended exit result codes: signalled} -setup { |
| 551 |
set tmp [makeFile {exit 0xC0000016} tmpfile.exec-13.6] |
| 552 |
} -constraints {win} -body { |
| 553 |
list [catch {exec [interpreter] $tmp} err] [lreplace $::errorCode 1 1 {}] |
| 554 |
} -cleanup { |
| 555 |
removeFile $tmp |
| 556 |
} -result {1 {CHILDKILLED {} SIGABRT SIGABRT}} |
| 557 |
|
| 558 |
# Switches before the first argument |
| 559 |
|
| 560 |
test exec-14.1 {-keepnewline switch} {exec} { |
| 561 |
exec -keepnewline [interpreter] $path(echo) foo |
| 562 |
} "foo\n" |
| 563 |
test exec-14.2 {-keepnewline switch} -constraints {exec} -body { |
| 564 |
exec -keepnewline |
| 565 |
} -returnCodes error -result {wrong # args: should be "exec ?-option ...? arg ?arg ...?"} |
| 566 |
test exec-14.3 {unknown switch} -constraints {exec} -body { |
| 567 |
exec -gorp |
| 568 |
} -returnCodes error -result {bad option "-gorp": must be -ignorestderr, -keepnewline, or --} |
| 569 |
test exec-14.4 {-- switch} -constraints {exec notValgrind} -body { |
| 570 |
exec -- -gorp |
| 571 |
} -returnCodes error -result {couldn't execute "-gorp": no such file or directory} |
| 572 |
test exec-14.5 {-ignorestderr switch} {exec} { |
| 573 |
# Alas, the use of -ignorestderr is buried here :-( |
| 574 |
exec [interpreter] $path(sh2) -c [list $path(echo2) foo bar] 2>@1 |
| 575 |
} "foo bar\nbar" |
| 576 |
|
| 577 |
# Redirecting standard error separately from standard output |
| 578 |
|
| 579 |
test exec-15.1 {standard error redirection} {exec} { |
| 580 |
exec [interpreter] $path(echo) "First line" > $path(gorp.file) |
| 581 |
list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" 2> $path(gorp.file)] \ |
| 582 |
[exec [interpreter] $path(cat) $path(gorp.file)] |
| 583 |
} {{} {foo bar}} |
| 584 |
test exec-15.2 {standard error redirection} {exec stdio} { |
| 585 |
list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" \ |
| 586 |
| [interpreter] $path(echo) biz baz >$path(gorp.file) 2> $path(gorp.file2)] \ |
| 587 |
[exec [interpreter] $path(cat) $path(gorp.file)] \ |
| 588 |
[exec [interpreter] $path(cat) $path(gorp.file2)] |
| 589 |
} {{} {biz baz} {foo bar}} |
| 590 |
test exec-15.3 {standard error redirection} {exec stdio} { |
| 591 |
list [exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" \ |
| 592 |
| [interpreter] $path(echo) biz baz 2>$path(gorp.file) > $path(gorp.file2)] \ |
| 593 |
[exec [interpreter] $path(cat) $path(gorp.file)] \ |
| 594 |
[exec [interpreter] $path(cat) $path(gorp.file2)] |
| 595 |
} {{} {foo bar} {biz baz}} |
| 596 |
test exec-15.4 {standard error redirection} {exec} { |
| 597 |
set f [open $path(gorp.file) w] |
| 598 |
puts $f "Line 1" |
| 599 |
flush $f |
| 600 |
exec [interpreter] $path(sh) -c "\"$path(echo)\" foo bar 1>&2" 2>@ $f |
| 601 |
puts $f "Line 3" |
| 602 |
close $f |
| 603 |
readfile $path(gorp.file) |
| 604 |
} {Line 1 |
| 605 |
foo bar |
| 606 |
Line 3} |
| 607 |
test exec-15.5 {standard error redirection} {exec} { |
| 608 |
exec [interpreter] $path(echo) "First line" > "$path(gorp.file)" |
| 609 |
exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" 2>> "$path(gorp.file)" |
| 610 |
readfile $path(gorp.file) |
| 611 |
} {First line |
| 612 |
foo bar} |
| 613 |
test exec-15.6 {standard error redirection} {exec stdio} { |
| 614 |
exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" > "$path(gorp.file2)" 2> "$path(gorp.file)" \ |
| 615 |
>& "$path(gorp.file)" 2> "$path(gorp.file2)" | [interpreter] $path(echo) biz baz |
| 616 |
list [readfile $path(gorp.file)] [readfile $path(gorp.file2)] |
| 617 |
} {{biz baz} {foo bar}} |
| 618 |
test exec-15.7 {standard error redirection 2>@1} {exec stdio} { |
| 619 |
# This redirects stderr output into normal result output from exec |
| 620 |
exec [interpreter] "$path(sh)" -c "\"$path(echo)\" foo bar 1>&2" 2>@1 |
| 621 |
} {foo bar} |
| 622 |
|
| 623 |
test exec-16.1 {flush output before exec} {exec} { |
| 624 |
set f [open $path(gorp.file) w] |
| 625 |
puts $f "First line" |
| 626 |
exec [interpreter] $path(echo) "Second line" >@ $f |
| 627 |
puts $f "Third line" |
| 628 |
close $f |
| 629 |
readfile $path(gorp.file) |
| 630 |
} {First line |
| 631 |
Second line |
| 632 |
Third line} |
| 633 |
test exec-16.2 {flush output before exec} {exec} { |
| 634 |
set f [open $path(gorp.file) w] |
| 635 |
puts $f "First line" |
| 636 |
exec [interpreter] << {puts stderr {Second line}} >&@ $f > $path(gorp.file2) |
| 637 |
puts $f "Third line" |
| 638 |
close $f |
| 639 |
readfile $path(gorp.file) |
| 640 |
} {First line |
| 641 |
Second line |
| 642 |
Third line} |
| 643 |
|
| 644 |
test exec-17.1 {inheriting standard I/O} -constraints {exec} -setup { |
| 645 |
set path(script) [makeFile {} script] |
| 646 |
set f [open $path(script) w] |
| 647 |
puts $f [list lassign [list \ |
| 648 |
[info nameofexecutable] $path(gorp.file) $path(echo) $path(sleep) \ |
| 649 |
] exe file echo sleep] |
| 650 |
puts $f { |
| 651 |
close stdout |
| 652 |
set f [open $file w] |
| 653 |
catch {exec $exe $echo foobar &} |
| 654 |
exec $exe $sleep 2 |
| 655 |
close $f |
| 656 |
} |
| 657 |
close $f |
| 658 |
} -body { |
| 659 |
catch {exec [interpreter] $path(script)} result |
| 660 |
list $result [readfile $path(gorp.file)] |
| 661 |
} -cleanup { |
| 662 |
removeFile $path(script) |
| 663 |
} -result {{} foobar} |
| 664 |
|
| 665 |
test exec-18.1 {exec deals with weird file names} -body { |
| 666 |
set path(fooblah) [makeFile {contents} "foo\[\{blah"] |
| 667 |
exec [interpreter] $path(cat) $path(fooblah) |
| 668 |
} -constraints {exec} -cleanup { |
| 669 |
removeFile $path(fooblah) |
| 670 |
} -result contents |
| 671 |
test exec-18.2 {exec cat deals with weird file names} -body { |
| 672 |
# This is cross-platform, but the cat isn't predictably correct on |
| 673 |
# Windows. |
| 674 |
set path(fooblah) [makeFile {contents} "foo\[\{blah"] |
| 675 |
exec cat $path(fooblah) |
| 676 |
} -constraints {exec tempNotWin} -cleanup { |
| 677 |
removeFile $path(fooblah) |
| 678 |
} -result contents |
| 679 |
|
| 680 |
# Note that this test cannot be adapted to work on Windows; that platform has |
| 681 |
# no kernel support for an analog of O_APPEND. OTOH, that means we can assume |
| 682 |
# that there is a POSIX shell... |
| 683 |
# |
| 684 |
# This test also fails in some cases when building with macOS |
| 685 |
test exec-19.1 {exec >> uses O_APPEND} -constraints {exec unix notValgrind noosxCI} -setup { |
| 686 |
set tmpfile [makeFile {0} tmpfile.exec-19.1] |
| 687 |
} -body { |
| 688 |
# Note that we have to allow for the current contents of the temporary |
| 689 |
# file, which is why the result is 14 and not 12 |
| 690 |
exec /bin/sh -c \ |
| 691 |
{for a in 1 2 3; do sleep 1; echo $a; done} >>$tmpfile & |
| 692 |
exec /bin/sh -c \ |
| 693 |
{for a in 4 5 6; do sleep 1; echo $a >&2; done} 2>>$tmpfile & |
| 694 |
exec /bin/sh -c \ |
| 695 |
{for a in a b c; do sleep 1; echo $a; done} >>$tmpfile & |
| 696 |
exec /bin/sh -c \ |
| 697 |
{for a in d e f; do sleep 1; echo $a >&2; done} 2>>$tmpfile & |
| 698 |
# The above four shell invocations take about 3 seconds to finish, so allow |
| 699 |
# 5s (in case the machine is busy) |
| 700 |
after 5000 |
| 701 |
# Check that no bytes have got lost through mixups with overlapping |
| 702 |
# appends, which is only guaranteed to work when we set O_APPEND on the |
| 703 |
# file descriptor in the [exec >>...] |
| 704 |
file size $tmpfile |
| 705 |
} -cleanup { |
| 706 |
removeFile $tmpfile |
| 707 |
} -result 26 |
| 708 |
|
| 709 |
# Tests to ensure batch files and .CMD (Bug 9ece99d58b) |
| 710 |
# can be executed on Windows |
| 711 |
test exec-20.0 {exec .bat file} -constraints {win} -body { |
| 712 |
set log [makeFile {} exec20.log] |
| 713 |
exec [makeFile "echo %1> $log" exec20.bat] "Testing exec-20.0" |
| 714 |
viewFile $log |
| 715 |
} -result "\"Testing exec-20.0\"" |
| 716 |
test exec-20.1 {exec .CMD file} -constraints {win} -body { |
| 717 |
set log [makeFile {} exec201.log] |
| 718 |
exec [makeFile "echo %1> $log" exec201.CMD] "Testing exec-20.1" |
| 719 |
viewFile $log |
| 720 |
} -result "\"Testing exec-20.1\"" |
| 721 |
|
| 722 |
test exec-bug-4f0b5767ac {exec App Execution Alias} -setup { |
| 723 |
# Ensure winget is on the PATH |
| 724 |
set savedEnv $::env(PATH) |
| 725 |
append env(PATH) ";" [file nativename [file dirname $wingetPath]] |
| 726 |
} -cleanup { |
| 727 |
set ::env(PATH) $savedEnv |
| 728 |
unset -nocomplain savedEnv |
| 729 |
} -constraints haveWinget -body { |
| 730 |
exec winget --info |
| 731 |
} -result "*Windows*" -match glob |
| 732 |
|
| 733 |
foreach cmdBuiltin { |
| 734 |
assoc call cd cls color copy date del dir echo |
| 735 |
erase exit ftype for if md mkdir mklink move path |
| 736 |
pause prompt rd ren rename rmdir set start time |
| 737 |
title type ver vol |
| 738 |
} { |
| 739 |
test auto_execok-$cmdBuiltin-1.0 "auto_execok $cmdBuiltin" \ |
| 740 |
-constraints win \ |
| 741 |
-body { |
| 742 |
string equal [auto_execok $cmdBuiltin] \ |
| 743 |
"[file normalize $::env(COMSPEC)] /c $cmdBuiltin" |
| 744 |
} -result 1 |
| 745 |
} |
| 746 |
unset cmdBuiltin |
| 747 |
|
| 748 |
# ---------------------------------------------------------------------- |
| 749 |
# cleanup |
| 750 |
|
| 751 |
foreach file {gorp.file gorp.file2 echo echo2 cat wc sh sh2 sleep exit err} { |
| 752 |
removeFile $file |
| 753 |
} |
| 754 |
unset -nocomplain path wingetPath |
| 755 |
|
| 756 |
::tcltest::cleanupTests |
| 757 |
return |
| 758 |
|
| 759 |
# Local Variables: |
| 760 |
# mode: tcl |
| 761 |
# End: |