]> Sergey Matveev's repositories - zsh-autoenv.git/blob - tests/setup.zsh
02f41b3790129b13cfd974fa7a19fbd06c06851a
[zsh-autoenv.git] / tests / setup.zsh
1 # Setup for tests.
2 #
3 # It returns 1 in case of errors, and no tests should be run then!
4 #
5 # Ensure we have our mocked out AUTOENV_AUTH_FILE
6 # (via .zshenv).
7
8 # Treat unset variables as errors.
9 # Not handled in varstash yet.
10 # setopt nounset
11
12 export AUTOENV_AUTH_FILE="$CRAMTMP/autoenv/.autoenv_auth"
13
14 # Abort this setup script on any error.
15 _save_errexit=${options[errexit]}
16 set -e
17
18 # Can be defined in .zshenv, e.g. tests/ZDOTDIR.loadviafunction/.zshenv.
19 if [[ -z $TEST_SOURCE_AUTOENV ]]; then
20   TEST_SOURCE_AUTOENV=(source $TESTDIR/../autoenv.plugin.zsh)
21 fi
22 $TEST_SOURCE_AUTOENV
23
24 # Reset any authentication.
25 if [[ -f $AUTOENV_AUTH_FILE ]]; then
26   echo -n >| $AUTOENV_AUTH_FILE
27 fi
28
29 # Add file ($1), version ($2), and optional hash ($3) to authentication file.
30 test_autoenv_add_to_env() {
31   emulate -L zsh
32   [[ -d ${AUTOENV_AUTH_FILE:h} ]] || mkdir -p ${AUTOENV_AUTH_FILE:h}
33   _autoenv_deauthorize $1
34   {
35     local ret_pair
36     _autoenv_hash_pair $1 2 ${2:-} && echo $ret_pair
37   } >>| $AUTOENV_AUTH_FILE
38 }
39
40 # Add enter and leave env files to authentication file.
41 test_autoenv_auth_env_files() {
42   local dir=${1:-$PWD}
43   test_autoenv_add_to_env $dir/$AUTOENV_FILE_ENTER
44   test_autoenv_add_to_env $dir/$AUTOENV_FILE_LEAVE
45 }
46
47 # Now keep on going on errors again.
48 options[errexit]=$_save_errexit
49 unset _save_errexit