Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty directories for --gather-test-outputs-in #603

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ The format is based on [Keep a Changelog][kac] and this project adheres to

* added missing shebang (#597)
* remaining instances of `run -<N>` being incorrectly documented as `run =<N>` (#599)
* allow `--gather-test-outputs-in <directory>` to work with existing, empty
directories (#603)
* also add `--clean-and-gather-test-outpust-in <directory>` for improved UX

#### Documentation

* typos (#596)
* fix typos and links (#596, #604)

## [1.7.0] - 2022-05-14

Expand Down Expand Up @@ -61,7 +64,7 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
#### Documentation

* remove 2018 in title, update copyright dates in README.md (#567)
* fix broken links and misspellings (#568, #604)
* fix broken links (#568)
* corrected invalid documentation of `run -N` (had `=N` instead) (#579)
* **CRITICAL**: using the incorrect form can lead to silent errors. See
[issue #578](https://github.com/bats-core/bats-core/issues/578) for more
Expand Down
23 changes: 17 additions & 6 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ version() {
}

abort() {
local print_usage=1
if [[ ${1:-} == --no-print-usage ]]; then
print_usage=
shift
fi
printf 'Error: %s\n' "$1" >&2
usage >&2
if [[ -n $print_usage ]]; then
usage >&2
fi
exit 1
}

Expand All @@ -37,8 +44,8 @@ HELP_TEXT_HEADER
-F, --formatter <type> Switch between formatters: pretty (default),
tap (default w/o term), tap13, junit
--gather-test-outputs-in <directory>
Gather the output of failing *and* passing tests
as files in directory
Gather the output of failing *and* passing tests
as files in directory (if existing, must be empty)
-h, --help Display this help message
-j, --jobs <jobs> Number of parallel jobs (requires GNU parallel)
--no-tempdir-cleanup Preserve test output temporary directory
Expand All @@ -49,7 +56,7 @@ HELP_TEXT_HEADER
Serialize test execution within files instead of
running them in parallel (requires --jobs >1)
--report-formatter <type> Switch between reporters (same options as --formatter)
-o, --output <dir> Directory to write report files
-o, --output <dir> Directory to write report files (must exist)
-p, --pretty Shorthand for "--formatter pretty"
--print-output-on-failure Automatically print the value of `$output` on failed tests
-r, --recursive Include tests in subdirectories
Expand Down Expand Up @@ -221,8 +228,12 @@ while [[ "$#" -ne 0 ]]; do
--gather-test-outputs-in)
shift
output_dir="$1"
if ! mkdir "$output_dir"; then
abort "Could not create $output_dir for --gather-test-outputs-in"
if [ -d "$output_dir" ]; then
if ! find "$output_dir" -mindepth 1 -exec false {} + 2>/dev/null; then
abort --no-print-usage "Directory '$output_dir' must be empty for --gather-test-outputs-in"
fi
elif ! mkdir "$output_dir" 2>/dev/null; then
abort --no-print-usage "Could not create '$output_dir' for --gather-test-outputs-in"
fi
flags+=(--gather-test-outputs-in "$output_dir")
;;
Expand Down
4 changes: 2 additions & 2 deletions libexec/bats-core/bats-exec-suite
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ bats_run_teardown_suite() {
trap bats_suite_exit_trap EXIT
set -eET
BATS_TEARDOWN_SUITE_COMPLETED=
teardown_suite
teardown_suite 2>&1
BATS_TEARDOWN_SUITE_COMPLETED=1
set +ET
}
Expand Down Expand Up @@ -219,7 +219,7 @@ fi

set -eET
BATS_SETUP_SUITE_COMPLETED=
setup_suite
setup_suite 2>&1
BATS_SETUP_SUITE_COMPLETED=1
set +ET

Expand Down
16 changes: 16 additions & 0 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,22 @@ EOF
[ "$(find "$OUTPUT_DIR" -type f | wc -l)" -eq 3 ]
}

@test "--gather-test-outputs-in allows directory to exist (only if empty)" {
local OUTPUT_DIR="$BATS_TEST_TMPDIR/logs"
bats_require_minimum_version 1.5.0

# anything existing, even if empty, 'hidden', etc. should cause failure
mkdir "$OUTPUT_DIR" && touch "$OUTPUT_DIR/.oops"
run -1 bats --verbose-run --gather-test-outputs-in "$OUTPUT_DIR" "$FIXTURE_ROOT/passing.bats"
[ "${lines[0]}" == "Error: Directory '$OUTPUT_DIR' must be empty for --gather-test-outputs-in" ]

# empty directory is just fine
rm "$OUTPUT_DIR/.oops" && rmdir "$OUTPUT_DIR" # avoiding rm -fr to avoid goofs
mkdir "$OUTPUT_DIR"
run -0 bats --verbose-run --gather-test-outputs-in "$OUTPUT_DIR" "$FIXTURE_ROOT/passing.bats"
[ "$(find "$OUTPUT_DIR" -type f | wc -l)" -eq 1 ]
}

@test "Tell about missing flock and shlock" {
if ! command -v parallel; then
skip "this test requires GNU parallel to be installed"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
setup_suite() {
echo setup_suite stdout
echo setup_suite stderr >&2
}

teardown_suite() {
echo teardown_suite stdout
echo teardown_suite stderr >&2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@test test {
:
}
6 changes: 6 additions & 0 deletions test/suite_setup_teardown.bats
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ setup() {
[ "${lines[3]}" == "not ok 2 teardown_suite" ]
[ "${lines[4]}" == "# (from function \`teardown_suite' in test file $RELATIVE_FIXTURE_ROOT/failure_in_teardown_suite/setup_suite.bash, line 7)" ]
[ "${lines[5]}" == "# \`false' failed" ]
}

@test "stderr from setup/teardown_suite does not overtake stdout" {
run -0 --separate-stderr bats "$FIXTURE_ROOT/stderr_in_setup_teardown_suite/"
[[ "$output" == *$'setup_suite stdout\nsetup_suite stderr'* ]] || false
[[ "$output" == *$'teardown_suite stdout\nteardown_suite stderr'* ]] || false
}