Skip to content

Commit c7212e7

Browse files
authored
Merge pull request #349 from TypedDevs/refactor/better-cleanup-temp
Better cleanup temporal files
2 parents f395cee + 5efd87e commit c7212e7

File tree

5 files changed

+39
-94
lines changed

5 files changed

+39
-94
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- temp_dir
1515
- cleanup_temp_files
1616
- log
17+
- Improved clean up temporal files and directories
1718

1819
## [0.17.0](https://github.com/TypedDevs/bashunit/compare/0.16.0...0.17.0) - 2024-10-01
1920

src/globals.sh

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,17 @@ function random_str() {
2525
}
2626

2727
function temp_file() {
28-
# shellcheck disable=SC2155
29-
local path="/tmp/bashunit_temp.$(random_str)"
30-
touch "$path"
31-
echo "$path"
28+
mkdir -p /tmp/bashunit-tmp && chmod -R 777 /tmp/bashunit-tmp
29+
mktemp --tmpdir="/tmp/bashunit-tmp" "XXXXXXX"
3230
}
3331

3432
function temp_dir() {
35-
# shellcheck disable=SC2155
36-
local dir="/tmp/bashunit_tempdir.$(random_str 5)}"
37-
mkdir -p "$dir"
38-
echo "$dir"
33+
mkdir -p /tmp/bashunit-tmp && chmod -R 777 /tmp/bashunit-tmp
34+
mktemp -d --tmpdir="/tmp/bashunit-tmp" "XXXXXXX"
3935
}
4036

