OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tcl/] [tests/] [exec.test] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# Commands covered:  exec
2
#
3
# This file contains a collection of tests for one or more of the Tcl
4
# built-in commands.  Sourcing this file into Tcl runs the tests and
5
# generates output 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
#
10
# See the file "license.terms" for information on usage and redistribution
11
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
#
13
# RCS: @(#) $Id: exec.test,v 1.1.1.1 2002-01-16 10:25:35 markom Exp $
14
 
15
if {[string compare test [info procs test]] == 1} then {source defs}
16
 
17
# If exec is not defined just return with no error
18
# Some platforms like the Macintosh do not have the exec command
19
if {[info commands exec] == ""} {
20
    puts "exec not implemented for this machine"
21
    return
22
}
23
if {$testConfig(stdio) == 0} {
24
    return
25
}
26
 
27
set f [open echo w]
28
puts $f {
29
    puts -nonewline [lindex $argv 0]
30
    foreach str [lrange $argv 1 end] {
31
        puts -nonewline " $str"
32
    }
33
    puts {}
34
}
35
close $f
36
 
37
set f [open cat w]
38
puts $f {
39
    if {$argv == {}} {
40
        set argv -
41
    }
42
    foreach name $argv {
43
        if {$name == "-"} {
44
            set f stdin
45
        } elseif {[catch {open $name r} f] != 0} {
46
            puts stderr $f
47
            continue
48
        }
49
        while {[eof $f] == 0} {
50
            puts -nonewline [read $f]
51
        }
52
        if {$f != "stdin"} {
53
            close $f
54
        }
55
    }
56
}
57
close $f
58
 
59
set f [open wc w]
60
puts $f {
61
    set data [read stdin]
62
    set lines [regsub -all "\n" $data {} dummy]
63
    set words [regsub -all "\[^ \t\n]+" $data {} dummy]
64
    set chars [string length $data]
65
    puts [format "%8.d%8.d%8.d" $lines $words $chars]
66
}
67
close $f
68
 
69
set f [open sh w]
70
puts $f {
71
    if {[lindex $argv 0] != "-c"} {
72
        error "sh: unexpected arguments $argv"
73
    }
74
    set cmd [lindex $argv 1]
75
    lappend cmd ";"
76
 
77
    set newcmd {}
78
 
79
    foreach arg $cmd {
80
        if {$arg == ";"} {
81
            eval exec >@stdout 2>@stderr [list [info nameofexecutable]] $newcmd
82
            set newcmd {}
83
            continue
84
        }
85
        if {$arg == "1>&2"} {
86
            set arg >@stderr
87
        }
88
        lappend newcmd $arg
89
    }
90
}
91
close $f
92
 
93
set f [open sleep w]
94
puts $f {
95
    after [expr $argv*1000]
96
}
97
close $f
98
 
99
set f [open exit w]
100
puts $f {
101
    exit $argv
102
}
103
close $f
104
 
105
# Basic operations.
106
 
107
test exec-1.1 {basic exec operation} {
108
    exec $tcltest echo a b c
109
} "a b c"
110
test exec-1.2 {pipelining} {
111
    exec $tcltest echo a b c d | $tcltest cat | $tcltest cat
112
} "a b c d"
113
test exec-1.3 {pipelining} {
114
    set a [exec $tcltest echo a b c d | $tcltest cat | $tcltest wc]
115
    list [scan $a "%d %d %d" b c d] $b $c
116
} {3 1 4}
117
set arg {12345678901234567890123456789012345678901234567890}
118
set arg "$arg$arg$arg$arg$arg$arg"
119
test exec-1.4 {long command lines} {
120
    exec $tcltest echo $arg
121
} $arg
122
set arg {}
123
 
124
# I/O redirection: input from Tcl command.
125
 
