Skip to content

Commit

Permalink
[spec/builtin-trap] Add failing OSH test for trap in subshell
Browse files Browse the repository at this point in the history
We're not disabling the traps like other shells.

Also add tests for the DEBUG trap, which is issue oils-for-unix#619.
  • Loading branch information
Andy C committed Mar 28, 2023
1 parent 14a48fc commit 2d063c0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
97 changes: 86 additions & 11 deletions spec/builtin-trap.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,78 @@ IN TRAP
FOO
## END

#### trap with command sub / subshell / pipeline
trap 'echo EXIT TRAP' EXIT

echo $(echo command sub)

( echo subshell )

echo pipeline | cat

## STDOUT:
command sub
subshell
pipeline
EXIT TRAP
## END

#### trap DEBUG
case $SH in (dash|mksh) exit ;; esac

debuglog() {
echo "debuglog [$@]"
echo " [$@]"
}
trap 'debuglog x y' DEBUG
echo 1
echo 2
trap 'debuglog $LINENO' DEBUG

echo a
echo b; echo c

echo d && echo e
echo f || echo g

(( h = 42 ))
[[ j == j ]]

## STDOUT:
debuglog [x y]
1
debuglog [x y]
2
[8]
a
[9]
b
[9]
c
[11]
d
[11]
e
[12]
f
[14]
[15]
## END
## N-I dash/mksh STDOUT:
## END

#### trap DEBUG and command sub / subshell
case $SH in (dash|mksh) exit ;; esac

debuglog() {
echo " [$@]"
}
trap 'debuglog $LINENO' DEBUG

echo "result =" $(echo command sub)
( echo subshell )
echo done

## STDOUT:
[8]
result = command sub
subshell
[10]
done
## END
## N-I dash/mksh STDOUT:
1
2
## END

#### trap DEBUG and pipeline
Expand All @@ -151,7 +207,7 @@ trap 'debuglog $LINENO' DEBUG
# gets run for each one of these
{ echo a; echo b; }

# only run for the last one
# only run for the last one, maybe I guess because traps aren't inherited?
{ echo x; echo y; } | wc -l

# gets run for both of these
Expand Down Expand Up @@ -408,3 +464,22 @@ before=0
USR1 trap status=0
after=0
## END

#### traps are cleared in subshell (started with &)
trap 'echo USR1' USR1

kill -USR1 $$

# Hm trap doesn't happen here
{ echo begin child; sleep 0.1; echo end child; } &
kill -USR1 $!
wait

echo done

## STDOUT:
USR1
begin child
end child
done
## END
2 changes: 1 addition & 1 deletion test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ builtin-bracket() {
}

builtin-trap() {
sh-spec spec/builtin-trap.test.sh --osh-failures-allowed 5 \
sh-spec spec/builtin-trap.test.sh --osh-failures-allowed 7 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}

Expand Down

0 comments on commit 2d063c0

Please sign in to comment.