Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/bin/sh Add a test case for "set -X".



details:   https://anonhg.NetBSD.org/src/rev/01c4fdf395cf
branches:  trunk
changeset: 827889:01c4fdf395cf
user:      kre <kre%NetBSD.org@localhost>
date:      Thu Nov 16 19:41:41 2017 +0000

description:
Add a test case for "set -X".

Currently (or when testing any shell that does not support -X) the
test will be skipped (also for [m]ksh (but not ksh93 etc) where there
is an absurdly badly named -X option, skip the new test for them as well.)

When -X appears in /bin/sh, this will verify that it is probably working
(the test is MUCH more gruelling than any rational use of -X would be.)

diffstat:

 tests/bin/sh/t_option.sh |  330 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 327 insertions(+), 3 deletions(-)

diffs (truncated from 365 to 300 lines):

diff -r 3ac529cd3a48 -r 01c4fdf395cf tests/bin/sh/t_option.sh
--- a/tests/bin/sh/t_option.sh  Thu Nov 16 19:41:02 2017 +0000
+++ b/tests/bin/sh/t_option.sh  Thu Nov 16 19:41:41 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_option.sh,v 1.5 2017/07/15 18:52:21 kre Exp $
+# $NetBSD: t_option.sh,v 1.6 2017/11/16 19:41:41 kre Exp $
 #
 # Copyright (c) 2016 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -77,7 +77,7 @@
                        # if we do not do this, -x tracing splatters stderr
                        # for some shells, -v does as well (is that correct?)
                        case "${opt}" in
-                       (*[xv]*)        exec 2>/dev/null;;
+                       (*[xXv]*)       exec 2>/dev/null;;
                        esac
 
                        o="$-"
@@ -498,6 +498,329 @@
           'set -x; for i in 111 222 333; do printf "%s" $i; done; echo; exit 0'
 }
 
