Bash · 1618 bytes Raw Blame History
1 #!/bin/sh
2 TEST_PREFIX="[set-shopt]"
3 . "$(cd "$(dirname "$0")/.." && pwd)/test_harness.sh"
4
5 section "1. set options"
6 compare_exit "set -e exits on error" 'set -e; false'
7 compare_output "set -e does not exit on conditional" 'set -e; if false; then echo no; fi; echo ok'
8 compare_exit "set -u errors on unset var" 'set -u; echo ${NONEXISTENT_XYZ_999} 2>/dev/null'
9 compare_exit "set -u triggers failure for unset" 'set -u; echo $NONEXISTENT_XYZ_999 2>/dev/null'
10 compare_exit "set -o pipefail catches pipe failure" 'set -o pipefail; false | true'
11 compare_output "set -o pipefail success" 'set -o pipefail; true | true; echo $?'
12
13 section "2. set positional parameters"
14 compare_output "set -- sets positional params" 'set -- a b c; echo $1 $2 $3'
15 compare_output "set -- count" 'set -- x y z; echo $#'
16 compare_output "set -- overwrites previous" 'set -- a b; set -- x y z; echo $1 $#'
17 compare_output "set -- empty clears params" 'set -- a b c; set --; echo $#'
18 compare_output "set -- with special chars" 'set -- "hello world" foo; echo "$1"'
19 compare_output "dollar-at expansion" 'set -- a b c; for x in "$@"; do echo $x; done'
20 compare_output "dollar-star expansion" 'set -- a b c; echo "$*"'
21
22 section "3. dollar-dash options string"
23 compare_output "dollar-dash shows options" 'set -u; case $- in *u*) echo yes;; *) echo no;; esac'
24
25 section "4. shopt"
26 check_exit "shopt -s extglob" 'shopt -s extglob 2>/dev/null; echo done' "0"
27 compare_exit "shopt -q queries option" 'shopt -q login_shell 2>/dev/null; true'
28 compare_exit "shopt -u unsets option" 'shopt -s extglob 2>/dev/null; shopt -u extglob 2>/dev/null; true'
29
30 print_summary