126
test exec-2.1 {redirecting input from immediate source} {
127
    exec $tcltest cat << "Sample text"
128
} {Sample text}
129
test exec-2.2 {redirecting input from immediate source} {
130
    exec << "Sample text" $tcltest cat | $tcltest cat
131
} {Sample text}
132
test exec-2.3 {redirecting input from immediate source} {
133
    exec $tcltest cat << "Sample text" | $tcltest cat
134
} {Sample text}
135
test exec-2.4 {redirecting input from immediate source} {
136
    exec $tcltest cat | $tcltest cat << "Sample text"
137
} {Sample text}
138
test exec-2.5 {redirecting input from immediate source} {
139
    exec $tcltest cat "<
140
} {Joined to arrows}
141
 
142
# I/O redirection: output to file.
143
 
144
file delete gorp.file
145
test exec-3.1 {redirecting output to file} {
146
    exec $tcltest echo "Some simple words" > gorp.file
147
    exec $tcltest cat gorp.file
148
} "Some simple words"
149
test exec-3.2 {redirecting output to file} {
150
    exec $tcltest echo "More simple words" | >gorp.file $tcltest cat | $tcltest cat
151
    exec $tcltest cat gorp.file
152
} "More simple words"
153
test exec-3.3 {redirecting output to file} {
154
    exec > gorp.file $tcltest echo "Different simple words" | $tcltest cat | $tcltest cat
155
    exec $tcltest cat gorp.file
156
} "Different simple words"
157
test exec-3.4 {redirecting output to file} {
158
    exec $tcltest echo "Some simple words" >gorp.file
159
    exec $tcltest cat gorp.file
160
} "Some simple words"
161
test exec-3.5 {redirecting output to file} {
162
    exec $tcltest echo "First line" >gorp.file
163
    exec $tcltest echo "Second line" >> gorp.file
164
    exec $tcltest cat gorp.file
165
} "First line\nSecond line"
166
test exec-3.6 {redirecting output to file} {
167
    exec $tcltest echo "First line" >gorp.file
168
    exec $tcltest echo "Second line" >>gorp.file
169
    exec $tcltest cat gorp.file
170
} "First line\nSecond line"
171
test exec-3.7 {redirecting output to file} {
172
    set f [open gorp.file w]
173
    puts $f "Line 1"
174
    flush $f
175
    exec $tcltest echo "More text" >@ $f
176
    exec $tcltest echo >@$f "Even more"
177
    puts $f "Line 3"
178
    close $f
179
    exec $tcltest cat gorp.file
180
} "Line 1\nMore text\nEven more\nLine 3"
181
 
182
# I/O redirection: output and stderr to file.
183
 
184
file delete gorp.file
185
test exec-4.1 {redirecting output and stderr to file} {
186
    exec $tcltest echo "test output" >& gorp.file
187
    exec $tcltest cat gorp.file
188
} "test output"
189
test exec-4.2 {redirecting output and stderr to file} {
190
    list [exec $tcltest sh -c "echo foo bar 1>&2" >&gorp.file] \
191
            [exec $tcltest cat gorp.file]
192
} {{} {foo bar}}
193
test exec-4.3 {redirecting output and stderr to file} {
194
    exec $tcltest echo "first line" > gorp.file
195
    list [exec $tcltest sh -c "echo foo bar 1>&2" >>&gorp.file] \
196
            [exec $tcltest cat gorp.file]
197
} "{} {first line\nfoo bar}"
198
test exec-4.4 {redirecting output and stderr to file} {
199
    set f [open gorp.file w]
200
    puts $f "Line 1"
201
    flush $f
202
    exec $tcltest echo "More text" >&@ $f
203
    exec $tcltest echo >&@$f "Even more"
204
    puts $f "Line 3"
205
    close $f
206
    exec $tcltest cat gorp.file
207
} "Line 1\nMore text\nEven more\nLine 3"
208
test exec-4.5 {redirecting output and stderr to file} {
209
    set f [open gorp.file w]
210
    puts $f "Line 1"
211
    flush $f
212
    exec >&@ $f $tcltest sh -c "echo foo bar 1>&2"
213
    exec >&@$f $tcltest sh -c "echo xyzzy 1>&2"
214
    puts $f "Line 3"
215
    close $f
216
    exec $tcltest cat gorp.file
217
} "Line 1\nfoo bar\nxyzzy\nLine 3"
218
 
