Skip to content

Commit 6cc7c95

Browse files
authored
Merge pull request #160 from mbland/bats-function
testing/log: Use lib/bats/helper-function
2 parents 0391bc9 + 609f056 commit 6cc7c95

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

lib/testing/log

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# You must source `_GO_CORE_DIR/lib/testing/environment` before this file.
66

77
. "${BASH_SOURCE[0]%/*}/stack-trace"
8+
. "$_GO_CORE_DIR/lib/bats/helper-function"
89
. "$_GO_CORE_DIR/lib/bats/assertions"
910

1011
# Log timestamps are disabled by for testing by default.
@@ -21,12 +22,14 @@ export _GO_LOG_TIMESTAMP_FORMAT=
2122
# Arguments:
2223
# ...: Lines comprising the `./go` script
2324
@go.create_log_script(){
25+
set "$DISABLE_BATS_SHELL_OPTIONS"
2426
@go.create_test_go_script \
2527
". \"\$_GO_USE_MODULES\" 'log'" \
2628
'if [[ -n "$TEST_LOG_FILE" ]]; then' \
2729
' @go.log_add_output_file "$TEST_LOG_FILE"' \
2830
'fi' \
2931
"$@"
32+
restore_bats_shell_options "$?"
3033
}
3134

3235
# Creates and executes a `./go` script that imports the `log` module
@@ -42,6 +45,7 @@ export _GO_LOG_TIMESTAMP_FORMAT=
4245
# Arguments:
4346
# label: Log level label to format
4447
@go.format_log_label() {
48+
set "$DISABLE_BATS_SHELL_OPTIONS"
4549
local label="$1"
4650

4751
. "$_GO_USE_MODULES" 'log'
@@ -53,6 +57,7 @@ export _GO_LOG_TIMESTAMP_FORMAT=
5357
exit 1
5458
fi
5559
printf '%s' "${__GO_LOG_LEVELS_FORMATTED[$__go_log_level_index]}"
60+
restore_bats_shell_options "$?"
5661
}
5762

5863
# Validates that `output` matches the expected `@go.log` output
@@ -78,38 +83,9 @@ export _GO_LOG_TIMESTAMP_FORMAT=
7883
# Arguments:
7984
# ...: Lines of expected log output
8085
@go.assert_log_equals() {
81-
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
82-
local __log_level_label
83-
local expected=()
84-
local i
85-
86-
. "$_GO_USE_MODULES" 'format' 'log'
87-
_@go.log_init
88-
89-
for ((i=0; $# != 0; ++i)); do
90-
if __@go.parse_log_level_label "$1"; then
91-
expected+=("$__log_level_label $2")
92-
93-
if [[ "${__log_level_label:0:1}" == $'\e' ]]; then
94-
expected["$((${#expected[@]} - 1))"]+=$'\e[0m'
95-
fi
96-
97-
if ! shift 2; then
98-
printf 'ERROR: Wrong number of arguments for log line %d.\n' "$i" >&2
99-
return_from_bats_assertion 1
100-
return
101-
fi
102-
else
103-
expected+=("$1")
104-
shift
105-
fi
106-
done
107-
108-
if ! assert_lines_equal "${expected[@]}"; then
109-
return_from_bats_assertion '1'
110-
else
111-
return_from_bats_assertion
112-
fi
86+
set "$DISABLE_BATS_SHELL_OPTIONS"
87+
__@go.assert_log_equals "$@"
88+
restore_bats_shell_options "$?"
11389
}
11490

11591
# Validates that a file matches the expected `@go.log` output
@@ -131,15 +107,15 @@ export _GO_LOG_TIMESTAMP_FORMAT=
131107
# log_file: Path to the log file to validate
132108
# ...: Lines of expected log output
133109
@go.assert_log_file_equals() {
134-
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
110+
set "$DISABLE_BATS_SHELL_OPTIONS"
135111
local log_file="$1"
136112
shift
137113

138114
if ! set_bats_output_and_lines_from_file "$log_file"; then
139-
return_from_bats_assertion '1'
115+
restore_bats_shell_options '1'
140116
else
141117
@go.assert_log_equals "$@"
142-
return_from_bats_assertion "$?"
118+
restore_bats_shell_options "$?"
143119
fi
144120
}
145121

@@ -156,6 +132,7 @@ export _GO_LOG_TIMESTAMP_FORMAT=
156132
# Stack trace lines from `@go.log_command` comprising the command logging
157133
# mechanism
158134
@go.set_log_command_stack_trace_items() {
135+
set "$DISABLE_BATS_SHELL_OPTIONS"
159136
if [[ "${#LOG_COMMAND_STACK_TRACE_ITEMS[@]}" -eq '0' ]]; then
160137
export LOG_COMMAND_STACK_TRACE_ITEMS
161138
LOG_COMMAND_STACK_TRACE_ITEMS=(
@@ -166,6 +143,7 @@ export _GO_LOG_TIMESTAMP_FORMAT=
166143
# definition, not the actual `done < <(_@go.log_command_invoke)` line.
167144
"$(@go.stack_trace_item "$_GO_CORE_DIR/lib/log" '@go.log_command')")
168145
fi
146+
restore_bats_shell_options "$?"
169147
}
170148

171149
# --------------------------------
@@ -174,6 +152,38 @@ export _GO_LOG_TIMESTAMP_FORMAT=
174152
# None of the functions below this line are part of the public interface.
175153
# --------------------------------
176154

155+
# Implementation for `@go.assert_log_equals`, extracted for efficiency
156+
#
157+
# Arguments:
158+
# ...: Lines of expected log output
159+
__@go.assert_log_equals() {
160+
local __log_level_label
161+
local expected=()
162+
local i
163+
164+
. "$_GO_USE_MODULES" 'format' 'log'
165+
_@go.log_init
166+
167+
for ((i=0; $# != 0; ++i)); do
168+
if __@go.parse_log_level_label "$1"; then
169+
expected+=("$__log_level_label $2")
170+
171+
if [[ "${__log_level_label:0:1}" == $'\e' ]]; then
172+
expected["$((${#expected[@]} - 1))"]+=$'\e[0m'
173+
fi
174+
175+
if ! shift 2; then
176+
printf 'ERROR: Wrong number of arguments for log line %d.\n' "$i" >&2
177+
return 1
178+
fi
179+
else
180+
expected+=("$1")
181+
shift
182+
fi
183+
done
184+
assert_lines_equal "${expected[@]}"
185+
}
186+
177187
# Determines whether a log level label is a valid member of `_GO_LOG_LEVELS`.
178188
#
179189
# If so, pads the label and assigns it to `__log_level_label`.

0 commit comments

Comments
 (0)