Skip to content

Commit 3c5b1ae

Browse files
authored
Merge pull request #111 from mbland/test-namespace
Put all `lib/testing` functions in `@go.` namespace
2 parents 0d5841b + 316cfc6 commit 3c5b1ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+554
-549
lines changed

lib/testing/environment

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ readonly TEST_GO_PLUGINS_DIR="$TEST_GO_SCRIPTS_DIR/plugins"
3939
#
4040
# Call this from `teardown` if your tests create any files or directories in
4141
# `TEST_GO_ROOTDIR`.
42-
remove_test_go_rootdir() {
42+
@go.remove_test_go_rootdir() {
4343
remove_bats_test_dirs
4444
}
4545

@@ -50,7 +50,7 @@ remove_test_go_rootdir() {
5050
#
5151
# Arguments:
5252
# ...: Lines comprising the `./go` script
53-
create_test_go_script() {
53+
@go.create_test_go_script() {
5454
create_bats_test_script 'go' \
5555
". '$_GO_CORE_DIR/go-core.bash' '$TEST_GO_SCRIPTS_RELATIVE_DIR'" \
5656
"$@"
@@ -82,7 +82,7 @@ test-go() {
8282
# Arguments:
8383
# script_name: Name of the command script
8484
# ...: Lines comprising the `./go` command script
85-
create_test_command_script() {
85+
@go.create_test_command_script() {
8686
create_bats_test_script "$TEST_GO_SCRIPTS_RELATIVE_DIR/$1" "${@:2}"
8787
}
8888

@@ -97,14 +97,14 @@ create_test_command_script() {
9797
# Arguments:
9898
# parent_name: Name of the "parent" command script
9999
# ...: Names of "child" command scripts
100-
create_parent_and_subcommands() {
100+
@go.create_parent_and_subcommands() {
101101
local parent="$1"
102102
shift
103103
local subcommand
104104

105-
create_test_command_script "$parent"
105+
@go.create_test_command_script "$parent"
106106

107107
for subcommand in "$@"; do
108-
create_test_command_script "$parent.d/$subcommand"
108+
@go.create_test_command_script "$parent.d/$subcommand"
109109
done
110110
}

lib/testing/log

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export _GO_LOG_TIMESTAMP_FORMAT=
2020
#
2121
# Arguments:
2222
# ...: Lines comprising the `./go` script
23-
create_log_script(){
24-
create_test_go_script \
23+
@go.create_log_script(){
24+
@go.create_test_go_script \
2525
". \"\$_GO_USE_MODULES\" 'log'" \
2626
'if [[ -n "$TEST_LOG_FILE" ]]; then' \
2727
' @go.log_add_output_file "$TEST_LOG_FILE"' \
@@ -31,17 +31,17 @@ create_log_script(){
3131

3232
# Creates and executes a `./go` script that imports the `log` module
3333
#
34-
# Globals and arguments are identical to `create_log_script`.
35-
run_log_script() {
36-
create_log_script "$@"
34+
# Globals and arguments are identical to `@go.create_log_script`.
35+
@go.run_log_script() {
36+
@go.create_log_script "$@"
3737
run "$TEST_GO_SCRIPT"
3838
}
3939

4040
# Format a `log` module log level label
4141
#
4242
# Arguments:
4343
# label: Log level label to format
44-
format_log_label() {
44+
@go.format_log_label() {
4545
local label="$1"
4646

4747
. "$_GO_USE_MODULES" 'log'
@@ -65,6 +65,10 @@ format_log_label() {
6565
# plaintext label or a label formatted with `format_log_label`. Any unknown
6666
# labels will be parsed as "a single string without a log label prefix".
6767
#
68+
# If the log label contains format codes, a formatting reset code will
69+
# automatically get appended to the expected log message, so there's no need to
70+
# add it explicitly.
71+
#
6872
# If you aren't looking for exact log output, consider using one of the standard
6973
# assertions from `_GO_CORE_DIR/lib/bats/assertions` instead.
7074
#
@@ -73,7 +77,7 @@ format_log_label() {
7377
#
7478
# Arguments:
7579
# ...: Lines of expected log output
76-
assert_log_equals() {
80+
@go.assert_log_equals() {
7781
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
7882
local __log_level_label
7983
local expected=()
@@ -83,7 +87,7 @@ assert_log_equals() {
8387
_@go.log_init
8488

8589
for ((i=0; $# != 0; ++i)); do
86-
if __parse_log_level_label "$1"; then
90+
if __@go.parse_log_level_label "$1"; then
8791
expected+=("$__log_level_label $2")
8892

8993
if [[ "${__log_level_label:0:1}" == $'\e' ]]; then
@@ -111,30 +115,30 @@ assert_log_equals() {
111115
# Validates that a file matches the expected `@go.log` output
112116
#
113117
# Aside from the first file path argument, the remaining arguments and semantics
114-
# are exactly the same as `assert_log_equals`.
118+
# are exactly the same as `@go.assert_log_equals`.
115119
#
116120
# In cases where `@go.log_add_output_file` is used to add a `TEST_LOG_FILE` to
117121
# all levels, and all test output comes from `@go.log`, this may be invoked
118-
# following `assert_log_equals` like so to ensure the log file output matches
119-
# standard output and standard error:
122+
# following `@go.assert_log_equals` like so to ensure the log file output
123+
# matches standard output and standard error:
120124
#
121-
# assert_log_file_equals "$TEST_LOG_FILE" "${lines[@]}"
125+
# @go.assert_log_file_equals "$TEST_LOG_FILE" "${lines[@]}"
122126
#
123127
# If you aren't looking for exact log output, consider using one of the standard
124128
# assertions from `_GO_CORE_DIR/lib/bats/assertions` instead.
125129
#
126130
# Arguments:
127131
# log_file: Path to the log file to validate
128132
# ...: Lines of expected log output
129-
assert_log_file_equals() {
133+
@go.assert_log_file_equals() {
130134
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
131135
local log_file="$1"
132136
shift
133137

134138
if ! set_bats_output_and_lines_from_file "$log_file"; then
135139
return_from_bats_assertion '1'
136140
else
137-
assert_log_equals "$@"
141+
@go.assert_log_equals "$@"
138142
return_from_bats_assertion "$?"
139143
fi
140144
}
@@ -151,16 +155,16 @@ assert_log_file_equals() {
151155
# LOG_COMMAND_STACK_TRACE_ITEMS:
152156
# Stack trace lines from `@go.log_command` comprising the command logging
153157
# mechanism
154-
set_log_command_stack_trace_items() {
158+
@go.set_log_command_stack_trace_items() {
155159
if [[ "${#LOG_COMMAND_STACK_TRACE_ITEMS[@]}" -eq '0' ]]; then
156160
export LOG_COMMAND_STACK_TRACE_ITEMS
157161
LOG_COMMAND_STACK_TRACE_ITEMS=(
158-
"$(stack_trace_item "$_GO_CORE_DIR/lib/log" '_@go.log_command_invoke' \
159-
' "${__go_log_command_args[@]}" 2>&1')"
162+
"$(@go.stack_trace_item "$_GO_CORE_DIR/lib/log" \
163+
'_@go.log_command_invoke' ' "${__go_log_command_args[@]}" 2>&1')"
160164
# For some reason, with the process redirection at the end of the
161165
# `while` loop, the stack trace reports the opening line of the function
162166
# definition, not the actual `done < <(_@go.log_command_invoke)` line.
163-
"$(stack_trace_item "$_GO_CORE_DIR/lib/log" '@go.log_command')")
167+
"$(@go.stack_trace_item "$_GO_CORE_DIR/lib/log" '@go.log_command')")
164168
fi
165169
}
166170

@@ -180,7 +184,7 @@ set_log_command_stack_trace_items() {
180184
# Returns:
181185
# zero if `level_label` exists in `_GO_LOG_LEVELS`, nonzero otherwise
182186
# __log_level_label: Assigned the padded version of `level_label` if valid
183-
__parse_log_level_label() {
187+
__@go.parse_log_level_label() {
184188
local level_label="$1"
185189
local try_level
186190
local __go_log_level_index

lib/testing/stack-trace

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

77
# Get the stack trace of a line from a file or function as it would appear in
8-
# @go.print_stack_trace output.
8+
# `@go.print_stack_trace` output.
99
#
1010
# Arguments:
1111
# filepath: Path to the file containing the line
1212
# function_name: Function in which the line appears, 'main', or 'source'
1313
# target_line: Line for which to produce a stack trace line
14-
stack_trace_item() {
14+
@go.stack_trace_item() {
1515
local filepath="$1"
1616
local function_name="$2"
1717
local target_line="$3"
1818

19-
__check_file_path_specified_and_present "$filepath"
19+
__@go.check_file_path_specified_and_present "$filepath"
2020

2121
if [[ -z "$function_name" ]]; then
2222
printf 'No function name specified for `%s`.\n' "$FUNCNAME" >&2
@@ -48,7 +48,7 @@ stack_trace_item() {
4848
# GO_CORE_STACK_TRACE_COMPONENTS:
4949
# Stack trace lines from `go-core.bash` comprising the command script
5050
# dispatch mechanism
51-
set_go_core_stack_trace_components() {
51+
@go.set_go_core_stack_trace_components() {
5252
local go_core_file="$_GO_CORE_DIR/go-core.bash"
5353
local stack_item
5454
local script="$TEST_GO_ROOTDIR/generate-go-core-stack-trace"
@@ -58,7 +58,7 @@ set_go_core_stack_trace_components() {
5858
create_bats_test_script "${script#$TEST_GO_ROOTDIR}" \
5959
". '$_GO_CORE_DIR/go-core.bash' '$TEST_GO_SCRIPTS_RELATIVE_DIR'" \
6060
'@go "$@"'
61-
create_test_command_script 'print-stack-trace' '@go.print_stack_trace'
61+
@go.create_test_command_script 'print-stack-trace' '@go.print_stack_trace'
6262

6363
for stack_item in $("$script" 'print-stack-trace'); do
6464
if [[ "$stack_item" =~ $go_core_file ]]; then
@@ -81,15 +81,15 @@ set_go_core_stack_trace_components() {
8181
# filepath: Path to the file used to generate the stack trace item
8282
# offset: Offset from the last line to get the line number (default 0)
8383
# funcname: Name of the function expected in the stack trace (default 'main')
84-
stack_trace_item_from_offset() {
84+
@go.stack_trace_item_from_offset() {
8585
local filepath="${1}"
8686
local offset="${2:-0}"
8787
local funcname="${3:-main}"
8888
local num_lines='0'
8989
local lineno
9090

91-
__check_file_path_specified_and_present "$filepath"
92-
count_lines "$filepath" 'num_lines'
91+
__@go.check_file_path_specified_and_present "$filepath"
92+
@go.count_lines "$filepath" 'num_lines'
9393
lineno="$((num_lines - offset))"
9494
printf ' %s:%d %s\n' "$filepath" "$lineno" "$funcname"
9595
}
@@ -99,13 +99,13 @@ stack_trace_item_from_offset() {
9999
# Arguments:
100100
# filepath: Path to the file
101101
# result_name: Name of the caller's variable in which to store the result
102-
count_lines() {
102+
@go.count_lines() {
103103
local filepath="$1"
104104
local result_name="$2"
105105
local line
106106
local i='0'
107107

108-
__check_file_path_specified_and_present "$filepath"
108+
__@go.check_file_path_specified_and_present "$filepath"
109109
if [[ -z "$result_name" ]]; then
110110
printf 'No result variable specified for `%s`.\n' "$FUNCNAME" >&2
111111
exit 1
@@ -129,7 +129,7 @@ count_lines() {
129129
#
130130
# Arguments:
131131
# filepath: File path value to confirm
132-
__check_file_path_specified_and_present() {
132+
__@go.check_file_path_specified_and_present() {
133133
if [[ -z "$1" ]]; then
134134
printf 'No file specified for `%s`.\n' "${FUNCNAME[1]}" >&2
135135
exit 1

lib/testing/stack-trace-item

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
. "$_GO_CORE_DIR/lib/bats/helpers"
99

10-
__stack_trace_item() {
10+
__@go.stack_trace_item() {
1111
local filepath="$1"
1212
local function_name="$2"
1313
local target_line="$3"
@@ -86,4 +86,4 @@ __stack_trace_item() {
8686
exit 1
8787
}
8888

89-
__stack_trace_item "$@"
89+
__@go.stack_trace_item "$@"

lib/testing/stubbing

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
# Replace a module in `_GO_CORE_DIR/lib` with a stub implementation
66
#
7-
# THIS IS POTENTIALLY DANGEROUS and you MUST call `restore_stubbed_core_modules`
8-
# in your `teardown` function if you use it!
7+
# THIS IS POTENTIALLY DANGEROUS and you MUST call
8+
# `@go.restore_stubbed_core_modules` in your `teardown` function if you use it!
99
#
1010
# Useful when you need to model core module behavior, but don't want it to do
1111
# real work or want to avoid a potentially complicated test setup.
1212
#
1313
# Arguments:
1414
# module_name: Name of the module to stub from `_GO_CORE_DIR/lib`
1515
# ...: Lines comprising the stubbed module implementation
16-
create_core_module_stub() {
16+
@go.create_core_module_stub() {
1717
local module_path="$_GO_CORE_DIR/lib/$1"
1818
shift
1919

@@ -28,10 +28,10 @@ create_core_module_stub() {
2828
chmod 600 "$module_path"
2929
}
3030

31-
# Restore all `_GO_CORE_DIR/lib` modules stubbed by `create_core_module_stub`
31+
# Restore all core modules stubbed by `@go.create_core_module_stub`
3232
#
33-
# YOU MUST CALL THIS FROM TEARDOWN IF YOU USE `create_core_module_stub`!
34-
restore_stubbed_core_modules() {
33+
# YOU MUST CALL THIS FROM TEARDOWN IF YOU USE `@go.create_core_module_stub`!
34+
@go.restore_stubbed_core_modules() {
3535
local module
3636

3737
for module in "$_GO_CORE_DIR/lib"/*.stubbed; do

tests/builtins/doc-only-scripts.bats

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
load ../environment
1212

1313
teardown() {
14-
remove_test_go_rootdir
14+
@go.remove_test_go_rootdir
1515
}
1616

1717
@test "$SUITE: 'edit' script should exit with an error" {
1818
local expected="ERROR: \"$TEST_GO_SCRIPT edit\" "
1919
expected+='is implemented directly within the @go function.'
2020

21-
create_test_go_script "_@go.source_builtin 'edit'"
21+
@go.create_test_go_script "_@go.source_builtin 'edit'"
2222
run "$TEST_GO_SCRIPT"
2323
assert_failure "$expected"
2424
}
@@ -27,7 +27,7 @@ teardown() {
2727
local expected="ERROR: \"$TEST_GO_SCRIPT run\" "
2828
expected+='is implemented directly within the @go function.'
2929

30-
create_test_go_script "_@go.source_builtin 'run'"
30+
@go.create_test_go_script "_@go.source_builtin 'run'"
3131
run "$TEST_GO_SCRIPT"
3232
assert_failure "$expected"
3333
}
@@ -37,7 +37,7 @@ teardown() {
3737
expected+='is implemented in the function generated by '
3838
expected+="\"$TEST_GO_SCRIPT env\"."
3939

40-
create_test_go_script "_@go.source_builtin 'unenv'"
40+
@go.create_test_go_script "_@go.source_builtin 'unenv'"
4141
run "$TEST_GO_SCRIPT"
4242
assert_failure "$expected"
4343
}

tests/changes.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ setup() {
77
}
88

99
teardown() {
10-
remove_test_go_rootdir
10+
@go.remove_test_go_rootdir
1111
}
1212

1313
@test "$SUITE: tab completions" {

tests/command-descriptions.bats

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ setup() {
77
}
88

99
teardown() {
10-
rm -rf "$TEST_GO_ROOTDIR"
10+
@go.remove_test_go_rootdir
1111
}
1212

1313
create_cmd_path_and_name_go_script() {
14-
create_test_go_script \
14+
@go.create_test_go_script \
1515
". '$_GO_CORE_DIR/lib/internal/command_descriptions'" \
1616
'declare __go_cmd_name' \
1717
'if _@go.check_command_path_and_parse_command_name "$@"; then' \
@@ -23,14 +23,14 @@ create_cmd_path_and_name_go_script() {
2323

2424
@test "$SUITE: check command path and parse command name passes" {
2525
create_cmd_path_and_name_go_script
26-
create_test_command_script 'foo'
26+
@go.create_test_command_script 'foo'
2727
run "$TEST_GO_SCRIPT" "$TEST_GO_SCRIPTS_DIR/foo"
2828
assert_success '__go_cmd_name: foo'
2929
}
3030

3131
@test "$SUITE: check sub-command path and parse command name passes" {
3232
create_cmd_path_and_name_go_script
33-
create_test_command_script 'foo.d/bar.d/baz'
33+
@go.create_test_command_script 'foo.d/bar.d/baz'
3434
run "$TEST_GO_SCRIPT" "$TEST_GO_SCRIPTS_DIR/foo.d/bar.d/baz"
3535
assert_success '__go_cmd_name: foo bar baz'
3636
}

0 commit comments

Comments
 (0)