urchin/readme.md

166 lines
5.6 KiB
Markdown
Raw Normal View History

2013-06-26 07:22:54 +00:00
__ _
__ ____________/ /_ (_)___
/ / / / ___/ ___/ __ \/ / __ \
/ /_/ / / / /__/ / / / / / / /
\__,_/_/ \___/_/ /_/_/_/ /_/
2012-10-08 14:01:29 +00:00
2013-06-19 08:20:13 +00:00
Urchin is a test framework for shell. It is implemented in
portable /bin/sh and should work on GNU/Linux, Mac OS X, and
other Unix platforms.
2012-10-08 14:01:29 +00:00
2012-10-11 18:57:10 +00:00
## Try it out
Urchin's tests are written in Urchin, so you can run them to see what Urchin
is like. Clone the repository
git clone git://github.com/tlevine/urchin.git
2012-10-11 18:57:10 +00:00
Run the tests
2013-07-02 08:43:59 +00:00
cd urchin
./urchin tests
2012-10-11 18:57:10 +00:00
The above command will run the tests in your system's default
2013-07-02 08:43:16 +00:00
shell, /bin/sh (on recent Ubuntu this is dash, but it could be
ksh or bash on other systems); to test urchin's cross-shell compatibility,
2013-07-02 08:43:16 +00:00
run this:
2013-06-27 18:44:19 +00:00
cd urchin
./cross-shell-tests
2012-10-11 18:57:10 +00:00
## Globally
2013-06-21 16:14:44 +00:00
Download Urchin like so (as root) (or use npm, below):
2012-10-08 14:01:29 +00:00
2013-06-21 16:14:44 +00:00
cd /usr/local/bin
wget https://raw.github.com/tlevine/urchin/master/urchin
2013-06-21 16:14:44 +00:00
chmod +x urchin
Can be installed with npm too:
npm install -g urchin
2012-10-08 14:01:29 +00:00
Now you can run it.
2012-10-10 19:51:06 +00:00
urchin <test directory>
2012-10-08 14:01:29 +00:00
Run `urchin -h` to get command-line help.
2012-10-08 14:01:29 +00:00
## Writing tests
2012-10-08 14:13:43 +00:00
Make a root directory for your tests. Inside it, put executable files that
2012-10-08 14:16:49 +00:00
exit `0` on success and something else on fail. Non-executable files and hidden
files (dotfiles) are ignored, so you can store fixtures right next to your
tests. Run urchin from inside the tests directory.
2012-10-08 14:01:29 +00:00
2013-06-19 08:20:13 +00:00
Urchin only cares about the exit status, so you can actually write your tests
2012-10-10 16:53:03 +00:00
in any language, not just shell.
2012-10-08 14:01:29 +00:00
## More about writing tests
2012-10-04 11:22:44 +00:00
Tests are organized recursively in directories, where the names of the files
and directories have special meanings.
tests/
setup
2012-10-10 19:47:21 +00:00
setup_dir
2012-10-04 11:22:44 +00:00
bar/
setup
test_that_something_works
2012-10-08 13:54:59 +00:00
teardown
2012-10-04 11:22:44 +00:00
baz/
jack-in-the-box/
setup
test_that_something_works
teardown
cat-in-the-box/
2012-10-08 13:54:59 +00:00
fixtures/
thingy.pdf
test_thingy
teardown
2012-10-04 11:22:44 +00:00
Directories are processed in a depth-first order. When a particular directory
2012-10-10 19:47:21 +00:00
is processed, `setup_dir` is run before everything else in the directory, including
2013-07-02 08:46:21 +00:00
subdirectories. `teardown_dir` is run after everything else in the directory.
2012-10-04 11:22:44 +00:00
2012-10-10 19:47:21 +00:00
A directory's `setup` file, if it exists, is run right before each test file
within the particular directory, and the `teardown` file is run right after.
2012-10-04 11:22:44 +00:00
2012-10-08 14:16:49 +00:00
Files are only run if they are executable, and files beginning with `.` are
2012-10-08 14:24:32 +00:00
ignored. Thus, fixtures and libraries can be included sloppily within the test
directory tree. The test passes if the file exits 0; otherwise, it fails.
2013-10-13 16:48:42 +00:00
### Writing cross-shell compatibility tests for testing shell code
While you could write your test scripts to explicitly invoke the functionality
to test with various shells, Urchin facilitates a more flexible approach.
The specific approach depends on your test scenario:
* (a) Your test scripts _invoke_ scripts containing portable shell code.
* (b) Your scripts _source_ scripts containing portable shell code.
#### (a) Cross-shell tests with test scripts that _invoke_ shell scripts
2015-07-05 17:39:38 +00:00
First, consider using [shall](https://github.com/mklement0/shall).
#!/usr/bin/env shall
echo This is a test file.
Alternatively, you can use urchin's built-in recognition of the
`TEST_SHELL` environment variable.
In your test scripts, invoke the shell scripts to test via the shell
specified in environment variable `TEST_SHELL` rather than directly;
e.g.: `$TEST_SHELL ../foo bar` (rather than just `../foo bar`).
Note that if you alsow want your test scripts to work when run directly,
outside of Urchin, be sure to target scripts that happen to be in the
current directory with prefix `./`; e.g., `$TEST_SHELL ./baz`
(rather than `$TEST_SHELL baz`).
Then, on invocation of Urchin, prepend a definition of environment variable
`TEST_SHELL` specifying the shell to test with, e.g.: `TEST_SHELL=zsh urchin ./tests`.
To test with multiple shells in sequence, use something like:
for shell in sh bash ksh zsh; do
TEST_SHELL=$shell urchin ./tests
done
If `TEST_SHELL` has no value, Urchin defines it as `/bin/sh`, so the test
scripts can rely on `$TEST_SHELL` always containing a value.
#### (b) Cross-shell tests with test scripts that _source_ shell scripts
If you _source_ shell code in your test scripts, it is the test scripts
themselves that must be run with the shell specified.
To that end, Urchin supports the `-s <shell>` option, which instructs
Urchin to invoke the test scripts with the specified shell; e.g., `-s bash`.
2014-10-18 14:41:22 +00:00
(In addition, Urchin sets environment variable `TEST_SHELL` to the specified
shell.)
Note that only test scripts that either have no shebang line at all or
have shebang line `#!/bin/sh` are invoked with the specified shell.
This allows non-shell test scripts or test scripts for specific
shells to coexist with those whose invocation should be controlled by `-s`.
To test with multiple shells in sequence, use something like:
for shell in sh bash ksh zsh; do
urchin -s $shell ./tests
done
<!--
2014-11-05 17:47:36 +00:00
#### (c) Cross shell tests with `urchin -x` (experimental)
2014-11-06 00:52:34 +00:00
If you run urchin with the `-x` flag, it will be as if you ran
2014-11-05 17:26:01 +00:00
`$TEST_SHELL`. Unless `$TEST_SHELL` isn't set, in which case it'll
be as if you ran `/bin/sh`. Putting this in she shebang line might
eventually work out to be a cleaner way of doing cross-shell testing.
2014-11-05 17:47:36 +00:00
#!/usr/bin/env urchin -x
2014-11-05 17:26:01 +00:00
test a = a
2014-11-06 00:52:34 +00:00
It might make sense if you do this.
export TEST_SHELL=zsh && urchin -x
export TEST_SHELL=bash && urchin -x
-->
2013-10-13 16:48:42 +00:00
## Alternatives to Urchin
Alternatives to Urchin are discussed in
[this blog post](https://blog.scraperwiki.com/2012/12/how-to-test-shell-scripts/).