From c01533920245599e4fbc4a1825520737b2c1a6ce Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Mon, 16 Nov 2015 22:54:52 +0000 Subject: [PATCH] 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. --- highlighters/main/test-data/path-space- .zsh | 6 +++-- .../main/test-data/path-tilde-named.zsh | 8 ++++--- highlighters/main/test-data/path.zsh | 8 ++++--- tests/README.md | 3 ++- tests/test-highlighting.zsh | 23 +++++++++++++++++-- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/highlighters/main/test-data/path-space- .zsh b/highlighters/main/test-data/path-space- .zsh index f6b889e..7d473a7 100644 --- a/highlighters/main/test-data/path-space- .zsh +++ b/highlighters/main/test-data/path-space- .zsh @@ -27,9 +27,11 @@ # 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=( "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 ) diff --git a/highlighters/main/test-data/path-tilde-named.zsh b/highlighters/main/test-data/path-tilde-named.zsh index be5bb5a..235cc1c 100644 --- a/highlighters/main/test-data/path-tilde-named.zsh +++ b/highlighters/main/test-data/path-tilde-named.zsh @@ -27,11 +27,13 @@ # 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=( "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 ) diff --git a/highlighters/main/test-data/path.zsh b/highlighters/main/test-data/path.zsh index 3688b9b..29384c2 100644 --- a/highlighters/main/test-data/path.zsh +++ b/highlighters/main/test-data/path.zsh @@ -27,9 +27,11 @@ # 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=( - "1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls - "4 39 $ZSH_HIGHLIGHT_STYLES[path]" # highlighters/main/test-data/path.zsh + "1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls + "4 7 $ZSH_HIGHLIGHT_STYLES[path]" # A/mu ) diff --git a/tests/README.md b/tests/README.md index 80525ac..7b17914 100644 --- a/tests/README.md +++ b/tests/README.md @@ -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., 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 diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh index 1eed974..b31db05 100755 --- a/tests/test-highlighting.zsh +++ b/tests/test-highlighting.zsh @@ -55,15 +55,19 @@ ZSH_HIGHLIGHT_HIGHLIGHTERS=($1) # Runs a highlighting test # $1: data file -run_test() { +run_test_internal() { local -a highlight_zone 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}" # Load the data and prepare checking it. PREBUFFER= BUFFER= ; - . "$1" + . "$srcdir"/"$1" # 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; } @@ -108,6 +112,21 @@ run_test() { 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. integer something_failed=0 for data_file in ${0:h:h}/highlighters/$1/test-data/*.zsh; do