Skip to content

Commit

Permalink
Allow empty directories for --gather-test-outputs-in
Browse files Browse the repository at this point in the history
This addresses the usability from #594.  The rationale for the previous behavior (not allowing the directory to exist) makes sense; test executions to the same to the same location, over time, aren't guaranteed to generate the same output files (in name or quantity), leading to older test outputs bleeding together with newer ones.  That reasoning is still maintained.
  • Loading branch information
Roberto Villarreal committed Jun 2, 2022
1 parent 9695e7c commit 166ceb8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,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 +49,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,7 +221,9 @@ while [[ "$#" -ne 0 ]]; do
--gather-test-outputs-in)
shift
output_dir="$1"
if ! mkdir "$output_dir"; then
if [ -d "$output_dir" ] && [ -n "$(ls -A "$output_dir")" ]; then
abort "Directory $output_dir must be empty for --gather-test-outputs-in"
elif ! [ -d "$output_dir" ] && ! mkdir "$output_dir"; then
abort "Could not create $output_dir for --gather-test-outputs-in"
fi
flags+=(--gather-test-outputs-in "$output_dir")
Expand Down
17 changes: 17 additions & 0 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,23 @@ 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

0 comments on commit 166ceb8

Please sign in to comment.