Skip to content

Commit e43e9e0

Browse files
committed
lib/testing: Rename and test format_log_label
1 parent 5c04be5 commit e43e9e0

File tree

5 files changed

+56
-30
lines changed

5 files changed

+56
-30
lines changed

lib/testing/log

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ run_log_script() {
3333
run "$TEST_GO_SCRIPT"
3434
}
3535

36+
# Format a `log` module log level label
37+
#
38+
# Note that this must be called before any other log assertion, because it needs
39+
# to set _GO_LOG_FORMATTING before importing the `log` module.
40+
#
41+
# Arguments:
42+
# label: Log level label to format
43+
format_log_label() {
44+
local label="$1"
45+
46+
if [[ -n "$__GO_LOG_INIT" ]]; then
47+
local err_format=("\`%s\` must be called before any other function"
48+
"or assertion that calls \`. \"\$_GO_USE_MODULES\" 'log'\`"
49+
'because it needs to set `_GO_LOG_FORMATTING`.')
50+
printf "${err_format[*]}" "$FUNCNAME" >&2
51+
exit 1
52+
fi
53+
54+
_GO_LOG_FORMATTING='true'
55+
. "$_GO_USE_MODULES" 'log'
56+
_@go.log_init
57+
58+
local __go_log_level_index=0
59+
if ! _@go.log_level_index "$label"; then
60+
printf 'Unknown log level label: %s\n' "$label" >&2
61+
exit 1
62+
fi
63+
printf '%s' "${__GO_LOG_LEVELS_FORMATTED[$__go_log_level_index]}"
64+
}
65+
3666
assert_log_equals() {
3767
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
3868
local level
@@ -84,30 +114,6 @@ assert_log_file_equals() {
84114
fi
85115
}
86116

87-
# Note that this must be called before any other log assertion, because it needs
88-
# to set _GO_LOG_FORMATTING before importing the `log` module.
89-
format_label() {
90-
local label="$1"
91-
92-
if [[ -n "$__GO_LOG_INIT" ]]; then
93-
echo "$FUNCNAME must be called before any other function or assertion" \
94-
"that calls \`. \$_GO_USE_MODULES 'log'\` because it needs to set" \
95-
"\`_GO_LOG_FORMATTING\`." >&2
96-
return 1
97-
fi
98-
99-
_GO_LOG_FORMATTING='true'
100-
. "$_GO_USE_MODULES" 'log'
101-
_@go.log_init
102-
103-
local __go_log_level_index=0
104-
if ! _@go.log_level_index "$label"; then
105-
echo "Unknown log level label: $label" >&2
106-
return 1
107-
fi
108-
echo "${__GO_LOG_LEVELS_FORMATTED[$__go_log_level_index]}"
109-
}
110-
111117
# Creates `LOG_COMMAND_STACK_TRACE_ITEMS` to help validate stack trace output.
112118
#
113119
# Call this before using "${LOG_COMMAND_STACK_TRACE_ITEMS[@]}" to inject
@@ -170,5 +176,3 @@ __expected_log_line() {
170176
printf '%b\n' "$level $message"
171177
fi
172178
}
173-
174-

tests/log/add-output-file.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ run_log_script_and_assert_status_and_output() {
6666
"@go.log_add_output_file '$TEST_GO_ROOTDIR/info.log' 'INFO'" \
6767
"@go.log INFO Hello, World!"
6868
assert_success
69-
assert_log_equals "$(format_label INFO)" 'Hello, World!'
69+
assert_log_equals "$(format_log_label INFO)" 'Hello, World!'
7070
assert_file_equals "$TEST_GO_ROOTDIR/info.log" "${lines[@]}"
7171
}
7272

tests/log/log-command.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ teardown() {
115115
}
116116

117117
@test "$SUITE: log failing command to standard error with formatting" {
118-
local formatted_run_level_label="$(format_label RUN)"
119-
local formatted_error_level_label="$(format_label ERROR)"
118+
local formatted_run_level_label="$(format_log_label RUN)"
119+
local formatted_error_level_label="$(format_log_label ERROR)"
120120

121121
_GO_LOG_FORMATTING='true' run_log_script \
122122
'function failing_function() {' \

tests/log/main.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ teardown() {
2323
'echo Goodbye, World!'
2424
assert_success
2525
assert_log_equals \
26-
"$(format_label INFO)" '\e[1m\e[36mHello, World!' \
26+
"$(format_log_label INFO)" '\e[1m\e[36mHello, World!' \
2727
'Goodbye, World!'
2828
}
2929

tests/testing/log.bats

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,25 @@ teardown() {
3636
"^ $_GO_CORE_DIR/lib/log:[0-9]+ _@go.log_command_invoke$" \
3737
"^ $_GO_CORE_DIR/lib/log:[0-9]+ @go.log_command$"
3838
}
39+
40+
@test "$SUITE: format_log_label" {
41+
run format_log_label INFO
42+
assert_success
43+
44+
local expected_message
45+
printf -v expected_message '%b' "$output Hello, World!\e[0m"
46+
47+
_GO_LOG_FORMATTING='true' run_log_script '@go.log INFO Hello, World!'
48+
assert_success "$expected_message"
49+
}
50+
51+
@test "$SUITE: format_log_label exits if log module already initialized" {
52+
__GO_LOG_INIT='true' run format_log_label INFO
53+
assert_failure
54+
assert_output_matches '^`format_log_label` must be called before any other '
55+
}
56+
57+
@test "$SUITE: format_log_label exits if log level label invalid" {
58+
run format_log_label FOOBAR
59+
assert_failure 'Unknown log level label: FOOBAR'
60+
}

0 commit comments

Comments
 (0)