219
# I/O redirection: input from file.
220
 
221
exec $tcltest echo "Just a few thoughts" > gorp.file
222
test exec-5.1 {redirecting input from file} {
223
    exec $tcltest cat < gorp.file
224
} {Just a few thoughts}
225
test exec-5.2 {redirecting input from file} {
226
    exec $tcltest cat | $tcltest cat < gorp.file
227
} {Just a few thoughts}
228
test exec-5.3 {redirecting input from file} {
229
    exec $tcltest cat < gorp.file | $tcltest cat
230
} {Just a few thoughts}
231
test exec-5.4 {redirecting input from file} {
232
    exec < gorp.file $tcltest cat | $tcltest cat
233
} {Just a few thoughts}
234
test exec-5.5 {redirecting input from file} {
235
    exec $tcltest cat 
236
} {Just a few thoughts}
237
test exec-5.6 {redirecting input from file} {
238
    set f [open gorp.file r]
239
    set result [exec $tcltest cat <@ $f]
240
    close $f
241
    set result
242
} {Just a few thoughts}
243
test exec-5.7 {redirecting input from file} {
244
    set f [open gorp.file r]
245
    set result [exec <@$f $tcltest cat]
246
    close $f
247
    set result
248
} {Just a few thoughts}
249
 
250
# I/O redirection: standard error through a pipeline.
251
 
252
test exec-6.1 {redirecting stderr through a pipeline} {
253
    exec $tcltest sh -c "echo foo bar" |& $tcltest cat
254
} "foo bar"
255
test exec-6.2 {redirecting stderr through a pipeline} {
256
    exec $tcltest sh -c "echo foo bar 1>&2" |& $tcltest cat
257
} "foo bar"
258
test exec-6.3 {redirecting stderr through a pipeline} {
259
    exec $tcltest sh -c "echo foo bar 1>&2" \
260
        |& $tcltest sh -c "echo second msg 1>&2 ; cat" |& $tcltest cat
261
} "second msg\nfoo bar"
262
 
263
# I/O redirection: combinations.
264
 
265
catch {exec rm -f gorp.file2}
266
test exec-7.1 {multiple I/O redirections} {
267
    exec << "command input" > gorp.file2 $tcltest cat < gorp.file
268
    exec $tcltest cat gorp.file2
269
} {Just a few thoughts}
270
test exec-7.2 {multiple I/O redirections} {
271
    exec < gorp.file << "command input" $tcltest cat
272
} {command input}
273
 
274
# Long input to command and output from command.
275
 
276
set a "0123456789 xxxxxxxxx abcdefghi ABCDEFGHIJK\n"
277
set a [concat $a $a $a $a]
278
set a [concat $a $a $a $a]
279
set a [concat $a $a $a $a]
280
set a [concat $a $a $a $a]
281
test exec-8.1 {long input and output} {
282
    exec $tcltest cat << $a
283
} $a
284
 
285
# Commands that return errors.
286
 
