Browse Source

Squashed '.bats-core/' content from commit b1da565

git-subtree-dir: .bats-core
git-subtree-split: b1da565f92
pull/340/head
Dan Schaper 8 years ago
commit
658ebc5943
57 changed files with 2668 additions and 0 deletions
  1. +15
    -0
      .appveyor.yml
  2. +3
    -0
      .gitattributes
  3. +20
    -0
      .travis.yml
  4. +83
    -0
      CONDUCT.md
  5. +8
    -0
      Dockerfile
  6. +41
    -0
      LICENSE
  7. +354
    -0
      README.md
  8. +1
    -0
      bin/bats
  9. +48
    -0
      install.sh
  10. +169
    -0
      libexec/bats
  11. +59
    -0
      libexec/bats-exec-suite
  12. +364
    -0
      libexec/bats-exec-test
  13. +173
    -0
      libexec/bats-format-tap-stream
  14. +54
    -0
      libexec/bats-preprocess
  15. +10
    -0
      man/Makefile
  16. +5
    -0
      man/README.md
  17. +103
    -0
      man/bats.1
  18. +110
    -0
      man/bats.1.ronn
  19. +178
    -0
      man/bats.7
  20. +156
    -0
      man/bats.7.ronn
  21. +8
    -0
      package.json
  22. +357
    -0
      test/bats.bats
  23. +3
    -0
      test/fixtures/bats/dos_line.bats
  24. +0
    -0
      test/fixtures/bats/empty.bats
  25. +8
    -0
      test/fixtures/bats/environment.bats
  26. +3
    -0
      test/fixtures/bats/expand_var_in_test_name.bats
  27. +5
    -0
      test/fixtures/bats/failing.bats
  28. +7
    -0
      test/fixtures/bats/failing_and_passing.bats
  29. +6
    -0
      test/fixtures/bats/failing_helper.bats
  30. +7
    -0
      test/fixtures/bats/failing_setup.bats
  31. +7
    -0
      test/fixtures/bats/failing_teardown.bats
  32. +6
    -0
      test/fixtures/bats/intact.bats
  33. +7
    -0
      test/fixtures/bats/invalid_tap.bats
  34. +6
    -0
      test/fixtures/bats/load.bats
  35. +16
    -0
      test/fixtures/bats/loop_keep_IFS.bats
  36. +19
    -0
      test/fixtures/bats/output.bats
  37. +3
    -0
      test/fixtures/bats/passing.bats
  38. +7
    -0
      test/fixtures/bats/passing_and_failing.bats
  39. +11
    -0
      test/fixtures/bats/passing_and_skipping.bats
  40. +11
    -0
      test/fixtures/bats/passing_failing_and_skipping.bats
  41. +11
    -0
      test/fixtures/bats/quoted_and_unquoted_test_names.bats
  42. +17
    -0
      test/fixtures/bats/setup.bats
  43. +9
    -0
      test/fixtures/bats/single_line.bats
  44. +7
    -0
      test/fixtures/bats/skipped.bats
  45. +17
    -0
      test/fixtures/bats/teardown.bats
  46. +7
    -0
      test/fixtures/bats/test_helper.bash
  47. +3
    -0
      test/fixtures/bats/unofficial_bash_strict_mode.bash
  48. +4
    -0
      test/fixtures/bats/unofficial_bash_strict_mode.bats
  49. +33
    -0
      test/fixtures/bats/whitespace.bats
  50. +3
    -0
      test/fixtures/bats/without_trailing_newline.bats
  51. +0
    -0
      test/fixtures/suite/empty/.gitkeep
  52. +3
    -0
      test/fixtures/suite/multiple/a.bats
  53. +7
    -0
      test/fixtures/suite/multiple/b.bats
  54. +3
    -0
      test/fixtures/suite/single/test.bats
  55. +64
    -0
      test/suite.bats
  56. +27
    -0
      test/test_helper.bash
  57. +2
    -0
      test/tmp/.gitignore

+ 15
- 0
.appveyor.yml View File

@ -0,0 +1,15 @@
version: 'v0.4.0.{build}'
build: off
# This presumes that Git bash is installed at `C:\Program Files\Git` and the
# bash we're using is `C:\Program Files\Git\bin\bash.exe`.
#
# If instead it finds the Windows Subsystem for Linux bash at
# `C:\Windows\System32\bash.exe`, it will fail with an error like:
# /mnt/c/.../bats-core/test/test_helper.bash: line 1:
# syntax error near unexpected token `$'{\r''
test_script:
- where bash
- bash --version
- bash -c 'time libexec/bats test'

+ 3
- 0
.gitattributes View File

