Skip to content

Commit

Permalink
Merge pull request #410 from rockandska/BATS_TMPDIR_set_order
Browse files Browse the repository at this point in the history
BATS_TMPDIR set order
  • Loading branch information
martin-schulze-vireso committed Apr 9, 2021
2 parents 0b31b02 + ed0bb5c commit 330c754
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 17 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog][kac] and this project adheres to

## [Unreleased]

### Changed

* added checks and improved documentation for `$BATS_TMPDIR` (#410)

### Fixed

* fix `bats_tap_stream_unknown: command not found` with pretty formatter, when
Expand Down
4 changes: 3 additions & 1 deletion docs/source/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ There are several global variables you can use to introspect on Bats tests:
- `$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_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_TMPDIR` is the base temporary directory used by bats to create its
temporary files / directories.
(default: `$TMPDIR`. If `$TMPDIR` is not set, `/tmp` is used.)
- `$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`)
Expand Down
6 changes: 0 additions & 6 deletions lib/bats-core/preprocessing.bash
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#!/usr/bin/env bash

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

BATS_TMPNAME="$BATS_RUN_TMPDIR/bats.$$"
BATS_PARENT_TMPNAME="$BATS_RUN_TMPDIR/bats.$PPID"
# shellcheck disable=SC2034
Expand Down
14 changes: 9 additions & 5 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ export BATS_TEST_PATTERN_COMMENT="[[:blank:]]*([^[:blank:]()]+)[[:blank:]]*\(?\)
export BATS_TEST_FILTER=
export PATH="$BATS_LIBEXEC:$PATH"
export BATS_ROOT_PID=$$
if [[ -z "${TMPDIR-}" ]]; then
export BATS_TMPDIR="/tmp"
else
export BATS_TMPDIR="${TMPDIR%/}"
fi
export BATS_TMPDIR="${TMPDIR:-/tmp}"
export BATS_RUN_TMPDIR=

if [[ ! -d "${BATS_TMPDIR}" ]];then
printf "Error: BATS_TMPDIR (%s) does not exist or is not a directory" "${BATS_TMPDIR}" >&2
exit 1
elif [[ ! -w "${BATS_TMPDIR}" ]];then
printf "Error: BATS_TMPDIR (%s) is not writable" "${BATS_TMPDIR}" >&2
exit 1
fi

arguments=()

# Unpack single-character options bundled together, e.g. -cr, -pr.
Expand Down
4 changes: 2 additions & 2 deletions 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" "February 2021" "bats-core" "Bash Automated Testing System"
.TH "BATS" "7" "March 2021" "bats-core" "Bash Automated Testing System"
.
.SH "NAME"
\fBbats\fR \- Bats test file format
Expand Down Expand Up @@ -173,7 +173,7 @@ There are several global variables you can use to introspect on Bats tests:
\fB$BATS_SUITE_TEST_NUMBER\fR is the (1\-based) index of the current test case in the test suite (over all files)\.
.
.IP "\(bu" 4
\fB$BATS_TMPDIR\fR is the location to a directory that may be used to store temporary files\.
\fB$BATS_TMPDIR\fR is the base temporary directory used by bats to create its temporary files / directories\. (default: \fB$TMPDIR\fR\. If \fB$TMPDIR\fR is not set, \fB/tmp\fR is used\.)
.
.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)
Expand Down
5 changes: 3 additions & 2 deletions man/bats.7.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ 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_TMPDIR` is the base temporary directory used by bats to create its
temporary files / directories.
(default: `$TMPDIR`. If `$TMPDIR` is not set, `/tmp` is used.)
* `$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`)
Expand Down
19 changes: 18 additions & 1 deletion test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,27 @@ EOF
[ "${lines[1]}" == "Reusing old run directories can lead to unexpected results ... aborting!" ]
}

@test "run should exit if tmpdir can't be created" {
@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})" ]
}

@test "Fail if BATS_TMPDIR does not exist or is not writable" {
export TMPDIR=$(mktemp -u "${BATS_RUN_TMPDIR}/donotexist.XXXXXX")
run bats "$FIXTURE_ROOT/BATS_TMPDIR.bats"
echo "$output"
[ "$status" -eq 1 ]
[ "${lines[0]}" = "Error: BATS_TMPDIR (${TMPDIR}) does not exist or is not a directory" ]
}

@test "Setting BATS_TMPDIR is ignored" {
unset TMPDIR # ensure we don't have a predefined value
expected="/tmp" run bats "$FIXTURE_ROOT/BATS_TMPDIR.bats"
echo "$output"
[ "$status" -eq 0 ]
BATS_TMPDIR="${BATS_RUN_TMPDIR}" expected="/tmp" run bats "$FIXTURE_ROOT/BATS_TMPDIR.bats"
[ "$status" -eq 0 ]
}
8 changes: 8 additions & 0 deletions test/fixtures/bats/BATS_TMPDIR.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@test "BATS_TMPDIR is set" {
[ "${BATS_TMPDIR}" == "${expected:-}" ]
}

@test "BATS_RUN_TMPDIR has BATS_TMPDIR as a prefix" {
local regex="^${BATS_TMPDIR}/.+"
[[ ${BATS_RUN_TMPDIR} =~ ${regex} ]]
}

0 comments on commit 330c754

Please sign in to comment.