tests: Provide an independent, auto-cleaned working directory to each test.

Fixes zsh-users/zsh-syntax-highlighting#182.
Prerequisite for testing issue #228.

* tests/test-highlighting.zsh
  (run_test): Move functionality to run_test_internal; make run_test be a wrapper
    that handles creating and cleaning up the tempdir.

* tests/README.md: Document the new feature.

* "highlighters/main/test-data/path-space- .zsh"
* highlighters/main/test-data/path-tilde-named.zsh
* highlighters/main/test-data/path.zsh
    Change test data to not depend on being run from the source directory.
This commit is contained in:
Daniel Shahaf 2015-11-16 22:54:52 +00:00
parent b5d02a2f49
commit c015339202
5 changed files with 37 additions and 11 deletions

View File

@ -27,9 +27,11 @@
# vim: ft=zsh sw=2 ts=2 et # vim: ft=zsh sw=2 ts=2 et
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
BUFFER='ls highlighters/main/test-data/path-space-\ .zsh' mkdir A
touch "A/mu with spaces"
BUFFER='ls A/mu\ with\ spaces'
expected_region_highlight=( expected_region_highlight=(
"1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls "1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls
"4 48 $ZSH_HIGHLIGHT_STYLES[path]" # highlighters/main/test-data/path-space-\ .zsh "4 19 $ZSH_HIGHLIGHT_STYLES[path]" # A/mu\ with\ spaces
) )

View File

@ -27,11 +27,13 @@
# vim: ft=zsh sw=2 ts=2 et # vim: ft=zsh sw=2 ts=2 et
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
hash -d D=highlighters/main/test-data mkdir mydir
touch mydir/path-tilde-named.test
hash -d D=mydir
BUFFER='ls ~D/path-tilde-named.zsh' BUFFER='ls ~D/path-tilde-named.test'
expected_region_highlight=( expected_region_highlight=(
"1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls "1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls
"4 26 $ZSH_HIGHLIGHT_STYLES[path]" # ~D/path-tilde-named.zsh "4 27 $ZSH_HIGHLIGHT_STYLES[path]" # ~D/path-tilde-named.test
) )

View File

@ -27,9 +27,11 @@
# vim: ft=zsh sw=2 ts=2 et # vim: ft=zsh sw=2 ts=2 et
# ------------------------------------------------------------------------------------------------- # -------------------------------------------------------------------------------------------------
BUFFER='ls highlighters/main/test-data/path.zsh' mkdir A
touch A/mu
BUFFER='ls A/mu'
expected_region_highlight=( expected_region_highlight=(
"1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls "1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls
"4 39 $ZSH_HIGHLIGHT_STYLES[path]" # highlighters/main/test-data/path.zsh "4 7 $ZSH_HIGHLIGHT_STYLES[path]" # A/mu
) )

View File

@ -17,7 +17,8 @@ _Note_: `$region_highlight` uses the same `"$i $j $style"` syntax but interprets
**Isolation**: Each test is run in a separate subshell, so any variables, aliases, functions, etc., **Isolation**: Each test is run in a separate subshell, so any variables, aliases, functions, etc.,
it defines will be visible to the tested code (that computes `$region_highlight`), but will not affect it defines will be visible to the tested code (that computes `$region_highlight`), but will not affect
subsequent tests. subsequent tests. The current working directory of tests is set to a newly-created empty directory,
which is automatically cleaned up after the test exits.
highlighting test highlighting test

View File

@ -55,15 +55,19 @@ ZSH_HIGHLIGHT_HIGHLIGHTERS=($1)
# Runs a highlighting test # Runs a highlighting test
# $1: data file # $1: data file
run_test() { run_test_internal() {
local -a highlight_zone local -a highlight_zone
local unused_highlight='bg=red,underline' # a style unused by anything else, for tests to use local unused_highlight='bg=red,underline' # a style unused by anything else, for tests to use
local tests_tempdir="$1"; shift
local srcdir="$PWD"
builtin cd -q -- "$tests_tempdir" || { echo >&2 "Bail out! cd failed: $?"; return 1 }
echo "# ${1:t:r}" echo "# ${1:t:r}"
# Load the data and prepare checking it. # Load the data and prepare checking it.
PREBUFFER= BUFFER= ; PREBUFFER= BUFFER= ;
. "$1" . "$srcdir"/"$1"
# Check the data declares $PREBUFFER or $BUFFER. # Check the data declares $PREBUFFER or $BUFFER.
[[ -z $PREBUFFER && -z $BUFFER ]] && { echo >&2 "Bail out! Either 'PREBUFFER' or 'BUFFER' must be declared and non-blank"; return 1; } [[ -z $PREBUFFER && -z $BUFFER ]] && { echo >&2 "Bail out! Either 'PREBUFFER' or 'BUFFER' must be declared and non-blank"; return 1; }
@ -108,6 +112,21 @@ run_test() {
done done
} }
run_test() {
# Do not combine the declaration and initialization: «local x="$(false)"» does not set $?.
local __tests_tempdir; __tests_tempdir="$(mktemp -d)"
if [[ $? -ne 0 ]] || [[ -z $__tests_tempdir ]] || [[ ! -d $__tests_tempdir ]]; then
echo >&2 "Bail out! mktemp failed"; return 1
fi
typeset -r __tests_tempdir # don't allow tests to override the variable that we will 'rm -rf' later on
{
run_test_internal "$__tests_tempdir" "$@"
} always {
rm -rf -- "$__tests_tempdir"
}
}
# Process each test data file in test data directory. # Process each test data file in test data directory.
integer something_failed=0 integer something_failed=0
for data_file in ${0:h:h}/highlighters/$1/test-data/*.zsh; do for data_file in ${0:h:h}/highlighters/$1/test-data/*.zsh; do