Skip to content

Commit 8d425f5

Browse files
committed
Suppressing dedicated file descriptors for LOG, ERR, OUT and STACK.
I can not figure out why I introduced them at first :(
1 parent 422e7b4 commit 8d425f5

File tree

3 files changed

+82
-81
lines changed

3 files changed

+82
-81
lines changed

README.md

+3-13
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,9 @@ EOF
510510
This test calls *code*, which calls *ps*, which is actually implemented by *_ps*. Since *code* does not use *ax* but only *a* as parameters, this test should fail. But...
511511

512512
```output
513-
Running test_code_gives_ps_appropriate_parameters... FAILURE
514-
expected [ax] but was [a]
515-
doc:13:_ps()
516-
doc:2:code()
517-
doc:18:test_code_gives_ps_appropriate_parameters()
518-
SUCCESS
513+
Running test_code_gives_ps_appropriate_parameters... SUCCESS
519514
```
520515

521-
This test displays FAILURE but it actually succeed, see SUCCESS at the end.
522516
The problem here is that *ps* fail (because of the failed *assert_equals* assertion). But *ps* is piped with *grep*:
523517

524518
```shell
@@ -551,14 +545,10 @@ The problem here is that we use a trick to make the code under test fail but the
551545
failure has nothing to do with the actual *assert_equals* failure. This is really
552546
bad, don't do that.
553547

554-
Even if the output really looks like what we would expect, don't do that:
548+
Moreover, *assert_equals* output is captured by *ps* and this just messes with the display of our test results:
555549

556550
```output
557-
Running test_code_gives_ps_appropriate_parameters... FAILURE
558-
expected [ax] but was [a]
559-
doc:7:_ps()
560-
doc:2:code()
561-
doc:12:test_code_gives_ps_appropriate_parameters()
551+
Running test_code_gives_ps_appropriate_parameters...
562552
```
563553

564554
The only correct alternative is for the fake *ps* to write *FAKE_PARAMS* in a file descriptor

bash_unit

+19-29
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ GREEN="${ESCAPE}[92m"
2323
YELLOW="${ESCAPE}[93m"
2424
BLUE="${ESCAPE}[94m"
2525

26-
# All tests informations must be sent to the following three
27-
# file descriptors:
28-
# LOG -> all test logs, such as SUCCESS, FAILURE, stacktrace...
29-
# OUT -> all code under test outputs relevant in the context
30-
# ERR -> all code under test error outputs relevant in the context
31-
exec {LOG}>&1
32-
exec {STACK}>&1
33-
exec {OUT}>&1
34-
exec {ERR}>&1
35-
3626
fail() {
3727
local message=$1
3828
local stdout=$2
@@ -188,61 +178,61 @@ color() {
188178
text_format() {
189179
notify_suite_starting() {
190180
test_file="$1"
191-
echo "Running tests in $test_file" >&$LOG
181+
echo "Running tests in $test_file"
192182
}
193183
notify_test_starting() {
194184
local test="$1"
195-
echo -n "Running $test... " | color "$BLUE" >&$LOG
185+
echo -n "Running $test... " | color "$BLUE"
196186
}
197187
notify_test_succeeded() {
198-
color "$GREEN" "SUCCESS" >&$LOG
188+
color "$GREEN" "SUCCESS"
199189
}
200190
notify_test_failed() {
201191
local message="$2"
202-
color "$RED" "FAILURE" >&$LOG
203-
[[ -z $message ]] || printf -- "$message\n" >&$LOG
192+
color "$RED" "FAILURE"
193+
[[ -z $message ]] || printf -- "$message\n"
204194
}
205195
notify_stdout() {
206-
sed 's:^:out> :' | color $GREEN >&$OUT
196+
sed 's:^:out> :' | color $GREEN
207197
}
208198
notify_stderr() {
209-
sed 's:^:err> :' | color $RED >&$ERR
199+
sed 's:^:err> :' | color $RED
210200
}
211201
notify_stack() {
212-
color "$YELLOW" >&$STACK
202+
color "$YELLOW"
213203
}
214204
}
215205

216206
tap_format() {
217207
notify_suite_starting() {
218208
test_file="$1"
219-
echo "# Running tests in $test_file" >&$LOG
209+
echo "# Running tests in $test_file"
220210
}
221211
notify_test_starting() {
222212
echo -n
223213
}
224214
notify_test_succeeded() {
225215
local test="$1"
226-
echo -n "ok" | color "$GREEN" >&$LOG
227-
echo -n ' - ' >&$LOG
228-
echo "$test" | color "$BLUE" >&$LOG
216+
echo -n "ok" | color "$GREEN"
217+
echo -n ' - '
218+
echo "$test" | color "$BLUE"
229219
}
230220
notify_test_failed() {
231221
local test="$1"
232222
local message="$2"
233-
echo -n "not ok" | color "$RED" >&$LOG
234-
echo -n " - " >&$LOG
235-
echo "$test" >&$LOG
236-
[[ -z $message ]] || printf -- "$message\n" | sed -u -e 's/^/# /' >&$LOG
223+
echo -n "not ok" | color "$RED"
224+
echo -n " - "
225+
echo "$test"
226+
[[ -z $message ]] || printf -- "$message\n" | sed -u -e 's/^/# /'
237227
}
238228
notify_stdout() {
239-
sed 's:^:# out> :' | color $GREEN >&$OUT
229+
sed 's:^:# out> :' | color $GREEN
240230
}
241231
notify_stderr() {
242-
sed 's:^:# err> :' | color $RED >&$ERR
232+
sed 's:^:# err> :' | color $RED
243233
}
244234
notify_stack() {
245-
sed 's:^:# :' | color "$YELLOW" >&$STACK
235+
sed 's:^:# :' | color "$YELLOW"
246236
}
247237
}
248238

tests/test_bash_unit.sh

+60-39
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ test_assert_shows_stderr_on_failure() {
8585
)"
8686

8787
assert_equals "\
88-
err> some error message" \
88+
some error message" \
8989
"$message"
9090
}
9191

@@ -95,8 +95,8 @@ test_assert_shows_stdout_on_failure() {
9595
)"
9696

9797
assert_equals "\
98-
out> some ok message
99-
out> another ok message" \
98+
some ok message
99+
another ok message" \
100100
"$message"
101101
}
102102

@@ -238,58 +238,79 @@ last_line() {
238238
}
239239

240240
with_bash_unit_muted() {
241-
with_bash_unit_redirected "$@"
241+
with_bash_unit_notifications_muted "$@"
242242
}
243243

244244
with_bash_unit_err() {
245-
with_bash_unit_redirected -e '&1' "$@"
245+
with_bash_unit_notifications_muted -e "$@"
246246
}
247247

248248
with_bash_unit_out() {
249-
with_bash_unit_redirected -o '&1' "$@"
249+
with_bash_unit_notifications_muted -o "$@"
250250
}
251251

252252
with_bash_unit_log() {
253-
with_bash_unit_redirected -l '&1' "$@"
253+
with_bash_unit_notifications_muted -l "$@"
254254
}
255255

256256
with_bash_unit_stack() {
257-
with_bash_unit_redirected -s '&1' "$@"
258-
}
259-
260-
with_bash_unit_redirected() {
261-
local log=/dev/null
262-
local stack=/dev/null
263-
local out=/dev/null
264-
local err=/dev/null
265-
266-
unset OPTIND
267-
while getopts "l:s:o:e:" option
268-
do
269-
case "$option" in
270-
l)
271-
log=$OPTARG
272-
;;
273-
s)
274-
stack=$OPTARG
275-
;;
276-
o)
277-
out=$OPTARG
278-
;;
279-
e)
280-
err=$OPTARG
281-
;;
282-
esac
283-
done
284-
shift $((OPTIND-1))
257+
with_bash_unit_notifications_muted -s "$@"
258+
}
285259

260+
with_bash_unit_notifications_muted() {
286261
(
287-
eval "exec {LOG}>$log"
288-
eval "exec {STACK}>$stack"
289-
eval "exec {OUT}>$out"
290-
eval "exec {ERR}>$err"
262+
mute
263+
unset OPTIND
264+
while getopts "lsoe" option
265+
do
266+
case "$option" in
267+
l)
268+
unmute_logs
269+
;;
270+
s)
271+
unmute_stack
272+
;;
273+
o)
274+
unmute_out
275+
;;
276+
e)
277+
unmute_err
278+
;;
279+
esac
280+
done
281+
shift $((OPTIND-1))
282+
291283
"$@"
292284
)
293285
}
294286

287+
unmute_logs() {
288+
notify_suite_starting() { echo "Running tests in $1" ; }
289+
notify_test_starting () { echo -n "Running $1... " ; }
290+
notify_test_succeeded() { echo "SUCCESS" ; }
291+
notify_test_failed () { echo "FAILURE" ; echo $2 ; }
292+
}
293+
294+
unmute_stack() {
295+
notify_stack() { cat ; }
296+
}
297+
298+
unmute_out() {
299+
notify_stdout() { cat ; }
300+
}
301+
302+
unmute_err() {
303+
notify_stderr() { cat ; }
304+
}
305+
306+
mute() {
307+
notify_suite_starting() { echo -n ; }
308+
notify_test_starting () { echo -n ; }
309+
notify_test_succeeded() { echo -n ; }
310+
notify_test_failed () { echo -n ; }
311+
notify_stack () { echo -n ; }
312+
notify_stdout () { echo -n ; }
313+
notify_stderr () { echo -n ; }
314+
}
315+
295316
BASH_UNIT=../bash_unit

0 commit comments

Comments
 (0)