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

Contributing assert-extras.sh #13

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

Conversation

dansoton
Copy link

Further to my comment in Issue #10 I've created a contender for what assert-extras.sh may look like, at least as a starting point. I've also backed it with a new test-extras.sh file for regression tests.

I think it could be useful to accept so that people could opt into a broader set of assertions by sourcing assert-extras.sh as well as assert.sh. Those not wanting to use these would be unaffected as they wouldn't source this new file.

Also see the commit which updates assert.sh's _assert_fail() function to return an rc of 1 so that all assertions will return an rc 1 if the assertion fails which allows a program to check if an assertion has failed without having to wrap in a '_clean' and 'assert_end' block like the current tests appear to do.

…and is 1 and not 0. This allows us to check if an assertion has passed or not without having to wrap in a '_clean' and 'assert_end' block like the current tests.sh script does. See the good usage of this new behavior in new file 'test-extras.sh' in a subsequent commit.
@@ -1,7 +1,5 @@
#!/bin/bash

set -e
Copy link
Author

Choose a reason for hiding this comment

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

We remove this because failed assertions now return an rc of 1 which would stop this test script with set -e.

Dan Rivett added 2 commits March 17, 2015 19:54
…d in quotes. This breaks commands that pass in multiline input such as the following simple examples:

assert 'echo "this
is a multiline echo" | wc -l | tr -d "[[:space:]]"' "2"

assert 'echo -e "this\nis a multiline echo" | wc -l | tr -d "[[:space:]]"' "2"

With this commit, the following tests above works.
@@ -60,7 +58,7 @@ assert "_clean; skip_if false; assert_raises true; assert_end;" \
assert "_clean; skip_if bash -c 'exit 1'; assert_raises false; assert_end;" \
"all 0 tests passed."
# subshells and pipes can be used in skip as well (albeit escaped)
assert "_clean; skip_if 'cat /etc/passwd | grep \$(echo \$USER)';
assert "_clean; skip_if 'id | grep \$(echo \$USER)';
Copy link
Author

Choose a reason for hiding this comment

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

This also works on Mac OS X, as the user isn't guaranteed to be in /etc/passwd on Mac OS.

Dan Rivett added 2 commits March 24, 2015 22:05
… as: assert_contains, assert_matches, assert_success, assert_failure.
@lehmannro
Copy link
Owner

This totally slipped my inbox. If I ever find the time I'm going to cherry-pick the changes to assert.sh core. Did you ever put the extras somewhere else, so we could distribute this as an add-on? I'd like to keep the core repository relatively small and have helpers etc. somewhere else.

@asbjornu
Copy link

asbjornu commented Sep 5, 2016

@lehmannro I'd say that having methods such as assert_startswith is not exactly "extra", but rather basic functionality in any assertion framework. I was unable to perform any meaningful assertions without the functions provided by this PR's assert-extras.sh.

I would at least love to have easy access to assert-extras.sh and think the changes to assert.sh contribtued here are ace. @dansoton can you perhaps dissect this PR into two separate ones; one for the changes to assert.sh and one for assert-extras.sh?

echo "Warning: Cannot find gnu version of command '$cmd' ($gnu_cmd) on path." \
"Falling back to standard command" >&2
fi
echo "cmd"
Copy link

@joseluis joseluis Dec 17, 2016

Choose a reason for hiding this comment

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

This needs to be echo "$cmd".

Right now this doesn't work in linux. Because the command it tries to use is cmd instead of grep. Tests in test-extras.sh fails with:

test #1 "echo 'foo' | cmd -F 'foo'" failed:
	program terminated with code 127 instead of 0
(...)
7 of 11 assert_contains tests failed in 0.111s.
6 of 9 assert_matches tests failed in 0.093s.

@joseluis joseluis mentioned this pull request Dec 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants