Skip to content

Commit

Permalink
Merge pull request #5 from lox/use-eval-for-parsing-patterns
Browse files Browse the repository at this point in the history
Use eval for parsing patterns
  • Loading branch information
lox authored Jun 20, 2019
2 parents 2afe7d3 + c411b89 commit 10e904a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
25 changes: 9 additions & 16 deletions binstub
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,14 @@ while IFS= read -r line; do
arguments=("$@")
parsed_patterns=()

# Check we can run xargs on the patterns. We use xargs to parse quoted substrings
# without having to use eval
if ! xargs_output="$(xargs printf '%b\0' <<< "${patterns}")" ; then
debug "xargs failed to parse ${patterns}"
eval "${_STUB_RESULT}"=2
break
fi
# Parse patterns into tokens using eval to respect quoted
# strings. This is less than ideal, but the pattern input
# is also already eval'd elsewhere. At least this handles
# things like newlines (which xargs doesn't)
eval "parsed_patterns=(${patterns})"

# Tokenize on null-bytes from xargs above
while IFS= read -rd '' token; do
parsed_patterns+=("$token")
done <<< "$xargs_output"

# debug "patterns [${#parsed_patterns[@]}] = $(printf "'%q' " "${parsed_patterns[@]}")"
# debug "arguments [${#arguments[@]}] = $(printf "'%q' " "${arguments[@]}")"
debug "patterns [${#parsed_patterns[@]}] = $(printf "'%q' " "${parsed_patterns[@]}")"
debug "arguments [${#arguments[@]}] = $(printf "'%q' " "${arguments[@]}")"

# Match the expected argument patterns to actual
# arguments.
Expand Down Expand Up @@ -148,6 +141,6 @@ else
echo "${_STUB_RESULT}=${!_STUB_RESULT}"
} > "${!_STUB_RUN}"

debug "result $status"
exit "$status"
debug "result ${!_STUB_RESULT}"
exit "${!_STUB_RESULT}"
fi
51 changes: 51 additions & 0 deletions tests/binstub.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bats

load '../stub'

# Uncomment to enable stub debug output:
# export MYCOMMAND_STUB_DEBUG=/dev/tty

@test "Stub a single command with basic arguments" {
stub mycommand "llamas : echo running llamas"

run mycommand llamas

[ "$status" -eq 0 ]
[[ "$output" == *"running llamas"* ]]

unstub mycommand
}

@test "Stub a command with multiple invocations" {
stub mycommand \
"llamas : echo running llamas" \
"alpacas : echo running alpacas"

run bash -c "mycommand llamas && mycommand alpacas"

[ "$status" -eq 0 ]
[[ "$output" == *"running llamas"* ]]
[[ "$output" == *"running alpacas"* ]]

unstub mycommand
}


@test "Invoke a stub multiple times" {
stub mycommand "llamas : echo running llamas"

run bash -c "mycommand llamas && mycommand alpacas"

[ "$status" -eq 1 ]
}

@test "Stub a single command with quoted strings" {
stub mycommand "llamas '' 'always llamas' : echo running llamas"

run mycommand llamas '' always\ llamas

[ "$status" -eq 0 ]
[[ "$output" == *"running llamas"* ]]

unstub mycommand
}

0 comments on commit 10e904a

Please sign in to comment.