/
spec_helper.sh
165 lines (138 loc) · 4.14 KB
/
spec_helper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#shellcheck shell=sh
set -eu
# shellcheck disable=SC2039
spec_helper_precheck() {
minimum_version "$SHELLSPEC_VERSION"
if [ "${PIPEFAIL:-}" ]; then
# TODO: set -o pipefail will be allowed in POSIX issue 8
# shellcheck disable=SC3040
if ( set -o pipefail ) 2>/dev/null; then
info "pipefail enabled"
else
warn "pipefail is not available"
unsetenv PIPEFAIL
fi
fi
if [ "${EXTGLOB:-}" ]; then
# shellcheck disable=SC3044
if shopt -s extglob 2>/dev/null; then
info "extglob enabled"
setenv EXTGLOB="extglob"
elif setopt extendedglob 2>/dev/null; then
info "extendedglob enabled"
setenv EXTGLOB="extendedglob"
else
warn "extglob is not available"
unsetenv EXTGLOB
fi
fi
if [ "$SHELLSPEC_NOEXEC_TMPDIR" ]; then
info "Some tests will be skipped" \
"because the files under tmp direcotry cannot be executed"
fi
}
spec_helper_loaded() {
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
# shellcheck disable=SC2153
IFS="${SHELLSPEC_LF}${SHELLSPEC_TAB}"
# shellcheck disable=SC3040
[ "${PIPEFAIL:-}" ] && set -o pipefail
unset PIPEFAIL ||:
# shellcheck disable=SC3044
case ${EXTGLOB:-} in
extglob) shopt -s extglob ;;
extendedglob) setopt extendedglob ;;
?*) echo "[error] EXTGLOB value is invalid: $EXTGLOB" >&2; exit 1
esac
unset EXTGLOB ||:
}
spec_helper_configure() {
import 'support/custom_matcher'
set_subject() {
if subject > /dev/null; then
SHELLSPEC_SUBJECT=$(subject; echo _)
SHELLSPEC_SUBJECT=${SHELLSPEC_SUBJECT%_}
else
unset SHELLSPEC_SUBJECT ||:
fi
}
set_status() {
if status > /dev/null; then
SHELLSPEC_STATUS=$(status; echo _)
SHELLSPEC_STATUS=${SHELLSPEC_STATUS%_}
else
unset SHELLSPEC_STATUS ||:
fi
}
set_stdout() {
if stdout > /dev/null; then
SHELLSPEC_STDOUT=$(stdout; echo _)
SHELLSPEC_STDOUT=${SHELLSPEC_STDOUT%_}
else
unset SHELLSPEC_STDOUT ||:
fi
}
set_fd() {
SHELLSPEC_STDIO_FILE_BASE=$SHELLSPEC_WORKDIR
if "fd$1" > /dev/null; then
"fd$1" > "$SHELLSPEC_STDIO_FILE_BASE.fd-$1"
else
@rm -f "$SHELLSPEC_STDIO_FILE_BASE.fd-$1"
fi
}
# modifier for test
shellspec_syntax shellspec_modifier__modifier_
shellspec_modifier__modifier_() {
[ "${SHELLSPEC_SUBJECT+x}" ] || return 1
shellspec_puts "$SHELLSPEC_SUBJECT"
}
shellspec_syntax shellspec_modifier__null_modifier_
shellspec_modifier__null_modifier_() { :; }
subject_mock() {
shellspec_output() { shellspec_puts "$1" >&2; }
}
modifier_mock() {
shellspec_output() { shellspec_puts "$1" >&2; }
}
matcher_mock() {
shellspec_output() { shellspec_puts "$1" >&2; }
shellspec_proxy "shellspec_matcher_do_match" "shellspec_matcher__match"
}
shellspec_syntax_alias 'shellspec_subject_switch' 'shellspec_subject_value'
switch_on() { shellspec_if "$SHELLSPEC_SUBJECT"; }
switch_off() { shellspec_unless "$SHELLSPEC_SUBJECT"; }
posh_pattern_matching_bug() {
# shellcheck disable=SC2194
case "a[d]" in (*"a[d]"*) false; esac # posh <= 0.12.6
}
invalid_posix_parameter_expansion() {
set -- "a*b" "a[*]"
[ "${1#"$2"}" = "a*b" ] && return 1 || return 0
}
not_found_find() {
"$SHELLSPEC_FIND" . -prune >/dev/null 2>&1 && return 1 || return 0
}
not_supported_find() {
"$SHELLSPEC_FIND" -L . -prune >/dev/null 2>&1 && return 1 || return 0
}
readonly_malfunction() { [ "$SHELLSPEC_DEFECT_READONLY" ]; }
posh_shell_flag_bug() { [ "$SHELLSPEC_DEFECT_SHELLFLAG" ]; }
not_exist_failglob() { [ ! "$SHELLSPEC_FAILGLOB_AVAILABLE" ]; }
busybox_w32() { [ "$SHELLSPEC_BUSYBOX_W32" ]; }
exists_tty() { [ "$SHELLSPEC_TTY" ]; }
not_exists_shopt() { [ ! "$SHELLSPEC_SHOPT_AVAILABLE" ]; }
noexec_tmpdir() { [ "$SHELLSPEC_NOEXEC_TMPDIR" ]; }
uppercase() {
set -- aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ
while IFS= read -r line; do
for i; do shellspec_replace_all line "${i%?}" "${i#?}"; done
echo "$line"
done
}
shellspec_before :
shellspec_after :
before_each :
after_each :
before_all :
after_all :
}