forked from jasonkarns/bats-mock
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from buildkite-plugins/flamefire_improvements
Flamefire improvements + bugfix
- Loading branch information
Showing
6 changed files
with
560 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,71 @@ | ||
BATS_MOCK_TMPDIR="${BATS_TMPDIR}" | ||
BATS_MOCK_BINDIR="${BATS_MOCK_TMPDIR}/bin" | ||
|
||
BATS_MOCK_REAL_mkdir=$(which mkdir) | ||
export BATS_MOCK_REAL_mkdir | ||
BATS_MOCK_REAL_ln=$(which ln) | ||
export BATS_MOCK_REAL_ln | ||
BATS_MOCK_REAL_touch=$(which touch) | ||
export BATS_MOCK_REAL_touch | ||
BATS_MOCK_REAL_rm=$(which rm) | ||
export BATS_MOCK_REAL_rm | ||
|
||
PATH="$BATS_MOCK_BINDIR:$PATH" | ||
|
||
stub() { | ||
local program="$1" | ||
local prefix="$(echo "$program" | tr a-z- A-Z_)" | ||
local prefix | ||
# shellcheck disable=SC2018,SC2019 # anything not A-Z0-9 will be _ | ||
# the "\n" is necessary to avoid adding a trailing _ to the name | ||
prefix="$(echo "$program" | tr a-z A-Z | tr -C "A-Z0-9\n" '_')" | ||
shift | ||
|
||
|
||
if [[ "$prefix" =~ ^[^A-Z_]+(.*)$ ]]; then | ||
# remove leading non A-Z_ characters to make a valid variable name | ||
prefix="${BASH_REMATCH[1]}" | ||
fi | ||
|
||
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}" | ||
"$BATS_MOCK_REAL_mkdir" -p "${BATS_MOCK_BINDIR}" | ||
"$BATS_MOCK_REAL_ln" -sf "${BASH_SOURCE[0]%stub.bash}binstub" "${BATS_MOCK_BINDIR}/${program}" | ||
|
||
rm -f "${BATS_MOCK_TMPDIR}/${program}-stub-plan" "${BATS_MOCK_TMPDIR}/${program}-stub-run" | ||
touch "${BATS_MOCK_TMPDIR}/${program}-stub-plan" | ||
"$BATS_MOCK_REAL_touch" "${BATS_MOCK_TMPDIR}/${program}-stub-plan" | ||
for arg in "$@"; do printf "%s\n" "$arg" >> "${BATS_MOCK_TMPDIR}/${program}-stub-plan"; done | ||
} | ||
|
||
unstub() { | ||
local allow_missing=0 | ||
if [ "$1" == "--allow-missing" ]; then | ||
allow_missing=1 | ||
shift | ||
fi | ||
local program="$1" | ||
local prefix="$(echo "$program" | tr a-z- A-Z_)" | ||
local path="${BATS_MOCK_BINDIR}/${program}" | ||
local prefix | ||
# shellcheck disable=SC2018,SC2019 # anything not A-Z0-9 will be _ | ||
# the "\n" is necessary to avoid adding a trailing _ to the name | ||
prefix="$(echo "${program}" | tr a-z A-Z | tr -C "A-Z0-9\n" '_')" | ||
|
||
if [[ "$prefix" =~ ^[^A-Z_]+(.*)$ ]]; then | ||
# remove leading non A-Z_ characters to make a valid variable name | ||
prefix="${BASH_REMATCH[1]}" | ||
fi | ||
|
||
export "${prefix}_STUB_END"=1 | ||
|
||
local STATUS=0 | ||
"$path" || STATUS="$?" | ||
if [ -f "$path" ]; then | ||
"$path" || STATUS="$?" | ||
elif [ $allow_missing -eq 0 ]; then | ||
echo "$program is not stubbed" >&2 | ||
STATUS=1 | ||
fi | ||
|
||
rm -f "$path" | ||
rm -f "${BATS_MOCK_TMPDIR}/${program}-stub-plan" "${BATS_MOCK_TMPDIR}/${program}-stub-run" | ||
"$BATS_MOCK_REAL_rm" -f "$path" | ||
"$BATS_MOCK_REAL_rm" -f "${BATS_MOCK_TMPDIR}/${program}-stub-plan" "${BATS_MOCK_TMPDIR}/${program}-stub-run" | ||
return "$STATUS" | ||
} |
Oops, something went wrong.