4137
function cleanup_temp_files() {
42-
rm -rf /tmp/bashunit_temp*
38+
rm -rf /tmp/bashunit-tmp/*
4339
}
4440

4541
# shellcheck disable=SC2145

src/main.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ function main::exec_tests() {
2929
logger::generate_report_html "$BASHUNIT_REPORT_HTML"
3030
fi
3131

32+
cleanup_temp_files
33+
3234
exit $exit_code
3335
}
3436

tests/acceptance/bashunit_direct_fn_call_test.sh

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,24 @@ function test_bashunit_direct_fn_call_non_existing_fn() {
8989

9090
# shellcheck disable=SC2155
9191
function test_bashunit_assert_exit_code_successful_with_inner_func() {
92-
local temp=$(mktemp)
92+
local temp=$(temp_file)
9393
# shellcheck disable=SC2116
9494
local output="$(./bashunit -a exit_code "0" "$(echo "unknown command")" 2> "$temp")"
9595

9696
assert_empty "$output"
97-
assert_same "Command not found: unknown command" "$(cat "$temp")"
98-
99-
rm "$temp"
97+
assert_file_contains "$temp" "Command not found: unknown command"
10098
}
10199

102100
# shellcheck disable=SC2155
103101
function test_bashunit_assert_exit_code_error_with_inner_func() {
104-
local temp=$(mktemp)
102+
local temp=$(temp_file)
105103
# shellcheck disable=SC2116
106104
local output="$(./bashunit -a exit_code "1" "$(echo "unknown command")" 2> "$temp")"
107105

108106
assert_empty "$output"
109107

110-
assert_contains\
111-
"$(console_results::print_failed_test "Main::exec assert" "0" "to be" "1")"\
112-
"$(cat "$temp")"
113-
114-
rm "$temp"
108+
assert_file_contains "$temp" \
109+
"$(console_results::print_failed_test "Main::exec assert" "0" "to be" "1")"
115110
}
116111

117112
function test_bashunit_assert_exit_code_str_successful_code() {
@@ -126,25 +121,20 @@ function test_bashunit_assert_exit_code_str_general_error() {
126121

127122
# shellcheck disable=SC2155
128123
function test_bashunit_assert_exit_code_str_successful_but_exit_code_error() {
129-
local temp=$(mktemp)
124+
local temp=$(temp_file)
130125
local output="$(./bashunit -a exit_code "1" "echo something to stdout" 2> "$temp")"
131126

132127
assert_same "something to stdout" "$output"
133128

134-
assert_contains\
135-
"$(console_results::print_failed_test "Main::exec assert" "1" "but got " "0")"\
136-
"$(cat "$temp")"
137-
138-
rm "$temp"
129+
assert_file_contains "$temp" \
130+
"$(console_results::print_failed_test "Main::exec assert" "1" "but got " "0")"
139131
}
140132

141133
# shellcheck disable=SC2155
142134
function test_bashunit_assert_exit_code_str_successful_and_exit_code_ok() {
143-
local temp=$(mktemp)
135+
local temp=$(temp_file)
144136
local output="$(./bashunit -a exit_code "0" "echo something to stdout" 2> "$temp")"
145137

146138
assert_same "something to stdout" "$output"
147139
assert_empty "$(cat "$temp")"
148-
149-
rm "$temp"
150140
}

tests/unit/directory_test.sh

Lines changed: 22 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/bash
2+
# shellcheck disable=SC2155
23

34
function test_successful_assert_directory_exists() {
4-
local a_directory
5-
a_directory="$(current_dir)"
5+
local a_directory="$(current_dir)"
66

77
assert_empty "$(assert_directory_exists "$a_directory")"
88
}
@@ -17,8 +17,7 @@ function test_unsuccessful_assert_directory_exists() {
1717
}
1818

1919
function test_assert_directory_exists_should_not_work_with_files() {
20-
local a_file
21-
a_file="$(current_dir)/$(current_filename)"
20+
local a_file="$(current_dir)/$(current_filename)"
2221

2322
assert_same\
2423
"$(console_results::print_failed_test \
@@ -33,8 +32,7 @@ function test_successful_assert_directory_not_exists() {
3332
}
3433

3534
function test_unsuccessful_assert_directory_not_exists() {
36-
local a_directory
37-
a_directory="$(current_dir)"
35+
local a_directory="$(current_dir)"
3836

3937
assert_same\
4038
"$(console_results::print_failed_test \
@@ -43,8 +41,7 @@ function test_unsuccessful_assert_directory_not_exists() {
4341
}
4442

4543
function test_successful_assert_is_directory() {
46-
local a_directory
47-
a_directory="$(current_dir)"
44+
local a_directory="$(current_dir)"
4845

4946
assert_empty "$(assert_is_directory "$a_directory")"
5047
}
@@ -59,8 +56,7 @@ function test_unsuccessful_assert_is_directory() {
5956
}
6057

6158
function test_unsuccessful_assert_is_directory_when_a_file_is_given() {
62-
local a_file
63-
a_file="$(current_dir)/$(current_filename)"
59+
local a_file="$(current_dir)/$(current_filename)"
6460

6561
assert_same\
6662
"$(console_results::print_failed_test\
@@ -69,17 +65,13 @@ function test_unsuccessful_assert_is_directory_when_a_file_is_given() {
6965
}
7066

7167
function test_successful_assert_is_directory_empty() {
72-
local a_directory
73-
a_directory=$(mktemp -d)
68+
local a_directory=$(temp_dir)
7469

7570
assert_empty "$(assert_is_directory_empty "$a_directory")"
76-
77-
rmdir "$a_directory"
7871
}
7972

8073
function test_unsuccessful_assert_is_directory_empty() {
81-
local a_directory
82-
a_directory="$(current_dir)"
74+
local a_directory="$(current_dir)"
8375

8476
assert_same\
8577
"$(console_results::print_failed_test \
@@ -88,36 +80,28 @@ function test_unsuccessful_assert_is_directory_empty() {
8880
}
8981

9082
function test_successful_assert_is_directory_not_empty() {
91-
local a_directory
92-
a_directory="$(current_dir)"
83+
local a_directory="$(current_dir)"
9384

9485
assert_empty "$(assert_is_directory_not_empty "$a_directory")"
9586
}
9687

9788
function test_unsuccessful_assert_is_directory_not_empty() {
98-
local a_directory
99-
a_directory=$(mktemp -d)
89+
local a_directory=$(temp_dir)
10090

10191
assert_same\
10292
"$(console_results::print_failed_test \
10393
"Unsuccessful assert is directory not empty" "$a_directory" "to not be empty" "but is empty")"\
10494
"$(assert_is_directory_not_empty "$a_directory")"
105-
106-
rmdir "$a_directory"
10795
}
10896

10997
function test_successful_assert_is_directory_readable() {
110-
local a_directory
111-
a_directory=$(mktemp -d)
98+
local a_directory=$(temp_dir)
11299

113100
assert_empty "$(assert_is_directory_readable "$a_directory")"
114-
115-
rmdir "$a_directory"
116101
}
117102

118103
function test_unsuccessful_assert_is_directory_readable_when_a_file_is_given() {
119-
local a_file
120-
a_file="$(current_dir)/$(current_filename)"
104+
local a_file="$(current_dir)/$(current_filename)"
121105

122106
assert_same\
123107
"$(console_results::print_failed_test\
@@ -131,106 +115,84 @@ function test_unsuccessful_assert_is_directory_readable_without_execution_permis
131115
return
132116
fi
133117

134-
local a_directory
135-
a_directory=$(mktemp -d)
118+
local a_directory=$(temp_dir)
136119
chmod a-x "$a_directory"
137120

138121
assert_same\
139122
"$(console_results::print_failed_test \
140123
"Unsuccessful assert is directory readable without execution permission" \
141124
"$a_directory" "to be readable" "but is not readable")"\
142125
"$(assert_is_directory_readable "$a_directory")"
143-
144-
rmdir "$a_directory"
145126
}
146127

147128
function test_unsuccessful_assert_is_directory_readable_without_read_permission() {
148129
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
149130
return
150131
fi
151132

152-
local a_directory
153-
a_directory=$(mktemp -d)
133+
local a_directory=$(temp_dir)
154134
chmod a-r "$a_directory"
155135

156136
assert_same\
157137
"$(console_results::print_failed_test \
158138
"Unsuccessful assert is directory readable without read permission" \
159139
"$a_directory" "to be readable" "but is not readable")"\
160140
"$(assert_is_directory_readable "$a_directory")"
161-
162-
rmdir "$a_directory"
163141
}
164142

165143
function test_successful_assert_is_directory_not_readable_without_read_permission() {
166144
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
167145
return
168146
fi
169147

170-
local a_directory
171-
a_directory=$(mktemp -d)
148+
local a_directory=$(temp_dir)
172149
chmod a-r "$a_directory"
173150

174151
assert_empty "$(assert_is_directory_not_readable "$a_directory")"
175-
176-
rmdir "$a_directory"
177152
}
178153

179154
function test_successful_assert_is_directory_not_readable_without_execution_permission() {
180155
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
181156
return
182157
fi
183158

184-
local a_directory
185-
a_directory=$(mktemp -d)
159+
local a_directory=$(temp_dir)
186160
chmod a-x "$a_directory"
187161

188162
assert_empty "$(assert_is_directory_not_readable "$a_directory")"
189-
190-
rmdir "$a_directory"
191163
}
192164

193165
function test_unsuccessful_assert_is_directory_not_readable() {
194-
local a_directory
195-
a_directory=$(mktemp -d)
166+
local a_directory=$(temp_dir)
196167

197168
assert_same\
198169
"$(console_results::print_failed_test \
199170
"Unsuccessful assert is directory not readable" "$a_directory" "to be not readable" "but is readable")"\
200171
"$(assert_is_directory_not_readable "$a_directory")"
201-
202-
rmdir "$a_directory"
203172
}
204173

205174
function test_successful_assert_is_directory_writable() {
206-
local a_directory
207-
a_directory=$(mktemp -d)
175+
local a_directory=$(temp_dir)
208176

209177
assert_empty "$(assert_is_directory_writable "$a_directory")"
210-
211-
rmdir "$a_directory"
212178
}
213179

214180
function test_unsuccessful_assert_is_directory_writable() {
215181
if [[ "$_OS" == "Windows" || $_DISTRO = "Alpine" ]]; then
216182
return
217183
fi
218184

219-
local a_directory
220-
a_directory=$(mktemp -d)
185+
local a_directory=$(temp_dir)
221186
chmod a-w "$a_directory"
222187

223188
assert_same\
224189
"$(console_results::print_failed_test \
225190
"Unsuccessful assert is directory writable" "$a_directory" "to be writable" "but is not writable")"\
226191
"$(assert_is_directory_writable "$a_directory")"
227-
228-
rmdir "$a_directory"
229192
}
230193

231194
function test_unsuccessful_assert_is_directory_writable_when_a_file_is_given() {
232-
local a_file
233-
a_file="$(current_dir)/$(current_filename)"
195+
local a_file="$(current_dir)/$(current_filename)"
234196

235197
assert_same\
236198
"$(console_results::print_failed_test\
@@ -244,24 +206,18 @@ function test_successful_assert_is_directory_not_writable() {
244206
return
245207
fi
246208

247-
local a_directory
248-
a_directory=$(mktemp -d)
209+
local a_directory=$(temp_dir)
249210
chmod a-w "$a_directory"
250211

251212
assert_empty "$(assert_is_directory_not_writable "$a_directory")"
252-
253-
rmdir "$a_directory"
254213
}
255214

256215
function test_unsuccessful_assert_is_directory_not_writable() {
257-
local a_directory
258-
a_directory=$(mktemp -d)
216+
local a_directory=$(temp_dir)
259217

260218
assert_same\
261219
"$(console_results::print_failed_test\
262220
"Unsuccessful assert is directory not writable" \
263221
"$a_directory" "to be not writable" "but is writable")"\
264222
"$(assert_is_directory_not_writable "$a_directory")"
265-
266-
rmdir "$a_directory"
267223
}

0 commit comments

Comments
 (0)