Skip to content

Commit 6fa112d

Browse files
committed
tests: test that script-level temp resources are cleaned up
Add a unit test and an acceptance test to verify that temp resources created in the `set_up_before_script` function are correctly cleaned up.
1 parent d0bfd31 commit 6fa112d

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
3+
# @data_provider execution_modes
4+
function test_script_temp_files_are_cleaned_up_after_test_run() {
5+
local mode="$1"
6+
local fixture_file="tests/acceptance/fixtures/script_with_setup_temp_file.sh"
7+
local temp_base_dir="${TMPDIR:-/tmp}/bashunit/tmp"
8+
local output
9+
10+
if [[ "$mode" == "parallel" ]]; then
11+
output=$(./bashunit --parallel "$fixture_file" 2>&1)
12+
else
13+
output=$(./bashunit "$fixture_file" 2>&1)
14+
fi
15+
16+
# Check that the test run was successful
17+
assert_contains "All tests passed" "$output"
18+
19+
# Check that no script-setup temp files remain in the temp directory
20+
local remaining_files
21+
if [[ -d "$temp_base_dir" ]]; then
22+
remaining_files=$(find "$temp_base_dir" -name "*script-setup*" 2>/dev/null || true)
23+
24+
assert_empty "$remaining_files"
25+
26+
# Manually clean up remaining files
27+
if [[ -n "$remaining_files" ]]; then
28+
echo "$remaining_files" | xargs rm -rf 2>/dev/null || true
29+
fi
30+
fi
31+
}
32+
33+
function execution_modes() {
34+
echo "sequential"
35+
echo "parallel"
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
function set_up_before_script() {
4+
SCRIPT_TEMP_FILE=$(temp_file "script-setup")
5+
SCRIPT_TEMP_DIR=$(temp_dir "script-setup")
6+
echo "Script temp file created: $SCRIPT_TEMP_FILE" > "$SCRIPT_TEMP_FILE"
7+
echo "Script temp dir created: $SCRIPT_TEMP_DIR" > "$SCRIPT_TEMP_DIR/marker.txt"
8+
}
9+
10+
function test_simple_passing_test() {
11+
assert_equals "1" "1"
12+
}
13+
14+
function test_another_simple_test() {
15+
assert_file_exists "$SCRIPT_TEMP_FILE"
16+
assert_directory_exists "$SCRIPT_TEMP_DIR"
17+
}

tests/unit/globals_test.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
set -euo pipefail
66

7+
function set_up_before_script() {
8+
SCRIPT_TEMP_FILE=$(temp_file "custom-prefix")
9+
SCRIPT_TEMP_DIR=$(temp_dir "custom-prefix")
10+
}
11+
712
function tear_down_after_script() {
813
export BASHUNIT_DEV_LOG=""
914
}
@@ -58,22 +63,30 @@ function test_globals_random_str_custom_len() {
5863
assert_matches "^[A-Za-z0-9]{3}$" "$(random_str 3)"
5964
}
6065

61-
function test_globals_temp_file() {
66+
function test_globals_temp_file_in_test_function() {
6267
# shellcheck disable=SC2155
6368
local temp_file=$(temp_file "custom-prefix")
6469
assert_file_exists "$temp_file"
6570
cleanup_temp_files
6671
assert_file_not_exists "$temp_file"
6772
}
6873

69-
function test_globals_temp_dir() {
74+
function test_globals_temp_dir_in_test_function() {
7075
# shellcheck disable=SC2155
7176
local temp_dir=$(temp_dir "custom-prefix")
7277
assert_directory_exists "$temp_dir"
7378
cleanup_temp_files
7479
assert_directory_not_exists "$temp_dir"
7580
}
7681

82+
function test_globals_temp_dir_and_file_in_script() {
83+
assert_directory_exists "$SCRIPT_TEMP_DIR"
84+
assert_file_exists "$SCRIPT_TEMP_FILE"
85+
cleanup_temp_files
86+
assert_directory_not_exists "$SCRIPT_TEMP_DIR"
87+
assert_file_not_exists "$SCRIPT_TEMP_FILE"
88+
}
89+
7790
function test_globals_log_level_error() {
7891
log "error" "hello," "error"
7992

0 commit comments

Comments
 (0)