Skip to content

Commit

Permalink
Make the ksh unit tests more hermetic
Browse files Browse the repository at this point in the history
Don't let the user environment locale vars affect the ksh unit tests.

Protect against unit tests inadvertantly changing critical env vars.

The ksh unit tests had been testing the default locale in effect when
the tests were run. Explicitly test the "C" locale instead. Note that
this is different from "C.UTF-8".

Fixes #255
  • Loading branch information
krader1961 committed Apr 27, 2018
1 parent c073b10 commit f4a481b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/cmd/ksh93/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ ld_library_path = 'LD_LIBRARY_PATH=' + ':'.join(
# The emptry string is POSIX locale.
# TODO: Check how ksh code handles '' string while setting locales and
# explicitly set string to 'C' or 'POSIX' here if possible.
locales = ['', 'C.UTF-8']
locales = ['C', 'C.UTF-8']

# Each entry in `all_tests` is an array of one or two elements. The first
# element is the test name. The second is an optional timeout if the default
Expand Down Expand Up @@ -118,7 +118,7 @@ foreach testspec : all_tests
endforeach

# shcomp tests
lang_var = 'LANG='
lang_var = 'LANG=C.UTF-8'
if not shcomp_tests_to_skip.contains(testname)
test(testname + '/shcomp', ksh93_exe, timeout: timeout,
args: [ test_driver, 'shcomp', testname],
Expand Down
10 changes: 10 additions & 0 deletions src/cmd/ksh93/tests/util/preamble.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
readonly test_file=${0##*/}
readonly test_name=$1

#
# Make sure the unit test can't inadvertantly modify several critical env vars.
#
readonly FULL_PATH
readonly ORIG_PATH
readonly SAFE_PATH
readonly TEST_DIR
readonly TEST_SRC_DIR
readonly BUILD_DIR

#
# Create some functions for reporting and counting errors.
#
Expand Down
19 changes: 16 additions & 3 deletions src/cmd/ksh93/tests/util/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ fi
#
# Setup the environment for the unit test.
#
export TEST_SRC_DIR=${0%/*/*}
export TEST_SRC_DIR=${0%/*/*} # capture the parent directory containing this script
readonly test_name=$1
readonly test_path=$TEST_SRC_DIR/$test_name.sh
readonly test_script=$test_name.sh
export BUILD_DIR=$PWD

#
# Create a temp dir and make it the CWD for the unit test. It will be removed by the unit test
# postscript it if we exit cleanly.
# postscript if there are no test failures.
#
# The use of `mktemp -dt` isn't ideal as it has slightly different meaning on BSD and GNU. But for
# our purposes that doesn't matter. It simply means the temp file name will contain the X's on a BSD
Expand All @@ -50,7 +50,8 @@ export PATH=$FULL_PATH

#
# Create a couple of named pipes (fifos) for the unit test to use as barriers rather than using
# arbitrary sleeps.
# arbitrary sleeps. The numeric suffix reflects the file-descriptor that will be open for
# read-write on the fifo; e.g., `exec 9<>fifo9`.
#
mkfifo fifo9
mkfifo fifo8
Expand All @@ -62,6 +63,18 @@ mkdir $TEST_DIR/home
export HOME=$TEST_DIR/home
export HISTFILE=$TEST_DIR/sh_history

#
# Make sure any locale vars set by the user (or the continuous build environment) don't affect the
# tests. The only var we don't unset or change is `LANG` because we expect `meson test` to set it.
#
unset LC_ALL
unset LC_COLLATE
unset LC_CTYPE
unset LC_MESSAGES
unset LC_MONETARY
unset LC_NUMERIC
unset LC_TIME

#
# Create the actual unit test script by concatenating the stock preamble and postscript to the unit
# test. Then run the composed script.
Expand Down
10 changes: 10 additions & 0 deletions src/cmd/ksh93/tests/util/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
#
readonly test_name=${0##*/}

#
# Make sure the unit test can't inadvertantly modify several critical env vars.
#
readonly FULL_PATH
readonly ORIG_PATH
readonly SAFE_PATH
readonly TEST_DIR
readonly TEST_SRC_DIR
readonly BUILD_DIR

#
# Create some functions for reporting and counting errors.
#
Expand Down

0 comments on commit f4a481b

Please sign in to comment.