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