Skip to content

Commit 75e3f45

Browse files
authored
Merge pull request sstephenson#246 from martin-schulze-vireso/feature/junit_output
feat: JUnit output
2 parents 0515ce0 + ff630de commit 75e3f45

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1603
-485
lines changed

.devcontainer/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ARG bashver=latest
2+
3+
FROM bash:${bashver}
4+
5+
# Install parallel and accept the citation notice (we aren't using this in a
6+
# context where it make sense to cite GNU Parallel).
7+
RUN echo "@edgecomm http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && \
8+
apk update && \
9+
apk add --no-cache parallel ncurses shellcheck@edgecomm && \
10+
mkdir -p ~/.parallel && touch ~/.parallel/will-cite
11+
12+
RUN ln -s /opt/bats/bin/bats /usr/sbin/bats
13+
COPY . /opt/bats/
14+
15+
ENTRYPOINT ["bash", "/usr/sbin/bats"]

.devcontainer/devcontainer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "Bats core development environment",
3+
"dockerFile": "Dockerfile"
4+
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ script:
3838
docker run -it "bash:${BASHVER}" --version &&
3939
time docker run -it "bats/bats:bash-${BASHVER}" --tap /opt/bats/test
4040
else
41-
time bin/bats --tap test
41+
time bin/bats --formatter tap test
4242
fi
4343
4444
notifications:

README.md

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ To run Bats' internal test suite (which is in the container image at
172172
To run a test suite from your local machine, mount in a volume and direct Bats
173173
to its path inside the container:
174174

175-
$ docker run -it -v "$(pwd):/code" bats/bats:latest /code/test
175+
$ docker run -it -v "$(pwd):/opt/bats" bats/bats:latest /opt/bats/test
176176

177177
This is a minimal Docker image. If more tools are required this can be used as a
178178
base image in a Dockerfile using `FROM <Docker image>`. In the future there may
@@ -192,24 +192,31 @@ supports:
192192

