Debian Codegraph @

tests/main.test

raw
1 # This file contains a collection of tests for generic/tclMain.c.
2
3 if {[catch {package require tcltest 2.0.2}]} {
4 puts stderr "Skipping tests in [info script]. tcltest 2.0.2 required."
5 return
6 }
7
8 namespace eval ::tcl::test::main {
9 namespace import ::tcltest::*
10
11 # Is [exec] defined?
12 testConstraint exec [llength [info commands exec]]
13
14 # Is the Tcltest package loaded?
15 # - that is, the special C-coded testing commands in tclTest.c
16 # - tests use testing commands introduced in Tcltest 8.4
17 testConstraint Tcltest [expr {
18 [llength [package provide Tcltest]]
19 && [package vsatisfies [package provide Tcltest] 8.4]}]
20
21 # Procedure to simulate interactive typing of commands, line by line
22 proc type {chan script} {
23 foreach line [split $script \n] {
24 if {[catch {
25 puts $chan $line
26 flush $chan
27 }]} {
28 return
29 }
30 # Grrr... Behavior depends on this value.
31 after 1000
32 }
33 }
34
35 cd [temporaryDirectory]
36 # Tests Tcl_Main-1.*: variable initializations
37
38 test Tcl_Main-1.1 {
39 Tcl_Main: startup script - normal
40 } -constraints {
41 stdio
42 } -setup {
43 makeFile {puts [list $argv0 $argv $tcl_interactive]} script
44 catch {set f [open "|[list [interpreter] script]" r]}
45 } -body {
46 read $f
47 } -cleanup {
48 close $f
49 removeFile script
50 } -result [list script {} 0]\n
51
52 test Tcl_Main-1.2 {
53 Tcl_Main: startup script - can't begin with '-'
54 } -constraints {
55 stdio
56 } -setup {
57 makeFile {puts [list $argv0 $argv $tcl_interactive]} -script
58 catch {set f [open "|[list [interpreter] -script]" w+]}
59 } -body {
60 puts $f {puts [list $argv0 $argv $tcl_interactive]; exit}
61 flush $f
62 read $f
63 } -cleanup {
64 close $f
65 removeFile -script
66 } -result [list [interpreter] -script 0]\n
67
68 test Tcl_Main-1.3 {
69 } -constraints {
70 stdio
71 } -setup {
72 makeFile {puts [list $argv0 $argv $tcl_interactive]} script
73 catch {set f [open "|[list [interpreter] script \u00c0]" r]}
74 } -body {
75 read $f
76 } -cleanup {
77 close $f
78 removeFile script
79 } -result [list script [list [encoding convertfrom [encoding system] \
80 [encoding convertto [encoding system] \u00c0]]] 0]\n
81
82 test Tcl_Main-1.4 {
83 } -constraints {
84 stdio
85 } -setup {
86 makeFile {puts [list $argv0 $argv $tcl_interactive]} script
87 catch {set f [open "|[list [interpreter] script \u20ac]" r]}
88 } -body {
89 read $f
90 } -cleanup {
91 close $f
92 removeFile script
93 } -result [list script [list [encoding convertfrom [encoding system] \
94 [encoding convertto [encoding system] \u20ac]]] 0]\n
95
96 test Tcl_Main-1.5 {
97 } -constraints {
98 stdio
99 } -setup {
100 makeFile {puts [list $argv0 $argv $tcl_interactive]} \u00c0
101 catch {set f [open "|[list [interpreter] \u00c0]" r]}
102 } -body {
103 read $f
104 } -cleanup {
105 close $f
106 removeFile \u00c0
107 } -result [list [list [encoding convertfrom [encoding system] \
108 [encoding convertto [encoding system] \u00c0]]] {} 0]\n
109
110 test Tcl_Main-1.6 {
111 } -constraints {
112 stdio
113 } -setup {
114 makeFile {puts [list $argv0 $argv $tcl_interactive]} \u20ac
115 catch {set f [open "|[list [interpreter] \u20ac]" r]}
116 } -body {
117 read $f
118 } -cleanup {
119 close $f
120 removeFile \u20ac
121 } -result [list [list [encoding convertfrom [encoding system] \
122 [encoding convertto [encoding system] \u20ac]]] {} 0]\n
123
124 test Tcl_Main-1.7 {
125 Tcl_Main: startup script - -encoding option
126 } -constraints {
127 stdio
128 } -setup {
129 set script [makeFile {} script]
130 file delete $script
131 set f [open $script w]
132 chan configure $f -encoding utf-8
133 puts $f {puts [list $argv0 $argv $tcl_interactive]}
134 puts -nonewline $f {puts [string equal \u20ac }
135 puts $f "\u20ac]"
136 close $f
137 catch {set f [open "|[list [interpreter] -encoding utf-8 script]" r]}
138 } -body {
139 read $f
140 } -cleanup {
141 close $f
142 removeFile script
143 } -result [list script {} 0]\n1\n
144
145 test Tcl_Main-1.8 {
146 Tcl_Main: startup script - -encoding option - mismatched encodings
147 } -constraints {
148 stdio
149 } -setup {
150 set script [makeFile {} script]
151 file delete $script
152 set f [open $script w]
153 chan configure $f -encoding utf-8
154 puts $f {puts [list $argv0 $argv $tcl_interactive]}
155 puts -nonewline $f {puts [string equal \u20ac }
156 puts $f "\u20ac]"
157 close $f
158 catch {set f [open "|[list [interpreter] -encoding iso8859-1 script]" r]}
159 } -body {
160 read $f
161 } -cleanup {
162 close $f
163 removeFile script
164 } -result [list script {} 0]\n0\n
165
166 test Tcl_Main-1.9 {
167 Tcl_Main: startup script - -encoding option - no abbrevation
168 } -constraints {
169 stdio
170 } -setup {
171 set script [makeFile {} script]
172 file delete $script
173 set f [open $script w]
174 chan configure $f -encoding utf-8
175 puts $f {puts [list $argv0 $argv $tcl_interactive]}
176 puts -nonewline $f {puts [string equal \u20ac }
177 puts $f "\u20ac]"
178 close $f
179 catch {set f [open "|[list [interpreter] -enc utf-8 script]" r+]}
180 } -body {
181 type $f {
182 puts $argv
183 }
184 list [catch {gets $f} line] $line
185 } -cleanup {
186 close $f
187 removeFile script
188 } -result {0 {-enc utf-8 script}}
189
190 # Tests Tcl_Main-2.*: application-initialization procedure
191
192 test Tcl_Main-2.1 {
193 Tcl_Main: appInitProc returns error
194 } -constraints {
195 exec Tcltest
196 } -setup {
197 makeFile {puts "In script"} script
198 } -body {
199 exec [interpreter] script -appinitprocerror >& result
200 set f [open result]
201 read $f
202 } -cleanup {
203 close $f
204 file delete result
205 removeFile script
206 } -result "application-specific initialization failed: \nIn script\n"
207
208 test Tcl_Main-2.2 {
209 Tcl_Main: appInitProc returns error
210 } -constraints {
211 exec Tcltest
212 } -body {
213 exec [interpreter] << {puts "In script"} -appinitprocerror >& result
214 set f [open result]
215 read $f
216 } -cleanup {
217 close $f
218 file delete result
219 } -result "application-specific initialization failed: \nIn script\n"
220
221 test Tcl_Main-2.3 {
222 Tcl_Main: appInitProc deletes interp
223 } -constraints {
224 exec Tcltest
225 } -setup {
226 makeFile {puts "In script"} script
227 } -body {
228 exec [interpreter] script -appinitprocdeleteinterp >& result
229 set f [open result]
230 read $f
231 } -cleanup {
232 close $f
233 file delete result
234 removeFile script
235 } -result "application-specific initialization failed: \n"
236
237 test Tcl_Main-2.4 {
238 Tcl_Main: appInitProc deletes interp
239 } -constraints {
240 exec Tcltest
241 } -body {
242 exec [interpreter] << {puts "In script"} \
243 -appinitprocdeleteinterp >& result
244 set f [open result]
245 read $f
246 } -cleanup {
247 close $f
248 file delete result
249 } -result "application-specific initialization failed: \n"
250
251 test Tcl_Main-2.5 {
252 Tcl_Main: appInitProc closes stderr
253 } -constraints {
254 exec Tcltest
255 } -body {
256 exec [interpreter] << {puts "In script"} \
257 -appinitprocclosestderr >& result
258 set f [open result]
259 read $f
260 } -cleanup {
261 close $f
262 file delete result
263 } -result "In script\n"
264
265 # Tests Tcl_Main-3.*: startup script evaluation
266
267 test Tcl_Main-3.1 {
268 Tcl_Main: startup script does not exist
269 } -constraints {
270 exec
271 } -setup {
272 if {[file exists no-such-file]} {
273 error "Can't run test Tcl_Main-3.1\
274 where a file named \"no-such-file\" exists"
275 }
276 } -body {
277 set code [catch {exec [interpreter] no-such-file >& result} result]
278 set f [open result]
279 list $code $result [read $f]
280 } -cleanup {
281 close $f
282 file delete result
283 } -match glob -result [list 1 {child process exited abnormally} \
284 {couldn't read file "no-such-file":*}]
285
286 test Tcl_Main-3.2 {
287 Tcl_Main: startup script raises error
288 } -constraints {
289 exec
290 } -setup {
291 makeFile {error ERROR} script
292 } -body {
293 set code [catch {exec [interpreter] script >& result} result]
294 set f [open result]
295 list $code $result [read $f]
296 } -cleanup {
297 close $f
298 file delete result
299 removeFile script
300 } -match glob -result [list 1 {child process exited abnormally} \
301 "ERROR\n while executing*"]
302
303 test Tcl_Main-3.3 {
304 Tcl_Main: startup script closes stderr
305 } -constraints {
306 exec
307 } -setup {
308 makeFile {close stderr; error ERROR} script
309 } -body {
310 set code [catch {exec [interpreter] script >& result} result]
311 set f [open result]
312 list $code $result [read $f]
313 } -cleanup {
314 close $f
315 file delete result
316 removeFile script
317 } -result [list 1 {child process exited abnormally} {}]
318
319 test Tcl_Main-3.4 {
320 Tcl_Main: startup script holds incomplete script
321 } -constraints {
322 exec
323 } -setup {
324 makeFile "if 1 \{" script
325 } -body {
326 set code [catch {exec [interpreter] script >& result} result]
327 set f [open result]
328 join [list $code $result [read $f]] \n
329 } -cleanup {
330 close $f
331 file delete result
332 removeFile script
333 } -match glob -result [join [list 1 {child process exited abnormally}\
334 "missing close-brace\n while executing*"] \n]
335
336 test Tcl_Main-3.5 {
337 Tcl_Main: startup script sets main loop
338 } -constraints {
339 exec Tcltest
340 } -setup {
341 makeFile {
342 rename exit _exit
343 proc exit {code} {
344 puts "In exit"
345 _exit $code
346 }
347 after 0 {
348 puts event
349 testexitmainloop
350 }
351 testexithandler create 0
352 testsetmainloop
353 } script
354 } -body {
355 exec [interpreter] script >& result
356 set f [open result]
357 read $f
358 } -cleanup {
359 close $f
360 file delete result
361 removeFile script
362 } -result "event\nExit MainLoop\nIn exit\neven 0\n"
363
364 test Tcl_Main-3.6 {
365 Tcl_Main: startup script sets main loop and closes stdin
366 } -constraints {
367 exec Tcltest
368 } -setup {
369 makeFile {
370 close stdin
371 testsetmainloop
372 rename exit _exit
373 proc exit {code} {
374 puts "In exit"
375 _exit $code
376 }
377 after 0 {
378 puts event
379 testexitmainloop
380 }
381 testexithandler create 0
382 } script
383 } -body {
384 exec [interpreter] script >& result
385 set f [open result]
386 read $f
387 } -cleanup {
388 close $f
389 file delete result
390 removeFile script
391 } -result "event\nExit MainLoop\nIn exit\neven 0\n"
392
393 test Tcl_Main-3.7 {
394 Tcl_Main: startup script deletes interp
395 } -constraints {
396 exec Tcltest
397 } -setup {
398 makeFile {
399 rename exit _exit
400 proc exit {code} {
401 puts "In exit"
402 _exit $code
403 }
404 testexithandler create 0
405 testinterpdelete {}
406 } script
407 } -body {
408 exec [interpreter] script >& result
409 set f [open result]
410 read $f
411 } -cleanup {
412 close $f
413 file delete result
414 removeFile script
415 } -result "even 0\n"
416
417 test Tcl_Main-3.8 {
418 Tcl_Main: startup script deletes interp and sets mainloop
419 } -constraints {
420 exec Tcltest
421 } -setup {
422 makeFile {
423 testsetmainloop
424 rename exit _exit
425 proc exit {code} {
426 puts "In exit"
427 _exit $code
428 }
429 testexitmainloop
430 testexithandler create 0
431 testinterpdelete {}
432 } script
433 } -body {
434 exec [interpreter] script >& result
435 set f [open result]
436 read $f
437 } -cleanup {
438 close $f
439 file delete result
440 removeFile script
441 } -result "Exit MainLoop\neven 0\n"
442
443 test Tcl_Main-3.9 {
444 Tcl_Main: startup script can set tcl_interactive without limit
445 } -constraints {
446 exec
447 } -setup {
448 makeFile {set tcl_interactive foo} script
449 } -body {
450 exec [interpreter] script >& result
451 set f [open result]
452 read $f
453 } -cleanup {
454 close $f
455 file delete result
456 removeFile script
457 } -result {}
458
459 # Tests Tcl_Main-4.*: rc file evaluation
460
461 test Tcl_Main-4.1 {
462 Tcl_Main: rcFile evaluation deletes interp
463 } -constraints {
464 exec Tcltest
465 } -setup {
466 set rc [makeFile {testinterpdelete {}} rc]
467 } -body {
468 exec [interpreter] << {puts "In script"} \
469 -appinitprocsetrcfile $rc >& result
470 set f [open result]
471 read $f
472 } -cleanup {
473 close $f
474 file delete result
475 removeFile rc
476 } -result "application-specific initialization failed: \n"
477
478 test Tcl_Main-4.2 {
479 Tcl_Main: rcFile evaluation closes stdin
480 } -constraints {
481 exec Tcltest
482 } -setup {
483 set rc [makeFile {close stdin} rc]
484 } -body {
485 exec [interpreter] << {puts "In script"} \
486 -appinitprocsetrcfile $rc >& result
487 set f [open result]
488 read $f
489 } -cleanup {
490 close $f
491 file delete result
492 removeFile rc
493 } -result "application-specific initialization failed: \n"
494
495 test Tcl_Main-4.3 {
496 Tcl_Main: rcFile evaluation closes stdin and sets main loop
497 } -constraints {
498 exec Tcltest
499 } -setup {
500 set rc [makeFile {
501 close stdin
502 testsetmainloop
503 after 0 testexitmainloop
504 testexithandler create 0
505 rename exit _exit
506 proc exit code {
507 puts "In exit"
508 _exit $code
509 }
510 } rc]
511 } -body {
512 exec [interpreter] << {puts "In script"} \
513 -appinitprocsetrcfile $rc >& result
514 set f [open result]
515 read $f
516 } -cleanup {
517 close $f
518 file delete result
519 removeFile rc
520 } -result "application-specific initialization failed:\
521 \nExit MainLoop\nIn exit\neven 0\n"
522
523 test Tcl_Main-4.4 {
524 Tcl_Main: rcFile evaluation sets main loop
525 } -constraints {
526 exec Tcltest
527 } -setup {
528 set rc [makeFile {
529 testsetmainloop
530 after 0 testexitmainloop
531 testexithandler create 0
532 rename exit _exit
533 proc exit code {
534 puts "In exit"
535 _exit $code
536 }
537 } rc]
538 } -body {
539 exec [interpreter] << {} \
540 -appinitprocsetrcfile $rc >& result
541 set f [open result]
542 read $f
543 } -cleanup {
544 close $f
545 file delete result
546 removeFile rc
547 } -result "application-specific initialization failed:\
548 \nExit MainLoop\nIn exit\neven 0\n"
549
550 test Tcl_Main-4.5 {
551 Tcl_Main: Bug 1481986
552 } -constraints {
553 exec Tcltest
554 } -setup {
555 set rc [makeFile {
556 testsetmainloop
557 after 0 {puts "Event callback"}
558 } rc]
559 } -body {
560 set f [open "|[list [interpreter] -appinitprocsetrcfile $rc]" w+]
561 after 1000
562 type $f {puts {Interactive output}
563 exit
564 }
565 read $f
566 } -cleanup {
567 catch {close $f}
568 removeFile rc
569 } -result "Event callback\nInteractive output\n"
570
571 # Tests Tcl_Main-5.*: interactive operations
572
573 test Tcl_Main-5.1 {
574 Tcl_Main: tcl_interactive must be boolean
575 } -constraints {
576 exec
577 } -body {
578 exec [interpreter] << {set tcl_interactive foo} >& result
579 set f [open result]
580 read $f
581 } -cleanup {
582 close $f
583 file delete result
584 } -result "can't set \"tcl_interactive\":\
585 variable must have boolean value\n"
586
587 test Tcl_Main-5.2 {
588 Tcl_Main able to handle non-blocking stdin
589 } -constraints {
590 exec
591 } -setup {
592 catch {set f [open "|[list [interpreter]]" w+]}
593 } -body {
594 type $f {
595 chan configure stdin -blocking 0
596 puts SUCCESS
597 }
598 list [catch {gets $f} line] $line
599 } -cleanup {
600 close $f
601 } -result [list 0 SUCCESS]
602
603 test Tcl_Main-5.3 {
604 Tcl_Main handles stdin EOF in mid-command
605 } -constraints {
606 exec
607 } -setup {
608 catch {set f [open "|[list [interpreter]]" w+]}
609 catch {chan configure $f -blocking 0}
610 } -body {
611 type $f "chan configure stdin -eofchar \\032
612 if 1 \{\n\032"
613 variable wait
614 chan event $f readable \
615 [list set [namespace which -variable wait] "child exit"]
616 set id [after 5000 [list set [namespace which -variable wait] timeout]]
617 vwait [namespace which -variable wait]
618 after cancel $id
619 set wait
620 } -cleanup {
621 if {$wait eq "timeout" && [testConstraint unix]} {
622 exec kill [pid $f]
623 }
624 close $f
625 } -result {child exit}
626
627 test Tcl_Main-5.4 {
628 Tcl_Main handles stdin EOF in mid-command
629 } -constraints {
630 exec
631 } -setup {
632 set cmd {makeFile "if 1 \{" script}
633 catch {set f [open "|[list [interpreter]] < [list [eval $cmd]]" r]}
634 catch {chan configure $f -blocking 0}
635 } -body {
636 variable wait
637 chan event $f readable \
638 [list set [namespace which -variable wait] "child exit"]
639 set id [after 5000 [list set [namespace which -variable wait] timeout]]
640 vwait [namespace which -variable wait]
641 after cancel $id
642 set wait
643 } -cleanup {
644 if {$wait eq "timeout" && [testConstraint unix]} {
645 exec kill [pid $f]
646 }
647 close $f
648 removeFile script
649 } -result {child exit}
650
651 test Tcl_Main-5.5 {
652 Tcl_Main: error raised in interactive mode
653 } -constraints {
654 exec
655 } -body {
656 exec [interpreter] << {error foo} >& result
657 set f [open result]
658 read $f
659 } -cleanup {
660 close $f
661 file delete result
662 } -result "foo\n"
663
664 test Tcl_Main-5.6 {
665 Tcl_Main: interactive mode: errors don't stop command loop
666 } -constraints {
667 exec
668 } -body {
669 exec [interpreter] << {
670 error foo
671 puts bar
672 } >& result
673 set f [open result]
674 read $f
675 } -cleanup {
676 close $f
677 file delete result
678 } -result "foo\nbar\n"
679
680 test Tcl_Main-5.7 {
681 Tcl_Main: interactive mode: closed stderr
682 } -constraints {
683 exec
684 } -body {
685 exec [interpreter] << {
686 close stderr
687 error foo
688 puts bar
689 } >& result
690 set f [open result]
691 read $f
692 } -cleanup {
693 close $f
694 file delete result
695 } -result "bar\n"
696
697 test Tcl_Main-5.8 {
698 Tcl_Main: interactive mode: close stdin
699 -> main loop & [exit] & exit handlers
700 } -constraints {
701 exec Tcltest
702 } -body {
703 exec [interpreter] << {
704 rename exit _exit
705 proc exit code {
706 puts "In exit"
707 _exit $code
708 }
709 testsetmainloop
710 testexitmainloop
711 testexithandler create 0
712 close stdin
713 } >& result
714 set f [open result]
715 read $f
716 } -cleanup {
717 close $f
718 file delete result
719 } -result "Exit MainLoop\nIn exit\neven 0\n"
720
721 test Tcl_Main-5.9 {
722 Tcl_Main: interactive mode: delete interp
723 -> main loop & exit handlers, but no [exit]
724 } -constraints {
725 exec Tcltest
726 } -body {
727 exec [interpreter] << {
728 rename exit _exit
729 proc exit code {
730 puts "In exit"
731 _exit $code
732 }
733 testsetmainloop
734 testexitmainloop
735 testexithandler create 0
736 testinterpdelete {}
737 } >& result
738 set f [open result]
739 read $f
740 } -cleanup {
741 close $f
742 file delete result
743 } -result "Exit MainLoop\neven 0\n"
744
745 test Tcl_Main-5.10 {
746 Tcl_Main: exit main loop in mid-interactive command
747 } -constraints {
748 exec Tcltest
749 } -setup {
750 catch {set f [open "|[list [interpreter]]" w+]}
751 catch {chan configure $f -blocking 0}
752 } -body {
753 type $f "testsetmainloop
754 after 2000 testexitmainloop
755 puts \{1 2"
756 after 4000
757 type $f "3 4\}"
758 set code1 [catch {gets $f} line1]
759 set code2 [catch {gets $f} line2]
760 set code3 [catch {gets $f} line3]
761 list $code1 $line1 $code2 $line2 $code3 $line3
762 } -cleanup {
763 close $f
764 } -result [list 0 {Exit MainLoop} 0 {1 2} 0 {3 4}]
765
766 test Tcl_Main-5.11 {
767 Tcl_Main: EOF in interactive main loop
768 } -constraints {
769 exec Tcltest
770 } -body {
771 exec [interpreter] << {
772 rename exit _exit
773 proc exit code {
774 puts "In exit"
775 _exit $code
776 }
777 testexithandler create 0
778 after 0 testexitmainloop
779 testsetmainloop
780 } >& result
781 set f [open result]
782 read $f
783 } -cleanup {
784 close $f
785 file delete result
786 } -result "Exit MainLoop\nIn exit\neven 0\n"
787
788 test Tcl_Main-5.12 {
789 Tcl_Main: close stdin in interactive main loop
790 } -constraints {
791 exec Tcltest
792 } -body {
793 exec [interpreter] << {
794 rename exit _exit
795 proc exit code {
796 puts "In exit"
797 _exit $code
798 }
799 testexithandler create 0
800 after 100 testexitmainloop
801 testsetmainloop
802 close stdin
803 puts "don't reach this"
804 } >& result
805 set f [open result]
806 read $f
807 } -cleanup {
808 close $f
809 file delete result
810 } -result "Exit MainLoop\nIn exit\neven 0\n"
811
812 test Tcl_Main-5.13 {
813 Bug 1775878
814 } -constraints {
815 exec
816 } -setup {
817 catch {set f [open "|[list [interpreter]]" w+]}
818 } -body {
819 type $f "puts \\"
820 type $f return
821 list [catch {gets $f} line] $line
822 } -cleanup {
823 close $f
824 } -result [list 0 return]
825
826 # Tests Tcl_Main-6.*: interactive operations with prompts
827
828 test Tcl_Main-6.1 {
829 Tcl_Main: enable prompts with tcl_interactive
830 } -constraints {
831 exec
832 } -body {
833 exec [interpreter] << {set tcl_interactive 1} >& result
834 set f [open result]
835 read $f
836 } -cleanup {
837 close $f
838 file delete result
839 } -result "1\n% "
840
841 test Tcl_Main-6.2 {
842 Tcl_Main: prompt deletes interp
843 } -constraints {
844 exec Tcltest
845 } -body {
846 exec [interpreter] << {
847 set tcl_prompt1 {testinterpdelete {}}
848 set tcl_interactive 1
849 puts "not reached"
850 } >& result
851 set f [open result]
852 read $f
853 } -cleanup {
854 close $f
855 file delete result
856 } -result "1\n"
857
858 test Tcl_Main-6.3 {
859 Tcl_Main: prompt closes stdin
860 } -constraints {
861 exec
862 } -body {
863 exec [interpreter] << {
864 set tcl_prompt1 {close stdin}
865 set tcl_interactive 1
866 puts "not reached"
867 } >& result
868 set f [open result]
869 read $f
870 } -cleanup {
871 close $f
872 file delete result
873 } -result "1\n"
874
875 test Tcl_Main-6.4 {
876 Tcl_Main: interactive output, closed stdout
877 } -constraints {
878 exec
879 } -body {
880 exec [interpreter] << {
881 set tcl_interactive 1
882 close stdout
883 set a NO
884 puts stderr YES
885 } >& result
886 set f [open result]
887 read $f
888 } -cleanup {
889 close $f
890 file delete result
891 } -result "1\n% YES\n"
892
893 test Tcl_Main-6.5 {
894 Tcl_Main: interactive entry to main loop
895 } -constraints {
896 exec Tcltest
897 } -body {
898 exec [interpreter] << {
899 set tcl_interactive 1
900 testsetmainloop
901 testexitmainloop} >& result
902 set f [open result]
903 read $f
904 } -cleanup {
905 close $f
906 file delete result
907 } -result "1\n% % % Exit MainLoop\n"
908
909 test Tcl_Main-6.6 {
910 Tcl_Main: number of prompts during stdin close exit
911 } -constraints {
912 exec
913 } -body {
914 exec [interpreter] << {
915 set tcl_interactive 1
916 close stdin} >& result
917 set f [open result]
918 read $f
919 } -cleanup {
920 close $f
921 file delete result
922 } -result "1\n% "
923
924 test Tcl_Main-6.7 {
925 [unknown]: interactive auto-completion.
926 } -constraints {
927 exec
928 } -body {
929 exec [interpreter] << {
930 proc foo\{ x {}
931 set ::auto_noexec xxx
932 set tcl_interactive 1
933 foo y} >& result
934 set f [open result]
935 read $f
936 } -cleanup {
937 close $f
938 file delete result
939 } -result "1\n% % "
940
941 # Tests Tcl_Main-7.*: exiting
942
943 test Tcl_Main-7.1 {
944 Tcl_Main: [exit] defined as no-op -> still have exithandlers
945 } -constraints {
946 exec Tcltest
947 } -body {
948 exec [interpreter] << {
949 proc exit args {}
950 testexithandler create 0
951 } >& result
952 set f [open result]
953 read $f
954 } -cleanup {
955 close $f
956 file delete result
957 } -result "even 0\n"
958
959 test Tcl_Main-7.2 {
960 Tcl_Main: [exit] defined as no-op -> still have exithandlers
961 } -constraints {
962 exec Tcltest
963 } -body {
964 exec [interpreter] << {
965 proc exit args {}
966 testexithandler create 0
967 after 0 testexitmainloop
968 testsetmainloop
969 } >& result
970 set f [open result]
971 read $f
972 } -cleanup {
973 close $f
974 file delete result
975 } -result "Exit MainLoop\neven 0\n"
976
977 # Tests Tcl_Main-8.*: StdinProc operations
978
979 test Tcl_Main-8.1 {
980 StdinProc: handles non-blocking stdin
981 } -constraints {
982 exec Tcltest
983 } -body {
984 exec [interpreter] << {
985 testsetmainloop
986 chan configure stdin -blocking 0
987 testexitmainloop
988 } >& result
989 set f [open result]
990 read $f
991 } -cleanup {
992 close $f
993 file delete result
994 } -result "Exit MainLoop\n"
995
996 test Tcl_Main-8.2 {
997 StdinProc: handles stdin EOF
998 } -constraints {
999 exec Tcltest
1000 } -body {
1001 exec [interpreter] << {
1002 testsetmainloop
1003 testexithandler create 0
1004 rename exit _exit
1005 proc exit code {
1006 puts "In exit"
1007 _exit $code
1008 }
1009 after 100 testexitmainloop
1010 } >& result
1011 set f [open result]
1012 read $f
1013 } -cleanup {
1014 close $f
1015 file delete result
1016 } -result "Exit MainLoop\nIn exit\neven 0\n"
1017
1018 test Tcl_Main-8.3 {
1019 StdinProc: handles interactive stdin EOF
1020 } -constraints {
1021 exec Tcltest
1022 } -body {
1023 exec [interpreter] << {
1024 testsetmainloop
1025 testexithandler create 0
1026 rename exit _exit
1027 proc exit code {
1028 puts "In exit"
1029 _exit $code
1030 }
1031 set tcl_interactive 1} >& result
1032 set f [open result]
1033 read $f
1034 } -cleanup {
1035 close $f
1036 file delete result
1037 } -result "1\n% even 0\n"
1038
1039 test Tcl_Main-8.4 {
1040 StdinProc: handles stdin close
1041 } -constraints {
1042 exec Tcltest
1043 } -body {
1044 exec [interpreter] << {
1045 testsetmainloop
1046 rename exit _exit
1047 proc exit code {
1048 puts "In exit"
1049 _exit $code
1050 }
1051 after 100 testexitmainloop
1052 after 0 puts 1
1053 close stdin
1054 } >& result
1055 set f [open result]
1056 read $f
1057 } -cleanup {
1058 close $f
1059 file delete result
1060 } -result "1\nExit MainLoop\nIn exit\n"
1061
1062 test Tcl_Main-8.5 {
1063 StdinProc: handles interactive stdin close
1064 } -constraints {
1065 exec Tcltest
1066 } -body {
1067 exec [interpreter] << {
1068 testsetmainloop
1069 set tcl_interactive 1
1070 rename exit _exit
1071 proc exit code {
1072 puts "In exit"
1073 _exit $code
1074 }
1075 after 100 testexitmainloop
1076 after 0 puts 1
1077 close stdin
1078 } >& result
1079 set f [open result]
1080 read $f
1081 } -cleanup {
1082 close $f
1083 file delete result
1084 } -result "1\n% % % after#0\n% after#1\n% 1\nExit MainLoop\nIn exit\n"
1085
1086 test Tcl_Main-8.6 {
1087 StdinProc: handles event loop re-entry
1088 } -constraints {
1089 exec Tcltest
1090 } -body {
1091 exec [interpreter] << {
1092 testsetmainloop
1093 after 100 {puts 1; set delay 1}
1094 vwait delay
1095 puts 2
1096 testexitmainloop
1097 } >& result
1098 set f [open result]
1099 read $f
1100 } -cleanup {
1101 close $f
1102 file delete result
1103 } -result "1\n2\nExit MainLoop\n"
1104
1105 test Tcl_Main-8.7 {
1106 StdinProc: handling of errors
1107 } -constraints {
1108 exec Tcltest
1109 } -body {
1110 exec [interpreter] << {
1111 testsetmainloop
1112 error foo
1113 testexitmainloop
1114 } >& result
1115 set f [open result]
1116 read $f
1117 } -cleanup {
1118 close $f
1119 file delete result
1120 } -result "foo\nExit MainLoop\n"
1121
1122 test Tcl_Main-8.8 {
1123 StdinProc: handling of errors, closed stderr
1124 } -constraints {
1125 exec Tcltest
1126 } -body {
1127 exec [interpreter] << {
1128 testsetmainloop
1129 close stderr
1130 error foo
1131 testexitmainloop
1132 } >& result
1133 set f [open result]
1134 read $f
1135 } -cleanup {
1136 close $f
1137 file delete result
1138 } -result "Exit MainLoop\n"
1139
1140 test Tcl_Main-8.9 {
1141 StdinProc: interactive output
1142 } -constraints {
1143 exec Tcltest
1144 } -body {
1145 exec [interpreter] << {
1146 testsetmainloop
1147 set tcl_interactive 1
1148 testexitmainloop} >& result
1149 set f [open result]
1150 read $f
1151 } -cleanup {
1152 close $f
1153 file delete result
1154 } -result "1\n% % Exit MainLoop\n"
1155
1156 test Tcl_Main-8.10 {
1157 StdinProc: interactive output, closed stdout
1158 } -constraints {
1159 exec Tcltest
1160 } -body {
1161 exec [interpreter] << {
1162 testsetmainloop
1163 close stdout
1164 set tcl_interactive 1
1165 testexitmainloop
1166 } >& result
1167 set f [open result]
1168 read $f
1169 } -cleanup {
1170 close $f
1171 file delete result
1172 } -result {}
1173
1174 test Tcl_Main-8.11 {
1175 StdinProc: prompt deletes interp
1176 } -constraints {
1177 exec Tcltest
1178 } -body {
1179 exec [interpreter] << {
1180 testsetmainloop
1181 set tcl_prompt1 {testinterpdelete {}}
1182 set tcl_interactive 1} >& result
1183 set f [open result]
1184 read $f
1185 } -cleanup {
1186 close $f
1187 file delete result
1188 } -result "1\n"
1189
1190 test Tcl_Main-8.12 {
1191 StdinProc: prompt closes stdin
1192 } -constraints {
1193 exec Tcltest
1194 } -body {
1195 exec [interpreter] << {
1196 testsetmainloop
1197 set tcl_prompt1 {close stdin}
1198 after 100 testexitmainloop
1199 set tcl_interactive 1
1200 puts "not reached"
1201 } >& result
1202 set f [open result]
1203 read $f
1204 } -cleanup {
1205 close $f
1206 file delete result
1207 } -result "1\nExit MainLoop\n"
1208
1209 test Tcl_Main-8.13 {
1210 Bug 1775878
1211 } -constraints {
1212 exec Tcltest
1213 } -body {
1214 exec [interpreter] << "testsetmainloop\nputs \\\npwd\ntestexitmainloop" >& result
1215 set f [open result]
1216 read $f
1217 } -cleanup {
1218 close $f
1219 file delete result
1220 } -result "pwd\nExit MainLoop\n"
1221
1222 # Tests Tcl_Main-9.*: Prompt operations
1223
1224 test Tcl_Main-9.1 {
1225 Prompt: custom prompt variables
1226 } -constraints {
1227 exec
1228 } -body {
1229 exec [interpreter] << {
1230 set tcl_prompt1 {puts -nonewline stdout "one "}
1231 set tcl_prompt2 {puts -nonewline stdout "two "}
1232 set tcl_interactive 1
1233 puts {This is
1234 a test}} >& result
1235 set f [open result]
1236 read $f
1237 } -cleanup {
1238 close $f
1239 file delete result
1240 } -result "1\none two This is\n\t\ta test\none "
1241
1242 test Tcl_Main-9.2 {
1243 Prompt: error in custom prompt variables
1244 } -constraints {
1245 exec
1246 } -body {
1247 exec [interpreter] << {
1248 set tcl_prompt1 {error foo}
1249 set tcl_interactive 1
1250 set errorInfo} >& result
1251 set f [open result]
1252 read $f
1253 } -cleanup {
1254 close $f
1255 file delete result
1256 } -result "1\nfoo\n% foo\n while executing\n\"error foo\"\n (script\
1257 that generates prompt)\nfoo\n% "
1258
1259 test Tcl_Main-9.3 {
1260 Prompt: error in custom prompt variables, closed stderr
1261 } -constraints {
1262 exec
1263 } -body {
1264 exec [interpreter] << {
1265 set tcl_prompt1 {close stderr; error foo}
1266 set tcl_interactive 1} >& result
1267 set f [open result]
1268 read $f
1269 } -cleanup {
1270 close $f
1271 file delete result
1272 } -result "1\n% "
1273
1274 test Tcl_Main-9.4 {
1275 Prompt: error in custom prompt variables, closed stdout
1276 } -constraints {
1277 exec
1278 } -body {
1279 exec [interpreter] << {
1280 set tcl_prompt1 {close stdout; error foo}
1281 set tcl_interactive 1} >& result
1282 set f [open result]
1283 read $f
1284 } -cleanup {
1285 close $f
1286 file delete result
1287 } -result "1\nfoo\n"
1288
1289 cd [workingDirectory]
1290
1291 cleanupTests
1292 }
1293
1294 namespace delete ::tcl::test::main
1295 return