Skip to content

Commit c015339

Browse files
committed
tests: Provide an independent, auto-cleaned working directory to each test.
Fixes #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.
1 parent b5d02a2 commit c015339

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

highlighters/main/test-data/path-space- .zsh

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
# vim: ft=zsh sw=2 ts=2 et
2828
# -------------------------------------------------------------------------------------------------
2929

30-
BUFFER='ls highlighters/main/test-data/path-space-\ .zsh'
30+
mkdir A
31+
touch "A/mu with spaces"
32+
BUFFER='ls A/mu\ with\ spaces'
3133

3234
expected_region_highlight=(
3335
"1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls
34-
"4 48 $ZSH_HIGHLIGHT_STYLES[path]" # highlighters/main/test-data/path-space-\ .zsh
36+
"4 19 $ZSH_HIGHLIGHT_STYLES[path]" # A/mu\ with\ spaces
3537
)

highlighters/main/test-data/path-tilde-named.zsh

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
# vim: ft=zsh sw=2 ts=2 et
2828
# -------------------------------------------------------------------------------------------------
2929

30-
hash -d D=highlighters/main/test-data
30+
mkdir mydir
31+
touch mydir/path-tilde-named.test
32+
hash -d D=mydir
3133

32-
BUFFER='ls ~D/path-tilde-named.zsh'
34+
BUFFER='ls ~D/path-tilde-named.test'
3335

3436
expected_region_highlight=(
3537
"1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls
36-
"4 26 $ZSH_HIGHLIGHT_STYLES[path]" # ~D/path-tilde-named.zsh
38+
"4 27 $ZSH_HIGHLIGHT_STYLES[path]" # ~D/path-tilde-named.test
3739
)

highlighters/main/test-data/path.zsh

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
# vim: ft=zsh sw=2 ts=2 et
2828
# -------------------------------------------------------------------------------------------------
2929

30-
BUFFER='ls highlighters/main/test-data/path.zsh'
30+
mkdir A
31+
touch A/mu
32+
BUFFER='ls A/mu'
3133

3234
expected_region_highlight=(
33-
"1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls
34-
"4 39 $ZSH_HIGHLIGHT_STYLES[path]" # highlighters/main/test-data/path.zsh
35+
"1 2 $ZSH_HIGHLIGHT_STYLES[command]" # ls
36+
"4 7 $ZSH_HIGHLIGHT_STYLES[path]" # A/mu
3537
)

tests/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ _Note_: `$region_highlight` uses the same `"$i $j $style"` syntax but interprets
1717

1818
**Isolation**: Each test is run in a separate subshell, so any variables, aliases, functions, etc.,
1919
it defines will be visible to the tested code (that computes `$region_highlight`), but will not affect
20-
subsequent tests.
20+
subsequent tests. The current working directory of tests is set to a newly-created empty directory,
21+
which is automatically cleaned up after the test exits.
2122

2223

2324
highlighting test

tests/test-highlighting.zsh

+21-2
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,19 @@ ZSH_HIGHLIGHT_HIGHLIGHTERS=($1)
5555

5656
# Runs a highlighting test
5757
# $1: data file
58-
run_test() {
58+
run_test_internal() {
5959
local -a highlight_zone
6060
local unused_highlight='bg=red,underline' # a style unused by anything else, for tests to use
6161

62+
local tests_tempdir="$1"; shift
63+
local srcdir="$PWD"
64+
builtin cd -q -- "$tests_tempdir" || { echo >&2 "Bail out! cd failed: $?"; return 1 }
65+
6266
echo "# ${1:t:r}"
6367

6468
# Load the data and prepare checking it.
6569
PREBUFFER= BUFFER= ;
66-
. "$1"
70+
. "$srcdir"/"$1"
6771

6872
# Check the data declares $PREBUFFER or $BUFFER.
6973
[[ -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() {
108112
done
109113
}
110114

115+
run_test() {
116+
# Do not combine the declaration and initialization: «local x="$(false)"» does not set $?.
117+
local __tests_tempdir; __tests_tempdir="$(mktemp -d)"
118+
if [[ $? -ne 0 ]] || [[ -z $__tests_tempdir ]] || [[ ! -d $__tests_tempdir ]]; then
119+
echo >&2 "Bail out! mktemp failed"; return 1
120+
fi
121+
typeset -r __tests_tempdir # don't allow tests to override the variable that we will 'rm -rf' later on
122+
123+
{
124+
run_test_internal "$__tests_tempdir" "$@"
125+
} always {
126+
rm -rf -- "$__tests_tempdir"
127+
}
128+
}
129+
111130
# Process each test data file in test data directory.
112131
integer something_failed=0
113132
for data_file in ${0:h:h}/highlighters/$1/test-data/*.zsh; do

0 commit comments

Comments
 (0)