Skip to content

Commit 422e7b4

Browse files
committed
Refactor format code.
notify_* functions are defined only once by a function, depending on the chosen output format.
1 parent b1c4b5b commit 422e7b4

File tree

1 file changed

+72
-87
lines changed

1 file changed

+72
-87
lines changed

bash_unit

Lines changed: 72 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fail() {
4242
[[ ! -z $stdout ]] && [ -s $stdout ] && cat $stdout | notify_stdout
4343
[[ ! -z $stderr ]] && [ -s $stderr ] && cat $stderr | notify_stderr
4444

45-
print_stack | grep -v ^$BASH_SOURCE | notify_stack
45+
stacktrace | notify_stack
4646
exit 1
4747
}
4848

@@ -129,6 +129,15 @@ fake() {
129129
export -f $command
130130
}
131131

132+
stacktrace() {
133+
local i=1
134+
while ! [ -z ${BASH_SOURCE[$i]} ]
135+
do
136+
echo ${BASH_SOURCE[$i]}:${BASH_LINENO[$((i-1))]}:${FUNCNAME[$i]}\(\)
137+
i=$(($i + 1))
138+
done | grep -v ^$BASH_SOURCE
139+
}
140+
132141
run_test_suite() {
133142
local failure=0
134143
for test in $(set | grep -E '^test.* ()' | grep -E "$test_pattern" | sed -e 's: .*::')
@@ -163,16 +172,7 @@ usage() {
163172

164173
# Formating
165174

166-
print_stack() {
167-
local i=1
168-
while ! [ -z ${BASH_SOURCE[$i]} ]
169-
do
170-
echo ${BASH_SOURCE[$i]}:${BASH_LINENO[$((i-1))]}:${FUNCNAME[$i]}\(\)
171-
i=$(($i + 1))
172-
done
173-
}
174-
175-
format() {
175+
color() {
176176
local color=$1
177177
shift
178178
if [ -t 1 ] ; then echo -en "$color" ; fi
@@ -185,76 +185,68 @@ format() {
185185
if [ -t 1 ] ; then echo -en "$NOCOLOR" ; fi
186186
}
187187

188-
notify_suite_starting() {
189-
test_file="$1"
190-
echo "Running tests in $test_file" >&$LOG
191-
}
192-
193-
notify_test_starting() {
194-
local test="$1"
195-
echo -n "Running $test... " | format "$BLUE" >&$LOG
196-
}
197-
198-
notify_test_succeeded() {
199-
format "$GREEN" "SUCCESS" >&$LOG
200-
}
201-
202-
notify_test_failed() {
203-
local message="$2"
204-
format "$RED" "FAILURE" >&$LOG
205-
[[ -z $message ]] || printf -- "$message\n" >&$LOG
206-
}
207-
208-
notify_stdout() {
209-
sed 's:^:out> :' | format $GREEN >&$OUT
210-
}
211-
212-
notify_stderr() {
213-
sed 's:^:err> :' | format $RED >&$ERR
214-
}
215-
216-
notify_stack() {
217-
format "$YELLOW" >&$STACK
218-
}
219-
220-
tap_notify_suite_starting() {
221-
test_file="$1"
222-
echo "# Running tests in $test_file" >&$LOG
223-
}
224-
225-
tap_notify_test_starting() {
226-
echo -n
227-
}
228-
229-
tap_notify_test_succeeded() {
230-
local test="$1"
231-
echo -n "ok" | format "$GREEN" >&$LOG
232-
echo -n ' - ' >&$LOG
233-
echo "$test" | format "$BLUE" >&$LOG
234-
}
235-
236-
tap_notify_test_failed() {
237-
local test="$1"
238-
local message="$2"
239-
echo -n "not ok" | format "$RED" >&$LOG
240-
echo -n " - " >&$LOG
241-
echo "$test" >&$LOG
242-
[[ -z $message ]] || printf -- "$message\n" | sed -u -e 's/^/# /' >&$LOG
243-
}
244-
245-
tap_notify_stdout() {
246-
sed 's:^:# out> :' | format $GREEN >&$OUT
247-
}
248-
249-
tap_notify_stderr() {
250-
sed 's:^:# err> :' | format $RED >&$ERR
188+
text_format() {
189+
notify_suite_starting() {
190+
test_file="$1"
191+
echo "Running tests in $test_file" >&$LOG
192+
}
193+
notify_test_starting() {
194+
local test="$1"
195+
echo -n "Running $test... " | color "$BLUE" >&$LOG
196+
}
197+
notify_test_succeeded() {
198+
color "$GREEN" "SUCCESS" >&$LOG
199+
}
200+
notify_test_failed() {
201+
local message="$2"
202+
color "$RED" "FAILURE" >&$LOG
203+
[[ -z $message ]] || printf -- "$message\n" >&$LOG
204+
}
205+
notify_stdout() {
206+
sed 's:^:out> :' | color $GREEN >&$OUT
207+
}
208+
notify_stderr() {
209+
sed 's:^:err> :' | color $RED >&$ERR
210+
}
211+
notify_stack() {
212+
color "$YELLOW" >&$STACK
213+
}
251214
}
252215

253-
tap_notify_stack() {
254-
sed 's:^:# :' | format "$YELLOW" >&$STACK
216+
tap_format() {
217+
notify_suite_starting() {
218+
test_file="$1"
219+
echo "# Running tests in $test_file" >&$LOG
220+
}
221+
notify_test_starting() {
222+
echo -n
223+
}
224+
notify_test_succeeded() {
225+
local test="$1"
226+
echo -n "ok" | color "$GREEN" >&$LOG
227+
echo -n ' - ' >&$LOG
228+
echo "$test" | color "$BLUE" >&$LOG
229+
}
230+
notify_test_failed() {
231+
local test="$1"
232+
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
237+
}
238+
notify_stdout() {
239+
sed 's:^:# out> :' | color $GREEN >&$OUT
240+
}
241+
notify_stderr() {
242+
sed 's:^:# err> :' | color $RED >&$ERR
243+
}
244+
notify_stack() {
245+
sed 's:^:# :' | color "$YELLOW" >&$STACK
246+
}
255247
}
256248

257-
output_format=default
249+
output_format=text
258250
test_pattern=""
259251
separator=""
260252
while getopts "p:f:" option
@@ -274,25 +266,18 @@ do
274266
done
275267
shift $((OPTIND-1))
276268

277-
#check test files exist and is readable
278269
for test_file in "$@"
279270
do
280271
test -e $test_file || usage "file does not exist: $test_file"
281272
test -r $test_file || usage "can not read file: $test_file"
282273
done
283274

284-
#set output format
285275
case "$output_format" in
286-
default)
276+
text)
277+
text_format
287278
;;
288279
tap)
289-
notify_suite_starting() { tap_notify_suite_starting "$@" ; }
290-
notify_test_starting() { tap_notify_test_starting "$@" ; }
291-
notify_test_succeeded() { tap_notify_test_succeeded "$@" ; }
292-
notify_test_failed() { tap_notify_test_failed "$@" ; }
293-
notify_stdout() { tap_notify_stdout "$@" ; }
294-
notify_stderr() { tap_notify_stderr "$@" ; }
295-
notify_stack() { tap_notify_stack "$@" ; }
280+
tap_format
296281
;;
297282
*)
298283
usage "unsupproted output format: $output_format"

0 commit comments

Comments
 (0)