|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "shpec" |
| 4 | +description: "Test your shell scripts" |
| 5 | +category: bash |
| 6 | +tags: [bash testing bdd] |
| 7 | +--- |
| 8 | + |
| 9 | +This package provides a command for testing your shell scripts. |
| 10 | + |
| 11 | +## Using shpec |
| 12 | +[shpec's repo](https://github.com/rylnd/shpec) itself is using shpec, so feel free to use it as an example. |
| 13 | +Here is the basic structure that you'll want: |
| 14 | + |
| 15 | + └── shpec |
| 16 | + └── an_example_shpec.sh |
| 17 | + └── another_shpec.sh |
| 18 | + |
| 19 | +Then to run your tests: |
| 20 | + |
| 21 | +{% highlight bash %} |
| 22 | +shpec [shpec_files] |
| 23 | +{% endhighlight %} |
| 24 | + |
| 25 | +If you'd like your tests to run automatically when they change, we recommend the [entr](http://entrproject.org/) utility: |
| 26 | + |
| 27 | +{% highlight bash %} |
| 28 | +find . -name "*_shpec.sh" | entr shpec |
| 29 | +{% endhighlight %} |
| 30 | + |
| 31 | +### Examples |
| 32 | +[shpec's own tests](https://github.com/rylnd/shpec/tree/master/shpec/shpec_shpec.sh) |
| 33 | +are a great place to start. For more examples, see the [wiki page](https://github.com/rylnd/shpec/wiki/Examples) |
| 34 | + |
| 35 | +### Matchers |
| 36 | +The general format is: |
| 37 | + |
| 38 | + assert matcher arguments |
| 39 | + |
| 40 | +where `matcher` is one of the following: |
| 41 | + |
| 42 | +#### Binary Matchers |
| 43 | +{% highlight bash %} |
| 44 | +equal # equality |
| 45 | +unequal # inequality |
| 46 | +gt # algebraic '>' |
| 47 | +lt # algebraic '<' |
| 48 | +match # regex match |
| 49 | +no_match # lack of regex match |
| 50 | +{% endhighlight %} |
| 51 | + |
| 52 | +#### Unary Matchers |
| 53 | +{% highlight bash %} |
| 54 | +present # string presence |
| 55 | +blank # string absence |
| 56 | +file_present # file presence |
| 57 | +file_absent # file absence |
| 58 | +symlink # tests a symlink's target |
| 59 | +test # evaluates a test string |
| 60 | +{% endhighlight %} |
| 61 | + |
| 62 | +#### Custom Matchers |
| 63 | +Custom matchers are loaded from `shpec/matchers/*.sh`. |
| 64 | + |
| 65 | +For example, here's how you'd create a `still_alive` matcher: |
| 66 | + |
| 67 | +{% highlight bash %} |
| 68 | +# in shpec/matchers/network.sh |
| 69 | +still_alive() { |
| 70 | + ping -oc1 "$1" > /dev/null 2>&1 |
| 71 | + assert equal "$?" 0 |
| 72 | +} |
| 73 | +{% endhighlight %} |
| 74 | + |
| 75 | +Then you can use that matcher like any other: |
| 76 | + |
| 77 | +{% highlight bash %} |
| 78 | +# in shpec/network_shpec.sh |
| 79 | +describe "my server" |
| 80 | + it "serves responses" |
| 81 | + assert still_alive "my-site.com" |
| 82 | + end |
| 83 | +end |
| 84 | +{% endhighlight %} |
| 85 | + |
| 86 | +## Installation |
| 87 | +You can either install with bpkg |
| 88 | +{% highlight bash %} |
| 89 | +$ bpkg install -g shpec |
| 90 | +{% endhighlight %} |
| 91 | +or with curl |
| 92 | +{% highlight bash %} |
| 93 | +sh -c "`curl -L https://raw.github.com/rylnd/shpec/master/install.sh`" |
| 94 | +{% endhighlight %} |
| 95 | + |
| 96 | +or with [antigen](https://github.com/zsh-users/antigen) for zsh by |
| 97 | +putting `antigen bundle rylnd/shpec` in your `.zshrc` |
| 98 | + |
| 99 | +## Links |
| 100 | +* [Source Code (GitHub)](https://github.com/rylnd/shpec); |
| 101 | +* Author: [Ryland Herrick](https://github.com/rylnd); |
0 commit comments