Permalink
Please sign in to comment.
Browse files
Spec tests and a skeleton for the trap builtin.
Also added demo/trap.sh, and moved some other scripts in gold/ to demo/. (These tests are difficult to automate.) - Print signal names with -l - Parse arguments and handle errors Still need to hook up the code to signals and hooks. Addresses issue #60.
- Loading branch information...
Showing
with
305 additions
and 16 deletions.
- +86 −8 core/builtin.py
- +24 −3 core/cmd_exec.py
- +4 −0 core/state.py
- +1 −1 {gold → demo}/fd-lib.sh
- +4 −0 demo/fd-main.sh
- 0 {gold → demo}/open-fds.sh
- +59 −0 demo/trap.sh
- 0 {gold → demo}/xtrace1.sh
- +0 −4 gold/fd-main.sh
- +122 −0 spec/builtin-trap.test.sh
- +5 −0 test/spec.sh
| @@ -1,5 +1,5 @@ | ||
| #!/bin/bash | ||
| . gold/open-fds.sh | ||
| . demo/open-fds.sh | ||
| count_func | ||
| @@ -0,0 +1,4 @@ | ||
| #!/bin/bash | ||
| . demo/fd-lib.sh | ||
File renamed without changes.
| @@ -0,0 +1,59 @@ | ||
| #!/bin/bash | ||
| # | ||
| # Demo of traps. | ||
| sigterm() { | ||
| echo "sigterm [$@] $?" | ||
| # quit the process -- otherwise we resume! | ||
| exit | ||
| } | ||
| child() { | ||
| trap 'sigterm x y' SIGTERM | ||
| echo child | ||
| for i in $(seq 5); do | ||
| sleep 1 | ||
| done | ||
| } | ||
| readonly SH=bash | ||
| #readonly SH=dash # bad trap | ||
| #readonly SH=mksh | ||
| #readonly SH=zsh | ||
| child2() { | ||
| $SH -c ' | ||
| sigterm() { | ||
| echo "sigterm [$@] $?" | ||
| # quit the process -- otherwise we resume! | ||
| exit | ||
| } | ||
| trap "sigterm x y" SIGTERM | ||
| trap -p | ||
| echo child | ||
| for i in $(seq 5); do | ||
| sleep 1 | ||
| done | ||
| ' & | ||
| } | ||
| start-and-kill() { | ||
| $0 child & | ||
| #child2 | ||
| echo "started $!" | ||
| sleep 0.1 # a little race to allow things to be printed | ||
| # NOTE: The process only dies after one second. The "sleep 1" is run until | ||
| # completion, and then the signal handler is run, which calls "exit". | ||
| echo "killing $!" | ||
| kill -s SIGTERM $! | ||
| wait $! | ||
| echo status=$? | ||
| } | ||
| "$@" |
File renamed without changes.
| @@ -0,0 +1,122 @@ | ||
| #!/bin/bash | ||
| ### trap -l | ||
| trap -l | grep SIGINT >/dev/null | ||
| ## status: 0 | ||
| ## N-I dash/mksh status: 1 | ||
| ### trap -p | ||
| trap -p | grep trap >/dev/null | ||
| ## status: 0 | ||
| ## N-I dash/mksh status: 1 | ||
| ### Register invalid trap | ||
| trap 'foo' SIGINVALID | ||
| ## status: 1 | ||
| ### Invalid trap invocation | ||
| trap 'foo' | ||
| echo status=$? | ||
| ## stdout: status=1 | ||
| ## OK bash stdout: status=2 | ||
| ## BUG mksh stdout: status=0 | ||
| ### exit 1 when trap code string is invalid | ||
| # All shells spew warnings to stderr, but don't actually exit! Bad! | ||
| trap 'echo <' EXIT | ||
| echo status=$? | ||
| ## stdout: status=1 | ||
| ## BUG mksh status: 1 | ||
| ## BUG mksh stdout: status=0 | ||
| ## BUG dash/bash status: 0 | ||
| ## BUG dash/bash stdout: status=0 | ||
| ### trap EXIT | ||
| cleanup() { | ||
| echo "cleanup [$@]" | ||
| } | ||
| trap 'cleanup x y z' EXIT | ||
| ## stdout: cleanup [x y z] | ||
| ### trap DEBUG | ||
| debuglog() { | ||
| echo "debuglog [$@]" | ||
| } | ||
| trap 'debuglog x y' DEBUG | ||
| echo 1 | ||
| echo 2 | ||
| ## STDOUT: | ||
| debuglog [x y] | ||
| 1 | ||
| debuglog [x y] | ||
| 2 | ||
| ## END | ||
| ## N-I dash/mksh STDOUT: | ||
| 1 | ||
| 2 | ||
| ## END | ||
| ### trap RETURN | ||
| profile() { | ||
| echo "profile [$@]" | ||
| } | ||
| g() { | ||
| echo -- | ||
| echo g | ||
| echo -- | ||
| return | ||
| } | ||
| f() { | ||
| echo -- | ||
| echo f | ||
| echo -- | ||
| g | ||
| } | ||
| # RETURN trap doesn't fire when a function returns, only when a script returns? | ||
| # That's not what the manual syas. | ||
| trap 'profile x y' RETURN | ||
| f | ||
| . spec/testdata/return-helper.sh | ||
| ## status: 42 | ||
| ## STDOUT: | ||
| -- | ||
| f | ||
| -- | ||
| -- | ||
| g | ||
| -- | ||
| return-helper.sh | ||
| profile [x y] | ||
| ## END | ||
| ## N-I dash/mksh STDOUT: | ||
| -- | ||
| f | ||
| -- | ||
| -- | ||
| g | ||
| -- | ||
| return-helper.sh | ||
| ## END | ||
| ### trap ERR and disable it | ||
| err() { | ||
| echo "err [$@] $?" | ||
| } | ||
| trap 'err x y' ERR | ||
| echo 1 | ||
| false | ||
| echo 2 | ||
| trap - ERR # disable trap | ||
| false | ||
| echo 3 | ||
| ## STDOUT: | ||
| 1 | ||
| err [x y] 1 | ||
| 2 | ||
| 3 | ||
| ## END | ||
| # N-I dash STDOUT: | ||
| 1 | ||
| 2 | ||
| 3 | ||
| ## END |
0 comments on commit
2cc3933