Skip to content

Commit

Permalink
Merge pull request #409 from rockandska/use_mktemp
Browse files Browse the repository at this point in the history
Use mktemp to avoid collisions on BATS_RUN_TMPDIR with docker and --no-tempdir-cleanup
  • Loading branch information
martin-schulze-vireso committed Mar 15, 2021
2 parents 9f1ae00 + 8f246d0 commit 5b5db9e
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 16 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The format is based on [Keep a Changelog][kac] and this project adheres to

* fix `bats_tap_stream_unknown: command not found` with pretty formatter, when
writing non compliant extended output (#412)
* avoid collisions on `$BATS_RUN_TMPDIR` with `--no-tempdir-cleanup` and docker
by using `mktemp` additionally to PID (#409)

## [1.3.0] - 2021-03-08

Expand Down
3 changes: 3 additions & 0 deletions docs/source/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ There are several global variables you can use to introspect on Bats tests:
- `$BATS_TEST_NUMBER` is the (1-based) index of the current test case in the test file.
- `$BATS_SUITE_TEST_NUMBER` is the (1-based) index of the current test case in the test suite (over all files).
- `$BATS_TMPDIR` is the location to a directory that may be used to store temporary files.
- `$BATS_RUN_TMPDIR` is the location to the temporary directory used by bats to
store all its internal temporary files during the tests.
(default: `$BATS_TMPDIR/bats-run-$BATS_ROOT_PID-XXXXXX`)
- `$BATS_FILE_EXTENSION` (default: `bats`) specifies the extension of test files that should be found when running a suite (via `bats [-r] suite_folder/`)

### Libraries and Add-ons
Expand Down
19 changes: 13 additions & 6 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ if [[ -z "${TMPDIR-}" ]]; then
else
export BATS_TMPDIR="${TMPDIR%/}"
fi
export BATS_RUN_TMPDIR="$BATS_TMPDIR/bats-run-$BATS_ROOT_PID"
export BATS_RUN_TMPDIR=

arguments=()

Expand Down Expand Up @@ -200,12 +200,19 @@ while [[ "$#" -ne 0 ]]; do
shift
done

if [[ -d "$BATS_RUN_TMPDIR" ]]; then
printf "Error: BATS_RUN_TMPDIR (%s) already exists\n" "$BATS_RUN_TMPDIR" >&2
printf "Reusing old run directories can lead to unexpected results ... aborting!\n" >&2
exit 1
if [[ -n "${BATS_RUN_TMPDIR:-}" ]];then
if [[ -d "$BATS_RUN_TMPDIR" ]]; then
printf "Error: BATS_RUN_TMPDIR (%s) already exists\n" "$BATS_RUN_TMPDIR" >&2
printf "Reusing old run directories can lead to unexpected results ... aborting!\n" >&2
exit 1
fi
if ! mkdir -p "$BATS_RUN_TMPDIR" ;then
printf "Error: Failed to create BATS_RUN_TMPDIR (%s)\n" "$BATS_RUN_TMPDIR" >&2
exit 1
fi
else
BATS_RUN_TMPDIR=$(mktemp -d "${BATS_TMPDIR}/bats-run-$BATS_ROOT_PID-XXXXXX")
fi
mkdir -p "$BATS_RUN_TMPDIR"
if [[ -n "$BATS_TEMPDIR_CLEANUP" ]]; then
trap 'rm -rf "$BATS_RUN_TMPDIR"' ERR EXIT
fi
Expand Down
5 changes: 4 additions & 1 deletion man/bats.7
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "BATS" "7" "November 2020" "bats-core" "Bash Automated Testing System"
.TH "BATS" "7" "February 2021" "bats-core" "Bash Automated Testing System"
.
.SH "NAME"
\fBbats\fR \- Bats test file format
Expand Down Expand Up @@ -176,6 +176,9 @@ There are several global variables you can use to introspect on Bats tests:
\fB$BATS_TMPDIR\fR is the location to a directory that may be used to store temporary files\.
.
.IP "\(bu" 4
\fB$BATS_RUN_TMPDIR\fR is the location to the temporary directory used by bats to store all its internal temporary files during the tests\. (default: \fB$BATS_TMPDIR/bats\-run\-$BATS_ROOT_PID\-XXXXXX\fR)
.
.IP "\(bu" 4
\fB$BATS_FILE_EXTENSION\fR (default: \fBbats\fR) specifies the extension of test files that should be found when running a suite (via \fBbats [\-r] suite_folder/\fR)
.
.IP "" 0
Expand Down
3 changes: 3 additions & 0 deletions man/bats.7.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ in the test file.
case in the test suite (over all files).
* `$BATS_TMPDIR` is the location to a directory that may be used to
store temporary files.
* `$BATS_RUN_TMPDIR` is the location to the temporary directory used by
bats to store all its internal temporary files during the tests.
(default: `$BATS_TMPDIR/bats-run-$BATS_ROOT_PID-XXXXXX`)
* `$BATS_FILE_EXTENSION` (default: `bats`) specifies the extension of
test files that should be found when running a suite (via
`bats [-r] suite_folder/`)
Expand Down
19 changes: 18 additions & 1 deletion test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -760,4 +760,21 @@ EOF
done

[[ -n "$tested_at_least_one_formatter" ]]
}
}

