-
Notifications
You must be signed in to change notification settings - Fork 37
/
stub.bash
47 lines (36 loc) · 1.16 KB
/
stub.bash
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
# shellcheck shell=bash
BATS_MOCK_TMPDIR="${BATS_TMPDIR}"
BATS_MOCK_BINDIR="${BATS_MOCK_TMPDIR}/bin"
PATH="$BATS_MOCK_BINDIR:$PATH"
stub() {
local program="$1"
local prefix
prefix="$(echo "$program" | tr a-z- A-Z_)"
shift
export "${prefix}_STUB_PLAN"="${BATS_MOCK_TMPDIR}/${program}-stub-plan"
export "${prefix}_STUB_RUN"="${BATS_MOCK_TMPDIR}/${program}-stub-run"
export "${prefix}_STUB_END"=
mkdir -p "${BATS_MOCK_BINDIR}"
ln -sf "${BASH_SOURCE[0]%stub.bash}binstub" "${BATS_MOCK_BINDIR}/${program}"
touch "${BATS_MOCK_TMPDIR}/${program}-stub-plan"
for arg in "$@"; do printf "%s\n" "$arg" >> "${BATS_MOCK_TMPDIR}/${program}-stub-plan"; done
}
stub_repeated() {
local program="$1"
# shellcheck disable=SC2155
local prefix="$(echo "$program" | tr a-z- A-Z_)"
export "${prefix}_STUB_NOINDEX"=1
stub "$@"
}
unstub() {
local program="$1"
local prefix
prefix="$(echo "$program" | tr a-z- A-Z_)"
local path="${BATS_MOCK_BINDIR}/${program}"
export "${prefix}_STUB_END"=1
local STATUS=0
"$path" || STATUS="$?"
rm -f "$path"
rm -f "${BATS_MOCK_TMPDIR}/${program}-stub-plan" "${BATS_MOCK_TMPDIR}/${program}-stub-run"
return "$STATUS"
}