+atf_test_case set_X
+set_X_head() {
+       atf_set "descr" "Tests that 'set -X' turns on command exec logging " \
+                       "and that it enables set -x and retains a single fd"
+}
+set_X_body() {
+
+       # First we need to verify that $TEST_SH supports -X
+       test_optional_on_off X                                  ||
+               atf_skip "$TEST_SH does not support -X"
+
+       # and that the -X it implements is the -X we expect
+       $TEST_SH -c 'exec 2>/dev/null;
+               set +x; set -X;
+               case "$-" in (*x*) exit 0;; esac;
+               exit 1'                                         ||
+                       atf_skip "$TEST_SH supports -X but not 'the' -X"
+
+       # Above has already tested that set -X => set -x
+       # Now test that set +X => set +x
+       # and that set -x and set +x do not affect -X
+
+       atf_check -s exit:0 -o empty -e ignore ${TEST_SH} -c \
+               'set -x; set +X; case "$-" in (*x*) echo FAIL; exit 1;; esac'
+
+       atf_check -s exit:0 -o empty -e ignore ${TEST_SH} -c \
+               'set -X; set +x;
+                case "$-" in (*x*) echo FAIL; exit 1;; esac
+                case "$-" in (*X*) exit 0;; esac; echo ERROR; exit 2'
+
+       atf_check -s exit:0 -o empty -e ignore ${TEST_SH} -c \
+               'set -X; set +x; set -x;
+                case "$-" in (*x*X*|*X*x*) exit 0;; esac; echo ERROR; exit 2'
+
+       atf_check -s exit:0 -o empty -e ignore ${TEST_SH} -c \
+               'set +X; set -x;
+                case "$-" in (*X*) echo FAIL; exit 1;; esac
+                case "$-" in (*x*) exit 0;; esac; echo ERROR; exit 2'
+
+       atf_check -s exit:0 -o empty -e ignore ${TEST_SH} -c \
+               'set +X; set -x; set +x;
+                case "$-" in (*[xX]*) echo FAULT; exit 3;; esac'
+
+       # The following just verify regular tracing using -X instead of -x
+       # These are the same tests as the -x test (set_x) performs.
+
+       # check that cmd output appears after -X is enabled
+       atf_check -s exit:0 \
+                       -o match:OKOK -o not-match:echo -o not-match:printf \
+                       -e not-match:printf -e match:OK -e match:echo \
+               ${TEST_SH} -ec 'printf "%s" OK; set -X; echo OK; exit 0'
+
+       # and that it stops again afer -X is disabled
+       atf_check -s exit:0 \
+                       -o match:OKOK -o not-match:echo -o not-match:printf \
+                       -e match:printf -e match:OK -e not-match:echo \
+           ${TEST_SH} -ec 'set -X; printf "%s" OK; set +X; echo OK; exit 0'
+
+       # also check that PS4 is output correctly
+       atf_check -s exit:0 \
+                       -o match:OK -o not-match:echo \
+                       -e match:OK -e match:Run:echo \
+               ${TEST_SH} -ec 'PS4=Run:; set -X; echo OK; exit 0'
+
+       # end copies of -x tests ...
+
+       # now check that we can move stderr around without affecting -X output
+
+       atf_check -s exit:0 \
+                       -o match:OKOK -o not-match:echo -o not-match:printf \
+                       -e match:printf -e match:OK -e match:echo \
+               ${TEST_SH} -ecX 'printf "%s" OK; exec 2>/dev/null; echo OK'
+       atf_check -s exit:0 \
+                       -o match:OKOK -o not-match:echo -o not-match:printf \
+                       -e match:printf -e match:OK -e match:echo \
+               ${TEST_SH} -ecX 'printf "%s" OK; exec 2>&1; echo OK'
+       atf_check -s exit:0 \
+                       -o match:OKOK -o not-match:echo -o not-match:printf \
+                       -e match:printf -e match:OK -e match:echo \
+               ${TEST_SH} -ecX 'printf "%s" OK; exec 2>&-; echo OK'
+
+       # and that we can put tracing on an external file, leaving stderr alone
+
+       atf_require_prog grep
+
+       rm -f X-trace
+       atf_check -s exit:0 \
+                       -o match:OKOK -o not-match:echo -o not-match:printf \
+                       -e empty \
+               ${TEST_SH} -ec 'PS4=; set -X 2>X-trace; printf "%s" OK; echo OK'
+       test -s X-trace || atf_fail "T1: Failed to create trace output file"
+       grep >/dev/null 2>&1 'printf.*%s.*OK' X-trace ||
+               atf_fail "T1: -X tracing missing printf"
+       grep >/dev/null 2>&1 'echo.*OK' X-trace ||
+               atf_fail "T1: -X tracing missing echo"
+
+       rm -f X-trace
+       atf_check -s exit:0 \
+                       -o match:OKOK -o not-match:echo -o not-match:printf \
+                       -e empty \
+               ${TEST_SH} -ec \
+                       'PS4=; set -X 2>X-trace;
+                       printf "%s" OK;
+                       exec 2>/dev/null;
+                       echo OK'
+       test -s X-trace || atf_fail "T2: Failed to create trace output file"
+       grep >/dev/null 2>&1 'printf.*%s.*OK' X-trace ||
+               atf_fail "T2: -X tracing missing printf"
+       grep >/dev/null 2>&1 'exec' X-trace ||
+               atf_fail "T2: -X tracing missing exec"
+       grep >/dev/null 2>&1 'echo.*OK' X-trace ||
+               atf_fail "T2: -X tracing missing echo after stderr redirect"
+
+       rm -f X-trace
+       atf_check -s exit:0 \
+                       -o match:OKOK -o not-match:echo -o not-match:printf \
+                       -e empty \
+               ${TEST_SH} -ec \
+                       'PS4=; set -X 2>X-trace;
+                       printf "%s" OK;
+                       set -X 2>/dev/null;
+                       echo OK'
+       test -s X-trace || atf_fail "T3: Failed to create trace output file"
+       grep >/dev/null 2>&1 'printf.*%s.*OK' X-trace ||
+               atf_fail "T3: -X tracing missing printf"
+       grep >/dev/null 2>&1 'set.*-X' X-trace ||
+               atf_fail "T3: -X tracing missing set -X"
+       grep >/dev/null 2>&1 'echo.*OK' X-trace &&
+               atf_fail "T3: -X tracing included echo after set -X redirect"
+
+       rm -f X-trace
+       atf_check -s exit:0 \
+                       -o match:OKOK -o not-match:echo -o not-match:printf \
+                       -e match:echo -e match:OK -e not-match:printf \
+               ${TEST_SH} -ec \
+                       'PS4=; set -X 2>X-trace;
+                       printf "%s" OK;
+                       set -X;
+                       echo OK'
+       test -s X-trace || atf_fail "T4: Failed to create trace output file"
+       grep >/dev/null 2>&1 'printf.*%s.*OK' X-trace ||
+               atf_fail "T4: -X tracing missing printf"
+       grep >/dev/null 2>&1 'set.*-X' X-trace ||
+               atf_fail "T4: -X tracing missing set -X"
+       grep >/dev/null 2>&1 'echo.*OK' X-trace &&
+               atf_fail "T4: -X tracing included echo after set -X redirect"
+
+       # Now check that -X and the tracing files work properly wrt functions
+
+       # a shell that supports -X should support "local -" ... but verify
+
+       ( ${TEST_SH} -c 'fn() { local - || exit 2; set -f; }; set +f; fn;
+               case "$-" in ("*f*") exit 1;; esac; exit 0' ) 2>/dev/null ||
+                       atf_skip "-X function test: 'local -' unsupported"
+
+       rm -f X-trace X-trace-fn
+       atf_check -s exit:0 \
+                       -o match:OKhelloGOOD            \
+                       -e empty                        \
+               ${TEST_SH} -c '
+                       say() {
+                               printf "%s" "$*"
+                       }
+                       funct() {
+                               local -
+
+                               set -X 2>X-trace-fn
+                               say hello
+                       }
+
+                       set -X 2>X-trace
+
+                       printf OK
+                       funct
+                       echo GOOD
+               '
+       test -s X-trace || atf_fail "T5: Failed to create trace output file"
+       test -s X-trace-fn || atf_fail "T5: Failed to create fn trace output"
+       grep >/dev/null 2>&1 'printf.*OK' X-trace ||
+               atf_fail "T5: -X tracing missing printf"
+       grep >/dev/null 2>&1 funct X-trace ||
+               atf_fail "T5: -X tracing missing funct"
+       grep >/dev/null 2>&1 'set.*-X' X-trace ||
+               atf_fail "T5: -X tracing missing set -X from in funct"
+       grep >/dev/null 2>&1 'echo.*GOOD' X-trace ||
+               atf_fail "T5: -X tracing missing echo after funct redirect"
+       grep >/dev/null 2>&1 'say.*hello' X-trace &&
+               atf_fail "T5: -X tracing included 'say' after funct redirect"
+       grep >/dev/null 2>&1 'say.*hello' X-trace-fn ||
+               atf_fail "T5: -X funct tracing missed 'say'"
+
+       rm -f X-trace X-trace-fn
+
+       atf_check -s exit:0 \
+                       -o match:OKhelloGOOD            \
+                       -e empty                        \
+               ${TEST_SH} -c '
+                       say() {
+                               printf "%s" "$*"
+                       }
+                       funct() {
+                               local -
+
+                               set +X
+                               say hello
+                       }
+
+                       set -X 2>X-trace
+
+                       printf OK
+                       funct
+                       echo GOOD
+               '
+       test -s X-trace || atf_fail "T6: Failed to create trace output file"
+       grep >/dev/null 2>&1 'printf.*OK' X-trace ||
+               atf_fail "T6: -X tracing missing printf"
+       grep >/dev/null 2>&1 funct X-trace ||
+               atf_fail "T6: -X tracing missing funct"
+       grep >/dev/null 2>&1 'set.*+X' X-trace ||
+               atf_fail "T6: -X tracing missing set +X from in funct"
+       grep >/dev/null 2>&1 'echo.*GOOD' X-trace ||
+               atf_fail "T6: -X tracing missing echo after funct redirect"
+       grep >/dev/null 2>&1 'say.*hello' X-trace &&
+               atf_fail "T6: -X tracing included 'say' after funct redirect"
+
+       rm -f X-trace
+
+       atf_check -s exit:0 \
+                       -o match:OKtracednotraceGOOD \
+                       -e match:say -e match:traced -e not-match:notrace \
+               ${TEST_SH} -c '
+                       say() {
+                               printf "%s" "$*"
+                       }
+                       funct() {
+                               local -
+
+                               set +X -x
+
+                               say traced
+                               exec 2>/dev/null
+                               say notrace
+
+                       }
+
+                       set -X 2>X-trace
+
+                       printf OK
+                       funct
+                       echo GOOD
+               '
+       test -s X-trace || atf_fail "T7: Failed to create trace output file"
+       grep >/dev/null 2>&1 'printf.*OK' X-trace ||
+               atf_fail "T7: -X tracing missing printf"
+       grep >/dev/null 2>&1 funct X-trace ||
+               atf_fail "T7: -X tracing missing funct"
+       grep >/dev/null 2>&1 'set.*+X.*-x' X-trace ||
+               atf_fail "T7: -X tracing missing set +X -x from in funct"
+       grep >/dev/null 2>&1 'echo.*GOOD' X-trace ||
+               atf_fail "T7: -X tracing missing echo after funct +X"
+       grep >/dev/null 2>&1 'say.*hello' X-trace &&
+               atf_fail "T7: -X tracing included 'say' after funct +X"
+
+       rm -f X-trace X-trace-fn
+       atf_check -s exit:0 \
+                       -o "match:OKg'daybye-bye.*hello.*GOOD"          \
+                       -e empty                                        \
+               ${TEST_SH} -c '
+                       say() {
+                               printf "%s" "$*"
+                       }
+                       fn1() {
+                               local -
+
+                               set -X 2>>X-trace-fn
+                               say "g'\''day"
+                               "$@"
+                               say bye-bye



Home | Main Index | Thread Index | Old Index