diff --git a/src/aggregate.sh b/src/aggregate.sh index 24f2639..12fb2fd 100644 --- a/src/aggregate.sh +++ b/src/aggregate.sh @@ -3,9 +3,14 @@ set -eu src=$(dirname $0) -cat ${src}/header.sh -cat ${src}/utils.sh -cat ${src}/asserts.sh -cat ${src}/mocking.sh -cat ${src}/cli.sh -cat ${src}/test-runner.sh +include() { + cat $1 + printf '\n' +} + +include ${src}/header.sh +include ${src}/utils.sh +include ${src}/asserts.sh +include ${src}/mocking.sh +include ${src}/cli.sh +include ${src}/test-runner.sh diff --git a/src/asserts.sh b/src/asserts.sh index 82c3729..1f1b8a9 100644 --- a/src/asserts.sh +++ b/src/asserts.sh @@ -18,3 +18,7 @@ _assert_succeeded() { _assert_failed() { test ${1} -ne 0 || assertion_failed "Expected failure exit code\nGot: <$1>" } + +_assert_contains() { + echo "$1" | grep -q "$2" || assertion_failed "Expected: <$1>\nTo contain pattern: <$2>" +} diff --git a/src/mocking.sh b/src/mocking.sh index 61eb52c..ca2a996 100644 --- a/src/mocking.sh +++ b/src/mocking.sh @@ -39,13 +39,12 @@ validate-args() { args="\$@" if [ "\${args}" != "${expected}" ]; then - - cat < \$0_error < Expected : <"${expected}"> OUT - exit 1 + exit 1 # Kept for historical reasons. Proper usage would be mock xyz --with-args "arg1 arg2" --and exit-code 1. fi EOF } @@ -159,5 +158,9 @@ _verify_mocks() { assertion_failed "Command '${command}' was expected to be called $(_format_count ${expected_calls} "time")\nCalled : $(_format_count ${call_count} "time")" fi done + + for invocation in $(find ${mock} -maxdepth 1 -name "invocation_*_error" 2>/dev/null || true); do + assertion_failed "$(cat $invocation)" + done done } diff --git a/test-fixtures/mocking-args-strict/src/code.sh b/test-fixtures/mocking-args-strict/src/code.sh new file mode 100755 index 0000000..6b8ef97 --- /dev/null +++ b/test-fixtures/mocking-args-strict/src/code.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# N.B. no `set -e` here! + +some-command one un +some-command three trois || true # Failure here doesnt matter to the actual code. +some-command two deux + +exit 0 \ No newline at end of file diff --git a/test-fixtures/mocking-args-strict/test/test_verify_arguments_order.sh b/test-fixtures/mocking-args-strict/test/test_verify_arguments_order.sh new file mode 100644 index 0000000..e374c8e --- /dev/null +++ b/test-fixtures/mocking-args-strict/test/test_verify_arguments_order.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +test_fails_when_verifying_arguments_even_if_exit_code_is_ignored() { + mock some-command --with-args "one un" --once + mock some-command --with-args "two deux" --once + mock some-command --with-args "three trois" --once + + ./code.sh +} \ No newline at end of file diff --git a/test/test_assert.sh b/test/test_assert.sh index 38e7125..5ae5622 100644 --- a/test/test_assert.sh +++ b/test/test_assert.sh @@ -31,7 +31,7 @@ test_assert_equals_with_strings_passing() { test_assert_equals_with_strings_failing() { assert "no" equals "yes" > assertion_output result=${?} - rm .assertion_error # The test runner would think the test failed + _hide_assertion_failure_from_test_runner assertion_error=$(cat assertion_output) @@ -49,3 +49,31 @@ test_assert_multiworks_string_works() { assert ${?} succeeded } + +test_assert_contains() { + assert "hello world" contains "^hello world$" + assert "hello world" contains "^hello" + assert "hello world" contains "world$" + assert "hello world" contains "w" +} + +test_assert_contains_fails_with_proper_error_message() { + assert "abc" contains "z$" > assertion_output + result=${?} + _hide_assertion_failure_from_test_runner + + assertion_error=$(cat assertion_output) + + expected_error=$(cat <<-EXP +Expected: +To contain pattern: +EXP +) + assert ${result} failed + assert "${assertion_error}" equals "${expected_error}" +} + +_hide_assertion_failure_from_test_runner() { + # The test runner would think the test failed + rm .assertion_error +} diff --git a/test/test_mocking_args_matching.sh b/test/test_mocking_args_matching.sh index e3cfeb0..eae385d 100644 --- a/test/test_mocking_args_matching.sh +++ b/test/test_mocking_args_matching.sh @@ -3,7 +3,7 @@ test_fails_if_given_arguments_isnt_right() { mock some-command --with-args "one two three" - some-command three two one > assertion_output + some-command three two one assert ${?} failed expected_error=$(cat <<-EXP @@ -12,10 +12,33 @@ Got : <"three two one"> Expected : <"one two three"> EXP ) - assert "$(cat assertion_output)" equals "${expected_error}" + error_filename=$(find $mocks -name 'invocation_*_error') + assert "$(cat $error_filename)" equals "${expected_error}" + + # Hide failure from runner. + rm $error_filename +} + +test_fails_when_verifying_arguments_even_when_exit_code_is_ignored() { + cp -aR ${TEST_ROOT_DIR}/../test-fixtures/mocking-args-strict/* . + + unset RUN_SINGLE_TEST + actual=$(${TEST_ROOT_DIR}/../target/sbtest.sh verify_arguments_order) + assert ${?} failed + + expected_error=$(cat <<-EXP +Unexpected invocation for command 'some-command': +Got : <"three trois"> +Expected : <"two deux"> +Unexpected invocation for command 'some-command': +Got : <"two deux"> +Expected : <"three trois"> +EXP +) + assert "${actual}" contains "${expected_error}" } -test_fails_if_2_with_args_argments_are_given() { +test_fails_if_2_with_args_arguments_are_given() { mock some-command --with-args "a" --with-args "b" > error assert ${?} failed