Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always verify mock arguments #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mat128
Copy link
Contributor

@mat128 mat128 commented Aug 29, 2018

Currently, mock arguments were verified inside the mock itself. Using a mock in a script not running in errexit mode (set -e) or hiding the failure meant silent failures and successful tests.

Signal the invocation failure through the mocks workspace and verify success during the verify_mocks phase.

Additionally, the build script has been adjusted to avoid having to add new lines at the end of every source file.

Finally, a "contains" assert has been added.

Currently, mock arguments were verified inside the mock itself. Using a
mock in a script not running in errexit mode (set -e) or hiding the
failure meant silent failures and successful tests.

Signal the invocation failure through the mocks workspace and verify
success during the verify_mocks phase.

Additionally, the build script has been adjusted to avoid having to add
new lines at the end of every source file.

Finally, a "contains" assert has been added.
Unexpected invocation for command 'some-command':
Got : <"\${args}">
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no really, this exit 1's goal was to break the test in case of wrong arguments. since you check that afterward now you can take that off.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, it breaks scripts that invoke it when they fail because of the exit code or when they are running in set -e mode. Removing it changes the contract with the test subject.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh of course, i thought the error file lookup was after a mock but it's at the end of the test. You're right sir ;)

src/asserts.sh Outdated
@@ -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 match pattern: <$2>"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be To contain pattern cause the assert says assert contains ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

EXP
)
assert "$(cat assertion_output)" equals "${expected_error}"
assert "${actual}" contains "${expected_error}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asserting how problems are displayed is an important part of the user experience of this library, by using contains instead of equals you seem to be hiding something, what is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stuff being hidden is caused by the test setup, we essentially have a fake test suite and the output contains much more than the errors themselves. By using contains we can focus on the important part for that test. Otherwise, this test will break when we change, for example, the test report header or footer.

Copy link
Contributor Author

@mat128 mat128 Oct 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exact text is the following:

Running Simple Bash Tests
-------------------------

verify_arguments_order.fails_when_verifying_arguments_even_if_exit_code_is_ignored...FAILED

=========================
FAIL: verify_arguments_order.fails_when_verifying_arguments_even_if_exit_code_is_ignored
-------- STDOUT ---------
Unexpected invocation for command 'some-command':
Got :      <"three trois">
Expected : <"two deux">
Unexpected invocation for command 'some-command':
Got :      <"two deux">
Expected : <"three trois">
-------- STDERR ---------

-------------------------

-------------------------
Ran 1 test

>>> FAILURE (1 problem) <<<

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i get the extent of this now, sorry for that.

The test you're replacing is kind of part of the doc on how to use mocking... yes the tests is the doc, so it's important that it's clear and understandable.

Therefore may i ask that the test remains there and that your test becomes a new one at the end of the file ?

And i will agree to the assert contains since this only changes stuff inside the mocking mechanism.

PS. (trailing new lines :) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

EXP
)
assert "$(cat assertion_output)" equals "${expected_error}"
assert "${actual}" contains "${expected_error}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i get the extent of this now, sorry for that.

The test you're replacing is kind of part of the doc on how to use mocking... yes the tests is the doc, so it's important that it's clear and understandable.

Therefore may i ask that the test remains there and that your test becomes a new one at the end of the file ?

And i will agree to the assert contains since this only changes stuff inside the mocking mechanism.

PS. (trailing new lines :) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants