# 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. # # Copyright 1991 Regents of the University of California # Permission to use, copy, modify, and distribute this # software and its documentation for any purpose and without # fee is hereby granted, provided that this copyright notice # appears in all copies. The University of California makes no # representations about the suitability of this software for any # purpose. It is provided "as is" without express or implied # warranty. # # $Header: /user6/ouster/tcl/tests/RCS/exec.test,v 1.17 92/04/28 10:01:37 ouster Exp $ (Berkeley) if {[string compare test [info procs test]] == 1} then {source defs} # Basic operations. test exec-1.1 {basic exec operation} { exec echo a b c } "a b c" test exec-1.2 {pipelining} { exec echo a b c d | cat | cat } "a b c d" test exec-1.3 {pipelining} { set a [exec echo a b c d | cat | wc] list [scan $a "%d %d %d" b c d] $b $c $d } {3 1 4 8} # I/O redirection: input from Tcl command. test exec-2.1 {redirecting input from immediate source} { exec cat << "Sample text" } {Sample text} test exec-2.2 {redirecting input from immediate source} { exec << "Sample text" cat | cat } {Sample text} test exec-2.3 {redirecting input from immediate source} { exec cat << "Sample text" | cat } {Sample text} test exec-2.4 {redirecting input from immediate source} { exec cat | cat << "Sample text" } {Sample text} # I/O redirection: output to file. catch [exec rm -f gorp.file] test exec-3.1 {redirecting output to file} { exec echo "Some simple words" > gorp.file exec cat gorp.file } "Some simple words" test exec-3.2 {redirecting output to file} { exec echo "More simple words" | > gorp.file cat | cat exec cat gorp.file } "More simple words" test exec-3.3 {redirecting output to file} { exec > gorp.file echo "Different simple words" | cat | cat exec cat gorp.file } "Different simple words" # I/O redirection: input from file. exec echo "Just a few thoughts" > gorp.file test exec-4.1 {redirecting input from file} { exec cat < gorp.file } {Just a few thoughts} test exec-4.2 {redirecting input from file} { exec cat | cat < gorp.file } {Just a few thoughts} test exec-4.3 {redirecting input from file} { exec cat < gorp.file | cat } {Just a few thoughts} test exec-4.4 {redirecting input from file} { exec < gorp.file cat | cat } {Just a few thoughts} # I/O redirection: combinations. catch {exec rm -f gorp.file2} test exec-5.1 {multiple I/O redirections} { exec << "command input" > gorp.file2 cat < gorp.file exec cat gorp.file2 } {Just a few thoughts} test exec-5.2 {multiple I/O redirections} { 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] test exec-6.1 {long input and output} { exec cat << $a } $a # Commands that return errors. test exec-7.1 {commands returning errors} { set x [catch {exec gorp456} msg] list $x $msg [lindex $errorCode 0] [lrange $errorCode 2 end] } {1 {couldn't find "gorp456" to execute} CHILDSTATUS 1} test exec-7.2 {commands returning errors} { set x [catch {exec foo123 | gorp456} msg] set x1 {couldn't find "foo123" to execute couldn't find "gorp456" to execute} set x2 {couldn't find "gorp456" to execute couldn't find "foo123" to execute} set y [expr {($msg == $x1) || ($msg == $x2)}] list $x $y [lindex $errorCode 0] [lrange $errorCode 2 end] } {1 1 CHILDSTATUS 1} test exec-7.3 {commands returning errors} { list [catch {exec sleep 1 | sh -c "exit 43" | sleep 1} msg] $msg } {1 {}} test exec-7.4 {commands returning errors} { list [catch {exec gorp456 | echo a b c} msg] $msg } {1 {a b c couldn't find "gorp456" to execute}} # Errors in executing the Tcl command, as opposed to errors in the # processes that are invoked. test exec-8.1 {errors in exec invocation} { list [catch {exec} msg] $msg } {1 {didn't specify command to execute}} test exec-8.2 {errors in exec invocation} { list [catch {exec | cat} msg] $msg } {1 {illegal use of | in command}} test exec-8.3 {errors in exec invocation} { list [catch {exec cat |} msg] $msg } {1 {illegal use of | in command}} test exec-8.4 {errors in exec invocation} { list [catch {exec cat | | cat} msg] $msg } {1 {illegal use of | in command}} test exec-8.5 {errors in exec invocation} { list [catch {exec cat <} msg] $msg } {1 {can't specify "<" as last word in command}} test exec-8.6 {errors in exec invocation} { list [catch {exec cat >} msg] $msg } {1 {can't specify ">" as last word in command}} test exec-8.7 {errors in exec invocation} { list [catch {exec cat <<} msg] $msg } {1 {can't specify "<<" as last word in command}} test exec-8.8 {errors in exec invocation} { list [catch {exec cat < a/b/c} msg] [string tolower $msg] } {1 {couldn't read file "a/b/c": no such file or directory}} test exec-8.9 {errors in exec invocation} { 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}} # Commands in background. test exec-9.1 {commands in background} { set x [lindex [time {exec sleep 5 &}] 0] expr $x<1000000 } 1 test exec-9.2 {commands in background} { list [catch {exec echo a &b} msg] $msg } {0 {a &b}} # Make sure "errorCode" is set correctly. test exec-10.1 {setting errorCode variable} { list [catch {exec cat < a/b/c} msg] [string tolower $errorCode] } {1 {unix enoent {no such file or directory}}} test exec-10.2 {setting errorCode variable} { list [catch {exec cat > a/b/c} msg] [string tolower $errorCode] } {1 {unix enoent {no such file or directory}}} test exec-10.3 {setting errorCode variable} { set x [catch {exec _weirdo_command_} msg] list $x $msg [lindex $errorCode 0] [lrange $errorCode 2 end] } {1 {couldn't find "_weirdo_command_" to execute} CHILDSTATUS 1} catch {exec rm -f gorp.file} catch {exec rm -f gorp.file2} return {}