Skip to content

Commit

Permalink
feat: add junit formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-schulze-vireso authored and sublimino committed Apr 26, 2020
1 parent 0515ce0 commit 3d7e49f
Show file tree
Hide file tree
Showing 62 changed files with 1,749 additions and 616 deletions.
15 changes: 15 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ARG bashver=latest

FROM bash:${bashver}

# Install parallel and accept the citation notice (we aren't using this in a
# context where it make sense to cite GNU Parallel).
RUN echo "@edgecomm http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && \
apk update && \
apk add --no-cache parallel ncurses shellcheck@edgecomm && \
mkdir -p ~/.parallel && touch ~/.parallel/will-cite

RUN ln -s /opt/bats/bin/bats /usr/sbin/bats
COPY . /opt/bats/

ENTRYPOINT ["bash", "/usr/sbin/bats"]
4 changes: 4 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Bats core development environment",
"dockerFile": "Dockerfile"
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ script:
docker run -it "bash:${BASHVER}" --version &&
time docker run -it "bats/bats:bash-${BASHVER}" --tap /opt/bats/test
else
time bin/bats --tap test
time bin/bats --formatter tap test
fi
notifications:
Expand Down
57 changes: 45 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ To run Bats' internal test suite (which is in the container image at
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/bats:latest /code/test
$ docker run -it -v "$(pwd):/opt/bats" bats/bats:latest /opt/bats/test