@test "run should exit if tmpdir exist" {
local dir
dir=$(mktemp -d "${BATS_RUN_TMPDIR}/BATS_RUN_TMPDIR_TEST.XXXXXX")
run bats --tempdir "${dir}" "$FIXTURE_ROOT/passing.bats"
[ "$status" -eq 1 ]
[ "${lines[0]}" == "Error: BATS_RUN_TMPDIR (${dir}) already exists" ]
[ "${lines[1]}" == "Reusing old run directories can lead to unexpected results ... aborting!" ]
}

@test "run should exit if tmpdir can't be created" {
local dir
dir=$(mktemp "${BATS_RUN_TMPDIR}/BATS_RUN_TMPDIR_TEST.XXXXXX")
run bats --tempdir "${dir}" "$FIXTURE_ROOT/passing.bats"
[ "$status" -eq 1 ]
[ "${lines[1]}" == "Error: Failed to create BATS_RUN_TMPDIR (${dir})" ]
}
4 changes: 2 additions & 2 deletions test/fixtures/parallel/must_not_parallelize_within_file.bats
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
setup_file() {
export FILE_MARKER=$(mktemp)
export FILE_MARKER=$(mktemp "${BATS_RUN_TMPDIR}/file_marker.XXXXXX")
if [[ -n "${DISABLE_IN_SETUP_FILE_FUNCTION}" ]]; then
export BATS_NO_PARALLELIZE_WITHIN_FILE=true
echo "setup_file() sets BATS_NO_PARALLELIZE_WITHIN_FILE=true" >&2
Expand Down Expand Up @@ -48,4 +48,4 @@ teardown() {
[[ "$OTHER_TEST_NAME" != "$BATS_TEST_NAME" ]]
[[ "${lines[1]}" == "end $OTHER_TEST_NAME" ]]
[[ "${lines[2]}" == "start $BATS_TEST_NAME" ]]
}
}
8 changes: 4 additions & 4 deletions test/parallel.bats
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ check_parallel_tests() { # <expected maximum parallelity>
# ensure that we really run parallelization across files!
# (setup should have skipped already, if there was no GNU parallel)
unset BATS_NO_PARALLELIZE_ACROSS_FILES
export FILE_MARKER=$(mktemp)
export FILE_MARKER=$(mktemp "${BATS_RUN_TMPDIR}/file_marker.XXXXXX")
! bats --jobs 2 "$FIXTURE_ROOT/must_not_parallelize_across_files/"
}

@test "--no-parallelize-across-files prevents parallelization across files" {
export FILE_MARKER=$(mktemp)
export FILE_MARKER=$(mktemp "${BATS_RUN_TMPDIR}/file_marker.XXXXXX")
bats --jobs 2 --no-parallelize-across-files "$FIXTURE_ROOT/must_not_parallelize_across_files/"
}

Expand All @@ -161,7 +161,7 @@ check_parallel_tests() { # <expected maximum parallelity>
# ensure that we really run parallelization across files!
# (setup should have skipped already, if there was no GNU parallel)
unset BATS_NO_PARALLELIZE_ACROSS_FILES
export FILE_MARKER=$(mktemp)
export FILE_MARKER=$(mktemp "${BATS_RUN_TMPDIR}/file_marker.XXXXXX")
! bats --jobs 2 --no-parallelize-within-files "$FIXTURE_ROOT/must_not_parallelize_across_files/"
}

Expand All @@ -179,4 +179,4 @@ check_parallel_tests() { # <expected maximum parallelity>

@test "BATS_NO_PARALLELIZE_WITHIN_FILE does not work from inside test function" {
! DISABLE_IN_TEST_FUNCTION=1 bats --jobs 2 "$FIXTURE_ROOT/must_not_parallelize_within_file.bats"
}
}
3 changes: 1 addition & 2 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ emulate_bats_env() {
export BATS_TEST_PATTERN="^[[:blank:]]*@test[[:blank:]]+(.*[^[:blank:]])[[:blank:]]+\{(.*)\$"
export BATS_TEST_FILTER=
export BATS_ROOT_PID=$$
export BATS_EMULATED_RUN_TMPDIR="$BATS_TMPDIR/bats-run-$BATS_ROOT_PID"
export BATS_EMULATED_RUN_TMPDIR=$(mktemp -d "${BATS_TMPDIR}/bats-run-test-tmpdir-${BATS_ROOT_PID}-XXXXXX")
export BATS_RUN_TMPDIR="$BATS_EMULATED_RUN_TMPDIR"
mkdir -p "$BATS_RUN_TMPDIR"
}

fixtures() {
Expand Down

0 comments on commit 5b5db9e

Please sign in to comment.