archie/tcl7.6/tests/exec.test

478 lines
16 KiB
Plaintext
Raw Normal View History

2024-05-27 16:13:40 +02:00
# Commands covered: exec
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands. Sourcing this file into Tcl runs the tests and
# generates output for errors. No output means no errors were found.
#
2024-05-27 16:40:40 +02:00
# Copyright (c) 1991-1994 The Regents of the University of California.
# Copyright (c) 1994 Sun Microsystems, Inc.
2024-05-27 16:13:40 +02:00
#
2024-05-27 16:40:40 +02:00
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
2024-05-27 16:13:40 +02:00
#
2024-05-27 16:40:40 +02:00
# SCCS: @(#) exec.test 1.54 96/08/27 14:40:01
2024-05-27 16:13:40 +02:00
if {[string compare test [info procs test]] == 1} then {source defs}
2024-05-27 16:40:40 +02:00
# If exec is not defined just return with no error
# Some platforms like the Macintosh do not have the exec command
if {[info commands exec] == ""} {
puts "exec not implemented for this machine"
return
}
2024-05-27 16:13:40 +02:00
# Basic operations.
2024-05-27 16:40:40 +02:00
test exec-1.1 {basic exec operation} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo a b c
} "a b c"
2024-05-27 16:40:40 +02:00
test exec-1.2 {pipelining} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo a b c d | cat | cat
} "a b c d"
2024-05-27 16:40:40 +02:00
test exec-1.3 {pipelining} {unixExecs} {
2024-05-27 16:13:40 +02:00
set a [exec echo a b c d | cat | wc]
2024-05-27 16:40:40 +02:00
list [scan $a "%d %d %d" b c d] $b $c
} {3 1 4}
set arg {12345678901234567890123456789012345678901234567890}
set arg "$arg$arg$arg$arg$arg$arg"
test exec-1.4 {long command lines} {unixExecs} {
exec echo $arg
} $arg
set arg {}
2024-05-27 16:13:40 +02:00
# I/O redirection: input from Tcl command.
2024-05-27 16:40:40 +02:00
test exec-2.1 {redirecting input from immediate source} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec cat << "Sample text"
} {Sample text}
2024-05-27 16:40:40 +02:00
test exec-2.2 {redirecting input from immediate source} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec << "Sample text" cat | cat
} {Sample text}
2024-05-27 16:40:40 +02:00
test exec-2.3 {redirecting input from immediate source} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec cat << "Sample text" | cat
} {Sample text}
2024-05-27 16:40:40 +02:00
test exec-2.4 {redirecting input from immediate source} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec cat | cat << "Sample text"
} {Sample text}
2024-05-27 16:40:40 +02:00
test exec-2.5 {redirecting input from immediate source} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec cat "<<Joined to arrows"
} {Joined to arrows}
# I/O redirection: output to file.
catch {exec rm -f gorp.file}
2024-05-27 16:40:40 +02:00
test exec-3.1 {redirecting output to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo "Some simple words" > gorp.file
exec cat gorp.file
} "Some simple words"
2024-05-27 16:40:40 +02:00
test exec-3.2 {redirecting output to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo "More simple words" | >gorp.file cat | cat
exec cat gorp.file
} "More simple words"
2024-05-27 16:40:40 +02:00
test exec-3.3 {redirecting output to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec > gorp.file echo "Different simple words" | cat | cat
exec cat gorp.file
} "Different simple words"
2024-05-27 16:40:40 +02:00
test exec-3.4 {redirecting output to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo "Some simple words" >gorp.file
exec cat gorp.file
} "Some simple words"
2024-05-27 16:40:40 +02:00
test exec-3.5 {redirecting output to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo "First line" >gorp.file
exec echo "Second line" >> gorp.file
exec cat gorp.file
} "First line\nSecond line"
2024-05-27 16:40:40 +02:00
test exec-3.6 {redirecting output to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo "First line" >gorp.file
exec echo "Second line" >>gorp.file
exec cat gorp.file
} "First line\nSecond line"
2024-05-27 16:40:40 +02:00
test exec-3.7 {redirecting output to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
set f [open gorp.file w]
puts $f "Line 1"
flush $f
exec echo "More text" >@ $f
exec echo >@$f "Even more"
puts $f "Line 3"
close $f
exec cat gorp.file
} "Line 1\nMore text\nEven more\nLine 3"
# I/O redirection: output and stderr to file.
catch {exec rm -f gorp.file}
2024-05-27 16:40:40 +02:00
test exec-4.1 {redirecting output and stderr to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo "test output" >& gorp.file
exec cat gorp.file
} "test output"
2024-05-27 16:40:40 +02:00
test exec-4.2 {redirecting output and stderr to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [exec sh -c "echo foo bar 1>&2" >&gorp.file] \
[exec cat gorp.file]
} {{} {foo bar}}
2024-05-27 16:40:40 +02:00
test exec-4.3 {redirecting output and stderr to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo "first line" > gorp.file
list [exec sh -c "echo foo bar 1>&2" >>&gorp.file] \
[exec cat gorp.file]
} "{} {first line\nfoo bar}"
2024-05-27 16:40:40 +02:00
test exec-4.4 {redirecting output and stderr to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
set f [open gorp.file w]
puts $f "Line 1"
flush $f
exec echo "More text" >&@ $f
exec echo >&@$f "Even more"
puts $f "Line 3"
close $f
exec cat gorp.file
} "Line 1\nMore text\nEven more\nLine 3"
2024-05-27 16:40:40 +02:00
test exec-4.5 {redirecting output and stderr to file} {unixExecs} {
2024-05-27 16:13:40 +02:00
set f [open gorp.file w]
puts $f "Line 1"
flush $f
exec >&@ $f sh -c "echo foo bar 1>&2"
exec >&@$f sh -c "echo xyzzy 1>&2"
puts $f "Line 3"
close $f
exec cat gorp.file
} "Line 1\nfoo bar\nxyzzy\nLine 3"
# I/O redirection: input from file.
2024-05-27 16:40:40 +02:00
catch {exec echo "Just a few thoughts" > gorp.file}
test exec-5.1 {redirecting input from file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec cat < gorp.file
} {Just a few thoughts}
2024-05-27 16:40:40 +02:00
test exec-5.2 {redirecting input from file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec cat | cat < gorp.file
} {Just a few thoughts}
2024-05-27 16:40:40 +02:00
test exec-5.3 {redirecting input from file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec cat < gorp.file | cat
} {Just a few thoughts}
2024-05-27 16:40:40 +02:00
test exec-5.4 {redirecting input from file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec < gorp.file cat | cat
} {Just a few thoughts}
2024-05-27 16:40:40 +02:00
test exec-5.5 {redirecting input from file} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec cat <gorp.file
} {Just a few thoughts}
2024-05-27 16:40:40 +02:00
test exec-5.6 {redirecting input from file} {unixExecs} {
2024-05-27 16:13:40 +02:00
set f [open gorp.file r]
set result [exec cat <@ $f]
close $f
set result
} {Just a few thoughts}
2024-05-27 16:40:40 +02:00
test exec-5.7 {redirecting input from file} {unixExecs} {
2024-05-27 16:13:40 +02:00
set f [open gorp.file r]
set result [exec <@$f cat]
close $f
set result
} {Just a few thoughts}
# I/O redirection: standard error through a pipeline.
2024-05-27 16:40:40 +02:00
test exec-6.1 {redirecting stderr through a pipeline} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec sh -c "echo foo bar" |& cat
} "foo bar"
2024-05-27 16:40:40 +02:00
test exec-6.2 {redirecting stderr through a pipeline} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec sh -c "echo foo bar 1>&2" |& cat
} "foo bar"
2024-05-27 16:40:40 +02:00
test exec-6.3 {redirecting stderr through a pipeline} {unixExecs} {
exec sh -c "echo foo bar 1>&2" \
|& sh -c "echo second msg 1>&2; cat" |& cat
2024-05-27 16:13:40 +02:00
} "second msg\nfoo bar"
# I/O redirection: combinations.
catch {exec rm -f gorp.file2}
2024-05-27 16:40:40 +02:00
test exec-7.1 {multiple I/O redirections} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec << "command input" > gorp.file2 cat < gorp.file
exec cat gorp.file2
} {Just a few thoughts}
2024-05-27 16:40:40 +02:00
test exec-7.2 {multiple I/O redirections} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec < gorp.file << "command input" cat
} {command input}
# Long input to command and output from command.
set a "0123456789 xxxxxxxxx abcdefghi ABCDEFGHIJK\n"
set a [concat $a $a $a $a]
set a [concat $a $a $a $a]
set a [concat $a $a $a $a]
set a [concat $a $a $a $a]
2024-05-27 16:40:40 +02:00
test exec-8.1 {long input and output} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec cat << $a
} $a
# Commands that return errors.
test exec-9.1 {commands returning errors} {
set x [catch {exec gorp456} msg]
2024-05-27 16:40:40 +02:00
list $x [string tolower $msg] [string tolower $errorCode]
} {1 {couldn't execute "gorp456": no such file or directory} {posix enoent {no such file or directory}}}
test exec-9.2 {commands returning errors} {unixExecs} {
string tolower [list [catch {exec echo foo | foo123} msg] $msg $errorCode]
} {1 {couldn't execute "foo123": no such file or directory} {posix enoent {no such file or directory}}}
test exec-9.3 {commands returning errors} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [catch {exec sleep 1 | sh -c "exit 43" | sleep 1} msg] $msg
} {1 {child process exited abnormally}}
2024-05-27 16:40:40 +02:00
test exec-9.4 {commands returning errors} {unixExecs} {
list [catch {exec sh -c "exit 43" | echo "foo bar"} msg] $msg
} {1 {foo bar
child process exited abnormally}}
test exec-9.5 {commands returning errors} {unixExecs} {
list [catch {exec gorp456 | echo a b c} msg] [string tolower $msg]
} {1 {couldn't execute "gorp456": no such file or directory}}
test exec-9.6 {commands returning errors} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [catch {exec sh -c "echo error msg 1>&2"} msg] $msg
} {1 {error msg}}
2024-05-27 16:40:40 +02:00
test exec-9.7 {commands returning errors} {unixExecs} {
list [catch {exec sh -c "echo error msg 1>&2" \
| sh -c "echo error msg 1>&2"} msg] $msg
2024-05-27 16:13:40 +02:00
} {1 {error msg
error msg}}
# Errors in executing the Tcl command, as opposed to errors in the
# processes that are invoked.
test exec-10.1 {errors in exec invocation} {
list [catch {exec} msg] $msg
} {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
test exec-10.2 {errors in exec invocation} {
list [catch {exec | cat} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.3 {errors in exec invocation} {
list [catch {exec cat |} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.4 {errors in exec invocation} {
list [catch {exec cat | | cat} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.5 {errors in exec invocation} {
list [catch {exec cat | |& cat} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.6 {errors in exec invocation} {
list [catch {exec cat |&} msg] $msg
} {1 {illegal use of | or |& in command}}
test exec-10.7 {errors in exec invocation} {
list [catch {exec cat <} msg] $msg
} {1 {can't specify "<" as last word in command}}
test exec-10.8 {errors in exec invocation} {
list [catch {exec cat >} msg] $msg
} {1 {can't specify ">" as last word in command}}
test exec-10.9 {errors in exec invocation} {
list [catch {exec cat <<} msg] $msg
} {1 {can't specify "<<" as last word in command}}
test exec-10.10 {errors in exec invocation} {
list [catch {exec cat >>} msg] $msg
} {1 {can't specify ">>" as last word in command}}
test exec-10.11 {errors in exec invocation} {
list [catch {exec cat >&} msg] $msg
} {1 {can't specify ">&" as last word in command}}
test exec-10.12 {errors in exec invocation} {
list [catch {exec cat >>&} msg] $msg
} {1 {can't specify ">>&" as last word in command}}
test exec-10.13 {errors in exec invocation} {
list [catch {exec cat >@} msg] $msg
} {1 {can't specify ">@" as last word in command}}
test exec-10.14 {errors in exec invocation} {
list [catch {exec cat <@} msg] $msg
} {1 {can't specify "<@" as last word in command}}
2024-05-27 16:40:40 +02:00
test exec-10.15 {errors in exec invocation} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [catch {exec cat < a/b/c} msg] [string tolower $msg]
} {1 {couldn't read file "a/b/c": no such file or directory}}
2024-05-27 16:40:40 +02:00
test exec-10.16 {errors in exec invocation} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
} {1 {couldn't write file "a/b/c": no such file or directory}}
2024-05-27 16:40:40 +02:00
test exec-10.17 {errors in exec invocation} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
} {1 {couldn't write file "a/b/c": no such file or directory}}
set f [open gorp.file w]
test exec-10.18 {errors in exec invocation} {
list [catch {exec cat <@ $f} msg] $msg
2024-05-27 16:40:40 +02:00
} "1 {channel \"$f\" wasn't opened for reading}"
2024-05-27 16:13:40 +02:00
close $f
set f [open gorp.file r]
test exec-10.19 {errors in exec invocation} {
list [catch {exec cat >@ $f} msg] $msg
2024-05-27 16:40:40 +02:00
} "1 {channel \"$f\" wasn't opened for writing}"
2024-05-27 16:13:40 +02:00
close $f
2024-05-27 16:40:40 +02:00
test exec-10.20 {errors in exec invocation} {
list [catch {exec ~non_existent_user/foo/bar} msg] $msg
} {1 {user "non_existent_user" doesn't exist}}
test exec-10.21 {errors in exec invocation} {unixExecs} {
list [catch {exec true | ~xyzzy_bad_user/x | false} msg] $msg
} {1 {user "xyzzy_bad_user" doesn't exist}}
2024-05-27 16:13:40 +02:00
# Commands in background.
2024-05-27 16:40:40 +02:00
test exec-11.1 {commands in background} {unixExecs} {
2024-05-27 16:13:40 +02:00
set x [lindex [time {exec sleep 2 &}] 0]
expr $x<1000000
} 1
2024-05-27 16:40:40 +02:00
test exec-11.2 {commands in background} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [catch {exec echo a &b} msg] $msg
} {0 {a &b}}
2024-05-27 16:40:40 +02:00
test exec-11.3 {commands in background} {unixExecs} {
2024-05-27 16:13:40 +02:00
llength [exec sleep 1 &]
} 1
2024-05-27 16:40:40 +02:00
test exec-11.4 {commands in background} {unixExecs} {
2024-05-27 16:13:40 +02:00
llength [exec sleep 1 | sleep 1 | sleep 1 &]
} 3
2024-05-27 16:40:40 +02:00
test exec-11.5 {commands in background} {unixExecs} {
set f [open gorp.file w]
puts $f { catch { exec echo foo & } }
close $f
string compare "foo" [exec [info nameofexecutable] gorp.file]
} 0
2024-05-27 16:13:40 +02:00
# Make sure that background commands are properly reaped when
# they eventually die.
2024-05-27 16:40:40 +02:00
catch {exec sleep 3}
test exec-12.1 {reaping background processes} {unixOnly nonPortable} {
for {set i 0} {$i < 20} {incr i} {
exec echo foo > /dev/null &
}
exec sleep 1
catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
lindex $msg 0
} 0
test exec-12.2 {reaping background processes} {unixExecs nonPortable} {
exec sleep 2 | sleep 2 | sleep 2 &
catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg
set x [lindex $msg 0]
exec sleep 3
catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg
list $x [lindex $msg 0]
} {3 0}
test exec-12.3 {reaping background processes} {unixOnly nonPortable} {
exec sleep 1000 &
exec sleep 1000 &
set x [exec ps | fgrep "sleep" | fgrep -v fgrep]
set pids {}
foreach i [split $x \n] {
lappend pids [lindex $i 0]
}
foreach i $pids {
catch {exec kill -STOP $i}
}
catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg
set x [lindex $msg 0]
foreach i $pids {
catch {exec kill -KILL $i}
}
catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg
list $x [lindex $msg 0]
} {2 0}
2024-05-27 16:13:40 +02:00
# Make sure "errorCode" is set correctly.
2024-05-27 16:40:40 +02:00
test exec-13.1 {setting errorCode variable} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [catch {exec cat < a/b/c} msg] [string tolower $errorCode]
} {1 {posix enoent {no such file or directory}}}
2024-05-27 16:40:40 +02:00
test exec-13.2 {setting errorCode variable} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [catch {exec cat > a/b/c} msg] [string tolower $errorCode]
} {1 {posix enoent {no such file or directory}}}
test exec-13.3 {setting errorCode variable} {
2024-05-27 16:40:40 +02:00
set x [catch {exec _weird_cmd_} msg]
list $x [string tolower $msg] [lindex $errorCode 0] \
[string tolower [lrange $errorCode 2 end]]
} {1 {couldn't execute "_weird_cmd_": no such file or directory} POSIX {{no such file or directory}}}
2024-05-27 16:13:40 +02:00
# Switches before the first argument
2024-05-27 16:40:40 +02:00
test exec-14.1 {-keepnewline switch} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec -keepnewline echo foo
} "foo\n"
test exec-14.2 {-keepnewline switch} {
list [catch {exec -keepnewline} msg] $msg
} {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
test exec-14.3 {unknown switch} {
list [catch {exec -gorp} msg] $msg
} {1 {bad switch "-gorp": must be -keepnewline or --}}
test exec-14.4 {-- switch} {
2024-05-27 16:40:40 +02:00
list [catch {exec -- -gorp} msg] [string tolower $msg]
} {1 {couldn't execute "-gorp": no such file or directory}}
2024-05-27 16:13:40 +02:00
# Redirecting standard error separately from standard output
2024-05-27 16:40:40 +02:00
test exec-15.1 {standard error redirection} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo "First line" > gorp.file
list [exec sh -c "echo foo bar 1>&2" 2> gorp.file] \
[exec cat gorp.file]
} {{} {foo bar}}
2024-05-27 16:40:40 +02:00
test exec-15.2 {standard error redirection} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [exec sh -c "echo foo bar 1>&2" | echo biz baz >gorp.file \
2> gorp.file2] [exec cat gorp.file] \
[exec cat gorp.file2]
} {{} {biz baz} {foo bar}}
2024-05-27 16:40:40 +02:00
test exec-15.3 {standard error redirection} {unixExecs} {
2024-05-27 16:13:40 +02:00
list [exec sh -c "echo foo bar 1>&2" | echo biz baz 2>gorp.file \
> gorp.file2] [exec cat gorp.file] \
[exec cat gorp.file2]
} {{} {foo bar} {biz baz}}
2024-05-27 16:40:40 +02:00
test exec-15.4 {standard error redirection} {unixExecs} {
2024-05-27 16:13:40 +02:00
set f [open gorp.file w]
puts $f "Line 1"
flush $f
exec sh -c "echo foo bar 1>&2" 2>@ $f
puts $f "Line 3"
close $f
exec cat gorp.file
} {Line 1
foo bar
Line 3}
2024-05-27 16:40:40 +02:00
test exec-15.5 {standard error redirection} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec echo "First line" > gorp.file
exec sh -c "echo foo bar 1>&2" 2>> gorp.file
exec cat gorp.file
} {First line
foo bar}
2024-05-27 16:40:40 +02:00
test exec-15.6 {standard error redirection} {unixExecs} {
2024-05-27 16:13:40 +02:00
exec sh -c "echo foo bar 1>&2" > gorp.file2 2> gorp.file \
>& gorp.file 2> gorp.file2 | echo biz baz
list [exec cat gorp.file] [exec cat gorp.file2]
} {{biz baz} {foo bar}}
2024-05-27 16:40:40 +02:00
test exec-16.1 {flush output before exec} {unixExecs} {
set f [open gorp.file w]
puts $f "First line"
exec echo "Second line" >@ $f
puts $f "Third line"
close $f
exec cat gorp.file
} {First line
Second line
Third line}
test exec-16.2 {flush output before exec} {unixExecs} {
set f [open gorp.file w]
puts $f "First line"
exec sh -c "echo Second line 1>&2" >&@ $f > gorp.file2
puts $f "Third line"
close $f
exec cat gorp.file
} {First line
Second line
Third line}
test exec-17.1 { inheriting standard I/O } {unixOrPc unixExecs} {
set f [open script w]
puts $f {close stdout
set f [open gorp.file w]
catch {exec echo foobar &}
exec sleep 2
close $f
}
close $f
catch {eval exec $tcltest script} result
set f [open gorp.file r]
lappend result [read $f]
close $f
set result
} {{foobar
}}
removeFile script
removeFile gorp.file
removeFile gorp.file2
2024-05-27 16:13:40 +02:00
return {}