This is a minimal Docker 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
Expand All @@ -198,14 +198,20 @@ Usage: bats [-cr] [-f <regex>] [-j <jobs>] [-p | -t] <test>...
<test> is the path to a Bats test file, or the path to a directory
containing Bats test files (ending with ".bats").
-c, --count Count the number of test cases without running any tests
-f, --filter Filter test cases by names matching the regular expression
-h, --help Display this help message
-j, --jobs Number of parallel jobs to run (requires GNU parallel)
-p, --pretty Show results in pretty format (default for terminals)
-r, --recursive Include tests in subdirectories
-t, --tap Show results in TAP format
-v, --version Display the version number
-c, --count Count the number of test cases without running any tests
-f, --filter Filter test cases by names matching the regular expression
-F, --formatter Switch between formatters (Default: tap, Options: junit, pretty, tap)
-h, --help Display this help message
-j, --jobs Number of parallel jobs to run (requires GNU parallel)
--parallel-preserve-environment
Preserve the environment When running via GNU parallel (run
`parallel --record-env` before!)
-o, --output A directory to output reports to
-p, --pretty [DEPRECATED] Show results in pretty format (default for terminals), use "--formatter pretty" instead
-r, --recursive Include tests in subdirectories
-t, --tap [DEPRECATED] Show results in TAP format, use "--formatter tap" instead
-T, --timing Add timing information
-v, --version Display the version number
For more information, see https://github.com/bats-core/bats-core
```
Expand Down Expand Up @@ -233,10 +239,26 @@ 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][TAP].

You can force TAP output from a terminal by invoking Bats with the `--tap`
You can force TAP output from a terminal by invoking Bats with the `--formatter tap`
option.

$ bats --tap addition.bats
$ bats --formatter tap addition.bats
1..2
ok 1 addition using bc
ok 2 addition using dc

By combining `-T` and `--formatter junit`, it is possible
to output junit-compatible report files.

$ bats --formatter junit -T addition.bats
1..2
ok 1 addition using bc
ok 2 addition using dc

Test reports will be output in the executing directory, but may be placed elsewhere
by specifying the `--output` flag.

$ bats --formatter junit -T addition.bats --output /tmp
1..2
ok 1 addition using bc
ok 2 addition using dc
Expand All @@ -254,6 +276,12 @@ with dependencies between tests (or tests that write to shared locations). When
enabling `--jobs` for the first time be sure to re-run bats multiple times to
identify any inter-test dependencies or non-deterministic test behaviour.

If your code relies on variables from the environment, or from `setup_file()`,
you need to specify `--parallel-preserve-environment` as well. Note that this
requires running `parallel --record-env` first as a setup step as GNU Parallel
will refuse to run without. Only environment variables that were **not** set
during this setup step will be preserved!

[gnu-parallel]: https://www.gnu.org/software/parallel/

## Writing tests
Expand Down Expand Up @@ -372,6 +400,11 @@ 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.

You can also define `setup_file` and `teardown_file`, which will run once per file,
before the first and after the last test, respectively.
__WARNING__ these will not be run in parallel mode!


### Code outside of test cases

You can include code in your test file outside of `@test` functions. For
Expand Down Expand Up @@ -521,7 +554,7 @@ See `docs/CHANGELOG.md`.

**Tuesday, September 19, 2017:** This was forked from [Bats][bats-orig] at
commit [0360811][]. It was created via `git clone --bare` and `git push
--mirror`.
--mirror`. See the [Background](#background) section above for more information.

[bats-orig]: https://github.com/sstephenson/bats
[0360811]: https://github.com/sstephenson/bats/commit/03608115df2071fff4eaaff1605768c275e5f81f
Expand Down
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Resolving deltas: 100% (661/661), done.
$ cd bats-core/
$ docker build --tag bats:latest .
...
$ docker run -it bats:latest --tap /opt/bats/test
$ docker run -it bats:latest --formatter tap /opt/bats/test
```

To mount your tests into the container, first build the image as above. Then, for example with `bats`:
Expand Down
3 changes: 2 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ if [[ -z "$PREFIX" ]]; then
exit 1
fi

install -d -m 755 "$PREFIX"/{bin,libexec/bats-core,share/man/man{1,7}}
install -d -m 755 "$PREFIX"/{bin,libexec/bats-core,lib/bats-core,share/man/man{1,7}}
install -m 755 "$BATS_ROOT/bin"/* "$PREFIX/bin"
install -m 755 "$BATS_ROOT/libexec/bats-core"/* "$PREFIX/libexec/bats-core"
install -m 755 "$BATS_ROOT/lib/bats-core"/* "$PREFIX/lib/bats-core"
install -m 644 "$BATS_ROOT/man/bats.1" "$PREFIX/share/man/man1"
install -m 644 "$BATS_ROOT/man/bats.7" "$PREFIX/share/man/man7"

Expand Down
32 changes: 32 additions & 0 deletions lib/bats-core/preprocessing.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

if [[ -z "$TMPDIR" ]]; then
BATS_TMPDIR='/tmp'
else
BATS_TMPDIR="${TMPDIR%/}"
fi

BATS_TMPNAME="$BATS_RUN_TMPDIR/bats.$$"
BATS_PARENT_TMPNAME="$BATS_RUN_TMPDIR/bats.$PPID"
# shellcheck disable=SC2034
BATS_OUT="${BATS_TMPNAME}.out" # used in bats-exec-file

bats_preprocess_source() {
BATS_TEST_SOURCE="${BATS_TMPNAME}.src"
bats-preprocess "$BATS_TEST_FILENAME" >"$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
# Dynamically loaded user files provided outside of Bats.
# shellcheck disable=SC1090
source "$BATS_TEST_SOURCE"
}
69 changes: 69 additions & 0 deletions lib/bats-core/test_functions.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash

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
printf 'bats: %s does not exist\n' "$filename" >&2
exit 1
fi

# Dynamically loaded user files provided outside of Bats.
# shellcheck disable=SC1090
source "${filename}"
}

run() {
local origFlags="$-"
set +eET
local origIFS="$IFS"
# 'output', 'status', 'lines' are global variables available to tests.
# shellcheck disable=SC2034
output="$("$@" 2>&1)"
# shellcheck disable=SC2034
status="$?"
# shellcheck disable=SC2034,SC2206
IFS=$'\n' lines=($output)
IFS="$origIFS"
set "-$origFlags"
}

setup() {
return 0
}

teardown() {
return 0
}

skip() {
# Following variables are used in bats-exec-test which sources this file
# shellcheck disable=SC2034
BATS_TEST_SKIPPED="${1:-1}"
# shellcheck disable=SC2034
BATS_TEST_COMPLETED=1
exit 0
}

bats_test_begin() {
BATS_TEST_DESCRIPTION="$1"
if [[ -n "$BATS_EXTENDED_SYNTAX" ]]; then
printf 'begin %d %s\n' "$BATS_TEST_NUMBER" "$BATS_TEST_DESCRIPTION" >&3
fi
setup
}

bats_test_function() {
local test_name="$1"
BATS_TEST_NAMES+=("$test_name")
}

0 comments on commit 3d7e49f

Please sign in to comment.