[PATCH] Clean up temporary directories

6 views
Skip to first unread message

Earl Chew

unread,
Jul 26, 2023, 8:54:43 PM7/26/23
to efibootg...@googlegroups.com, earl...@yahoo.com
To reduce clutter in the temporary directory, group the test
subdirectories in a common parent. On test success, remove
the corresponding subdirectory that holds data for the test.
The subdirectory is retained to aid diagnosis in the case
that the test fails.

Signed-off-by: Earl Chew <earl...@yahoo.com>
---
tests/bg_setenv.bats | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)

diff --git a/tests/bg_setenv.bats b/tests/bg_setenv.bats
index 67cee8f..aa38bb7 100755
--- a/tests/bg_setenv.bats
+++ b/tests/bg_setenv.bats
@@ -17,6 +17,19 @@ setup() {
# file respectively
DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
PATH="$DIR/..:$PATH"
+
+ # Place the temporaries in directory specific to the tests for
+ # the package to reduce the clutter in the parent directory.
+ export TMPDIR="${TMPDIR-/tmp}/efibootguardtest"
+ rm -rf "$TMPDIR"
+ mkdir -p "$TMPDIR"
+}
+
+teardown() {
+ # Remove the temporary test directory if the tests succeeded
+ # and did not leave any partial computation. Otherwise leave
+ # information to help diagnose test failures.
+ find "$TMPDIR" -maxdepth 0 -type d -empty -delete
}

create_sample_bgenv() {
@@ -46,6 +59,8 @@ foo = bar" ]]

run md5sum "$envfile"
[[ "$output" =~ ^6ad1dd1d98209a03d7b4fc2d2f16f9ec\s*.* ]]
+
+ rm -rf "${envfile%/*}"
}

@test "create an empty BGENV.DAT" {
@@ -68,6 +83,8 @@ watchdog timeout: 0 seconds
ustate: 0 (OK)

user variables:" ]]
+
+ rm -rf "${envfile%/*}"
}

@test "modify BGENV, discard existing values" {
@@ -90,6 +107,8 @@ user variables:" ]]

run md5sum "$envfile"
[[ "$output" =~ ^15bc40c9feae99cc879cfc55e0132caa\s*.* ]]
+
+ rm -rf "${envfile%/*}"
}

@test "modify BGENV, preserve existing values" {
@@ -113,6 +132,8 @@ foo = bar" ]]

run md5sum "$envfile"
[[ "$output" =~ ^a24b154a48e1f33b79b87e0fa5eff8a1\s*.* ]]
+
+ rm -rf "${envfile%/*}"
}

@test "bg_printenv ustate" {
@@ -123,6 +144,8 @@ foo = bar" ]]
run bg_printenv "--filepath=$envfile" --output ustate
[[ "$output" = "Values:
ustate: 0 (OK)" ]]
+
+ rm -rf "${envfile%/*}"
}

@test "bg_printenv with all fields is the same as omitting fields" {
@@ -133,6 +156,8 @@ ustate: 0 (OK)" ]]
expected_output=$(bg_printenv "--filepath=$envfile")
run bg_printenv "--filepath=$envfile" --output in_progress,revision,kernel,kernelargs,watchdog_timeout,ustate,user
[[ "$output" = "$expected_output" ]]
+
+ rm -rf "${envfile%/*}"
}

@test "bg_printenv ustate raw" {
@@ -142,6 +167,8 @@ ustate: 0 (OK)" ]]
create_sample_bgenv "$envfile"
run bg_printenv "--filepath=$envfile" --output ustate --raw
[[ "$output" = "USTATE=0" ]]
+
+ rm -rf "${envfile%/*}"
}

@test "bg_printenv multiple fields raw" {
@@ -153,4 +180,6 @@ ustate: 0 (OK)" ]]
[[ "$output" = "KERNEL=C:BOOT:kernel.efi
KERNELARGS=root=/dev/sda
USTATE=0" ]]
+
+ rm -rf "${envfile%/*}"
}
--
2.39.1

Michael Adler

unread,
Jul 27, 2023, 2:43:22 AM7/27/23
to Earl Chew, efibootg...@googlegroups.com
Hi Earl,

> To reduce clutter in the temporary directory

agreed. However, this can be achieved in a simpler way: Replace every
occurence of

envfile="$(mktemp -d)/BGENV.DAT"

with

envfile="$BATS_TEST_TMPDIR/BGENV.DAT"

bats does the cleanup itself [1].

Kind Regards,
Michael

[1]: https://bats-core.readthedocs.io/en/stable/writing-tests.html#special-variables

--
Michael Adler

Siemens AG
Technology
Connectivity & Edge
Smart Embedded Systems
T CED SES-DE
Otto-Hahn-Ring 6
81739 Munich, Germany

Siemens Aktiengesellschaft: Chairman of the Supervisory Board: Jim Hagemann
Snabe; Managing Board: Roland Busch, Chairman, President and Chief Executive
Officer; Cedrik Neike, Matthias Rebellius, Ralf P. Thomas, Judith Wiese;
Registered offices: Berlin and Munich, Germany; Commercial registries:
Berlin-Charlottenburg, HRB 12300, Munich, HRB 6684; WEEE-Reg.-No. DE 23691322
Reply all
Reply to author
Forward
0 new messages