Skip to content

Commit

Permalink
Validate arguments on mocks
Browse files Browse the repository at this point in the history
This is the first working version, will only support
ordered successive calls, upcoming change should have
param-base action triggers
  • Loading branch information
staz003 authored and Martin Roy committed Nov 25, 2016
1 parent 04ecdf8 commit 1b80343
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ clean.sh
test

test_clean_works() {
mock rm --and exit-code 0
mock rm --with-args "somewhere/some-file" --and exit-code 0

bash ./clean.sh some-file

Expand Down
4 changes: 2 additions & 2 deletions examples/2-mocking/test/test_clean.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/bash

test_clean_works() {
mock rm --and exit-code 0
mock rm --with-args "somewhere/some-file" --and exit-code 0

bash ./clean.sh some-file

assert ${?} succeeded
}

test_clean_works_fails() {
mock rm --and exit-code 1
mock rm --with-args "somewhere/non-existing-file" --and exit-code 1

bash ./clean.sh non-existing-file

Expand Down
44 changes: 37 additions & 7 deletions src/mocking.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

action-echo() {
text="$@"
cat <<EOF
Expand All @@ -13,22 +12,51 @@ exit ${return_code}
EOF
}

validate-args() {
expected="$@"
cat <<EOF
args="\$@"
if [ "\${args}" != "${expected}" ]; then
cat <<OUT
Unexpected invocation for command 'some-command':
Got : <"\${args}">
Expected : <"${expected}">
OUT
exit 1
fi
EOF
}

mock() {
local executable=$1; shift
local actions=()
local actions_params=()
local action_count=0
local has_args_validation=false

while [[ $# -gt 0 ]]; do
key="$1"
local key="$1"

case ${key} in
--and)
actions[$action_count]="$2"
actions[$action_count]="action-$2"
actions_params[$action_count]="$3"
shift; shift
((action_count=action_count + 1))
;;
--with-args)
if [ ${has_args_validation} == false ]; then
actions[$action_count]="validate-args"
actions_params[$action_count]="$2"; shift
((action_count=action_count + 1))
has_args_validation=true
else
echo "Cannot expect more than 1 set of argument for an invocation, please use 'mock' multiple times"
return 1
fi
;;
esac
shift
done
Expand All @@ -52,9 +80,8 @@ mock() {

echo "#!/bin/bash" > ${invocation_file}

for i in ${!actions[@]};
do
action-${actions[$i]} ${actions_params[$i]} >> ${invocation_file}
for i in ${!actions[@]}; do
${actions[$i]} ${actions_params[$i]} >> ${invocation_file}
done

chmod +x ${invocation_file}
Expand All @@ -63,13 +90,16 @@ mock() {
_mock_handler() {
cat <<EOF
#!/bin/bash
args="\$@"
workspace=$1
invocation_index=\$(cat \${workspace}/invocation_index)
if [ \${invocation_index} -lt \$(cat \${workspace}/invocation_count) ]; then
echo \$((invocation_index + 1)) > \${workspace}/invocation_index
fi
\${workspace}/invocation_\${invocation_index}
\${workspace}/invocation_\${invocation_index} \${args}
EOF
}
23 changes: 23 additions & 0 deletions test/test_mocking_args_matching.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

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

some-command three two one > assertion_output
assert ${?} failed

expected_error=$(cat <<-EXP
Unexpected invocation for command 'some-command':
Got : <"three two one">
Expected : <"one two three">
EXP
)
assert "$(cat assertion_output)" equals "${expected_error}"
}

test_fails_if_2_with_args_argments_are_given() {
mock some-command --with-args "a" --with-args "b" > error
assert ${?} failed

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

0 comments on commit 1b80343

Please sign in to comment.