193193
```
194194
Bats x.y.z
195-
Usage: bats [-cr] [-f <regex>] [-j <jobs>] [-p | -t] <test>...
195+
Usage: bats [OPTIONS] <tests>
196196
bats [-h | -v]
197197
198-
<test> is the path to a Bats test file, or the path to a directory
199-
containing Bats test files (ending with ".bats").
200-
201-
-c, --count Count the number of test cases without running any tests
202-
-f, --filter Filter test cases by names matching the regular expression
203-
-h, --help Display this help message
204-
-j, --jobs Number of parallel jobs to run (requires GNU parallel)
205-
-p, --pretty Show results in pretty format (default for terminals)
206-
-r, --recursive Include tests in subdirectories
207-
-t, --tap Show results in TAP format
208-
-v, --version Display the version number
198+
<tests> is the path to a Bats test file, or the path to a directory
199+
containing Bats test files (ending with ".bats")
200+
201+
-c, --count Count test cases without running any tests
202+
-f, --filter <regex> Only run tests that match the regular expression
203+
-F, --formatter <type> Switch between formatters: pretty (default),
204+
tap (default w/o term), junit
205+
-h, --help Display this help message
206+
-j, --jobs <jobs> Number of parallel jobs (requires GNU parallel)
207+
--parallel-preserve-environment
208+
Preserve the current environment for "--jobs"
209+
(run `parallel --record-env` before)
210+
--no-tempdir-cleanup Preserve test output temporary directory
211+
-o, --output <dir> Directory to write report files
212+
-p, --pretty Shorthand for "--formatter pretty"
213+
-r, --recursive Include tests in subdirectories
214+
-t, --tap Shorthand for "--formatter tap"
215+
-T, --timing Add timing information to tests
216+
-v, --version Display the version number
209217
210218
For more information, see https://github.com/bats-core/bats-core
211219
```
212-
> **Mac OSX/Darwin Warning:** If you're executing bats directly (`bin/bats`) you need to `brew install coreutils` to obtain `greadlink`. Darwin's readlink does not include the -f option. This may be fixed [by this PR](https://github.com/bats-core/bats-core/pull/217), which needs reviewers.
213220

214221
To run your tests, invoke the `bats` interpreter with one or more paths to test
215222
files ending with the `.bats` extension, or paths to directories containing test
@@ -233,10 +240,26 @@ If Bats is not connected to a terminal—in other words, if you run it from a
233240
continuous integration system, or redirect its output to a file—the results are
234241
displayed in human-readable, machine-parsable [TAP format][TAP].
235242

236-
You can force TAP output from a terminal by invoking Bats with the `--tap`
243+
You can force TAP output from a terminal by invoking Bats with the `--formatter tap`
237244
option.
238245

239-
$ bats --tap addition.bats
246+
$ bats --formatter tap addition.bats
247+
1..2
248+
ok 1 addition using bc
249+
ok 2 addition using dc
250+
251+
By combining `-T` and `--formatter junit`, it is possible
252+
to output junit-compatible report files.
253+
254+
$ bats --formatter junit -T addition.bats
255+
1..2
256+
ok 1 addition using bc
257+
ok 2 addition using dc
258+
259+
Test reports will be output in the executing directory, but may be placed elsewhere
260+
by specifying the `--output` flag.
261+
262+
$ bats --formatter junit -T addition.bats --output /tmp
240263
1..2
241264
ok 1 addition using bc
242265
ok 2 addition using dc
@@ -254,6 +277,12 @@ with dependencies between tests (or tests that write to shared locations). When
254277
enabling `--jobs` for the first time be sure to re-run bats multiple times to
255278
identify any inter-test dependencies or non-deterministic test behaviour.
256279

280+
If your code relies on variables from the environment, or from `setup_file()`,
281+
you need to specify `--parallel-preserve-environment` as well. Note that this
282+
requires running `parallel --record-env` first as a setup step as GNU Parallel
283+
will refuse to run without. Only environment variables that were **not** set
284+
during this setup step will be preserved!
285+
257286
[gnu-parallel]: https://www.gnu.org/software/parallel/
258287

259288
## Writing tests
@@ -372,6 +401,11 @@ You can define special `setup` and `teardown` functions, which run before and
372401
after each test case, respectively. Use these to load fixtures, set up your
373402
environment, and clean up when you're done.
374403

404+
You can also define `setup_file` and `teardown_file`, which will run once per file,
405+
before the first and after the last test, respectively.
406+
__WARNING__ these will not be run in parallel mode!
407+
408+
375409
### Code outside of test cases
376410

377411
You can include code in your test file outside of `@test` functions. For
@@ -521,7 +555,7 @@ See `docs/CHANGELOG.md`.
521555

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

526560
[bats-orig]: https://github.com/sstephenson/bats
527561
[0360811]: https://github.com/sstephenson/bats/commit/03608115df2071fff4eaaff1605768c275e5f81f

docs/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Resolving deltas: 100% (661/661), done.
2020
$ cd bats-core/
2121
$ docker build --tag bats:latest .
2222
...
23-
$ docker run -it bats:latest --tap /opt/bats/test
23+
$ docker run -it bats:latest --formatter tap /opt/bats/test
2424
```
2525

2626
To mount your tests into the container, first build the image as above. Then, for example with `bats`:

install.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ if [[ -z "$PREFIX" ]]; then
1212
exit 1
1313
fi
1414

15-
install -d -m 755 "$PREFIX"/{bin,libexec/bats-core,share/man/man{1,7}}
15+
install -d -m 755 "$PREFIX"/{bin,libexec/bats-core,lib/bats-core,share/man/man{1,7}}
1616
install -m 755 "$BATS_ROOT/bin"/* "$PREFIX/bin"
1717
install -m 755 "$BATS_ROOT/libexec/bats-core"/* "$PREFIX/libexec/bats-core"
18+
install -m 755 "$BATS_ROOT/lib/bats-core"/* "$PREFIX/lib/bats-core"
1819
install -m 644 "$BATS_ROOT/man/bats.1" "$PREFIX/share/man/man1"
1920
install -m 644 "$BATS_ROOT/man/bats.7" "$PREFIX/share/man/man7"
2021

lib/bats-core/preprocessing.bash

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
if [[ -z "$TMPDIR" ]]; then
4+
BATS_TMPDIR='/tmp'
5+
else
6+
BATS_TMPDIR="${TMPDIR%/}"
7+
fi
8+
9+
BATS_TMPNAME="$BATS_RUN_TMPDIR/bats.$$"
10+
BATS_PARENT_TMPNAME="$BATS_RUN_TMPDIR/bats.$PPID"
11+
# shellcheck disable=SC2034
12+
BATS_OUT="${BATS_TMPNAME}.out" # used in bats-exec-file
13+
14+
bats_preprocess_source() {
15+
BATS_TEST_SOURCE="${BATS_TMPNAME}.src"
16+
bats-preprocess "$BATS_TEST_FILENAME" >"$BATS_TEST_SOURCE"
17+
trap 'bats_cleanup_preprocessed_source' ERR EXIT
18+
trap 'bats_cleanup_preprocessed_source; exit 1' INT
19+
}
20+
21+
bats_cleanup_preprocessed_source() {
22+
rm -f "$BATS_TEST_SOURCE"
23+
}
24+
25+
bats_evaluate_preprocessed_source() {
26+
if [[ -z "$BATS_TEST_SOURCE" ]]; then
27+
BATS_TEST_SOURCE="${BATS_PARENT_TMPNAME}.src"
28+
fi
29+
# Dynamically loaded user files provided outside of Bats.
30+
# shellcheck disable=SC1090
31+
source "$BATS_TEST_SOURCE"
32+
}

lib/bats-core/test_functions.bash

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
3+
BATS_TEST_DIRNAME="${BATS_TEST_FILENAME%/*}"
4+
BATS_TEST_NAMES=()
5+
6+
load() {
7+
local name="$1"
8+
local filename
9+
10+
if [[ "${name:0:1}" == '/' ]]; then
11+
filename="${name}"
12+
else
13+
filename="$BATS_TEST_DIRNAME/${name}.bash"
14+
fi
15+
16+
if [[ ! -f "$filename" ]]; then
17+
printf 'bats: %s does not exist\n' "$filename" >&2
18+
exit 1
19+
fi
20+
21+
# Dynamically loaded user files provided outside of Bats.
22+
# shellcheck disable=SC1090
23+
source "${filename}"
24+
}
25+
26+
run() {
27+
local origFlags="$-"
28+
set +eET
29+
local origIFS="$IFS"
30+
# 'output', 'status', 'lines' are global variables available to tests.
31+
# shellcheck disable=SC2034
32+
output="$("$@" 2>&1)"
33+
# shellcheck disable=SC2034
34+
status="$?"
35+
# shellcheck disable=SC2034,SC2206
36+
IFS=$'\n' lines=($output)
37+
IFS="$origIFS"
38+
set "-$origFlags"
39+
}
40+
41+
setup() {
42+
return 0
43+
}
44+
45+
teardown() {
46+
return 0
47+
}
48+
49+
skip() {
50+
# Following variables are used in bats-exec-test which sources this file
51+
# shellcheck disable=SC2034
52+
BATS_TEST_SKIPPED="${1:-1}"
53+
# shellcheck disable=SC2034
54+
BATS_TEST_COMPLETED=1
55+
exit 0
56+
}
57+
58+
bats_test_begin() {
59+
BATS_TEST_DESCRIPTION="$1"
60+
if [[ -n "$BATS_EXTENDED_SYNTAX" ]]; then
61+
printf 'begin %d %s\n' "$BATS_TEST_NUMBER" "$BATS_TEST_DESCRIPTION" >&3
62+
fi
63+
setup
64+
}
65+
66+
bats_test_function() {
67+
local test_name="$1"
68+
BATS_TEST_NAMES+=("$test_name")
69+
}

0 commit comments

Comments
 (0)