├── .gitattributes ├── .travis.yml ├── CONDUCT.md ├── LICENSE ├── README.md ├── bin └── bats ├── install.sh ├── libexec ├── bats ├── bats-exec-suite ├── bats-exec-test ├── bats-format-tap-stream └── bats-preprocess ├── man ├── Makefile ├── README.md ├── bats.1 ├── bats.1.ronn ├── bats.7 └── bats.7.ronn ├── package.json └── test ├── bats.bats ├── fixtures ├── bats │ ├── dos_line.bats │ ├── empty.bats │ ├── environment.bats │ ├── failing.bats │ ├── failing_and_passing.bats │ ├── failing_helper.bats │ ├── failing_setup.bats │ ├── failing_teardown.bats │ ├── intact.bats │ ├── invalid_tap.bats │ ├── load.bats │ ├── loop_keep_IFS.bats │ ├── output.bats │ ├── passing.bats │ ├── passing_and_failing.bats │ ├── passing_and_skipping.bats │ ├── passing_failing_and_skipping.bats │ ├── setup.bats │ ├── single_line.bats │ ├── skipped.bats │ ├── teardown.bats │ ├── test_helper.bash │ └── without_trailing_newline.bats └── suite │ ├── empty │ └── .gitkeep │ ├── multiple │ ├── a.bats │ └── b.bats │ └── single │ └── test.bats ├── suite.bats ├── test_helper.bash └── tmp └── .gitignore /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.sh eol=lf 3 | libexec/* eol=lf 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | script: bin/bats --tap test 3 | notifications: 4 | email: 5 | on_success: never 6 | -------------------------------------------------------------------------------- /CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting one of the project maintainers listed below. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Project Maintainers 69 | 70 | * Sam Stephenson <> 71 | 72 | ## Attribution 73 | 74 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 75 | available at [http://contributor-covenant.org/version/1/4][version] 76 | 77 | [homepage]: http://contributor-covenant.org 78 | [version]: http://contributor-covenant.org/version/1/4/ 79 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Sam Stephenson 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bats: Bash Automated Testing System 2 | 3 | Bats is a [TAP](http://testanything.org)-compliant testing framework 4 | for Bash. It provides a simple way to verify that the UNIX programs 5 | you write behave as expected. 6 | 7 | A Bats test file is a Bash script with special syntax for defining 8 | test cases. Under the hood, each test case is just a function with a 9 | description. 10 | 11 | ```bash 12 | #!/usr/bin/env bats 13 | 14 | @test "addition using bc" { 15 | result="$(echo 2+2 | bc)" 16 | [ "$result" -eq 4 ] 17 | } 18 | 19 | @test "addition using dc" { 20 | result="$(echo 2 2+p | dc)" 21 | [ "$result" -eq 4 ] 22 | } 23 | ``` 24 | 25 | Bats is most useful when testing software written in Bash, but you can 26 | use it to test any UNIX program. 27 | 28 | Test cases consist of standard shell commands. Bats makes use of 29 | Bash's `errexit` (`set -e`) option when running test cases. If every 30 | command in the test case exits with a `0` status code (success), the 31 | test passes. In this way, each line is an assertion of truth. 32 | 33 | 34 | ## Running tests 35 | 36 | To run your tests, invoke the `bats` interpreter with a path to a test 37 | file. The file's test cases are run sequentially and in isolation. If 38 | all the test cases pass, `bats` exits with a `0` status code. If there 39 | are any failures, `bats` exits with a `1` status code. 40 | 41 | When you run Bats from a terminal, you'll see output as each test is 42 | performed, with a check-mark next to the test's name if it passes or 43 | an "X" if it fails. 44 | 45 | $ bats addition.bats 46 | ✓ addition using bc 47 | ✓ addition using dc 48 | 49 | 2 tests, 0 failures 50 | 51 | If Bats is not connected to a terminal—in other words, if you 52 | run it from a continuous integration system, or redirect its output to 53 | a file—the results are displayed in human-readable, machine-parsable 54 | [TAP format](http://testanything.org). 55 | 56 | You can force TAP output from a terminal by invoking Bats with the 57 | `--tap` option. 58 | 59 | $ bats --tap addition.bats 60 | 1..2 61 | ok 1 addition using bc 62 | ok 2 addition using dc 63 | 64 | ### Test suites 65 | 66 | You can invoke the `bats` interpreter with multiple test file 67 | arguments, or with a path to a directory containing multiple `.bats` 68 | files. Bats will run each test file individually and aggregate the 69 | results. If any test case fails, `bats` exits with a `1` status code. 70 | 71 | 72 | ## Writing tests 73 | 74 | Each Bats test file is evaluated _n+1_ times, where _n_ is the number of 75 | test cases in the file. The first run counts the number of test cases, 76 | then iterates over the test cases and executes each one in its own 77 | process. 78 | 79 | For more details about how Bats evaluates test files, see 80 | [Bats Evaluation Process](https://github.com/sstephenson/bats/wiki/Bats-Evaluation-Process) 81 | on the wiki. 82 | 83 | ### `run`: Test other commands 84 | 85 | Many Bats tests need to run a command and then make assertions about 86 | its exit status and output. Bats includes a `run` helper that invokes 87 | its arguments as a command, saves the exit status and output into 88 | special global variables, and then returns with a `0` status code so 89 | you can continue to make assertions in your test case. 90 | 91 | For example, let's say you're testing that the `foo` command, when 92 | passed a nonexistent filename, exits with a `1` status code and prints 93 | an error message. 94 | 95 | ```bash 96 | @test "invoking foo with a nonexistent file prints an error" { 97 | run foo nonexistent_filename 98 | [ "$status" -eq 1 ] 99 | [ "$output" = "foo: no such file 'nonexistent_filename'" ] 100 | } 101 | ``` 102 | 103 | The `$status` variable contains the status code of the command, and 104 | the `$output` variable contains the combined contents of the command's 105 | standard output and standard error streams. 106 | 107 | A third special variable, the `$lines` array, is available for easily 108 | accessing individual lines of output. For example, if you want to test 109 | that invoking `foo` without any arguments prints usage information on 110 | the first line: 111 | 112 | ```bash 113 | @test "invoking foo without arguments prints usage" { 114 | run foo 115 | [ "$status" -eq 1 ] 116 | [ "${lines[0]}" = "usage: foo " ] 117 | } 118 | ``` 119 | 120 | ### `load`: Share common code 121 | 122 | You may want to share common code across multiple test files. Bats 123 | includes a convenient `load` command for sourcing a Bash source file 124 | relative to the location of the current test file. For example, if you 125 | have a Bats test in `test/foo.bats`, the command 126 | 127 | ```bash 128 | load test_helper 129 | ``` 130 | 131 | will source the script `test/test_helper.bash` in your test file. This 132 | can be useful for sharing functions to set up your environment or load 133 | fixtures. 134 | 135 | ### `skip`: Easily skip tests 136 | 137 | Tests can be skipped by using the `skip` command at the point in a 138 | test you wish to skip. 139 | 140 | ```bash 141 | @test "A test I don't want to execute for now" { 142 | skip 143 | run foo 144 | [ "$status" -eq 0 ] 145 | } 146 | ``` 147 | 148 | Optionally, you may include a reason for skipping: 149 | 150 | ```bash 151 | @test "A test I don't want to execute for now" { 152 | skip "This command will return zero soon, but not now" 153 | run foo 154 | [ "$status" -eq 0 ] 155 | } 156 | ``` 157 | 158 | Or you can skip conditionally: 159 | 160 | ```bash 161 | @test "A test which should run" { 162 | if [ foo != bar ]; then 163 | skip "foo isn't bar" 164 | fi 165 | 166 | run foo 167 | [ "$status" -eq 0 ] 168 | } 169 | ``` 170 | 171 | ### `setup` and `teardown`: Pre- and post-test hooks 172 | 173 | You can define special `setup` and `teardown` functions, which run 174 | before and after each test case, respectively. Use these to load 175 | fixtures, set up your environment, and clean up when you're done. 176 | 177 | ### Code outside of test cases 178 | 179 | You can include code in your test file outside of `@test` functions. 180 | For example, this may be useful if you want to check for dependencies 181 | and fail immediately if they're not present. However, any output that 182 | you print in code outside of `@test`, `setup` or `teardown` functions 183 | must be redirected to `stderr` (`>&2`). Otherwise, the output may 184 | cause Bats to fail by polluting the TAP stream on `stdout`. 185 | 186 | ### Special variables 187 | 188 | There are several global variables you can use to introspect on Bats 189 | tests: 190 | 191 | * `$BATS_TEST_FILENAME` is the fully expanded path to the Bats test 192 | file. 193 | * `$BATS_TEST_DIRNAME` is the directory in which the Bats test file is 194 | located. 195 | * `$BATS_TEST_NAMES` is an array of function names for each test case. 196 | * `$BATS_TEST_NAME` is the name of the function containing the current 197 | test case. 198 | * `$BATS_TEST_DESCRIPTION` is the description of the current test 199 | case. 200 | * `$BATS_TEST_NUMBER` is the (1-based) index of the current test case 201 | in the test file. 202 | * `$BATS_TMPDIR` is the location to a directory that may be used to 203 | store temporary files. 204 | 205 | 206 | ## Installing Bats from source 207 | 208 | Check out a copy of the Bats repository. Then, either add the Bats 209 | `bin` directory to your `$PATH`, or run the provided `install.sh` 210 | command with the location to the prefix in which you want to install 211 | Bats. For example, to install Bats into `/usr/local`, 212 | 213 | $ git clone https://github.com/sstephenson/bats.git 214 | $ cd bats 215 | $ ./install.sh /usr/local 216 | 217 | Note that you may need to run `install.sh` with `sudo` if you do not 218 | have permission to write to the installation prefix. 219 | 220 | 221 | ## Support 222 | 223 | The Bats source code repository is [hosted on 224 | GitHub](https://github.com/sstephenson/bats). There you can file bugs 225 | on the issue tracker or submit tested pull requests for review. 226 | 227 | For real-world examples from open-source projects using Bats, see 228 | [Projects Using Bats](https://github.com/sstephenson/bats/wiki/Projects-Using-Bats) 229 | on the wiki. 230 | 231 | To learn how to set up your editor for Bats syntax highlighting, see 232 | [Syntax Highlighting](https://github.com/sstephenson/bats/wiki/Syntax-Highlighting) 233 | on the wiki. 234 | 235 | 236 | ## Version history 237 | 238 | *0.4.0* (August 13, 2014) 239 | 240 | * Improved the display of failing test cases. Bats now shows the 241 | source code of failing test lines, along with full stack traces 242 | including function names, filenames, and line numbers. 243 | * Improved the display of the pretty-printed test summary line to 244 | include the number of skipped tests, if any. 245 | * Improved the speed of the preprocessor, dramatically shortening test 246 | and suite startup times. 247 | * Added support for absolute pathnames to the `load` helper. 248 | * Added support for single-line `@test` definitions. 249 | * Added bats(1) and bats(7) manual pages. 250 | * Modified the `bats` command to default to TAP output when the `$CI` 251 | variable is set, to better support environments such as Travis CI. 252 | 253 | *0.3.1* (October 28, 2013) 254 | 255 | * Fixed an incompatibility with the pretty formatter in certain 256 | environments such as tmux. 257 | * Fixed a bug where the pretty formatter would crash if the first line 258 | of a test file's output was invalid TAP. 259 | 260 | *0.3.0* (October 21, 2013) 261 | 262 | * Improved formatting for tests run from a terminal. Failing tests 263 | are now colored in red, and the total number of failing tests is 264 | displayed at the end of the test run. When Bats is not connected to 265 | a terminal (e.g. in CI runs), or when invoked with the `--tap` flag, 266 | output is displayed in standard TAP format. 267 | * Added the ability to skip tests using the `skip` command. 268 | * Added a message to failing test case output indicating the file and 269 | line number of the statement that caused the test to fail. 270 | * Added "ad-hoc" test suite support. You can now invoke `bats` with 271 | multiple filename or directory arguments to run all the specified 272 | tests in aggregate. 273 | * Added support for test files with Windows line endings. 274 | * Fixed regular expression warnings from certain versions of Bash. 275 | * Fixed a bug running tests containing lines that begin with `-e`. 276 | 277 | *0.2.0* (November 16, 2012) 278 | 279 | * Added test suite support. The `bats` command accepts a directory 280 | name containing multiple test files to be run in aggregate. 281 | * Added the ability to count the number of test cases in a file or 282 | suite by passing the `-c` flag to `bats`. 283 | * Preprocessed sources are cached between test case runs in the same 284 | file for better performance. 285 | 286 | *0.1.0* (December 30, 2011) 287 | 288 | * Initial public release. 289 | 290 | --- 291 | 292 | © 2014 Sam Stephenson. Bats is released under an MIT-style license; 293 | see `LICENSE` for details. 294 | -------------------------------------------------------------------------------- /bin/bats: -------------------------------------------------------------------------------- 1 | ../libexec/bats -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | resolve_link() { 5 | $(type -p greadlink readlink | head -1) "$1" 6 | } 7 | 8 | abs_dirname() { 9 | local cwd="$(pwd)" 10 | local path="$1" 11 | 12 | while [ -n "$path" ]; do 13 | cd "${path%/*}" 14 | local name="${path##*/}" 15 | path="$(resolve_link "$name" || true)" 16 | done 17 | 18 | pwd 19 | cd "$cwd" 20 | } 21 | 22 | PREFIX="$1" 23 | if [ -z "$1" ]; then 24 | { echo "usage: $0 " 25 | echo " e.g. $0 /usr/local" 26 | } >&2 27 | exit 1 28 | fi 29 | 30 | BATS_ROOT="$(abs_dirname "$0")" 31 | mkdir -p "$PREFIX"/{bin,libexec,share/man/man{1,7}} 32 | cp -R "$BATS_ROOT"/bin/* "$PREFIX"/bin 33 | cp -R "$BATS_ROOT"/libexec/* "$PREFIX"/libexec 34 | cp "$BATS_ROOT"/man/bats.1 "$PREFIX"/share/man/man1 35 | cp "$BATS_ROOT"/man/bats.7 "$PREFIX"/share/man/man7 36 | 37 | echo "Installed Bats to $PREFIX/bin/bats" 38 | -------------------------------------------------------------------------------- /libexec/bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | version() { 5 | echo "Bats 0.4.0" 6 | } 7 | 8 | usage() { 9 | version 10 | echo "Usage: bats [-c] [-p | -t] [ ...]" 11 | } 12 | 13 | help() { 14 | usage 15 | echo 16 | echo " is the path to a Bats test file, or the path to a directory" 17 | echo " containing Bats test files." 18 | echo 19 | echo " -c, --count Count the number of test cases without running any tests" 20 | echo " -h, --help Display this help message" 21 | echo " -p, --pretty Show results in pretty format (default for terminals)" 22 | echo " -t, --tap Show results in TAP format" 23 | echo " -v, --version Display the version number" 24 | echo 25 | echo " For more information, see https://github.com/sstephenson/bats" 26 | echo 27 | } 28 | 29 | resolve_link() { 30 | $(type -p greadlink readlink | head -1) "$1" 31 | } 32 | 33 | abs_dirname() { 34 | local cwd="$(pwd)" 35 | local path="$1" 36 | 37 | while [ -n "$path" ]; do 38 | cd "${path%/*}" 39 | local name="${path##*/}" 40 | path="$(resolve_link "$name" || true)" 41 | done 42 | 43 | pwd 44 | cd "$cwd" 45 | } 46 | 47 | expand_path() { 48 | { cd "$(dirname "$1")" 2>/dev/null 49 | local dirname="$PWD" 50 | cd "$OLDPWD" 51 | echo "$dirname/$(basename "$1")" 52 | } || echo "$1" 53 | } 54 | 55 | BATS_LIBEXEC="$(abs_dirname "$0")" 56 | export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")" 57 | export BATS_CWD="$(abs_dirname .)" 58 | export PATH="$BATS_LIBEXEC:$PATH" 59 | 60 | options=() 61 | arguments=() 62 | for arg in "$@"; do 63 | if [ "${arg:0:1}" = "-" ]; then 64 | if [ "${arg:1:1}" = "-" ]; then 65 | options[${#options[*]}]="${arg:2}" 66 | else 67 | index=1 68 | while option="${arg:$index:1}"; do 69 | [ -n "$option" ] || break 70 | options[${#options[*]}]="$option" 71 | let index+=1 72 | done 73 | fi 74 | else 75 | arguments[${#arguments[*]}]="$arg" 76 | fi 77 | done 78 | 79 | unset count_flag pretty 80 | [ -t 0 ] && [ -t 1 ] && pretty="1" 81 | [ -n "$CI" ] && pretty="" 82 | 83 | for option in "${options[@]}"; do 84 | case "$option" in 85 | "h" | "help" ) 86 | help 87 | exit 0 88 | ;; 89 | "v" | "version" ) 90 | version 91 | exit 0 92 | ;; 93 | "c" | "count" ) 94 | count_flag="-c" 95 | ;; 96 | "t" | "tap" ) 97 | pretty="" 98 | ;; 99 | "p" | "pretty" ) 100 | pretty="1" 101 | ;; 102 | * ) 103 | usage >&2 104 | exit 1 105 | ;; 106 | esac 107 | done 108 | 109 | if [ "${#arguments[@]}" -eq 0 ]; then 110 | usage >&2 111 | exit 1 112 | fi 113 | 114 | filenames=() 115 | for filename in "${arguments[@]}"; do 116 | if [ -d "$filename" ]; then 117 | shopt -s nullglob 118 | for suite_filename in "$(expand_path "$filename")"/*.bats; do 119 | filenames["${#filenames[@]}"]="$suite_filename" 120 | done 121 | shopt -u nullglob 122 | else 123 | filenames["${#filenames[@]}"]="$(expand_path "$filename")" 124 | fi 125 | done 126 | 127 | if [ "${#filenames[@]}" -eq 1 ]; then 128 | command="bats-exec-test" 129 | else 130 | command="bats-exec-suite" 131 | fi 132 | 133 | if [ -n "$pretty" ]; then 134 | extended_syntax_flag="-x" 135 | formatter="bats-format-tap-stream" 136 | else 137 | extended_syntax_flag="" 138 | formatter="cat" 139 | fi 140 | 141 | set -o pipefail execfail 142 | exec "$command" $count_flag $extended_syntax_flag "${filenames[@]}" | "$formatter" 143 | -------------------------------------------------------------------------------- /libexec/bats-exec-suite: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | count_only_flag="" 5 | if [ "$1" = "-c" ]; then 6 | count_only_flag=1 7 | shift 8 | fi 9 | 10 | extended_syntax_flag="" 11 | if [ "$1" = "-x" ]; then 12 | extended_syntax_flag="-x" 13 | shift 14 | fi 15 | 16 | trap "kill 0; exit 1" int 17 | 18 | count=0 19 | for filename in "$@"; do 20 | let count+="$(bats-exec-test -c "$filename")" 21 | done 22 | 23 | if [ -n "$count_only_flag" ]; then 24 | echo "$count" 25 | exit 26 | fi 27 | 28 | echo "1..$count" 29 | status=0 30 | offset=0 31 | for filename in "$@"; do 32 | index=0 33 | { 34 | IFS= read -r # 1..n 35 | while IFS= read -r line; do 36 | case "$line" in 37 | "begin "* ) 38 | let index+=1 39 | echo "${line/ $index / $(($offset + $index)) }" 40 | ;; 41 | "ok "* | "not ok "* ) 42 | [ -n "$extended_syntax_flag" ] || let index+=1 43 | echo "${line/ $index / $(($offset + $index)) }" 44 | [ "${line:0:6}" != "not ok" ] || status=1 45 | ;; 46 | * ) 47 | echo "$line" 48 | ;; 49 | esac 50 | done 51 | } < <( bats-exec-test $extended_syntax_flag "$filename" ) 52 | offset=$(($offset + $index)) 53 | done 54 | 55 | exit "$status" 56 | -------------------------------------------------------------------------------- /libexec/bats-exec-test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | set -E 4 | set -T 5 | 6 | BATS_COUNT_ONLY="" 7 | if [ "$1" = "-c" ]; then 8 | BATS_COUNT_ONLY=1 9 | shift 10 | fi 11 | 12 | BATS_EXTENDED_SYNTAX="" 13 | if [ "$1" = "-x" ]; then 14 | BATS_EXTENDED_SYNTAX="$1" 15 | shift 16 | fi 17 | 18 | BATS_TEST_FILENAME="$1" 19 | if [ -z "$BATS_TEST_FILENAME" ]; then 20 | echo "usage: bats-exec " >&2 21 | exit 1 22 | elif [ ! -f "$BATS_TEST_FILENAME" ]; then 23 | echo "bats: $BATS_TEST_FILENAME does not exist" >&2 24 | exit 1 25 | else 26 | shift 27 | fi 28 | 29 | BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")" 30 | BATS_TEST_NAMES=() 31 | 32 | load() { 33 | local name="$1" 34 | local filename 35 | 36 | if [ "${name:0:1}" = "/" ]; then 37 | filename="${name}" 38 | else 39 | filename="$BATS_TEST_DIRNAME/${name}.bash" 40 | fi 41 | 42 | [ -f "$filename" ] || { 43 | echo "bats: $filename does not exist" >&2 44 | exit 1 45 | } 46 | 47 | source "${filename}" 48 | } 49 | 50 | run() { 51 | local e E T oldIFS 52 | [[ ! "$-" =~ e ]] || e=1 53 | [[ ! "$-" =~ E ]] || E=1 54 | [[ ! "$-" =~ T ]] || T=1 55 | set +e 56 | set +E 57 | set +T 58 | output="$("$@" 2>&1)" 59 | status="$?" 60 | oldIFS=$IFS 61 | IFS=$'\n' lines=($output) 62 | [ -z "$e" ] || set -e 63 | [ -z "$E" ] || set -E 64 | [ -z "$T" ] || set -T 65 | IFS=$oldIFS 66 | } 67 | 68 | setup() { 69 | true 70 | } 71 | 72 | teardown() { 73 | true 74 | } 75 | 76 | skip() { 77 | BATS_TEST_SKIPPED=${1:-1} 78 | BATS_TEST_COMPLETED=1 79 | exit 0 80 | } 81 | 82 | bats_test_begin() { 83 | BATS_TEST_DESCRIPTION="$1" 84 | if [ -n "$BATS_EXTENDED_SYNTAX" ]; then 85 | echo "begin $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 86 | fi 87 | setup 88 | } 89 | 90 | bats_test_function() { 91 | local test_name="$1" 92 | BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name" 93 | } 94 | 95 | bats_capture_stack_trace() { 96 | BATS_PREVIOUS_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" ) 97 | BATS_CURRENT_STACK_TRACE=() 98 | 99 | local test_pattern=" $BATS_TEST_NAME $BATS_TEST_SOURCE" 100 | local setup_pattern=" setup $BATS_TEST_SOURCE" 101 | local teardown_pattern=" teardown $BATS_TEST_SOURCE" 102 | 103 | local frame 104 | local index=1 105 | 106 | while frame="$(caller "$index")"; do 107 | BATS_CURRENT_STACK_TRACE["${#BATS_CURRENT_STACK_TRACE[@]}"]="$frame" 108 | if [[ "$frame" = *"$test_pattern" || \ 109 | "$frame" = *"$setup_pattern" || \ 110 | "$frame" = *"$teardown_pattern" ]]; then 111 | break 112 | else 113 | let index+=1 114 | fi 115 | done 116 | 117 | BATS_SOURCE="$(bats_frame_filename "${BATS_CURRENT_STACK_TRACE[0]}")" 118 | BATS_LINENO="$(bats_frame_lineno "${BATS_CURRENT_STACK_TRACE[0]}")" 119 | } 120 | 121 | bats_print_stack_trace() { 122 | local frame 123 | local index=1 124 | local count="${#@}" 125 | 126 | for frame in "$@"; do 127 | local filename="$(bats_trim_filename "$(bats_frame_filename "$frame")")" 128 | local lineno="$(bats_frame_lineno "$frame")" 129 | 130 | if [ $index -eq 1 ]; then 131 | echo -n "# (" 132 | else 133 | echo -n "# " 134 | fi 135 | 136 | local fn="$(bats_frame_function "$frame")" 137 | if [ "$fn" != "$BATS_TEST_NAME" ]; then 138 | echo -n "from function \`$fn' " 139 | fi 140 | 141 | if [ $index -eq $count ]; then 142 | echo "in test file $filename, line $lineno)" 143 | else 144 | echo "in file $filename, line $lineno," 145 | fi 146 | 147 | let index+=1 148 | done 149 | } 150 | 151 | bats_print_failed_command() { 152 | local frame="$1" 153 | local status="$2" 154 | local filename="$(bats_frame_filename "$frame")" 155 | local lineno="$(bats_frame_lineno "$frame")" 156 | 157 | local failed_line="$(bats_extract_line "$filename" "$lineno")" 158 | local failed_command="$(bats_strip_string "$failed_line")" 159 | echo -n "# \`${failed_command}' " 160 | 161 | if [ $status -eq 1 ]; then 162 | echo "failed" 163 | else 164 | echo "failed with status $status" 165 | fi 166 | } 167 | 168 | bats_frame_lineno() { 169 | local frame="$1" 170 | local lineno="${frame%% *}" 171 | echo "$lineno" 172 | } 173 | 174 | bats_frame_function() { 175 | local frame="$1" 176 | local rest="${frame#* }" 177 | local fn="${rest%% *}" 178 | echo "$fn" 179 | } 180 | 181 | bats_frame_filename() { 182 | local frame="$1" 183 | local rest="${frame#* }" 184 | local filename="${rest#* }" 185 | 186 | if [ "$filename" = "$BATS_TEST_SOURCE" ]; then 187 | echo "$BATS_TEST_FILENAME" 188 | else 189 | echo "$filename" 190 | fi 191 | } 192 | 193 | bats_extract_line() { 194 | local filename="$1" 195 | local lineno="$2" 196 | sed -n "${lineno}p" "$filename" 197 | } 198 | 199 | bats_strip_string() { 200 | local string="$1" 201 | printf "%s" "$string" | sed -e "s/^[ "$'\t'"]*//" -e "s/[ "$'\t'"]*$//" 202 | } 203 | 204 | bats_trim_filename() { 205 | local filename="$1" 206 | local length="${#BATS_CWD}" 207 | 208 | if [ "${filename:0:length+1}" = "${BATS_CWD}/" ]; then 209 | echo "${filename:length+1}" 210 | else 211 | echo "$filename" 212 | fi 213 | } 214 | 215 | bats_debug_trap() { 216 | if [ "$BASH_SOURCE" != "$1" ]; then 217 | bats_capture_stack_trace 218 | fi 219 | } 220 | 221 | bats_error_trap() { 222 | BATS_ERROR_STATUS="$?" 223 | BATS_ERROR_STACK_TRACE=( "${BATS_PREVIOUS_STACK_TRACE[@]}" ) 224 | trap - debug 225 | } 226 | 227 | bats_teardown_trap() { 228 | trap "bats_exit_trap" exit 229 | local status=0 230 | teardown >>"$BATS_OUT" 2>&1 || status="$?" 231 | 232 | if [ $status -eq 0 ]; then 233 | BATS_TEARDOWN_COMPLETED=1 234 | elif [ -n "$BATS_TEST_COMPLETED" ]; then 235 | BATS_ERROR_STATUS="$status" 236 | BATS_ERROR_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" ) 237 | fi 238 | 239 | bats_exit_trap 240 | } 241 | 242 | bats_exit_trap() { 243 | local status 244 | local skipped 245 | trap - err exit 246 | 247 | skipped="" 248 | if [ -n "$BATS_TEST_SKIPPED" ]; then 249 | skipped=" # skip" 250 | if [ "1" != "$BATS_TEST_SKIPPED" ]; then 251 | skipped+=" ($BATS_TEST_SKIPPED)" 252 | fi 253 | fi 254 | 255 | if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then 256 | echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 257 | bats_print_stack_trace "${BATS_ERROR_STACK_TRACE[@]}" >&3 258 | bats_print_failed_command "${BATS_ERROR_STACK_TRACE[${#BATS_ERROR_STACK_TRACE[@]}-1]}" "$BATS_ERROR_STATUS" >&3 259 | sed -e "s/^/# /" < "$BATS_OUT" >&3 260 | status=1 261 | else 262 | echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3 263 | status=0 264 | fi 265 | 266 | rm -f "$BATS_OUT" 267 | exit "$status" 268 | } 269 | 270 | bats_perform_tests() { 271 | echo "1..$#" 272 | test_number=1 273 | status=0 274 | for test_name in "$@"; do 275 | "$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1 276 | let test_number+=1 277 | done 278 | exit "$status" 279 | } 280 | 281 | bats_perform_test() { 282 | BATS_TEST_NAME="$1" 283 | if [ "$(type -t "$BATS_TEST_NAME" || true)" = "function" ]; then 284 | BATS_TEST_NUMBER="$2" 285 | if [ -z "$BATS_TEST_NUMBER" ]; then 286 | echo "1..1" 287 | BATS_TEST_NUMBER="1" 288 | fi 289 | 290 | BATS_TEST_COMPLETED="" 291 | BATS_TEARDOWN_COMPLETED="" 292 | trap "bats_debug_trap \"\$BASH_SOURCE\"" debug 293 | trap "bats_error_trap" err 294 | trap "bats_teardown_trap" exit 295 | "$BATS_TEST_NAME" >>"$BATS_OUT" 2>&1 296 | BATS_TEST_COMPLETED=1 297 | 298 | else 299 | echo "bats: unknown test name \`$BATS_TEST_NAME'" >&2 300 | exit 1 301 | fi 302 | } 303 | 304 | if [ -z "$TMPDIR" ]; then 305 | BATS_TMPDIR="/tmp" 306 | else 307 | BATS_TMPDIR="${TMPDIR%/}" 308 | fi 309 | 310 | BATS_TMPNAME="$BATS_TMPDIR/bats.$$" 311 | BATS_PARENT_TMPNAME="$BATS_TMPDIR/bats.$PPID" 312 | BATS_OUT="${BATS_TMPNAME}.out" 313 | 314 | bats_preprocess_source() { 315 | BATS_TEST_SOURCE="${BATS_TMPNAME}.src" 316 | { tr -d '\r' < "$BATS_TEST_FILENAME"; echo; } | bats-preprocess > "$BATS_TEST_SOURCE" 317 | trap "bats_cleanup_preprocessed_source" err exit 318 | trap "bats_cleanup_preprocessed_source; exit 1" int 319 | } 320 | 321 | bats_cleanup_preprocessed_source() { 322 | rm -f "$BATS_TEST_SOURCE" 323 | } 324 | 325 | bats_evaluate_preprocessed_source() { 326 | if [ -z "$BATS_TEST_SOURCE" ]; then 327 | BATS_TEST_SOURCE="${BATS_PARENT_TMPNAME}.src" 328 | fi 329 | source "$BATS_TEST_SOURCE" 330 | } 331 | 332 | exec 3<&1 333 | 334 | if [ "$#" -eq 0 ]; then 335 | bats_preprocess_source 336 | bats_evaluate_preprocessed_source 337 | 338 | if [ -n "$BATS_COUNT_ONLY" ]; then 339 | echo "${#BATS_TEST_NAMES[@]}" 340 | else 341 | bats_perform_tests "${BATS_TEST_NAMES[@]}" 342 | fi 343 | else 344 | bats_evaluate_preprocessed_source 345 | bats_perform_test "$@" 346 | fi 347 | -------------------------------------------------------------------------------- /libexec/bats-format-tap-stream: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | # Just stream the TAP output (sans extended syntax) if tput is missing 5 | command -v tput >/dev/null || exec grep -v "^begin " 6 | 7 | header_pattern='[0-9]+\.\.[0-9]+' 8 | IFS= read -r header 9 | 10 | if [[ "$header" =~ $header_pattern ]]; then 11 | count="${header:3}" 12 | index=0 13 | failures=0 14 | skipped=0 15 | name="" 16 | count_column_width=$(( ${#count} * 2 + 2 )) 17 | else 18 | # If the first line isn't a TAP plan, print it and pass the rest through 19 | printf "%s\n" "$header" 20 | exec cat 21 | fi 22 | 23 | update_screen_width() { 24 | screen_width="$(tput cols)" 25 | count_column_left=$(( $screen_width - $count_column_width )) 26 | } 27 | 28 | trap update_screen_width WINCH 29 | update_screen_width 30 | 31 | begin() { 32 | go_to_column 0 33 | printf_with_truncation $(( $count_column_left - 1 )) " %s" "$name" 34 | clear_to_end_of_line 35 | go_to_column $count_column_left 36 | printf "%${#count}s/${count}" "$index" 37 | go_to_column 1 38 | } 39 | 40 | pass() { 41 | go_to_column 0 42 | printf " ✓ %s" "$name" 43 | advance 44 | } 45 | 46 | skip() { 47 | local reason="$1" 48 | [ -z "$reason" ] || reason=": $reason" 49 | go_to_column 0 50 | printf " - %s (skipped%s)" "$name" "$reason" 51 | advance 52 | } 53 | 54 | fail() { 55 | go_to_column 0 56 | set_color 1 bold 57 | printf " ✗ %s" "$name" 58 | advance 59 | } 60 | 61 | log() { 62 | set_color 1 63 | printf " %s\n" "$1" 64 | clear_color 65 | } 66 | 67 | summary() { 68 | printf "\n%d test%s" "$count" "$(plural "$count")" 69 | 70 | printf ", %d failure%s" "$failures" "$(plural "$failures")" 71 | 72 | if [ "$skipped" -gt 0 ]; then 73 | printf ", %d skipped" "$skipped" 74 | fi 75 | 76 | printf "\n" 77 | } 78 | 79 | printf_with_truncation() { 80 | local width="$1" 81 | shift 82 | local string="$(printf "$@")" 83 | 84 | if [ "${#string}" -gt "$width" ]; then 85 | printf "%s..." "${string:0:$(( $width - 4 ))}" 86 | else 87 | printf "%s" "$string" 88 | fi 89 | } 90 | 91 | go_to_column() { 92 | local column="$1" 93 | printf "\x1B[%dG" $(( $column + 1 )) 94 | } 95 | 96 | clear_to_end_of_line() { 97 | printf "\x1B[K" 98 | } 99 | 100 | advance() { 101 | clear_to_end_of_line 102 | echo 103 | clear_color 104 | } 105 | 106 | set_color() { 107 | local color="$1" 108 | local weight="$2" 109 | printf "\x1B[%d;%dm" $(( 30 + $color )) "$( [ "$weight" = "bold" ] && echo 1 || echo 22 )" 110 | } 111 | 112 | clear_color() { 113 | printf "\x1B[0m" 114 | } 115 | 116 | plural() { 117 | [ "$1" -eq 1 ] || echo "s" 118 | } 119 | 120 | _buffer="" 121 | 122 | buffer() { 123 | _buffer="${_buffer}$("$@")" 124 | } 125 | 126 | flush() { 127 | printf "%s" "$_buffer" 128 | _buffer="" 129 | } 130 | 131 | finish() { 132 | flush 133 | printf "\n" 134 | } 135 | 136 | trap finish EXIT 137 | 138 | while IFS= read -r line; do 139 | case "$line" in 140 | "begin "* ) 141 | let index+=1 142 | name="${line#* $index }" 143 | buffer begin 144 | flush 145 | ;; 146 | "ok "* ) 147 | skip_expr="ok $index # skip (\(([^)]*)\))?" 148 | if [[ "$line" =~ $skip_expr ]]; then 149 | let skipped+=1 150 | buffer skip "${BASH_REMATCH[2]}" 151 | else 152 | buffer pass 153 | fi 154 | ;; 155 | "not ok "* ) 156 | let failures+=1 157 | buffer fail 158 | ;; 159 | "# "* ) 160 | buffer log "${line:2}" 161 | ;; 162 | esac 163 | done 164 | 165 | buffer summary 166 | -------------------------------------------------------------------------------- /libexec/bats-preprocess: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | encode_name() { 5 | local name="$1" 6 | local result="test_" 7 | 8 | if [[ ! "$name" =~ [^[:alnum:]\ _-] ]]; then 9 | name="${name//_/-5f}" 10 | name="${name//-/-2d}" 11 | name="${name// /_}" 12 | result+="$name" 13 | else 14 | local length="${#name}" 15 | local char i 16 | 17 | for ((i=0; i [ ...] 9 | 10 | is the path to a Bats test file, or the path to a directory 11 | containing Bats test files. 12 | 13 | 14 | DESCRIPTION 15 | ----------- 16 | 17 | Bats is a TAP-compliant testing framework for Bash. It provides a simple 18 | way to verify that the UNIX programs you write behave as expected. 19 | 20 | A Bats test file is a Bash script with special syntax for defining 21 | test cases. Under the hood, each test case is just a function with a 22 | description. 23 | 24 | Test cases consist of standard shell commands. Bats makes use of 25 | Bash's `errexit` (`set -e`) option when running test cases. If every 26 | command in the test case exits with a `0` status code (success), the 27 | test passes. In this way, each line is an assertion of truth. 28 | 29 | See `bats`(7) for more information on writing Bats tests. 30 | 31 | 32 | RUNNING TESTS 33 | ------------- 34 | 35 | To run your tests, invoke the `bats` interpreter with a path to a test 36 | file. The file's test cases are run sequentially and in isolation. If 37 | all the test cases pass, `bats` exits with a `0` status code. If there 38 | are any failures, `bats` exits with a `1` status code. 39 | 40 | You can invoke the `bats` interpreter with multiple test file arguments, 41 | or with a path to a directory containing multiple `.bats` files. Bats 42 | will run each test file individually and aggregate the results. If any 43 | test case fails, `bats` exits with a `1` status code. 44 | 45 | 46 | OPTIONS 47 | ------- 48 | 49 | * `-c`, `--count`: 50 | Count the number of test cases without running any tests 51 | * `-h`, `--help`: 52 | Display help message 53 | * `-p`, `--pretty`: 54 | Show results in pretty format (default for terminals) 55 | * `-t`, `--tap`: 56 | Show results in TAP format 57 | * `-v`, `--version`: 58 | Display the version number 59 | 60 | 61 | OUTPUT 62 | ------ 63 | 64 | When you run Bats from a terminal, you'll see output as each test is 65 | performed, with a check-mark next to the test's name if it passes or 66 | an "X" if it fails. 67 | 68 | $ bats addition.bats 69 | ✓ addition using bc 70 | ✓ addition using dc 71 | 72 | 2 tests, 0 failures 73 | 74 | If Bats is not connected to a terminal--in other words, if you run it 75 | from a continuous integration system or redirect its output to a 76 | file--the results are displayed in human-readable, machine-parsable 77 | TAP format. You can force TAP output from a terminal by invoking Bats 78 | with the `--tap` option. 79 | 80 | $ bats --tap addition.bats 81 | 1..2 82 | ok 1 addition using bc 83 | ok 2 addition using dc 84 | 85 | 86 | EXIT STATUS 87 | ----------- 88 | 89 | The `bats` interpreter exits with a value of `0` if all test cases pass, 90 | or `1` if one or more test cases fail. 91 | 92 | 93 | SEE ALSO 94 | -------- 95 | 96 | Bats wiki: _https://github.com/sstephenson/bats/wiki/_ 97 | 98 | `bash`(1), `bats`(7) 99 | 100 | 101 | COPYRIGHT 102 | --------- 103 | 104 | (c) 2014 Sam Stephenson 105 | 106 | Bats is released under the terms of an MIT-style license. 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /man/bats.7: -------------------------------------------------------------------------------- 1 | .\" generated with Ronn/v0.7.3 2 | .\" http://github.com/rtomayko/ronn/tree/0.7.3 3 | . 4 | .TH "BATS" "7" "November 2013" "" "" 5 | . 6 | .SH "NAME" 7 | \fBbats\fR \- Bats test file format 8 | . 9 | .SH "DESCRIPTION" 10 | A Bats test file is a Bash script with special syntax for defining test cases\. Under the hood, each test case is just a function with a description\. 11 | . 12 | .IP "" 4 13 | . 14 | .nf 15 | 16 | #!/usr/bin/env bats 17 | 18 | @test "addition using bc" { 19 | result="$(echo 2+2 | bc)" 20 | [ "$result" \-eq 4 ] 21 | } 22 | 23 | @test "addition using dc" { 24 | result="$(echo 2 2+p | dc)" 25 | [ "$result" \-eq 4 ] 26 | } 27 | . 28 | .fi 29 | . 30 | .IP "" 0 31 | . 32 | .P 33 | Each Bats test file is evaluated n+1 times, where \fIn\fR is the number of test cases in the file\. The first run counts the number of test cases, then iterates over the test cases and executes each one in its own process\. 34 | . 35 | .SH "THE RUN HELPER" 36 | Many Bats tests need to run a command and then make assertions about its exit status and output\. Bats includes a \fBrun\fR helper that invokes its arguments as a command, saves the exit status and output into special global variables, and then returns with a \fB0\fR status code so you can continue to make assertions in your test case\. 37 | . 38 | .P 39 | For example, let\'s say you\'re testing that the \fBfoo\fR command, when passed a nonexistent filename, exits with a \fB1\fR status code and prints an error message\. 40 | . 41 | .IP "" 4 42 | . 43 | .nf 44 | 45 | @test "invoking foo with a nonexistent file prints an error" { 46 | run foo nonexistent_filename 47 | [ "$status" \-eq 1 ] 48 | [ "$output" = "foo: no such file \'nonexistent_filename\'" ] 49 | } 50 | . 51 | .fi 52 | . 53 | .IP "" 0 54 | . 55 | .P 56 | The \fB$status\fR variable contains the status code of the command, and the \fB$output\fR variable contains the combined contents of the command\'s standard output and standard error streams\. 57 | . 58 | .P 59 | A third special variable, the \fB$lines\fR array, is available for easily accessing individual lines of output\. For example, if you want to test that invoking \fBfoo\fR without any arguments prints usage information on the first line: 60 | . 61 | .IP "" 4 62 | . 63 | .nf 64 | 65 | @test "invoking foo without arguments prints usage" { 66 | run foo 67 | [ "$status" \-eq 1 ] 68 | [ "${lines[0]}" = "usage: foo " ] 69 | } 70 | . 71 | .fi 72 | . 73 | .IP "" 0 74 | . 75 | .SH "THE LOAD COMMAND" 76 | You may want to share common code across multiple test files\. Bats includes a convenient \fBload\fR command for sourcing a Bash source file relative to the location of the current test file\. For example, if you have a Bats test in \fBtest/foo\.bats\fR, the command 77 | . 78 | .IP "" 4 79 | . 80 | .nf 81 | 82 | load test_helper 83 | . 84 | .fi 85 | . 86 | .IP "" 0 87 | . 88 | .P 89 | will source the script \fBtest/test_helper\.bash\fR in your test file\. This can be useful for sharing functions to set up your environment or load fixtures\. 90 | . 91 | .SH "THE SKIP COMMAND" 92 | Tests can be skipped by using the \fBskip\fR command at the point in a test you wish to skip\. 93 | . 94 | .IP "" 4 95 | . 96 | .nf 97 | 98 | @test "A test I don\'t want to execute for now" { 99 | skip 100 | run foo 101 | [ "$status" \-eq 0 ] 102 | } 103 | . 104 | .fi 105 | . 106 | .IP "" 0 107 | . 108 | .P 109 | Optionally, you may include a reason for skipping: 110 | . 111 | .IP "" 4 112 | . 113 | .nf 114 | 115 | @test "A test I don\'t want to execute for now" { 116 | skip "This command will return zero soon, but not now" 117 | run foo 118 | [ "$status" \-eq 0 ] 119 | } 120 | . 121 | .fi 122 | . 123 | .IP "" 0 124 | . 125 | .P 126 | Or you can skip conditionally: 127 | . 128 | .IP "" 4 129 | . 130 | .nf 131 | 132 | @test "A test which should run" { 133 | if [ foo != bar ]; then 134 | skip "foo isn\'t bar" 135 | fi 136 | 137 | run foo 138 | [ "$status" \-eq 0 ] 139 | } 140 | . 141 | .fi 142 | . 143 | .IP "" 0 144 | . 145 | .SH "SETUP AND TEARDOWN FUNCTIONS" 146 | You can define special \fBsetup\fR and \fBteardown\fR functions which run before and after each test case, respectively\. Use these to load fixtures, set up your environment, and clean up when you\'re done\. 147 | . 148 | .SH "CODE OUTSIDE OF TEST CASES" 149 | You can include code in your test file outside of \fB@test\fR functions\. For example, this may be useful if you want to check for dependencies and fail immediately if they\'re not present\. However, any output that you print in code outside of \fB@test\fR, \fBsetup\fR or \fBteardown\fR functions must be redirected to \fBstderr\fR (\fB>&2\fR)\. Otherwise, the output may cause Bats to fail by polluting the TAP stream on \fBstdout\fR\. 150 | . 151 | .SH "SPECIAL VARIABLES" 152 | There are several global variables you can use to introspect on Bats tests: 153 | . 154 | .IP "\(bu" 4 155 | \fB$BATS_TEST_FILENAME\fR is the fully expanded path to the Bats test file\. 156 | . 157 | .IP "\(bu" 4 158 | \fB$BATS_TEST_DIRNAME\fR is the directory in which the Bats test file is located\. 159 | . 160 | .IP "\(bu" 4 161 | \fB$BATS_TEST_NAMES\fR is an array of function names for each test case\. 162 | . 163 | .IP "\(bu" 4 164 | \fB$BATS_TEST_NAME\fR is the name of the function containing the current test case\. 165 | . 166 | .IP "\(bu" 4 167 | \fB$BATS_TEST_DESCRIPTION\fR is the description of the current test case\. 168 | . 169 | .IP "\(bu" 4 170 | \fB$BATS_TEST_NUMBER\fR is the (1\-based) index of the current test case in the test file\. 171 | . 172 | .IP "\(bu" 4 173 | \fB$BATS_TMPDIR\fR is the location to a directory that may be used to store temporary files\. 174 | . 175 | .IP "" 0 176 | . 177 | .SH "SEE ALSO" 178 | \fBbash\fR(1), \fBbats\fR(1) 179 | -------------------------------------------------------------------------------- /man/bats.7.ronn: -------------------------------------------------------------------------------- 1 | bats(7) -- Bats test file format 2 | ================================ 3 | 4 | 5 | DESCRIPTION 6 | ----------- 7 | 8 | A Bats test file is a Bash script with special syntax for defining 9 | test cases. Under the hood, each test case is just a function with a 10 | description. 11 | 12 | #!/usr/bin/env bats 13 | 14 | @test "addition using bc" { 15 | result="$(echo 2+2 | bc)" 16 | [ "$result" -eq 4 ] 17 | } 18 | 19 | @test "addition using dc" { 20 | result="$(echo 2 2+p | dc)" 21 | [ "$result" -eq 4 ] 22 | } 23 | 24 | 25 | Each Bats test file is evaluated n+1 times, where _n_ is the number of 26 | test cases in the file. The first run counts the number of test cases, 27 | then iterates over the test cases and executes each one in its own 28 | process. 29 | 30 | 31 | THE RUN HELPER 32 | -------------- 33 | 34 | Many Bats tests need to run a command and then make assertions about 35 | its exit status and output. Bats includes a `run` helper that invokes 36 | its arguments as a command, saves the exit status and output into 37 | special global variables, and then returns with a `0` status code so 38 | you can continue to make assertions in your test case. 39 | 40 | For example, let's say you're testing that the `foo` command, when 41 | passed a nonexistent filename, exits with a `1` status code and prints 42 | an error message. 43 | 44 | @test "invoking foo with a nonexistent file prints an error" { 45 | run foo nonexistent_filename 46 | [ "$status" -eq 1 ] 47 | [ "$output" = "foo: no such file 'nonexistent_filename'" ] 48 | } 49 | 50 | The `$status` variable contains the status code of the command, and 51 | the `$output` variable contains the combined contents of the command's 52 | standard output and standard error streams. 53 | 54 | A third special variable, the `$lines` array, is available for easily 55 | accessing individual lines of output. For example, if you want to test 56 | that invoking `foo` without any arguments prints usage information on 57 | the first line: 58 | 59 | @test "invoking foo without arguments prints usage" { 60 | run foo 61 | [ "$status" -eq 1 ] 62 | [ "${lines[0]}" = "usage: foo " ] 63 | } 64 | 65 | 66 | THE LOAD COMMAND 67 | ---------------- 68 | 69 | You may want to share common code across multiple test files. Bats 70 | includes a convenient `load` command for sourcing a Bash source file 71 | relative to the location of the current test file. For example, if you 72 | have a Bats test in `test/foo.bats`, the command 73 | 74 | load test_helper 75 | 76 | will source the script `test/test_helper.bash` in your test file. This 77 | can be useful for sharing functions to set up your environment or load 78 | fixtures. 79 | 80 | 81 | THE SKIP COMMAND 82 | ---------------- 83 | 84 | Tests can be skipped by using the `skip` command at the point in a 85 | test you wish to skip. 86 | 87 | @test "A test I don't want to execute for now" { 88 | skip 89 | run foo 90 | [ "$status" -eq 0 ] 91 | } 92 | 93 | Optionally, you may include a reason for skipping: 94 | 95 | @test "A test I don't want to execute for now" { 96 | skip "This command will return zero soon, but not now" 97 | run foo 98 | [ "$status" -eq 0 ] 99 | } 100 | 101 | Or you can skip conditionally: 102 | 103 | @test "A test which should run" { 104 | if [ foo != bar ]; then 105 | skip "foo isn't bar" 106 | fi 107 | 108 | run foo 109 | [ "$status" -eq 0 ] 110 | } 111 | 112 | 113 | SETUP AND TEARDOWN FUNCTIONS 114 | ---------------------------- 115 | 116 | You can define special `setup` and `teardown` functions which run 117 | before and after each test case, respectively. Use these to load 118 | fixtures, set up your environment, and clean up when you're done. 119 | 120 | 121 | CODE OUTSIDE OF TEST CASES 122 | -------------------------- 123 | 124 | You can include code in your test file outside of `@test` functions. 125 | For example, this may be useful if you want to check for dependencies 126 | and fail immediately if they're not present. However, any output that 127 | you print in code outside of `@test`, `setup` or `teardown` functions 128 | must be redirected to `stderr` (`>&2`). Otherwise, the output may 129 | cause Bats to fail by polluting the TAP stream on `stdout`. 130 | 131 | 132 | SPECIAL VARIABLES 133 | ----------------- 134 | 135 | There are several global variables you can use to introspect on Bats 136 | tests: 137 | 138 | * `$BATS_TEST_FILENAME` is the fully expanded path to the Bats test 139 | file. 140 | * `$BATS_TEST_DIRNAME` is the directory in which the Bats test file is 141 | located. 142 | * `$BATS_TEST_NAMES` is an array of function names for each test case. 143 | * `$BATS_TEST_NAME` is the name of the function containing the current 144 | test case. 145 | * `$BATS_TEST_DESCRIPTION` is the description of the current test 146 | case. 147 | * `$BATS_TEST_NUMBER` is the (1-based) index of the current test case 148 | in the test file. 149 | * `$BATS_TMPDIR` is the location to a directory that may be used to 150 | store temporary files. 151 | 152 | 153 | SEE ALSO 154 | -------- 155 | 156 | `bash`(1), `bats`(1) 157 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bats", 3 | "version": "v0.4.0", 4 | "description": "Bash Automated Testing System", 5 | "install": "./install.sh ${PREFIX:-/usr/local}", 6 | "scripts": [ "libexec/bats", "libexec/bats-exec-suite", "libexec/bats-exec-test", "libexec/bats-format-tap-stream", "libexec/bats-preprocess", "bin/bats" ] 7 | } 8 | 9 | -------------------------------------------------------------------------------- /test/bats.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | fixtures bats 5 | 6 | @test "no arguments prints usage instructions" { 7 | run bats 8 | [ $status -eq 1 ] 9 | [ $(expr "${lines[1]}" : "Usage:") -ne 0 ] 10 | } 11 | 12 | @test "-v and --version print version number" { 13 | run bats -v 14 | [ $status -eq 0 ] 15 | [ $(expr "$output" : "Bats [0-9][0-9.]*") -ne 0 ] 16 | } 17 | 18 | @test "-h and --help print help" { 19 | run bats -h 20 | [ $status -eq 0 ] 21 | [ "${#lines[@]}" -gt 3 ] 22 | } 23 | 24 | @test "invalid filename prints an error" { 25 | run bats nonexistent 26 | [ $status -eq 1 ] 27 | [ $(expr "$output" : ".*does not exist") -ne 0 ] 28 | } 29 | 30 | @test "empty test file runs zero tests" { 31 | run bats "$FIXTURE_ROOT/empty.bats" 32 | [ $status -eq 0 ] 33 | [ "$output" = "1..0" ] 34 | } 35 | 36 | @test "one passing test" { 37 | run bats "$FIXTURE_ROOT/passing.bats" 38 | [ $status -eq 0 ] 39 | [ "${lines[0]}" = "1..1" ] 40 | [ "${lines[1]}" = "ok 1 a passing test" ] 41 | } 42 | 43 | @test "summary passing tests" { 44 | run filter_control_sequences bats -p $FIXTURE_ROOT/passing.bats 45 | [ $status -eq 0 ] 46 | [ "${lines[1]}" = "1 test, 0 failures" ] 47 | } 48 | 49 | @test "summary passing and skipping tests" { 50 | run filter_control_sequences bats -p $FIXTURE_ROOT/passing_and_skipping.bats 51 | [ $status -eq 0 ] 52 | [ "${lines[2]}" = "2 tests, 0 failures, 1 skipped" ] 53 | } 54 | 55 | @test "summary passing and failing tests" { 56 | run filter_control_sequences bats -p $FIXTURE_ROOT/failing_and_passing.bats 57 | [ $status -eq 0 ] 58 | [ "${lines[4]}" = "2 tests, 1 failure" ] 59 | } 60 | 61 | @test "summary passing, failing and skipping tests" { 62 | run filter_control_sequences bats -p $FIXTURE_ROOT/passing_failing_and_skipping.bats 63 | [ $status -eq 0 ] 64 | [ "${lines[5]}" = "3 tests, 1 failure, 1 skipped" ] 65 | } 66 | 67 | @test "one failing test" { 68 | run bats "$FIXTURE_ROOT/failing.bats" 69 | [ $status -eq 1 ] 70 | [ "${lines[0]}" = '1..1' ] 71 | [ "${lines[1]}" = 'not ok 1 a failing test' ] 72 | [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing.bats, line 4)" ] 73 | [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ] 74 | } 75 | 76 | @test "one failing and one passing test" { 77 | run bats "$FIXTURE_ROOT/failing_and_passing.bats" 78 | [ $status -eq 1 ] 79 | [ "${lines[0]}" = '1..2' ] 80 | [ "${lines[1]}" = 'not ok 1 a failing test' ] 81 | [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_and_passing.bats, line 2)" ] 82 | [ "${lines[3]}" = "# \`false' failed" ] 83 | [ "${lines[4]}" = 'ok 2 a passing test' ] 84 | } 85 | 86 | @test "failing test with significant status" { 87 | STATUS=2 run bats "$FIXTURE_ROOT/failing.bats" 88 | [ $status -eq 1 ] 89 | [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ] 90 | } 91 | 92 | @test "failing helper function logs the test case's line number" { 93 | run bats "$FIXTURE_ROOT/failing_helper.bats" 94 | [ $status -eq 1 ] 95 | [ "${lines[1]}" = 'not ok 1 failing helper function' ] 96 | [ "${lines[2]}" = "# (from function \`failing_helper' in file $RELATIVE_FIXTURE_ROOT/test_helper.bash, line 6," ] 97 | [ "${lines[3]}" = "# in test file $RELATIVE_FIXTURE_ROOT/failing_helper.bats, line 5)" ] 98 | [ "${lines[4]}" = "# \`failing_helper' failed" ] 99 | } 100 | 101 | @test "test environments are isolated" { 102 | run bats "$FIXTURE_ROOT/environment.bats" 103 | [ $status -eq 0 ] 104 | } 105 | 106 | @test "setup is run once before each test" { 107 | rm -f "$TMP/setup.log" 108 | run bats "$FIXTURE_ROOT/setup.bats" 109 | [ $status -eq 0 ] 110 | run cat "$TMP/setup.log" 111 | [ ${#lines[@]} -eq 3 ] 112 | } 113 | 114 | @test "teardown is run once after each test, even if it fails" { 115 | rm -f "$TMP/teardown.log" 116 | run bats "$FIXTURE_ROOT/teardown.bats" 117 | [ $status -eq 1 ] 118 | run cat "$TMP/teardown.log" 119 | [ ${#lines[@]} -eq 3 ] 120 | } 121 | 122 | @test "setup failure" { 123 | run bats "$FIXTURE_ROOT/failing_setup.bats" 124 | [ $status -eq 1 ] 125 | [ "${lines[1]}" = 'not ok 1 truth' ] 126 | [ "${lines[2]}" = "# (from function \`setup' in test file $RELATIVE_FIXTURE_ROOT/failing_setup.bats, line 2)" ] 127 | [ "${lines[3]}" = "# \`false' failed" ] 128 | } 129 | 130 | @test "passing test with teardown failure" { 131 | PASS=1 run bats "$FIXTURE_ROOT/failing_teardown.bats" 132 | [ $status -eq 1 ] 133 | [ "${lines[1]}" = 'not ok 1 truth' ] 134 | [ "${lines[2]}" = "# (from function \`teardown' in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 2)" ] 135 | [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ] 136 | } 137 | 138 | @test "failing test with teardown failure" { 139 | PASS=0 run bats "$FIXTURE_ROOT/failing_teardown.bats" 140 | [ $status -eq 1 ] 141 | [ "${lines[1]}" = 'not ok 1 truth' ] 142 | [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 6)" ] 143 | [ "${lines[3]}" = $'# `[ "$PASS" = "1" ]\' failed' ] 144 | } 145 | 146 | @test "teardown failure with significant status" { 147 | PASS=1 STATUS=2 run bats "$FIXTURE_ROOT/failing_teardown.bats" 148 | [ $status -eq 1 ] 149 | [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ] 150 | } 151 | 152 | @test "failing test file outside of BATS_CWD" { 153 | cd "$TMP" 154 | run bats "$FIXTURE_ROOT/failing.bats" 155 | [ $status -eq 1 ] 156 | [ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing.bats, line 4)" ] 157 | } 158 | 159 | @test "load sources scripts relative to the current test file" { 160 | run bats "$FIXTURE_ROOT/load.bats" 161 | [ $status -eq 0 ] 162 | } 163 | 164 | @test "load aborts if the specified script does not exist" { 165 | HELPER_NAME="nonexistent" run bats "$FIXTURE_ROOT/load.bats" 166 | [ $status -eq 1 ] 167 | } 168 | 169 | @test "load sources scripts by absolute path" { 170 | HELPER_NAME="${FIXTURE_ROOT}/test_helper.bash" run bats "$FIXTURE_ROOT/load.bats" 171 | [ $status -eq 0 ] 172 | } 173 | 174 | @test "load aborts if the script, specified by an absolute path, does not exist" { 175 | HELPER_NAME="${FIXTURE_ROOT}/nonexistent" run bats "$FIXTURE_ROOT/load.bats" 176 | [ $status -eq 1 ] 177 | } 178 | 179 | @test "output is discarded for passing tests and printed for failing tests" { 180 | run bats "$FIXTURE_ROOT/output.bats" 181 | [ $status -eq 1 ] 182 | [ "${lines[6]}" = '# failure stdout 1' ] 183 | [ "${lines[7]}" = '# failure stdout 2' ] 184 | [ "${lines[11]}" = '# failure stderr' ] 185 | } 186 | 187 | @test "-c prints the number of tests" { 188 | run bats -c "$FIXTURE_ROOT/empty.bats" 189 | [ $status -eq 0 ] 190 | [ "$output" = "0" ] 191 | 192 | run bats -c "$FIXTURE_ROOT/output.bats" 193 | [ $status -eq 0 ] 194 | [ "$output" = "4" ] 195 | } 196 | 197 | @test "dash-e is not mangled on beginning of line" { 198 | run bats "$FIXTURE_ROOT/intact.bats" 199 | [ $status -eq 0 ] 200 | [ "${lines[1]}" = "ok 1 dash-e on beginning of line" ] 201 | } 202 | 203 | @test "dos line endings are stripped before testing" { 204 | run bats "$FIXTURE_ROOT/dos_line.bats" 205 | [ $status -eq 0 ] 206 | } 207 | 208 | @test "test file without trailing newline" { 209 | run bats "$FIXTURE_ROOT/without_trailing_newline.bats" 210 | [ $status -eq 0 ] 211 | [ "${lines[1]}" = "ok 1 truth" ] 212 | } 213 | 214 | @test "skipped tests" { 215 | run bats "$FIXTURE_ROOT/skipped.bats" 216 | [ $status -eq 0 ] 217 | [ "${lines[1]}" = "ok 1 # skip a skipped test" ] 218 | [ "${lines[2]}" = "ok 2 # skip (a reason) a skipped test with a reason" ] 219 | } 220 | 221 | @test "extended syntax" { 222 | run bats-exec-test -x "$FIXTURE_ROOT/failing_and_passing.bats" 223 | [ $status -eq 1 ] 224 | [ "${lines[1]}" = 'begin 1 a failing test' ] 225 | [ "${lines[2]}" = 'not ok 1 a failing test' ] 226 | [ "${lines[5]}" = 'begin 2 a passing test' ] 227 | [ "${lines[6]}" = 'ok 2 a passing test' ] 228 | } 229 | 230 | @test "pretty and tap formats" { 231 | run bats --tap "$FIXTURE_ROOT/passing.bats" 232 | tap_output="$output" 233 | [ $status -eq 0 ] 234 | 235 | run bats --pretty "$FIXTURE_ROOT/passing.bats" 236 | pretty_output="$output" 237 | [ $status -eq 0 ] 238 | 239 | [ "$tap_output" != "$pretty_output" ] 240 | } 241 | 242 | @test "pretty formatter bails on invalid tap" { 243 | run bats --tap "$FIXTURE_ROOT/invalid_tap.bats" 244 | [ $status -eq 1 ] 245 | [ "${lines[0]}" = "This isn't TAP!" ] 246 | [ "${lines[1]}" = "Good day to you" ] 247 | } 248 | 249 | @test "single-line tests" { 250 | run bats "$FIXTURE_ROOT/single_line.bats" 251 | [ $status -eq 1 ] 252 | [ "${lines[1]}" = 'ok 1 empty' ] 253 | [ "${lines[2]}" = 'ok 2 passing' ] 254 | [ "${lines[3]}" = 'ok 3 input redirection' ] 255 | [ "${lines[4]}" = 'not ok 4 failing' ] 256 | [ "${lines[5]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/single_line.bats, line 9)" ] 257 | [ "${lines[6]}" = $'# `@test "failing" { false; }\' failed' ] 258 | } 259 | 260 | @test "testing IFS not modified by run" { 261 | run bats "$FIXTURE_ROOT/loop_keep_IFS.bats" 262 | [ $status -eq 0 ] 263 | [ "${lines[1]}" = "ok 1 loop_func" ] 264 | } 265 | -------------------------------------------------------------------------------- /test/fixtures/bats/dos_line.bats: -------------------------------------------------------------------------------- 1 | @test "foo" { 2 | echo "foo" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/bats/empty.bats: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sstephenson/bats/03608115df2071fff4eaaff1605768c275e5f81f/test/fixtures/bats/empty.bats -------------------------------------------------------------------------------- /test/fixtures/bats/environment.bats: -------------------------------------------------------------------------------- 1 | @test "setting a variable" { 2 | variable=1 3 | [ $variable -eq 1 ] 4 | } 5 | 6 | @test "variables do not persist across tests" { 7 | [ -z "$variable" ] 8 | } 9 | -------------------------------------------------------------------------------- /test/fixtures/bats/failing.bats: -------------------------------------------------------------------------------- 1 | @test "a failing test" { 2 | true 3 | true 4 | eval "( exit ${STATUS:-1} )" 5 | } 6 | -------------------------------------------------------------------------------- /test/fixtures/bats/failing_and_passing.bats: -------------------------------------------------------------------------------- 1 | @test "a failing test" { 2 | false 3 | } 4 | 5 | @test "a passing test" { 6 | true 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/bats/failing_helper.bats: -------------------------------------------------------------------------------- 1 | load "test_helper" 2 | 3 | @test "failing helper function" { 4 | true 5 | failing_helper 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/bats/failing_setup.bats: -------------------------------------------------------------------------------- 1 | setup() { 2 | false 3 | } 4 | 5 | @test "truth" { 6 | true 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/bats/failing_teardown.bats: -------------------------------------------------------------------------------- 1 | teardown() { 2 | eval "( exit ${STATUS:-1} )" 3 | } 4 | 5 | @test "truth" { 6 | [ "$PASS" = "1" ] 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/bats/intact.bats: -------------------------------------------------------------------------------- 1 | @test "dash-e on beginning of line" { 2 | run cat - <&2 8 | } 9 | 10 | @test "failure writing to stdout" { 11 | echo "failure stdout 1" 12 | echo "failure stdout 2" 13 | false 14 | } 15 | 16 | @test "failure writing to stderr" { 17 | echo "failure stderr" >&2 18 | false 19 | } 20 | -------------------------------------------------------------------------------- /test/fixtures/bats/passing.bats: -------------------------------------------------------------------------------- 1 | @test "a passing test" { 2 | true 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/bats/passing_and_failing.bats: -------------------------------------------------------------------------------- 1 | @test "a passing test" { 2 | true 3 | } 4 | 5 | @test "a failing test" { 6 | false 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/bats/passing_and_skipping.bats: -------------------------------------------------------------------------------- 1 | @test "a passing test" { 2 | true 3 | } 4 | 5 | @test "a skipping test" { 6 | skip 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/bats/passing_failing_and_skipping.bats: -------------------------------------------------------------------------------- 1 | @test "a passing test" { 2 | true 3 | } 4 | 5 | @test "a skipping test" { 6 | skip 7 | } 8 | 9 | @test "a failing test" { 10 | false 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/bats/setup.bats: -------------------------------------------------------------------------------- 1 | LOG="$TMP/setup.log" 2 | 3 | setup() { 4 | echo "$BATS_TEST_NAME" >> "$LOG" 5 | } 6 | 7 | @test "one" { 8 | [ "$(tail -n 1 "$LOG")" = "test_one" ] 9 | } 10 | 11 | @test "two" { 12 | [ "$(tail -n 1 "$LOG")" = "test_two" ] 13 | } 14 | 15 | @test "three" { 16 | [ "$(tail -n 1 "$LOG")" = "test_three" ] 17 | } 18 | -------------------------------------------------------------------------------- /test/fixtures/bats/single_line.bats: -------------------------------------------------------------------------------- 1 | @test "empty" { } 2 | 3 | @test "passing" { true; } 4 | 5 | @test "input redirection" { diff - <( echo hello ); } <> "$LOG" 5 | } 6 | 7 | @test "one" { 8 | true 9 | } 10 | 11 | @test "two" { 12 | false 13 | } 14 | 15 | @test "three" { 16 | true 17 | } 18 | -------------------------------------------------------------------------------- /test/fixtures/bats/test_helper.bash: -------------------------------------------------------------------------------- 1 | help_me() { 2 | true 3 | } 4 | 5 | failing_helper() { 6 | false 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/bats/without_trailing_newline.bats: -------------------------------------------------------------------------------- 1 | @test "truth" { 2 | true 3 | } -------------------------------------------------------------------------------- /test/fixtures/suite/empty/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sstephenson/bats/03608115df2071fff4eaaff1605768c275e5f81f/test/fixtures/suite/empty/.gitkeep -------------------------------------------------------------------------------- /test/fixtures/suite/multiple/a.bats: -------------------------------------------------------------------------------- 1 | @test "truth" { 2 | true 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/suite/multiple/b.bats: -------------------------------------------------------------------------------- 1 | @test "more truth" { 2 | true 3 | } 4 | 5 | @test "quasi-truth" { 6 | [ -z "$FLUNK" ] 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/suite/single/test.bats: -------------------------------------------------------------------------------- 1 | @test "a passing test" { 2 | true 3 | } 4 | -------------------------------------------------------------------------------- /test/suite.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | fixtures suite 5 | 6 | @test "running a suite with no test files" { 7 | run bats "$FIXTURE_ROOT/empty" 8 | [ $status -eq 0 ] 9 | [ "$output" = "1..0" ] 10 | } 11 | 12 | @test "running a suite with one test file" { 13 | run bats "$FIXTURE_ROOT/single" 14 | [ $status -eq 0 ] 15 | [ "${lines[0]}" = "1..1" ] 16 | [ "${lines[1]}" = "ok 1 a passing test" ] 17 | } 18 | 19 | @test "counting tests in a suite" { 20 | run bats -c "$FIXTURE_ROOT/single" 21 | [ $status -eq 0 ] 22 | [ "$output" -eq 1 ] 23 | 24 | run bats -c "$FIXTURE_ROOT/multiple" 25 | [ $status -eq 0 ] 26 | [ "$output" -eq 3 ] 27 | } 28 | 29 | @test "aggregated output of multiple tests in a suite" { 30 | run bats "$FIXTURE_ROOT/multiple" 31 | [ $status -eq 0 ] 32 | [ "${lines[0]}" = "1..3" ] 33 | echo "$output" | grep "^ok . truth" 34 | echo "$output" | grep "^ok . more truth" 35 | echo "$output" | grep "^ok . quasi-truth" 36 | } 37 | 38 | @test "a failing test in a suite results in an error exit code" { 39 | FLUNK=1 run bats "$FIXTURE_ROOT/multiple" 40 | [ $status -eq 1 ] 41 | [ "${lines[0]}" = "1..3" ] 42 | echo "$output" | grep "^not ok . quasi-truth" 43 | } 44 | 45 | @test "running an ad-hoc suite by specifying multiple test files" { 46 | run bats "$FIXTURE_ROOT/multiple/a.bats" "$FIXTURE_ROOT/multiple/b.bats" 47 | [ $status -eq 0 ] 48 | [ "${lines[0]}" = "1..3" ] 49 | echo "$output" | grep "^ok . truth" 50 | echo "$output" | grep "^ok . more truth" 51 | echo "$output" | grep "^ok . quasi-truth" 52 | } 53 | 54 | @test "extended syntax in suite" { 55 | FLUNK=1 run bats-exec-suite -x "$FIXTURE_ROOT/multiple/"*.bats 56 | [ $status -eq 1 ] 57 | [ "${lines[0]}" = "1..3" ] 58 | [ "${lines[1]}" = "begin 1 truth" ] 59 | [ "${lines[2]}" = "ok 1 truth" ] 60 | [ "${lines[3]}" = "begin 2 more truth" ] 61 | [ "${lines[4]}" = "ok 2 more truth" ] 62 | [ "${lines[5]}" = "begin 3 quasi-truth" ] 63 | [ "${lines[6]}" = "not ok 3 quasi-truth" ] 64 | } 65 | -------------------------------------------------------------------------------- /test/test_helper.bash: -------------------------------------------------------------------------------- 1 | fixtures() { 2 | FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures/$1" 3 | RELATIVE_FIXTURE_ROOT="$(bats_trim_filename "$FIXTURE_ROOT")" 4 | } 5 | 6 | setup() { 7 | export TMP="$BATS_TEST_DIRNAME/tmp" 8 | } 9 | 10 | filter_control_sequences() { 11 | "$@" | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g' 12 | } 13 | 14 | teardown() { 15 | [ -d "$TMP" ] && rm -f "$TMP"/* 16 | } 17 | -------------------------------------------------------------------------------- /test/tmp/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | 3 | --------------------------------------------------------------------------------