bootstrap
3944 bytes
1#!/usr/bin/env zsh
2# Bootstrap script for dotenv plugin tests
3# This is sourced before any tests run and provides shared utilities
4
5# Load the dotenv plugin
6source "$PWD/dotenv.plugin.zsh"
7ZSH_DOTENV_PROMPT=false
8ZSH_DOTENV_FILE=/dev/null
9
10# Helper: Parse dotenv file in test mode
11_parse_dotenv_test() {
12 parse_dotenv "$1" "test"
13}
14
15# Helper: Parse dotenv file in export mode
16_parse_dotenv_export() {
17 unset "${(k)parameters[(R)*export*]}" 2>/dev/null || true
18
19 parse_dotenv "$1" "test"
20
21 for key in "${(k)DOTENV_TEST_VARS}"; do
22 typeset -x "$key"="${DOTENV_TEST_VARS[$key]}"
23 done
24}
25
26# Helper: Run parse_dotenv suppressing stderr
27_parse_dotenv_quiet() {
28 parse_dotenv "$@" 2>/dev/null
29}
30
31# Helper: Create a temporary test fixture
32_create_temp_fixture() {
33 local fixture
34 fixture==(:) # Create temp file
35 echo "$fixture"
36}
37
38_write_temp_fixture() {
39 local fixture="$1"
40 > "$fixture"
41}
42
43
44# Helper: Source file with allexport and capture variables
45# Usage: _source_with_allexport "file.env"
46# Result is in DOTENV_SOURCE_VARS associative array
47_source_with_allexport() {
48 local filename="$1"
49
50 # Source with allexport in a subshell with no exported variables
51
52 # The return and capture of the exported variables is a bit of a pain:
53 # 1. We first store the key=value pairs in $vars associative array, which is
54 # defined before allexport is set to avoid appearing in results.
55 # 2. Afterwards, we join all keys and values of the associative with null delimiters. With
56 # "$(@kv)vars}" we get keys and values with quotes, to retain empty values. With (pj:\0:)
57 # we join them with nulls.
58 # 3. The caller reads this output with "${(@0)}" to split by nulls and quoting to retain
59 # empty values, and then uses it to populate an associative array.
60 # Don't try to understand this or change it unless you have to. Debugging is a nightmare.
61 typeset -gA DOTENV_SOURCE_VARS
62 DOTENV_SOURCE_VARS=("${(@0)"$(
63 local -A vars
64
65 # Clear all exports first
66 zmodload zsh/parameter
67 unset ${(k)parameters[(R)*export*]} 2>/dev/null || true
68
69 # Source file with allexport
70 setopt localoptions allexport
71 source "$filename"
72
73 # Set all exported variables into an associative array
74 for key in ${(k)parameters[(R)*export*]}; do
75 vars[$key]="${(P)key}"
76 done
77
78 print -rn -- "${(@kvpj:\0:)vars}"
79 )"}")
80}
81
82
83## ZUnit assertion helpers
84
85_zunit_assert_function_exists() {
86 [[ "${+functions[$1]}" -eq 1 ]] && return 0
87 echo "Function '$1' does not exist"
88 exit 1
89}
90
91_zunit_assert_var_same_as() {
92 local tvalue=${${:-${(Pt)1%-*}}:-unset} tcomp=${${:-${(Pt)2%-*}}:-unset}
93 if [[ $tvalue != $tcomp ]]; then
94 echo "Type mismatch: '$1' ($tvalue) and '$2' ($tcomp)"
95 exit 78
96 fi
97
98 # Special case for associative arrays
99 if [[ ${(Pt)1} == "association" ]]; then
100 local -A value=("${(P@kv)1}") comparison=("${(P@kv)2}")
101 local -aU keys=("${(@k)value}" "${(@k)comparison}")
102
103 local ret=0 key
104 for key in "${keys[@]}"; do
105 # Key match checks
106 if [[ -v "value[$key]" && ! -v "comparison[$key]" ]]; then
107 echo "'$1[$key]' is set (value='${value[$key]}')"
108 ret=1
109 elif [[ ! -v "value[$key]" && -v "comparison[$key]" ]]; then
110 echo "'$1[$key]' is not set (expected='${comparison[$key]}')"
111 ret=1
112 # Value match checks
113 elif [[ "${value[$key]}" != "${comparison[$key]}" ]]; then
114 echo "'$1[$key]' value mismatch: '${value[$key]}' is not the same as '${comparison[$key]}'"
115 ret=1
116 fi
117 done
118
119 exit $ret
120 fi
121
122 # Generic case
123 local value="${(P)1}" comparison="${(P)2}"
124 [[ "$value" != "$comparison" ]] || exit 0
125 echo "'$1' value mismatch: '$value' is not the same as '$comparison'"
126 exit 1
127}
128
129_zunit_assert_var_is_set() {
130 [[ -v "$1" ]] && return 0
131 echo "Variable '$1' is not set"
132 exit 1
133}
134
135_zunit_assert_var_is_not_set() {
136 [[ ! -v "$1" ]] && return 0
137 echo "Variable '$1' is set"
138 exit 1
139}