| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Test the child process FD state of each shell.
 | 
| 4 | #
 | 
| 5 | # This used to be a spec test, but I found it wasn't consistent when running in
 | 
| 6 | # parallel under test/spec-runner.sh.
 | 
| 7 | #
 | 
| 8 | # Also see demo/fd-main.sh.
 | 
| 9 | #
 | 
| 10 | # Usage:
 | 
| 11 | #   ./fd-state.sh <function name>
 | 
| 12 | 
 | 
| 13 | set -o nounset
 | 
| 14 | set -o pipefail
 | 
| 15 | set -o errexit
 | 
| 16 | 
 | 
| 17 | readonly SCRIPT=_tmp/list-fds.sh
 | 
| 18 | 
 | 
| 19 | #### File Descriptor State is clean when running script
 | 
| 20 | count-fds() {
 | 
| 21 |   local sh=$1
 | 
| 22 | 
 | 
| 23 |   # Run it and count output
 | 
| 24 |   $sh $SCRIPT _tmp/fd.txt
 | 
| 25 |   count=$(cat _tmp/fd.txt | wc -l)
 | 
| 26 |   echo "count=$count"
 | 
| 27 | 
 | 
| 28 |   # bash and dash are very orderly: there are 3 pipes and then 10 or 255
 | 
| 29 |   # has the script.sh.
 | 
| 30 |   # mksh and zsh have /dev/tty saved as well.  Not sure why.
 | 
| 31 | 
 | 
| 32 |   # for debugging failures
 | 
| 33 |   if test "$count" -ne 4; then
 | 
| 34 |     cat _tmp/fd.txt >&2
 | 
| 35 |   fi
 | 
| 36 |   # stdout: count=4
 | 
| 37 |   # OK mksh/zsh stdout: count=5
 | 
| 38 |   # stdout-json: ""
 | 
| 39 | }
 | 
| 40 | 
 | 
| 41 | main() {
 | 
| 42 |   # tail -n + 2: get rid of first line
 | 
| 43 |   cat >$SCRIPT <<'EOF'
 | 
| 44 | out=$1
 | 
| 45 | ls -l /proc/$$/fd | tail -n +2 > $out
 | 
| 46 | EOF
 | 
| 47 | 
 | 
| 48 |   # TODO: Make assertions here for OSH.
 | 
| 49 |   for sh in bash dash mksh zsh bin/osh _bin/osh; do
 | 
| 50 | 
 | 
| 51 |     echo
 | 
| 52 |     echo "=== $sh ==="
 | 
| 53 |     echo
 | 
| 54 | 
 | 
| 55 |     if ! which $sh; then
 | 
| 56 |       continue
 | 
| 57 |     fi
 | 
| 58 |     count-fds $sh
 | 
| 59 |   done
 | 
| 60 | }
 | 
| 61 | 
 | 
| 62 | main "$@"
 |