287
test exec-9.1 {commands returning errors} {
288
    set x [catch {exec gorp456} msg]
289
    list $x [string tolower $msg] [string tolower $errorCode]
290
} {1 {couldn't execute "gorp456": no such file or directory} {posix enoent {no such file or directory}}}
291
test exec-9.2 {commands returning errors} {
292
    string tolower [list [catch {exec $tcltest echo foo | foo123} msg] $msg $errorCode]
293
} {1 {couldn't execute "foo123": no such file or directory} {posix enoent {no such file or directory}}}
294
test exec-9.3 {commands returning errors} {
295
    list [catch {exec $tcltest sleep 1 | $tcltest exit 43 | $tcltest sleep 1} msg] $msg
296
} {1 {child process exited abnormally}}
297
test exec-9.4 {commands returning errors} {
298
    list [catch {exec $tcltest exit 43 | $tcltest echo "foo bar"} msg] $msg
299
} {1 {foo bar
300
child process exited abnormally}}
301
test exec-9.5 {commands returning errors} {
302
    list [catch {exec gorp456 | $tcltest echo a b c} msg] [string tolower $msg]
303
} {1 {couldn't execute "gorp456": no such file or directory}}
304
test exec-9.6 {commands returning errors} {
305
    list [catch {exec $tcltest sh -c "echo error msg 1>&2"} msg] $msg
306
} {1 {error msg}}
307
test exec-9.7 {commands returning errors} {
308
    list [catch {exec $tcltest sh -c "echo error msg 1>&2" \
309
                     | $tcltest sh -c "echo error msg 1>&2"} msg] $msg
310
} {1 {error msg
311
error msg}}
312
 
313
# Errors in executing the Tcl command, as opposed to errors in the
314
# processes that are invoked.
315
 
316
test exec-10.1 {errors in exec invocation} {
317
    list [catch {exec} msg] $msg
318
} {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
319
test exec-10.2 {errors in exec invocation} {
320
    list [catch {exec | cat} msg] $msg
321
} {1 {illegal use of | or |& in command}}
322
test exec-10.3 {errors in exec invocation} {
323
    list [catch {exec cat |} msg] $msg
324
} {1 {illegal use of | or |& in command}}
325
test exec-10.4 {errors in exec invocation} {
326
    list [catch {exec cat | | cat} msg] $msg
327
} {1 {illegal use of | or |& in command}}
328
test exec-10.5 {errors in exec invocation} {
329
    list [catch {exec cat | |& cat} msg] $msg
330
} {1 {illegal use of | or |& in command}}
331
test exec-10.6 {errors in exec invocation} {
332
    list [catch {exec cat |&} msg] $msg
333
} {1 {illegal use of | or |& in command}}
334
test exec-10.7 {errors in exec invocation} {
335
    list [catch {exec cat <} msg] $msg
336
} {1 {can't specify "<" as last word in command}}
337
test exec-10.8 {errors in exec invocation} {
338
    list [catch {exec cat >} msg] $msg
339
} {1 {can't specify ">" as last word in command}}
340
test exec-10.9 {errors in exec invocation} {
341
    list [catch {exec cat <<} msg] $msg
342
} {1 {can't specify "<<" as last word in command}}
343
test exec-10.10 {errors in exec invocation} {
344
    list [catch {exec cat >>} msg] $msg
345
} {1 {can't specify ">>" as last word in command}}
346
test exec-10.11 {errors in exec invocation} {
347
    list [catch {exec cat >&} msg] $msg
348
} {1 {can't specify ">&" as last word in command}}
349
test exec-10.12 {errors in exec invocation} {
350
    list [catch {exec cat >>&} msg] $msg
351
} {1 {can't specify ">>&" as last word in command}}
352
test exec-10.13 {errors in exec invocation} {
353
    list [catch {exec cat >@} msg] $msg
354
} {1 {can't specify ">@" as last word in command}}
355
test exec-10.14 {errors in exec invocation} {
356
    list [catch {exec cat <@} msg] $msg
357
} {1 {can't specify "<@" as last word in command}}
358
test exec-10.15 {errors in exec invocation} {
359
    list [catch {exec cat < a/b/c} msg] [string tolower $msg]
360
} {1 {couldn't read file "a/b/c": no such file or directory}}
361
test exec-10.16 {errors in exec invocation} {
362
    list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
363
} {1 {couldn't write file "a/b/c": no such file or directory}}
364
test exec-10.17 {errors in exec invocation} {
365
    list [catch {exec cat << foo > a/b/c} msg] [string tolower $msg]
366
} {1 {couldn't write file "a/b/c": no such file or directory}}
367
set f [open gorp.file w]
368
test exec-10.18 {errors in exec invocation} {
369
    list [catch {exec cat <@ $f} msg] $msg
370
} "1 {channel \"$f\" wasn't opened for reading}"
371
close $f
372
set f [open gorp.file r]
373
test exec-10.19 {errors in exec invocation} {
374
    list [catch {exec cat >@ $f} msg] $msg
375
} "1 {channel \"$f\" wasn't opened for writing}"
376
close $f
377
test exec-10.20 {errors in exec invocation} {
378
    list [catch {exec ~non_existent_user/foo/bar} msg] $msg
379
} {1 {user "non_existent_user" doesn't exist}}
380
test exec-10.21 {errors in exec invocation} {
381
    list [catch {exec $tcltest true | ~xyzzy_bad_user/x | false} msg] $msg
382
} {1 {user "xyzzy_bad_user" doesn't exist}}
383
 
384
# Commands in background.
385
 
386
test exec-11.1 {commands in background} {
387
    set x [lindex [time {exec $tcltest sleep 2 &}] 0]
388
    expr $x<1000000
389
} 1
390
test exec-11.2 {commands in background} {
391
    list [catch {exec $tcltest echo a &b} msg] $msg
392
} {0 {a &b}}
393
test exec-11.3 {commands in background} {
394
    llength [exec $tcltest sleep 1 &]
395
} 1
396
test exec-11.4 {commands in background} {
397
    llength [exec $tcltest sleep 1 | $tcltest sleep 1 | $tcltest sleep 1 &]
398
} 3
399
test exec-11.5 {commands in background} {
400
    set f [open gorp.file w]
401
    puts $f { catch { exec [info nameofexecutable] echo foo & } }
402
    close $f
403
    string compare "foo" [exec $tcltest gorp.file]
404
} 0
405
 
406
# Make sure that background commands are properly reaped when
407
# they eventually die.
408
 
409
exec $tcltest sleep 3
410
test exec-12.1 {reaping background processes} {unixOnly nonPortable} {
411
    for {set i 0} {$i < 20} {incr i} {
412
        exec echo foo > /dev/null &
413
    }
414
    exec sleep 1
415
    catch {exec ps | fgrep "echo foo" | fgrep -v fgrep | wc} msg
416
    lindex $msg 0
417
} 0
418
test exec-12.2 {reaping background processes} {unixOnly nonPortable} {
419
    exec sleep 2 | sleep 2 | sleep 2 &
420
    catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg
421
    set x [lindex $msg 0]
422
    exec sleep 3
423
    catch {exec ps | fgrep -i "sleep" | fgrep -i -v fgrep | wc} msg
424
    list $x [lindex $msg 0]
425
} {3 0}
426
test exec-12.3 {reaping background processes} {unixOnly nonPortable} {
427
    exec sleep 1000 &
428
    exec sleep 1000 &
429
    set x [exec ps | fgrep "sleep" | fgrep -v fgrep]
430
    set pids {}
431
    foreach i [split $x \n] {
432
        lappend pids [lindex $i 0]
433
    }
434
    foreach i $pids {
435
        catch {exec kill -STOP $i}
436
    }
437
    catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg
438
    set x [lindex $msg 0]
439
 
440
    foreach i $pids {
441
        catch {exec kill -KILL $i}
442
    }
443
    catch {exec ps | fgrep "sleep" | fgrep -v fgrep | wc} msg
444
    list $x [lindex $msg 0]
445
} {2 0}
446
 
447
# Make sure "errorCode" is set correctly.
448
 
449
test exec-13.1 {setting errorCode variable} {
450
    list [catch {exec $tcltest cat < a/b/c} msg] [string tolower $errorCode]
451
} {1 {posix enoent {no such file or directory}}}
452
test exec-13.2 {setting errorCode variable} {
453
    list [catch {exec $tcltest cat > a/b/c} msg] [string tolower $errorCode]
454
} {1 {posix enoent {no such file or directory}}}
455
test exec-13.3 {setting errorCode variable} {
456
    set x [catch {exec _weird_cmd_} msg]
457
    list $x [string tolower $msg] [lindex $errorCode 0] \
458
            [string tolower [lrange $errorCode 2 end]]
459
} {1 {couldn't execute "_weird_cmd_": no such file or directory} POSIX {{no such file or directory}}}
460
 
461
# Switches before the first argument
462
 
463
test exec-14.1 {-keepnewline switch} {
464
    exec -keepnewline $tcltest echo foo
465
} "foo\n"
466
test exec-14.2 {-keepnewline switch} {
467
    list [catch {exec -keepnewline} msg] $msg
468
} {1 {wrong # args: should be "exec ?switches? arg ?arg ...?"}}
469
test exec-14.3 {unknown switch} {
470
    list [catch {exec -gorp} msg] $msg
471
} {1 {bad switch "-gorp": must be -keepnewline or --}}
472
test exec-14.4 {-- switch} {
473
    list [catch {exec -- -gorp} msg] [string tolower $msg]
474
} {1 {couldn't execute "-gorp": no such file or directory}}
475
 
476
# Redirecting standard error separately from standard output
477
 
478
test exec-15.1 {standard error redirection} {
479
    exec $tcltest echo "First line" > gorp.file
480
    list [exec $tcltest sh -c "echo foo bar 1>&2" 2> gorp.file] \
481
            [exec $tcltest cat gorp.file]
482
} {{} {foo bar}}
483
test exec-15.2 {standard error redirection} {
484
    list [exec $tcltest sh -c "echo foo bar 1>&2" \
485
                | $tcltest echo biz baz >gorp.file 2> gorp.file2] \
486
            [exec $tcltest cat gorp.file] \
487
            [exec $tcltest cat gorp.file2]
488
} {{} {biz baz} {foo bar}}
489
test exec-15.3 {standard error redirection} {
490
    list [exec $tcltest sh -c "echo foo bar 1>&2" \
491
                | $tcltest echo biz baz 2>gorp.file > gorp.file2] \
492
            [exec $tcltest cat gorp.file] \
493
            [exec $tcltest cat gorp.file2]
494
} {{} {foo bar} {biz baz}}
495
test exec-15.4 {standard error redirection} {
496
    set f [open gorp.file w]
497
    puts $f "Line 1"
498
    flush $f
499
    exec $tcltest sh -c "echo foo bar 1>&2" 2>@ $f
500
    puts $f "Line 3"
501
    close $f
502
    exec $tcltest cat gorp.file
503
} {Line 1
504
foo bar
505
Line 3}
506
test exec-15.5 {standard error redirection} {
507
    exec $tcltest echo "First line" > gorp.file
508
    exec $tcltest sh -c "echo foo bar 1>&2" 2>> gorp.file
509
    exec $tcltest cat gorp.file
510
} {First line
511
foo bar}
512
test exec-15.6 {standard error redirection} {
513
    exec $tcltest sh -c "echo foo bar 1>&2" > gorp.file2 2> gorp.file \
514
            >& gorp.file 2> gorp.file2 | $tcltest echo biz baz
515
    list [exec $tcltest cat gorp.file] [exec $tcltest cat gorp.file2]
516
} {{biz baz} {foo bar}}
517
 
518
test exec-16.1 {flush output before exec} {
519
    set f [open gorp.file w]
520
    puts $f "First line"
521
    exec $tcltest echo "Second line" >@ $f
522
    puts $f "Third line"
523
    close $f
524
    exec $tcltest cat gorp.file
525
} {First line
526
Second line
527
Third line}
528
test exec-16.2 {flush output before exec} {} {
529
    set f [open gorp.file w]
530
    puts $f "First line"
531
    exec $tcltest << {puts stderr {Second line}} >&@ $f > gorp.file2
532
    puts $f "Third line"
533
    close $f
534
    exec $tcltest cat gorp.file
535
} {First line
536
Second line
537
Third line}
538
 
539
test exec-17.1 { inheriting standard I/O } {
540
    set f [open script w]
541
    puts $f {close stdout
542
        set f [open gorp.file w]
543
        catch {exec [info nameofexecutable] echo foobar &}
544
        exec [info nameofexecutable] sleep 2
545
        close $f
546
    }
547
    close $f
548
    catch {exec $tcltest script} result
549
    set f [open gorp.file r]
550
    lappend result [read $f]
551
    close $f
552
    set result
553
} {{foobar
554
}}
555
 
556
file delete script gorp.file gorp.file2
557
file delete echo cat wc sh sleep exit

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.