Skip to content

Commit

Permalink
Add support for quotes and double quotes in --with-args
Browse files Browse the repository at this point in the history
Comparing a variable was parsing the quotes/double-quotes even when
trying to escape them wiht string manipulation. Solution is a file
content because it leaves the content as is.
  • Loading branch information
Martin Roy authored and lindycoder committed Mar 8, 2019
1 parent df2180d commit 1fe4052
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/mocking.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ EOF
}

validate-args() {
expected="$@"
file=$(mktemp validate-args.XXXXXXXXX)
echo "$@" > ${file}

cat <<EOF
args="\$@"
if [ "\${args}" != "${expected}" ]; then
if [ "\${args}" != "\$(cat ${file})" ]; then
cat <<OUT
Unexpected invocation for command 'some-command':
Got : <"\${args}">
Expected : <"${expected}">
Expected : <"\$(cat ${file})">
OUT
exit 1
fi
Expand Down
21 changes: 21 additions & 0 deletions test/test_mocking_args_matching.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,24 @@ test_fails_if_2_with_args_argments_are_given() {

assert "$(cat error)" equals "Cannot expect more than 1 set of argument for an invocation, please use 'mock' multiple times"
}

test_supports_escaped_quotes_in_args() {
mock some-command --with-args "one 'two' three"

some-command one \'two\' three
assert ${?} succeeded
}

test_supports_escaped_double_quotes_in_args() {
mock some-command --with-args "one \"two\" three"

some-command one \"two\" three
assert ${?} succeeded
}

test_supports_escaped_double_quotes_in_single_quotes_in_args() {
mock some-command --with-args 'one "two" three'

some-command 'one "two" three'
assert ${?} succeeded
}

0 comments on commit 1fe4052

Please sign in to comment.