@ -0,0 +1,3 @@
* text=auto
*.sh eol=lf
libexec/* eol=lf

+ 20
- 0
.travis.yml View File

@ -0,0 +1,20 @@
language: bash
os:
- linux
- osx
services:
- docker
script:
- |
bash -c 'time bin/bats --tap test'
if [[ "$TRAVIS_OS_NAME" == 'linux' ]]; then
docker build --tag bats:latest .
docker run -it bats:latest --tap /opt/bats/test
fi
notifications:
email:
on_success: never

+ 83
- 0
CONDUCT.md View File

@ -0,0 +1,83 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting one of the project maintainers listed below. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Project Maintainers
### bats-core organization:
* Bianca Tamayo <<hi@biancatamayo.me>>
## Project Original Author(s)
* Sam Stephenson <<sstephenson@gmail.com>>
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/

+ 8
- 0
Dockerfile View File

@ -0,0 +1,8 @@
FROM alpine:3.6
COPY . /opt/bats/
RUN apk --no-cache add bash \
&& ln -s /opt/bats/libexec/bats /usr/sbin/bats
ENTRYPOINT ["bats"]

+ 41
- 0
LICENSE View File

@ -0,0 +1,41 @@
Copyright (c) 2017 Bianca Tamayo and bats-core organization
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright (c) 2014 Sam Stephenson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 354
- 0
README.md View File

@ -0,0 +1,354 @@
## BATS-core: Bash Automated Testing System (2017)
[![Build Status](https://travis-ci.org/bats-core/bats-core.svg?branch=master)](https://travis-ci.org/bats-core/bats-core)
### Background:
### What is this repo?
**Tuesday, September 19, 2017:** This is a mirrored fork of [bats](https://github.com/sstephenson/bats), at [0360811](https://github.com/sstephenson/bats/commit/03608115df2071fff4eaaff1605768c275e5f81f). It was created via `git clone --bare` and `git push --mirror`.
#### Why was it created?
The original bats repository needed new maintainers, and has not been actively maintained since 2013. While there were volunteers for maintainers, attempts to organize issues, and outstanding PRs, the lack of write-access to the repo hindered progress severely.
## What's the plan and why?
The rough plan, originally [outlined here](https://github.com/sstephenson/bats/issues/150#issuecomment-323845404) is to create a new, mirrored mainline (this repo!). An excerpt:
> **1. Roadmap 1.0:**
> There are already existing high-quality PRs, and often-requested features and issues, especially here at [#196](https://github.com/sstephenson/bats/issues/196). Leverage these and **consolidate into a single roadmap**.
>
>**2. Create or choose a fork or *mirror* of this repo to use as the new mainline:**
>Repoint existing PRs (whichever ones are possible) to the new mainline, get that repo to a stable 1.0. IMO we should create an organization and grant 2-3 people admin and write access.
>
Doing it this way accomplishes two things:
1. Removes the dependency on the original maintainer
2. Enables collaboration and contribution flow again
3. Allows the possibility of merging back to original, or merging from original if or when the need arises
4. Prevents lock-out by giving administrative access to more than one person, increases transferability
## Misc
- We are `#bats` on freenode
---
# Bats: Bash Automated Testing System
Bats is a [TAP](http://testanything.org)-compliant testing framework
for Bash. It provides a simple way to verify that the UNIX programs
you write behave as expected.
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.
```bash
#!/usr/bin/env bats
@test "addition using bc" {
result="$(echo 2+2 | bc)"
[ "$result" -eq 4 ]
}
@test "addition using dc" {
result="$(echo 2 2+p | dc)"
[ "$result" -eq 4 ]
}
```
Bats is most useful when testing software written in Bash, but you can
use it to test any UNIX program.
Test cases consist of standard shell commands. Bats makes use of
Bash's `errexit` (`set -e`) option when running test cases. If every
command in the test case exits with a `0` status code (success), the
test passes. In this way, each line is an assertion of truth.
## Running tests
To run your tests, invoke the `bats` interpreter with a path to a test
file. The file's test cases are run sequentially and in isolation. If
all the test cases pass, `bats` exits with a `0` status code. If there
are any failures, `bats` exits with a `1` status code.
When you run Bats from a terminal, you'll see output as each test is
performed, with a check-mark next to the test's name if it passes or
an "X" if it fails.
$ bats addition.bats
✓ addition using bc
✓ addition using dc
2 tests, 0 failures
If Bats is not connected to a terminal—in other words, if you
run it from a continuous integration system, or redirect its output to
a file—the results are displayed in human-readable, machine-parsable
[TAP format](http://testanything.org).
You can force TAP output from a terminal by invoking Bats with the
`--tap` option.
$ bats --tap addition.bats
1..2
ok 1 addition using bc
ok 2 addition using dc
### Test suites
You can invoke the `bats` interpreter with multiple test file
arguments, or with a path to a directory containing multiple `.bats`
files. Bats will run each test file individually and aggregate the
results. If any test case fails, `bats` exits with a `1` status code.
## Writing tests
Each Bats test file is evaluated _n+1_ times, where _n_ 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.
For more details about how Bats evaluates test files, see
[Bats Evaluation Process](https://github.com/bats-core/bats-core/wiki/Bats-Evaluation-Process)
on the wiki.
### `run`: Test other commands
Many Bats tests need to run a command and then make assertions about
its exit status and output. Bats includes a `run` helper that invokes
its arguments as a command, saves the exit status and output into
special global variables, and then returns with a `0` status code so
you can continue to make assertions in your test case.
For example, let's say you're testing that the `foo` command, when
passed a nonexistent filename, exits with a `1` status code and prints
an error message.
```bash
@test "invoking foo with a nonexistent file prints an error" {
run foo nonexistent_filename
[ "$status" -eq 1 ]
[ "$output" = "foo: no such file 'nonexistent_filename'" ]
}
```
The `$status` variable contains the status code of the command, and
the `$output` variable contains the combined contents of the command's
standard output and standard error streams.
A third special variable, the `$lines` array, is available for easily
accessing individual lines of output. For example, if you want to test
that invoking `foo` without any arguments prints usage information on
the first line:
```bash
@test "invoking foo without arguments prints usage" {
run foo
[ "$status" -eq 1 ]
[ "${lines[0]}" = "usage: foo <filename>" ]
}
```
### `load`: Share common code
You may want to share common code across multiple test files. Bats
includes a convenient `load` 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 `test/foo.bats`, the command
```bash
load test_helper
```
will source the script `test/test_helper.bash` in your test file. This
can be useful for sharing functions to set up your environment or load
fixtures.
### `skip`: Easily skip tests
Tests can be skipped by using the `skip` command at the point in a
test you wish to skip.
```bash
@test "A test I don't want to execute for now" {
skip
run foo
[ "$status" -eq 0 ]
}
```
Optionally, you may include a reason for skipping:
```bash
@test "A test I don't want to execute for now" {
skip "This command will return zero soon, but not now"
run foo
[ "$status" -eq 0 ]
}
```
Or you can skip conditionally:
```bash
@test "A test which should run" {
if [ foo != bar ]; then
skip "foo isn't bar"
fi
run foo
[ "$status" -eq 0 ]
}
```
### `setup` and `teardown`: Pre- and post-test hooks
You can define special `setup` and `teardown` 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.
### Code outside of test cases
You can include code in your test file outside of `@test` 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 `@test`, `setup` or `teardown` functions
must be redirected to `stderr` (`>&2`). Otherwise, the output may
cause Bats to fail by polluting the TAP stream on `stdout`.
### Special variables
There are several global variables you can use to introspect on Bats
tests:
* `$BATS_TEST_FILENAME` is the fully expanded path to the Bats test
file.
* `$BATS_TEST_DIRNAME` is the directory in which the Bats test file is
located.
* `$BATS_TEST_NAMES` is an array of function names for each test case.
* `$BATS_TEST_NAME` is the name of the function containing the current
test case.
* `$BATS_TEST_DESCRIPTION` is the description of the current test
case.
* `$BATS_TEST_NUMBER` is the (1-based) index of the current test case
in the test file.
* `$BATS_TMPDIR` is the location to a directory that may be used to
store temporary files.
## Installing Bats from source
Check out a copy of the Bats repository. Then, either add the Bats
`bin` directory to your `$PATH`, or run the provided `install.sh`
command with the location to the prefix in which you want to install
Bats. For example, to install Bats into `/usr/local`,
$ git clone https://github.com/bats-core/bats-core.git
$ cd bats-core
$ ./install.sh /usr/local
Note that you may need to run `install.sh` with `sudo` if you do not
have permission to write to the installation prefix.
## Running Bats in Docker
Check out a copy of the Bats repository, then build a container image:
$ git clone https://github.com/bats-core/bats-core.git
$ cd bats-core
$ docker build --tag bats:latest .
This creates a local Docker image called `bats:latest` based on [Alpine
Linux](https://github.com/gliderlabs/docker-alpine/blob/master/docs/usage.md).
To run Bats' internal test suite (which is in the container image at
`/opt/bats/test`):
$ docker run -it bats:latest /opt/bats/test
To run a test suite from your local machine, mount in a volume and direct
Bats to its path inside the container:
$ docker run -it -v "$(pwd):/code" bats:latest /code/test
This is a minimal image. If more tools are required this can be used as a
base image in a Dockerfile using `FROM <Docker image>`.
In the future there may be images based on Debian, and/or with more tools
installed (`curl` and `openssl`, for example). If you require a specific
configuration please search and +1 an issue or
[raise a new issue](https://github.com/bats-core/bats-core/issues).
## Support
The Bats source code repository is [hosted on
GitHub](https://github.com/bats-core/bats-core). There you can file bugs
on the issue tracker or submit tested pull requests for review.
For real-world examples from open-source projects using Bats, see
[Projects Using Bats](https://github.com/bats-core/bats-core/wiki/Projects-Using-Bats)
on the wiki.
To learn how to set up your editor for Bats syntax highlighting, see
[Syntax Highlighting](https://github.com/bats-core/bats-core/wiki/Syntax-Highlighting)
on the wiki.
## Version history
*0.4.0* (August 13, 2014)
* Improved the display of failing test cases. Bats now shows the
source code of failing test lines, along with full stack traces
including function names, filenames, and line numbers.
* Improved the display of the pretty-printed test summary line to
include the number of skipped tests, if any.
* Improved the speed of the preprocessor, dramatically shortening test
and suite startup times.
* Added support for absolute pathnames to the `load` helper.
* Added support for single-line `@test` definitions.
* Added bats(1) and bats(7) manual pages.
* Modified the `bats` command to default to TAP output when the `$CI`
variable is set, to better support environments such as Travis CI.
*0.3.1* (October 28, 2013)
* Fixed an incompatibility with the pretty formatter in certain
environments such as tmux.
* Fixed a bug where the pretty formatter would crash if the first line
of a test file's output was invalid TAP.
*0.3.0* (October 21, 2013)
* Improved formatting for tests run from a terminal. Failing tests
are now colored in red, and the total number of failing tests is
displayed at the end of the test run. When Bats is not connected to
a terminal (e.g. in CI runs), or when invoked with the `--tap` flag,
output is displayed in standard TAP format.
* Added the ability to skip tests using the `skip` command.
* Added a message to failing test case output indicating the file and
line number of the statement that caused the test to fail.
* Added "ad-hoc" test suite support. You can now invoke `bats` with
multiple filename or directory arguments to run all the specified
tests in aggregate.
* Added support for test files with Windows line endings.
* Fixed regular expression warnings from certain versions of Bash.
* Fixed a bug running tests containing lines that begin with `-e`.
*0.2.0* (November 16, 2012)
* Added test suite support. The `bats` command accepts a directory
name containing multiple test files to be run in aggregate.
* Added the ability to count the number of test cases in a file or
suite by passing the `-c` flag to `bats`.
* Preprocessed sources are cached between test case runs in the same
file for better performance.
*0.1.0* (December 30, 2011)
* Initial public release.
---
© 2017 Bianca Tamayo (bats-core organization)
© 2014 Sam Stephenson
Bats is released under an MIT-style license;
see `LICENSE` for details.

+ 1
- 0
bin/bats View File

@ -0,0 +1 @@
../libexec/bats

+ 48
- 0
install.sh View File

@ -0,0 +1,48 @@
#!/usr/bin/env bash
set -e
resolve_link() {
$(type -p greadlink readlink | head -n1) "$1"
}
abs_dirname() {
local cwd="$(pwd)"
local path="$1"
while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done
pwd
cd "$cwd"
}
PREFIX="$1"
if [ -z "$1" ]; then
{ echo "usage: $0 <prefix>"
echo " e.g. $0 /usr/local"
} >&2
exit 1
fi
BATS_ROOT="$(abs_dirname "$0")"
mkdir -p "$PREFIX"/{bin,libexec,share/man/man{1,7}}
cp -R "$BATS_ROOT"/bin/* "$PREFIX"/bin
cp -R "$BATS_ROOT"/libexec/* "$PREFIX"/libexec
cp "$BATS_ROOT"/man/bats.1 "$PREFIX"/share/man/man1
cp "$BATS_ROOT"/man/bats.7 "$PREFIX"/share/man/man7
# fix broken symbolic link file
if [ ! -L "$PREFIX"/bin/bats ]; then
dir="$(readlink -e "$PREFIX")"
rm -f "$dir"/bin/bats
ln -s "$dir"/libexec/bats "$dir"/bin/bats
fi
# fix file permission
chmod a+x "$PREFIX"/bin/*
chmod a+x "$PREFIX"/libexec/*
echo "Installed Bats to $PREFIX/bin/bats"

+ 169
- 0
libexec/bats View File

@ -0,0 +1,169 @@
#!/usr/bin/env bash
set -e
version() {
echo "Bats 0.4.0"
}
usage() {
version
echo "Usage: bats [-c] [-p | -t] <test> [<test> ...]"
}
help() {
usage
echo
echo " <test> is the path to a Bats test file, or the path to a directory"
echo " containing Bats test files."
echo
echo " -c, --count Count the number of test cases without running any tests"
echo " -h, --help Display this help message"
echo " -p, --pretty Show results in pretty format (default for terminals)"
echo " -t, --tap Show results in TAP format"
echo " -v, --version Display the version number"
echo
echo " For more information, see https://github.com/bats-core/bats-core"
echo
}
BATS_READLINK=
resolve_link() {
if [[ -z "$BATS_READLINK" ]]; then
if command -v 'greadlink' >/dev/null; then
BATS_READLINK='greadlink'
elif command -v 'readlink' >/dev/null; then
BATS_READLINK='readlink'
else
BATS_READLINK='true'
fi
fi
"$BATS_READLINK" "$1" || return 0
}
abs_dirname() {
local cwd="$PWD"
local path="$1"
while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name")"
done
printf -v "$2" -- '%s' "$PWD"
cd "$cwd"
}
expand_path() {
local path="${1%/}"
local dirname="${path%/*}"
if [[ "$dirname" == "$path" ]]; then
dirname="$PWD"
elif cd "$dirname" 2>/dev/null; then
dirname="$PWD"
cd "$OLDPWD"
else
printf '%s' "$path"
return
fi
printf -v "$2" '%s/%s' "$dirname" "${path##*/}"
}
abs_dirname "$0" 'BATS_LIBEXEC'
abs_dirname "$BATS_LIBEXEC" 'BATS_PREFIX'
abs_dirname '.' 'BATS_CWD'
export BATS_PREFIX
export BATS_CWD
export BATS_TEST_PATTERN="^[[:blank:]]*@test[[:blank:]]+(.*[^[:blank:]])[[:blank:]]+\{(.*)\$"
export PATH="$BATS_LIBEXEC:$PATH"
options=()
arguments=()
for arg in "$@"; do
if [ "${arg:0:1}" = "-" ]; then
if [ "${arg:1:1}" = "-" ]; then
options[${#options[*]}]="${arg:2}"
else
index=1
while option="${arg:$index:1}"; do
[ -n "$option" ] || break
options[${#options[*]}]="$option"
let index+=1
done
fi
else
arguments[${#arguments[*]}]="$arg"
fi
done
unset count_flag pretty
count_flag=''
pretty=''
[ -t 0 ] && [ -t 1 ] && pretty="1"
[ -n "${CI:-}" ] && pretty=""
if [[ "${#options[@]}" -ne '0' ]]; then
for option in "${options[@]}"; do
case "$option" in
"h" | "help" )
help
exit 0
;;
"v" | "version" )
version
exit 0
;;
"c" | "count" )
count_flag="-c"
;;
"t" | "tap" )
pretty=""
;;
"p" | "pretty" )
pretty="1"
;;
* )
usage >&2
exit 1
;;
esac
done
fi
if [ "${#arguments[@]}" -eq 0 ]; then
usage >&2
exit 1
fi
filenames=()
for filename in "${arguments[@]}"; do
expand_path "$filename" 'filename'
if [ -d "$filename" ]; then
shopt -s nullglob
for suite_filename in "$filename"/*.bats; do
filenames["${#filenames[@]}"]="$suite_filename"
done
shopt -u nullglob
else
filenames["${#filenames[@]}"]="$filename"
fi
done
if [ "${#filenames[@]}" -eq 1 ]; then
command="bats-exec-test"
else
command="bats-exec-suite"
fi
set -o pipefail execfail
if [ -z "$pretty" ]; then
exec "$command" $count_flag "${filenames[@]}"
else
extended_syntax_flag="-x"
formatter="bats-format-tap-stream"
exec "$command" $count_flag $extended_syntax_flag "${filenames[@]}" | "$formatter"
fi

+ 59
- 0
libexec/bats-exec-suite View File

@ -0,0 +1,59 @@
#!/usr/bin/env bash
set -e
count_only_flag=""
if [ "$1" = "-c" ]; then
count_only_flag=1
shift
fi
extended_syntax_flag=""
if [ "$1" = "-x" ]; then
extended_syntax_flag="-x"
shift
fi
trap "kill 0; exit 1" int
count=0
for filename in "$@"; do
while IFS= read -r line; do
if [[ "$line" =~ $BATS_TEST_PATTERN ]]; then
let count+=1
fi
done <"$filename"
done
if [ -n "$count_only_flag" ]; then
echo "$count"
exit
fi
echo "1..$count"
status=0
offset=0
for filename in "$@"; do
index=0
{
IFS= read -r # 1..n
while IFS= read -r line; do
case "$line" in
"begin "* )
let index+=1
echo "${line/ $index / $(($offset + $index)) }"
;;
"ok "* | "not ok "* )
[ -n "$extended_syntax_flag" ] || let index+=1
echo "${line/ $index / $(($offset + $index)) }"
[ "${line:0:6}" != "not ok" ] || status=1
;;
* )
echo "$line"
;;
esac
done
} < <( bats-exec-test $extended_syntax_flag "$filename" )
offset=$(($offset + $index))
done
exit "$status"

+ 364
- 0
libexec/bats-exec-test View File

@ -0,0 +1,364 @@
#!/usr/bin/env bash
set -e
set -E
set -T
BATS_COUNT_ONLY=""
if [ "$1" = "-c" ]; then
BATS_COUNT_ONLY=1
shift
fi
BATS_EXTENDED_SYNTAX=""
if [ "$1" = "-x" ]; then
BATS_EXTENDED_SYNTAX="$1"
shift
fi
BATS_TEST_FILENAME="$1"
if [ -z "$BATS_TEST_FILENAME" ]; then
echo "usage: bats-exec <filename>" >&2
exit 1
elif [ ! -f "$BATS_TEST_FILENAME" ]; then
echo "bats: $BATS_TEST_FILENAME does not exist" >&2
exit 1
else
shift
fi
BATS_TEST_DIRNAME="${BATS_TEST_FILENAME%/*}"
BATS_TEST_NAMES=()
load() {
local name="$1"
local filename
if [ "${name:0:1}" = "/" ]; then
filename="${name}"
else
filename="$BATS_TEST_DIRNAME/${name}.bash"
fi
if [[ ! -f "$filename" ]]; then
echo "bats: $filename does not exist" >&2
exit 1
fi
source "${filename}"
}
run() {
local e E T oldIFS
[[ ! "$-" =~ e ]] || e=1
[[ ! "$-" =~ E ]] || E=1
[[ ! "$-" =~ T ]] || T=1
set +e
set +E
set +T
output="$("$@" 2>&1)"
status="$?"
oldIFS=$IFS
IFS=$'\n' lines=($output)
[ -z "$e" ] || set -e
[ -z "$E" ] || set -E
[ -z "$T" ] || set -T
IFS=$oldIFS
}
setup() {
true
}
teardown() {
true
}
BATS_TEST_SKIPPED=''
skip() {
BATS_TEST_SKIPPED=${1:-1}
BATS_TEST_COMPLETED=1
exit 0
}
bats_test_begin() {
BATS_TEST_DESCRIPTION="$1"
if [ -n "$BATS_EXTENDED_SYNTAX" ]; then
echo "begin $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
fi
setup
}
bats_test_function() {
local test_name="$1"
BATS_TEST_NAMES+=("$test_name")
}
BATS_CURRENT_STACK_TRACE=()
BATS_PREVIOUS_STACK_TRACE=()
bats_capture_stack_trace() {
if [[ "${#BATS_CURRENT_STACK_TRACE[@]}" -ne '0' ]]; then
BATS_PREVIOUS_STACK_TRACE=("${BATS_CURRENT_STACK_TRACE[@]}")
fi
BATS_CURRENT_STACK_TRACE=()
local test_pattern=" $BATS_TEST_NAME $BATS_TEST_SOURCE"
local setup_pattern=" setup $BATS_TEST_SOURCE"
local teardown_pattern=" teardown $BATS_TEST_SOURCE"
local frame
local i
for ((i=2; i != ${#FUNCNAME[@]}; ++i)); do
frame="${BASH_LINENO[$((i-1))]} ${FUNCNAME[$i]} ${BASH_SOURCE[$i]}"
BATS_CURRENT_STACK_TRACE["${#BATS_CURRENT_STACK_TRACE[@]}"]="$frame"
if [[ "$frame" = *"$test_pattern" || \
"$frame" = *"$setup_pattern" || \
"$frame" = *"$teardown_pattern" ]]; then
break
fi
done
bats_frame_filename "${BATS_CURRENT_STACK_TRACE[0]}" 'BATS_SOURCE'
bats_frame_lineno "${BATS_CURRENT_STACK_TRACE[0]}" 'BATS_LINENO'
}
bats_print_stack_trace() {
local frame
local index=1
local count="${#@}"
local filename
local lineno
for frame in "$@"; do
bats_frame_filename "$frame" 'filename'
bats_trim_filename "$filename" 'filename'
bats_frame_lineno "$frame" 'lineno'
if [ $index -eq 1 ]; then
echo -n "# ("
else
echo -n "# "
fi
local fn
bats_frame_function "$frame" 'fn'
if [ "$fn" != "$BATS_TEST_NAME" ]; then
echo -n "from function \`$fn' "
fi
if [ $index -eq $count ]; then
echo "in test file $filename, line $lineno)"
else
echo "in file $filename, line $lineno,"
fi
let index+=1
done
}
bats_print_failed_command() {
local frame="$1"
local status="$2"
local filename
local lineno
local failed_line
local failed_command
bats_frame_filename "$frame" 'filename'
bats_frame_lineno "$frame" 'lineno'
bats_extract_line "$filename" "$lineno" 'failed_line'
bats_strip_string "$failed_line" 'failed_command'
printf '%s' "# \`${failed_command}' "
if [ $status -eq 1 ]; then
echo "failed"
else
echo "failed with status $status"
fi
}
bats_frame_lineno() {
printf -v "$2" '%s' "${1%% *}"
}
bats_frame_function() {
local __bff_function="${1#* }"
printf -v "$2" '%s' "${__bff_function%% *}"
}
bats_frame_filename() {
local __bff_filename="${1#* }"
__bff_filename="${__bff_filename#* }"
if [ "$__bff_filename" = "$BATS_TEST_SOURCE" ]; then
__bff_filename="$BATS_TEST_FILENAME"
fi
printf -v "$2" '%s' "$__bff_filename"
}
bats_extract_line() {
local __bats_extract_line_line
local __bats_extract_line_index='0'
while IFS= read -r __bats_extract_line_line; do
if [[ "$((++__bats_extract_line_index))" -eq "$2" ]]; then
printf -v "$3" '%s' "${__bats_extract_line_line%$'\r'}"
break
fi
done <"$1"
}
bats_strip_string() {
[[ "$1" =~ ^[[:space:]]*(.*)[[:space:]]*$ ]]
printf -v "$2" '%s' "${BASH_REMATCH[1]}"
}
bats_trim_filename() {
if [[ "$1" =~ ^${BATS_CWD}/ ]]; then
printf -v "$2" '%s' "${1#$BATS_CWD/}"
else
printf -v "$2" '%s' "$1"
fi
}
bats_debug_trap() {
if [ "$BASH_SOURCE" != "$1" ]; then
bats_capture_stack_trace
fi
}
# When running under Bash 3.2.57(1)-release on macOS, the `ERR` trap may not
# always fire, but the `EXIT` trap will. For this reason we call it at the very
# beginning of `bats_teardown_trap` (the `DEBUG` trap for the call will move
# `BATS_CURRENT_STACK_TRACE` to `BATS_PREVIOUS_STACK_TRACE`) and check the value
# of `$?` before taking other actions.
bats_error_trap() {
local status="$?"
if [[ "$status" -ne '0' ]]; then
BATS_ERROR_STATUS="$status"
BATS_ERROR_STACK_TRACE=( "${BATS_PREVIOUS_STACK_TRACE[@]}" )
trap - debug
fi
}
bats_teardown_trap() {
bats_error_trap
trap "bats_exit_trap" exit
local status=0
teardown >>"$BATS_OUT" 2>&1 || status="$?"
if [ $status -eq 0 ]; then
BATS_TEARDOWN_COMPLETED=1
elif [ -n "$BATS_TEST_COMPLETED" ]; then
BATS_ERROR_STATUS="$status"
BATS_ERROR_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" )
fi
bats_exit_trap
}
bats_exit_trap() {
local status
local skipped=''
trap - err exit
if [ -n "$BATS_TEST_SKIPPED" ]; then
skipped=" # skip"
if [ "1" != "$BATS_TEST_SKIPPED" ]; then
skipped+=" $BATS_TEST_SKIPPED"
fi
fi
if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then
echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
bats_print_stack_trace "${BATS_ERROR_STACK_TRACE[@]}" >&3
bats_print_failed_command "${BATS_ERROR_STACK_TRACE[${#BATS_ERROR_STACK_TRACE[@]}-1]}" "$BATS_ERROR_STATUS" >&3
sed -e "s/^/# /" < "$BATS_OUT" >&3
status=1
else
echo "ok ${BATS_TEST_NUMBER} ${BATS_TEST_DESCRIPTION}${skipped}" >&3
status=0
fi
rm -f "$BATS_OUT"
exit "$status"
}
bats_perform_tests() {
echo "1..$#"
test_number=1
status=0
for test_name in "$@"; do
"$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1
let test_number+=1
done
exit "$status"
}
bats_perform_test() {
BATS_TEST_NAME="$1"
if declare -F "$BATS_TEST_NAME" >/dev/null; then
BATS_TEST_NUMBER="$2"
if [ -z "$BATS_TEST_NUMBER" ]; then
echo "1..1"
BATS_TEST_NUMBER="1"
fi
BATS_TEST_COMPLETED=""
BATS_TEARDOWN_COMPLETED=""
trap "bats_debug_trap \"\$BASH_SOURCE\"" debug
trap "bats_error_trap" err
trap "bats_teardown_trap" exit
"$BATS_TEST_NAME" >>"$BATS_OUT" 2>&1
BATS_TEST_COMPLETED=1
else
echo "bats: unknown test name \`$BATS_TEST_NAME'" >&2
exit 1
fi
}
if [ -z "$TMPDIR" ]; then
BATS_TMPDIR="/tmp"
else
BATS_TMPDIR="${TMPDIR%/}"
fi
BATS_TMPNAME="$BATS_TMPDIR/bats.$$"
BATS_PARENT_TMPNAME="$BATS_TMPDIR/bats.$PPID"
BATS_OUT="${BATS_TMPNAME}.out"
bats_preprocess_source() {
BATS_TEST_SOURCE="${BATS_TMPNAME}.src"
. bats-preprocess <<< "$(< "$BATS_TEST_FILENAME")"$'\n' > "$BATS_TEST_SOURCE"
trap "bats_cleanup_preprocessed_source" err exit
trap "bats_cleanup_preprocessed_source; exit 1" int
}
bats_cleanup_preprocessed_source() {
rm -f "$BATS_TEST_SOURCE"
}
bats_evaluate_preprocessed_source() {
if [ -z "$BATS_TEST_SOURCE" ]; then
BATS_TEST_SOURCE="${BATS_PARENT_TMPNAME}.src"
fi
source "$BATS_TEST_SOURCE"
}
exec 3<&1
if [ "$#" -eq 0 ]; then
bats_preprocess_source
bats_evaluate_preprocessed_source
if [ -n "$BATS_COUNT_ONLY" ]; then
echo "${#BATS_TEST_NAMES[@]}"
else
bats_perform_tests "${BATS_TEST_NAMES[@]}"
fi
else
bats_evaluate_preprocessed_source
bats_perform_test "$@"
fi

+ 173
- 0
libexec/bats-format-tap-stream View File

@ -0,0 +1,173 @@
#!/usr/bin/env bash
set -e
# Just stream the TAP output (sans extended syntax) if tput is missing
command -v tput >/dev/null || exec grep -v "^begin "
header_pattern='[0-9]+\.\.[0-9]+'
IFS= read -r header
if [[ "$header" =~ $header_pattern ]]; then
count="${header:3}"
index=0
failures=0
skipped=0
name=""
count_column_width=$(( ${#count} * 2 + 2 ))
else
# If the first line isn't a TAP plan, print it and pass the rest through
printf "%s\n" "$header"
exec cat
fi
update_screen_width() {
screen_width="$(tput cols)"
count_column_left=$(( $screen_width - $count_column_width ))
}
trap update_screen_width WINCH
update_screen_width
begin() {
go_to_column 0
printf_with_truncation $(( $count_column_left - 1 )) " %s" "$name"
clear_to_end_of_line
go_to_column $count_column_left
printf "%${#count}s/${count}" "$index"
go_to_column 1
}
pass() {
go_to_column 0
printf " ✓ %s" "$name"
advance
}
skip() {
local reason="$1"
[ -z "$reason" ] || reason=": $reason"
go_to_column 0
printf " - %s (skipped%s)" "$name" "$reason"
advance
}
fail() {
go_to_column 0
set_color 1 bold
printf " ✗ %s" "$name"
advance
}
log() {
set_color 1
printf " %s\n" "$1"
clear_color
}
summary() {
printf "\n%d test" "$count"
if [[ "$count" -ne '1' ]]; then
printf 's'
fi
printf ", %d failure" "$failures"
if [[ "$failures" -ne '1' ]]; then
printf 's'
fi
if [ "$skipped" -gt 0 ]; then
printf ", %d skipped" "$skipped"
fi
printf "\n"
}
printf_with_truncation() {
local width="$1"
shift
local string
printf -v 'string' -- "$@"
if [ "${#string}" -gt "$width" ]; then
printf "%s..." "${string:0:$(( $width - 4 ))}"
else
printf "%s" "$string"
fi
}
go_to_column() {
local column="$1"
printf "\x1B[%dG" $(( $column + 1 ))
}
clear_to_end_of_line() {
printf "\x1B[K"
}
advance() {
clear_to_end_of_line
echo
clear_color
}
set_color() {
local color="$1"
local weight='22'
if [[ "$2" == 'bold' ]]; then
weight='1'
fi
printf "\x1B[%d;%dm" $(( 30 + $color )) "$weight"
}
clear_color() {
printf "\x1B[0m"
}
_buffer=""
buffer() {
_buffer="${_buffer}$("$@")"
}
flush() {
printf "%s" "$_buffer"
_buffer=""
}
finish() {
flush
printf "\n"
}
trap finish EXIT
while IFS= read -r line; do
case "$line" in
"begin "* )
let index+=1
name="${line#* $index }"
buffer begin
flush
;;
"ok "* )
skip_expr="ok $index (.*) # skip ?(([^)]*))?"
if [[ "$line" =~ $skip_expr ]]; then
let skipped+=1
buffer skip "${BASH_REMATCH[2]}"
else
buffer pass
fi
;;
"not ok "* )
let failures+=1
buffer fail
;;
"# "* )
buffer log "${line:2}"
;;
esac
done
buffer summary

+ 54
- 0
libexec/bats-preprocess View File

@ -0,0 +1,54 @@
#!/usr/bin/env bash
set -e
encode_name() {
local name="$1"
local result="test_"
local hex_code
if [[ ! "$name" =~ [^[:alnum:]\ _-] ]]; then
name="${name//_/-5f}"
name="${name//-/-2d}"
name="${name// /_}"
result+="$name"
else
local length="${#name}"
local char i
for ((i=0; i<length; i++)); do
char="${name:$i:1}"
if [ "$char" = " " ]; then
result+="_"
elif [[ "$char" =~ [[:alnum:]] ]]; then
result+="$char"
else
printf -v 'hex_code' -- "-%02x" \'"$char"
result+="$hex_code"
fi
done
fi
printf -v "$2" '%s' "$result"
}
tests=()
index=0
while IFS= read -r line; do
line="${line//$'\r'}"
let index+=1
if [[ "$line" =~ $BATS_TEST_PATTERN ]]; then
name="${BASH_REMATCH[1]#[\'\"]}"
name="${name%[\'\"]}"
body="${BASH_REMATCH[2]}"
encode_name "$name" 'encoded_name'
tests["${#tests[@]}"]="$encoded_name"
echo "${encoded_name}() { bats_test_begin \"${name}\" ${index}; ${body}"
else
printf "%s\n" "$line"
fi
done
for test_name in "${tests[@]}"; do
echo "bats_test_function ${test_name}"
done

+ 10
- 0
man/Makefile View File

@ -0,0 +1,10 @@
RONN := ronn
PAGES := bats.1 bats.7
all: $(PAGES)
bats.1: bats.1.ronn
$(RONN) -r $<
bats.7: bats.7.ronn
$(RONN) -r $<

+ 5
- 0
man/README.md View File

@ -0,0 +1,5 @@
Bats man pages are generated with [Ronn](http://rtomayko.github.io/ronn/).
After making changes to `bats.1.ronn` or `bats.7.ronn`, run `make` in
this directory to generate `bats.1` and `bats.7`. **Do not edit the
`bats.1` or `bats.7` files directly.**

+ 103
- 0
man/bats.1 View File

@ -0,0 +1,103 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "BATS" "1" "August 2014" "" ""
.
.SH "NAME"
\fBbats\fR \- Bash Automated Testing System
.
.SH "SYNOPSIS"
bats [\-c] [\-p | \-t] \fItest\fR [\fItest\fR \.\.\.]
.
.P
\fItest\fR is the path to a Bats test file, or the path to a directory containing Bats test files\.
.
.SH "DESCRIPTION"
Bats is a TAP\-compliant testing framework for Bash\. It provides a simple way to verify that the UNIX programs you write behave as expected\.
.
.P
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\.
.
.P
Test cases consist of standard shell commands\. Bats makes use of Bash\'s \fBerrexit\fR (\fBset \-e\fR) option when running test cases\. If every command in the test case exits with a \fB0\fR status code (success), the test passes\. In this way, each line is an assertion of truth\.
.
.P
See \fBbats\fR(7) for more information on writing Bats tests\.
.
.SH "RUNNING TESTS"
To run your tests, invoke the \fBbats\fR interpreter with a path to a test file\. The file\'s test cases are run sequentially and in isolation\. If all the test cases pass, \fBbats\fR exits with a \fB0\fR status code\. If there are any failures, \fBbats\fR exits with a \fB1\fR status code\.
.
.P
You can invoke the \fBbats\fR interpreter with multiple test file arguments, or with a path to a directory containing multiple \fB\.bats\fR files\. Bats will run each test file individually and aggregate the results\. If any test case fails, \fBbats\fR exits with a \fB1\fR status code\.
.
.SH "OPTIONS"
.
.TP
\fB\-c\fR, \fB\-\-count\fR
Count the number of test cases without running any tests
.
.TP
\fB\-h\fR, \fB\-\-help\fR
Display help message
.
.TP
\fB\-p\fR, \fB\-\-pretty\fR
Show results in pretty format (default for terminals)
.
.TP
\fB\-t\fR, \fB\-\-tap\fR
Show results in TAP format
.
.TP
\fB\-v\fR, \fB\-\-version\fR
Display the version number
.
.SH "OUTPUT"
When you run Bats from a terminal, you\'ll see output as each test is performed, with a check\-mark next to the test\'s name if it passes or an "X" if it fails\.
.
.IP "" 4
.
.nf
$ bats addition\.bats
✓ addition using bc
✓ addition using dc
2 tests, 0 failures
.
.fi
.
.IP "" 0
.
.P
If Bats is not connected to a terminal\-\-in other words, if you run it from a continuous integration system or redirect its output to a file\-\-the results are displayed in human\-readable, machine\-parsable TAP format\. You can force TAP output from a terminal by invoking Bats with the \fB\-\-tap\fR option\.
.
.IP "" 4
.
.nf
$ bats \-\-tap addition\.bats
1\.\.2
ok 1 addition using bc
ok 2 addition using dc
.
.fi
.
.IP "" 0
.
.SH "EXIT STATUS"
The \fBbats\fR interpreter exits with a value of \fB0\fR if all test cases pass, or \fB1\fR if one or more test cases fail\.
.
.SH "SEE ALSO"
Bats wiki: \fIhttps://github\.com/bats\-core/bats\-core/wiki/\fR
.
.P
\fBbash\fR(1), \fBbats\fR(7)
.
.SH "COPYRIGHT"
(c) 2017 Bianca Tamayo (bats-core organization)
(c) 2014 Sam Stephenson
.
.P
Bats is released under the terms of an MIT\-style license\.

+ 110
- 0
man/bats.1.ronn View File

@ -0,0 +1,110 @@
bats(1) -- Bash Automated Testing System
========================================
SYNOPSIS
--------
bats [-c] [-p | -t] <test> [<test> ...]
<test> is the path to a Bats test file, or the path to a directory
containing Bats test files.
DESCRIPTION
-----------
Bats is a TAP-compliant testing framework for Bash. It provides a simple
way to verify that the UNIX programs you write behave as expected.
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.
Test cases consist of standard shell commands. Bats makes use of
Bash's `errexit` (`set -e`) option when running test cases. If every
command in the test case exits with a `0` status code (success), the
test passes. In this way, each line is an assertion of truth.
See `bats`(7) for more information on writing Bats tests.
RUNNING TESTS
-------------
To run your tests, invoke the `bats` interpreter with a path to a test
file. The file's test cases are run sequentially and in isolation. If
all the test cases pass, `bats` exits with a `0` status code. If there
are any failures, `bats` exits with a `1` status code.
You can invoke the `bats` interpreter with multiple test file arguments,
or with a path to a directory containing multiple `.bats` files. Bats
will run each test file individually and aggregate the results. If any
test case fails, `bats` exits with a `1` status code.
OPTIONS
-------
* `-c`, `--count`:
Count the number of test cases without running any tests
* `-h`, `--help`:
Display help message
* `-p`, `--pretty`:
Show results in pretty format (default for terminals)
* `-t`, `--tap`:
Show results in TAP format
* `-v`, `--version`:
Display the version number
OUTPUT
------
When you run Bats from a terminal, you'll see output as each test is
performed, with a check-mark next to the test's name if it passes or
an "X" if it fails.
$ bats addition.bats
✓ addition using bc
✓ addition using dc
2 tests, 0 failures
If Bats is not connected to a terminal--in other words, if you run it
from a continuous integration system or redirect its output to a
file--the results are displayed in human-readable, machine-parsable
TAP format. You can force TAP output from a terminal by invoking Bats
with the `--tap` option.
$ bats --tap addition.bats
1..2
ok 1 addition using bc
ok 2 addition using dc
EXIT STATUS
-----------
The `bats` interpreter exits with a value of `0` if all test cases pass,
or `1` if one or more test cases fail.
SEE ALSO
--------
Bats wiki: _https://github.com/bats\-core/bats\-core/wiki/_
`bash`(1), `bats`(7)
COPYRIGHT
---------
(c) 2017 Bianca Tamayo (bats-core organization)
(c) 2014 Sam Stephenson
Bats is released under the terms of an MIT-style license.

+ 178
- 0
man/bats.7 View File

@ -0,0 +1,178 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "BATS" "7" "November 2013" "" ""
.
.SH "NAME"
\fBbats\fR \- Bats test file format
.
.SH "DESCRIPTION"
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\.
.
.IP "" 4
.
.nf
#!/usr/bin/env bats
@test "addition using bc" {
result="$(echo 2+2 | bc)"
[ "$result" \-eq 4 ]
}
@test "addition using dc" {
result="$(echo 2 2+p | dc)"
[ "$result" \-eq 4 ]
}
.
.fi
.
.IP "" 0
.
.P
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\.
.
.SH "THE RUN HELPER"
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\.
.
.P
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\.
.
.IP "" 4
.
.nf
@test "invoking foo with a nonexistent file prints an error" {
run foo nonexistent_filename
[ "$status" \-eq 1 ]
[ "$output" = "foo: no such file \'nonexistent_filename\'" ]
}
.
.fi
.
.IP "" 0
.
.P
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\.
.
.P
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:
.
.IP "" 4
.
.nf
@test "invoking foo without arguments prints usage" {
run foo
[ "$status" \-eq 1 ]
[ "${lines[0]}" = "usage: foo <filename>" ]
}
.
.fi
.
.IP "" 0
.
.SH "THE LOAD COMMAND"
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
.
.IP "" 4
.
.nf
load test_helper
.
.fi
.
.IP "" 0
.
.P
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\.
.
.SH "THE SKIP COMMAND"
Tests can be skipped by using the \fBskip\fR command at the point in a test you wish to skip\.
.
.IP "" 4
.
.nf
@test "A test I don\'t want to execute for now" {
skip
run foo
[ "$status" \-eq 0 ]
}
.
.fi
.
.IP "" 0
.
.P
Optionally, you may include a reason for skipping:
.
.IP "" 4
.
.nf
@test "A test I don\'t want to execute for now" {
skip "This command will return zero soon, but not now"
run foo
[ "$status" \-eq 0 ]
}
.
.fi
.
.IP "" 0
.
.P
Or you can skip conditionally:
.
.IP "" 4
.
.nf
@test "A test which should run" {
if [ foo != bar ]; then
skip "foo isn\'t bar"
fi
run foo
[ "$status" \-eq 0 ]
}
.
.fi
.
.IP "" 0
.
.SH "SETUP AND TEARDOWN FUNCTIONS"
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\.
.
.SH "CODE OUTSIDE OF TEST CASES"
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\.
.
.SH "SPECIAL VARIABLES"
There are several global variables you can use to introspect on Bats tests:
.
.IP "\(bu" 4
\fB$BATS_TEST_FILENAME\fR is the fully expanded path to the Bats test file\.
.
.IP "\(bu" 4
\fB$BATS_TEST_DIRNAME\fR is the directory in which the Bats test file is located\.
.
.IP "\(bu" 4
\fB$BATS_TEST_NAMES\fR is an array of function names for each test case\.
.
.IP "\(bu" 4
\fB$BATS_TEST_NAME\fR is the name of the function containing the current test case\.
.
.IP "\(bu" 4
\fB$BATS_TEST_DESCRIPTION\fR is the description of the current test case\.
.
.IP "\(bu" 4
\fB$BATS_TEST_NUMBER\fR is the (1\-based) index of the current test case in the test file\.
.
.IP "\(bu" 4
\fB$BATS_TMPDIR\fR is the location to a directory that may be used to store temporary files\.
.
.IP "" 0
.
.SH "SEE ALSO"
\fBbash\fR(1), \fBbats\fR(1)

+ 156
- 0
man/bats.7.ronn View File

@ -0,0 +1,156 @@
bats(7) -- Bats test file format
================================
DESCRIPTION
-----------
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.
#!/usr/bin/env bats
@test "addition using bc" {
result="$(echo 2+2 | bc)"
[ "$result" -eq 4 ]
}
@test "addition using dc" {
result="$(echo 2 2+p | dc)"
[ "$result" -eq 4 ]
}
Each Bats test file is evaluated n+1 times, where _n_ 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.
THE RUN HELPER
--------------
Many Bats tests need to run a command and then make assertions about
its exit status and output. Bats includes a `run` helper that invokes
its arguments as a command, saves the exit status and output into
special global variables, and then returns with a `0` status code so
you can continue to make assertions in your test case.
For example, let's say you're testing that the `foo` command, when
passed a nonexistent filename, exits with a `1` status code and prints
an error message.
@test "invoking foo with a nonexistent file prints an error" {
run foo nonexistent_filename
[ "$status" -eq 1 ]
[ "$output" = "foo: no such file 'nonexistent_filename'" ]
}
The `$status` variable contains the status code of the command, and
the `$output` variable contains the combined contents of the command's
standard output and standard error streams.
A third special variable, the `$lines` array, is available for easily
accessing individual lines of output. For example, if you want to test
that invoking `foo` without any arguments prints usage information on
the first line:
@test "invoking foo without arguments prints usage" {
run foo
[ "$status" -eq 1 ]
[ "${lines[0]}" = "usage: foo <filename>" ]
}
THE LOAD COMMAND
----------------
You may want to share common code across multiple test files. Bats
includes a convenient `load` 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 `test/foo.bats`, the command
load test_helper
will source the script `test/test_helper.bash` in your test file. This
can be useful for sharing functions to set up your environment or load
fixtures.
THE SKIP COMMAND
----------------
Tests can be skipped by using the `skip` command at the point in a
test you wish to skip.
@test "A test I don't want to execute for now" {
skip
run foo
[ "$status" -eq 0 ]
}
Optionally, you may include a reason for skipping:
@test "A test I don't want to execute for now" {
skip "This command will return zero soon, but not now"
run foo
[ "$status" -eq 0 ]
}
Or you can skip conditionally:
@test "A test which should run" {
if [ foo != bar ]; then
skip "foo isn't bar"
fi
run foo
[ "$status" -eq 0 ]
}
SETUP AND TEARDOWN FUNCTIONS
----------------------------
You can define special `setup` and `teardown` 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.
CODE OUTSIDE OF TEST CASES
--------------------------
You can include code in your test file outside of `@test` 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 `@test`, `setup` or `teardown` functions
must be redirected to `stderr` (`>&2`). Otherwise, the output may
cause Bats to fail by polluting the TAP stream on `stdout`.
SPECIAL VARIABLES
-----------------
There are several global variables you can use to introspect on Bats
tests:
* `$BATS_TEST_FILENAME` is the fully expanded path to the Bats test
file.
* `$BATS_TEST_DIRNAME` is the directory in which the Bats test file is
located.
* `$BATS_TEST_NAMES` is an array of function names for each test case.
* `$BATS_TEST_NAME` is the name of the function containing the current
test case.
* `$BATS_TEST_DESCRIPTION` is the description of the current test
case.
* `$BATS_TEST_NUMBER` is the (1-based) index of the current test case
in the test file.
* `$BATS_TMPDIR` is the location to a directory that may be used to
store temporary files.
SEE ALSO
--------
`bash`(1), `bats`(1)

+ 8
- 0
package.json View File

@ -0,0 +1,8 @@
{
"name": "bats",
"version": "v0.4.0",
"description": "Bash Automated Testing System",
"install": "./install.sh ${PREFIX:-/usr/local}",
"scripts": [ "libexec/bats", "libexec/bats-exec-suite", "libexec/bats-exec-test", "libexec/bats-format-tap-stream", "libexec/bats-preprocess", "bin/bats" ]
}

+ 357
- 0
test/bats.bats View File

@ -0,0 +1,357 @@
#!/usr/bin/env bats
load test_helper
fixtures bats
@test "no arguments prints usage instructions" {
run bats
[ $status -eq 1 ]
[ $(expr "${lines[1]}" : "Usage:") -ne 0 ]
}
@test "-v and --version print version number" {
run bats -v
[ $status -eq 0 ]
[ $(expr "$output" : "Bats [0-9][0-9.]*") -ne 0 ]
}
@test "-h and --help print help" {
run bats -h
[ $status -eq 0 ]
[ "${#lines[@]}" -gt 3 ]
}
@test "invalid filename prints an error" {
run bats nonexistent
[ $status -eq 1 ]
[ $(expr "$output" : ".*does not exist") -ne 0 ]
}
@test "empty test file runs zero tests" {
run bats "$FIXTURE_ROOT/empty.bats"
[ $status -eq 0 ]
[ "$output" = "1..0" ]
}
@test "one passing test" {
run bats "$FIXTURE_ROOT/passing.bats"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..1" ]
[ "${lines[1]}" = "ok 1 a passing test" ]
}
@test "summary passing tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/passing.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "1 test, 0 failures" ]
}
@test "summary passing and skipping tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/passing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[3]}" = "3 tests, 0 failures, 2 skipped" ]
}
@test "tap passing and skipping tests" {
run filter_control_sequences bats --tap "$FIXTURE_ROOT/passing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..3" ]
[ "${lines[1]}" = "ok 1 a passing test" ]
[ "${lines[2]}" = "ok 2 a skipped test with no reason # skip" ]
[ "${lines[3]}" = "ok 3 a skipped test with a reason # skip for a really good reason" ]
}
@test "summary passing and failing tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/failing_and_passing.bats"
[ $status -eq 0 ]
[ "${lines[4]}" = "2 tests, 1 failure" ]
}
@test "summary passing, failing and skipping tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/passing_failing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[5]}" = "3 tests, 1 failure, 1 skipped" ]
}
@test "tap passing, failing and skipping tests" {
run filter_control_sequences bats --tap "$FIXTURE_ROOT/passing_failing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..3" ]
[ "${lines[1]}" = "ok 1 a passing test" ]
[ "${lines[2]}" = "ok 2 a skipping test # skip" ]
[ "${lines[3]}" = "not ok 3 a failing test" ]
}
@test "one failing test" {
run bats "$FIXTURE_ROOT/failing.bats"
[ $status -eq 1 ]
emit_debug_output
[ "${lines[0]}" = '1..1' ]
[ "${lines[1]}" = 'not ok 1 a failing test' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing.bats, line 4)" ]
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ]
}
@test "one failing and one passing test" {
run bats "$FIXTURE_ROOT/failing_and_passing.bats"
[ $status -eq 1 ]
[ "${lines[0]}" = '1..2' ]
[ "${lines[1]}" = 'not ok 1 a failing test' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_and_passing.bats, line 2)" ]
[ "${lines[3]}" = "# \`false' failed" ]
[ "${lines[4]}" = 'ok 2 a passing test' ]
}
@test "failing test with significant status" {
STATUS=2 run bats "$FIXTURE_ROOT/failing.bats"
[ $status -eq 1 ]
emit_debug_output
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ]
}
@test "failing helper function logs the test case's line number" {
run bats "$FIXTURE_ROOT/failing_helper.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 failing helper function' ]
[ "${lines[2]}" = "# (from function \`failing_helper' in file $RELATIVE_FIXTURE_ROOT/test_helper.bash, line 6," ]
[ "${lines[3]}" = "# in test file $RELATIVE_FIXTURE_ROOT/failing_helper.bats, line 5)" ]
[ "${lines[4]}" = "# \`failing_helper' failed" ]
}
@test "test environments are isolated" {
run bats "$FIXTURE_ROOT/environment.bats"
[ $status -eq 0 ]
}
@test "setup is run once before each test" {
rm -f "$TMP/setup.log"
run bats "$FIXTURE_ROOT/setup.bats"
[ $status -eq 0 ]
run cat "$TMP/setup.log"
[ ${#lines[@]} -eq 3 ]
}
@test "teardown is run once after each test, even if it fails" {
rm -f "$TMP/teardown.log"
run bats "$FIXTURE_ROOT/teardown.bats"
[ $status -eq 1 ]
run cat "$TMP/teardown.log"
[ ${#lines[@]} -eq 3 ]
}
@test "setup failure" {
run bats "$FIXTURE_ROOT/failing_setup.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 truth' ]
[ "${lines[2]}" = "# (from function \`setup' in test file $RELATIVE_FIXTURE_ROOT/failing_setup.bats, line 2)" ]
[ "${lines[3]}" = "# \`false' failed" ]
}
@test "passing test with teardown failure" {
PASS=1 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 truth' ]
[ "${lines[2]}" = "# (from function \`teardown' in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 2)" ]
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ]
}
@test "failing test with teardown failure" {
PASS=0 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 truth' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 6)" ]
[ "${lines[3]}" = $'# `[ "$PASS" = "1" ]\' failed' ]
}
@test "teardown failure with significant status" {
PASS=1 STATUS=2 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ]
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ]
}
@test "failing test file outside of BATS_CWD" {
cd "$TMP"
run bats "$FIXTURE_ROOT/failing.bats"
[ $status -eq 1 ]
emit_debug_output
[ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing.bats, line 4)" ]
}
@test "load sources scripts relative to the current test file" {
run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 0 ]
}
@test "load aborts if the specified script does not exist" {
HELPER_NAME="nonexistent" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 1 ]
}
@test "load sources scripts by absolute path" {
HELPER_NAME="${FIXTURE_ROOT}/test_helper.bash" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 0 ]
}
@test "load aborts if the script, specified by an absolute path, does not exist" {
HELPER_NAME="${FIXTURE_ROOT}/nonexistent" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 1 ]
}
@test "output is discarded for passing tests and printed for failing tests" {
run bats "$FIXTURE_ROOT/output.bats"
[ $status -eq 1 ]
[ "${lines[6]}" = '# failure stdout 1' ]
[ "${lines[7]}" = '# failure stdout 2' ]
[ "${lines[11]}" = '# failure stderr' ]
}
@test "-c prints the number of tests" {
run bats -c "$FIXTURE_ROOT/empty.bats"
[ $status -eq 0 ]
[ "$output" = "0" ]
run bats -c "$FIXTURE_ROOT/output.bats"
[ $status -eq 0 ]
[ "$output" = "4" ]
}
@test "dash-e is not mangled on beginning of line" {
run bats "$FIXTURE_ROOT/intact.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 dash-e on beginning of line" ]
}
@test "dos line endings are stripped before testing" {
run bats "$FIXTURE_ROOT/dos_line.bats"
[ $status -eq 0 ]
}
@test "test file without trailing newline" {
run bats "$FIXTURE_ROOT/without_trailing_newline.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 truth" ]
}
@test "skipped tests" {
run bats "$FIXTURE_ROOT/skipped.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 a skipped test # skip" ]
[ "${lines[2]}" = "ok 2 a skipped test with a reason # skip a reason" ]
}
@test "extended syntax" {
run bats-exec-test -x "$FIXTURE_ROOT/failing_and_passing.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'begin 1 a failing test' ]
[ "${lines[2]}" = 'not ok 1 a failing test' ]
[ "${lines[5]}" = 'begin 2 a passing test' ]
[ "${lines[6]}" = 'ok 2 a passing test' ]
}
@test "pretty and tap formats" {
run bats --tap "$FIXTURE_ROOT/passing.bats"
tap_output="$output"
[ $status -eq 0 ]
run bats --pretty "$FIXTURE_ROOT/passing.bats"
pretty_output="$output"
[ $status -eq 0 ]
[ "$tap_output" != "$pretty_output" ]
}
@test "pretty formatter bails on invalid tap" {
run bats --tap "$FIXTURE_ROOT/invalid_tap.bats"
[ $status -eq 1 ]
[ "${lines[0]}" = "This isn't TAP!" ]
[ "${lines[1]}" = "Good day to you" ]
}
@test "single-line tests" {
run bats "$FIXTURE_ROOT/single_line.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'ok 1 empty' ]
[ "${lines[2]}" = 'ok 2 passing' ]
[ "${lines[3]}" = 'ok 3 input redirection' ]
[ "${lines[4]}" = 'not ok 4 failing' ]
[ "${lines[5]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/single_line.bats, line 9)" ]
[ "${lines[6]}" = $'# `@test "failing" { false; }\' failed' ]
}
@test "testing IFS not modified by run" {
run bats "$FIXTURE_ROOT/loop_keep_IFS.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 loop_func" ]
}
@test "expand variables in test name" {
SUITE='test/suite' run bats "$FIXTURE_ROOT/expand_var_in_test_name.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 test/suite: test with variable in name" ]
}
@test "handle quoted and unquoted test names" {
run bats "$FIXTURE_ROOT/quoted_and_unquoted_test_names.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 single-quoted name" ]
[ "${lines[2]}" = "ok 2 double-quoted name" ]
[ "${lines[3]}" = "ok 3 unquoted name" ]
}
@test 'ensure compatibility with unofficial Bash strict mode' {
local expected='ok 1 unofficial Bash strict mode conditions met'
# Run Bats under `set -u` to catch as many unset variable accesses as
# possible.
run bash -u "${BATS_TEST_DIRNAME%/*}/libexec/bats" \
"$FIXTURE_ROOT/unofficial_bash_strict_mode.bats"
if [[ "$status" -ne '0' || "${lines[1]}" != "$expected" ]]; then
cat <<END_OF_ERR_MSG
This test failed because the Bats internals are violating one of the
constraints imposed by:
--------
$(< "$FIXTURE_ROOT/unofficial_bash_strict_mode.bash")
--------
See:
- https://github.com/sstephenson/bats/issues/171
- http://redsymbol.net/articles/unofficial-bash-strict-mode/
If there is no error output from the test fixture, run the following to
debug the problem:
$ bash -u bats $RELATIVE_FIXTURE_ROOT/unofficial_bash_strict_mode.bats
If there's no error output even with this command, make sure you're using the
latest version of Bash, as versions before 4.1-alpha may not produce any error
output for unset variable accesses.
If there's no output even when running the latest Bash, the problem may reside
in the DEBUG trap handler. A particularly sneaky issue is that in Bash before
4.1-alpha, an expansion of an empty array, e.g. "\${FOO[@]}", is considered
an unset variable access. The solution is to add a size check before the
expansion, e.g. [[ "\${#FOO[@]}" -ne '0' ]].
END_OF_ERR_MSG
emit_debug_output && return 1
fi
}
@test "parse @test lines with various whitespace combinations" {
run bats "$FIXTURE_ROOT/whitespace.bats"
emit_debug_output
[ $status -eq 0 ]
[ "${lines[1]}" = 'ok 1 no extra whitespace' ]
[ "${lines[2]}" = 'ok 2 tab at beginning of line' ]
[ "${lines[3]}" = 'ok 3 tab before description' ]
[ "${lines[4]}" = 'ok 4 tab before opening brace' ]
[ "${lines[5]}" = 'ok 5 tabs at beginning of line and before description' ]
[ "${lines[6]}" = 'ok 6 tabs at beginning, before description, before brace' ]
[ "${lines[7]}" = 'ok 7 extra whitespace around single-line test' ]
[ "${lines[8]}" = 'ok 8 no extra whitespace around single-line test' ]
[ "${lines[9]}" = 'ok 9 parse unquoted name between extra whitespace' ]
[ "${lines[10]}" = 'ok 10 {' ] # unquoted single brace is a valid description
[ "${lines[11]}" = 'ok 11 ' ] # empty name from single quote
}

+ 3
- 0
test/fixtures/bats/dos_line.bats View File

@ -0,0 +1,3 @@
@test "foo" {
echo "foo"
}

+ 0
- 0
test/fixtures/bats/empty.bats View File


+ 8
- 0
test/fixtures/bats/environment.bats View File

@ -0,0 +1,8 @@
@test "setting a variable" {
variable=1
[ $variable -eq 1 ]
}
@test "variables do not persist across tests" {
[ -z "$variable" ]
}

+ 3
- 0
test/fixtures/bats/expand_var_in_test_name.bats View File

@ -0,0 +1,3 @@
@test "$SUITE: test with variable in name" {
true
}

+ 5
- 0
test/fixtures/bats/failing.bats View File

@ -0,0 +1,5 @@
@test "a failing test" {
true
true
eval "( exit ${STATUS:-1} )"
}

+ 7
- 0
test/fixtures/bats/failing_and_passing.bats View File

@ -0,0 +1,7 @@
@test "a failing test" {
false
}
@test "a passing test" {
true
}

+ 6
- 0
test/fixtures/bats/failing_helper.bats View File

@ -0,0 +1,6 @@
load "test_helper"
@test "failing helper function" {
true
failing_helper
}

+ 7
- 0
test/fixtures/bats/failing_setup.bats View File

@ -0,0 +1,7 @@
setup() {
false
}
@test "truth" {
true
}

+ 7
- 0
test/fixtures/bats/failing_teardown.bats View File

@ -0,0 +1,7 @@
teardown() {
eval "( exit ${STATUS:-1} )"
}
@test "truth" {
[ "$PASS" = "1" ]
}

+ 6
- 0
test/fixtures/bats/intact.bats View File

@ -0,0 +1,6 @@
@test "dash-e on beginning of line" {
run cat - <<INPUT
-e
INPUT
test "$output" = "-e"
}

+ 7
- 0
test/fixtures/bats/invalid_tap.bats View File

@ -0,0 +1,7 @@
echo "This isn't TAP!"
echo "Good day to you"
exit 1
@test "truth" {
true
}

+ 6
- 0
test/fixtures/bats/load.bats View File

@ -0,0 +1,6 @@
[ -n "$HELPER_NAME" ] || HELPER_NAME="test_helper"
load "$HELPER_NAME"
@test "calling a loaded helper" {
help_me
}

+ 16
- 0
test/fixtures/bats/loop_keep_IFS.bats View File

@ -0,0 +1,16 @@
# see issue #89
loop_func() {
local search="none one two tree"
local d
for d in $search ; do
echo $d
done
}
@test "loop_func" {
run loop_func
[[ "${lines[3]}" == 'tree' ]]
run loop_func
[[ "${lines[2]}" == 'two' ]]
}

+ 19
- 0
test/fixtures/bats/output.bats View File

@ -0,0 +1,19 @@
@test "success writing to stdout" {
echo "success stdout 1"
echo "success stdout 2"
}
@test "success writing to stderr" {
echo "success stderr" >&2
}
@test "failure writing to stdout" {
echo "failure stdout 1"
echo "failure stdout 2"
false
}
@test "failure writing to stderr" {
echo "failure stderr" >&2
false
}

+ 3
- 0
test/fixtures/bats/passing.bats View File

@ -0,0 +1,3 @@
@test "a passing test" {
true
}

+ 7
- 0
test/fixtures/bats/passing_and_failing.bats View File

@ -0,0 +1,7 @@
@test "a passing test" {
true
}
@test "a failing test" {
false
}

+ 11
- 0
test/fixtures/bats/passing_and_skipping.bats View File

@ -0,0 +1,11 @@
@test "a passing test" {
true
}
@test "a skipped test with no reason" {
skip
}
@test "a skipped test with a reason" {
skip "for a really good reason"
}

+ 11
- 0
test/fixtures/bats/passing_failing_and_skipping.bats View File

@ -0,0 +1,11 @@
@test "a passing test" {
true
}
@test "a skipping test" {
skip
}
@test "a failing test" {
false
}

+ 11
- 0
test/fixtures/bats/quoted_and_unquoted_test_names.bats View File

@ -0,0 +1,11 @@
@test 'single-quoted name' {
true
}
@test "double-quoted name" {
true
}
@test unquoted name {
true
}

+ 17
- 0
test/fixtures/bats/setup.bats View File

@ -0,0 +1,17 @@
LOG="$TMP/setup.log"
setup() {
echo "$BATS_TEST_NAME" >> "$LOG"
}
@test "one" {
[ "$(tail -n 1 "$LOG")" = "test_one" ]
}
@test "two" {
[ "$(tail -n 1 "$LOG")" = "test_two" ]
}
@test "three" {
[ "$(tail -n 1 "$LOG")" = "test_three" ]
}

+ 9
- 0
test/fixtures/bats/single_line.bats View File

@ -0,0 +1,9 @@
@test "empty" { }
@test "passing" { true; }
@test "input redirection" { diff - <( echo hello ); } <<EOS
hello
EOS
@test "failing" { false; }

+ 7
- 0
test/fixtures/bats/skipped.bats View File

@ -0,0 +1,7 @@
@test "a skipped test" {
skip
}
@test "a skipped test with a reason" {
skip "a reason"
}

+ 17
- 0
test/fixtures/bats/teardown.bats View File

@ -0,0 +1,17 @@
LOG="$TMP/teardown.log"
teardown() {
echo "$BATS_TEST_NAME" >> "$LOG"
}
@test "one" {
true
}
@test "two" {
false
}
@test "three" {
true
}

+ 7
- 0
test/fixtures/bats/test_helper.bash View File

@ -0,0 +1,7 @@
help_me() {
true
}
failing_helper() {
false
}

+ 3
- 0
test/fixtures/bats/unofficial_bash_strict_mode.bash View File

@ -0,0 +1,3 @@
#! /usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

+ 4
- 0
test/fixtures/bats/unofficial_bash_strict_mode.bats View File

@ -0,0 +1,4 @@
load unofficial_bash_strict_mode
@test "unofficial Bash strict mode conditions met" {
:
}

+ 33
- 0
test/fixtures/bats/whitespace.bats View File

@ -0,0 +1,33 @@
@test "no extra whitespace" {
:
}
@test "tab at beginning of line" {
:
}
@test "tab before description" {
:
}
@test "tab before opening brace" {
:
}
@test "tabs at beginning of line and before description" {
:
}
@test "tabs at beginning, before description, before brace" {
:
}
@test "extra whitespace around single-line test" { :; }
@test "no extra whitespace around single-line test" {:;}
@test parse unquoted name between extra whitespace {:;}
@test { {:;} # unquote single brace is a valid description
@test ' {:;} # empty name from single quote

+ 3
- 0
test/fixtures/bats/without_trailing_newline.bats View File

@ -0,0 +1,3 @@
@test "truth" {
true
}

+ 0
- 0
test/fixtures/suite/empty/.gitkeep View File


+ 3
- 0
test/fixtures/suite/multiple/a.bats View File

@ -0,0 +1,3 @@
@test "truth" {
true
}

+ 7
- 0
test/fixtures/suite/multiple/b.bats View File

@ -0,0 +1,7 @@
@test "more truth" {
true
}
@test "quasi-truth" {
[ -z "$FLUNK" ]
}

+ 3
- 0
test/fixtures/suite/single/test.bats View File

@ -0,0 +1,3 @@
@test "a passing test" {
true
}

+ 64
- 0
test/suite.bats View File

@ -0,0 +1,64 @@
#!/usr/bin/env bats
load test_helper
fixtures suite
@test "running a suite with no test files" {
run bats "$FIXTURE_ROOT/empty"
[ $status -eq 0 ]
[ "$output" = "1..0" ]
}
@test "running a suite with one test file" {
run bats "$FIXTURE_ROOT/single"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..1" ]
[ "${lines[1]}" = "ok 1 a passing test" ]
}
@test "counting tests in a suite" {
run bats -c "$FIXTURE_ROOT/single"
[ $status -eq 0 ]
[ "$output" -eq 1 ]
run bats -c "$FIXTURE_ROOT/multiple"
[ $status -eq 0 ]
[ "$output" -eq 3 ]
}
@test "aggregated output of multiple tests in a suite" {
run bats "$FIXTURE_ROOT/multiple"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..3" ]
echo "$output" | grep "^ok . truth"
echo "$output" | grep "^ok . more truth"
echo "$output" | grep "^ok . quasi-truth"
}
@test "a failing test in a suite results in an error exit code" {
FLUNK=1 run bats "$FIXTURE_ROOT/multiple"
[ $status -eq 1 ]
[ "${lines[0]}" = "1..3" ]
echo "$output" | grep "^not ok . quasi-truth"
}
@test "running an ad-hoc suite by specifying multiple test files" {
run bats "$FIXTURE_ROOT/multiple/a.bats" "$FIXTURE_ROOT/multiple/b.bats"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..3" ]
echo "$output" | grep "^ok . truth"
echo "$output" | grep "^ok . more truth"
echo "$output" | grep "^ok . quasi-truth"
}
@test "extended syntax in suite" {
FLUNK=1 run bats-exec-suite -x "$FIXTURE_ROOT/multiple/"*.bats
[ $status -eq 1 ]
[ "${lines[0]}" = "1..3" ]
[ "${lines[1]}" = "begin 1 truth" ]
[ "${lines[2]}" = "ok 1 truth" ]
[ "${lines[3]}" = "begin 2 more truth" ]
[ "${lines[4]}" = "ok 2 more truth" ]
[ "${lines[5]}" = "begin 3 quasi-truth" ]
[ "${lines[6]}" = "not ok 3 quasi-truth" ]
}

+ 27
- 0
test/test_helper.bash View File

@ -0,0 +1,27 @@
fixtures() {
FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures/$1"
bats_trim_filename "$FIXTURE_ROOT" 'RELATIVE_FIXTURE_ROOT'
}
setup() {
export TMP="$BATS_TEST_DIRNAME/tmp"
}
filter_control_sequences() {
"$@" | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g'
}
if ! command -v tput >/dev/null; then
tput() {
printf '1000\n'
}
export -f tput
fi
emit_debug_output() {
printf '%s\n' 'output:' "$output" >&2
}
teardown() {
[ -d "$TMP" ] && rm -f "$TMP"/*
}

+ 2
- 0
test/tmp/.gitignore View File

@ -0,0 +1,2 @@
*

Loading…
Cancel
Save