Skip to content

Commit 5c04be5

Browse files
committed
lib/testing: Test create/run_log_script, test-go
Also moves `test-go` from `lib/testing/log` to `lib/testing/environment`, as it may prove generally useful.
1 parent 03162bd commit 5c04be5

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

lib/testing/environment

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ create_test_go_script() {
6262
fi
6363
}
6464

65+
# Sets `_GO_CMD` to make for neater test output when running `TEST_GO_SCRIPT`
66+
#
67+
# Use `run test-go` instead of `run "$TEST_GO_SCRIPT"` to achieve the effect.
68+
#
69+
# Useful for tests that contain `_GO_CMD` in the output, to avoid clouding the
70+
# output with the full `TEST_GO_SCRIPT` path.
71+
#
72+
# Arguments:
73+
# ...: Command line arguments for `TEST_GO_SCRIPT`
74+
test-go() {
75+
_GO_CMD="$FUNCNAME" "$TEST_GO_SCRIPT" "$@"
76+
}
77+
6578
# Creates an executable `./go` command script in `TEST_GO_SCRIPTS_DIR`
6679
#
6780
# The script will be created as `$TEST_GO_SCRIPTS_DIR/$script_name`.

lib/testing/log

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

77
. "${BASH_SOURCE[0]%/*}/stack-trace"
88

9+
# Creates a `./go` script that imports the `log` module
10+
#
11+
# If `TEST_LOG_FILE` is defined, it will add that file for logging output as
12+
# well.
13+
#
14+
# Globals:
15+
# TEST_LOG_FILE: (Optional) Path to the log file to add for all log levels
16+
#
17+
# Arguments:
18+
# ...: Lines comprising the `./go` script
919
create_log_script(){
1020
create_test_go_script \
1121
". \"\$_GO_USE_MODULES\" 'log'" \
@@ -15,17 +25,14 @@ create_log_script(){
1525
"$@"
1626
}
1727

28+
# Creates and executes a `./go` script that imports the `log` module
29+
#
30+
# Globals and arguments are identical to `create_log_script`.
1831
run_log_script() {
1932
create_log_script "$@"
2033
run "$TEST_GO_SCRIPT"
2134
}
2235

23-
# For tests that run command scripts via @go, set _GO_CMD to make sure that's
24-
# the variable included in the log.
25-
test-go() {
26-
env _GO_CMD="$FUNCNAME" "$TEST_GO_SCRIPT" "$@"
27-
}
28-
2936
assert_log_equals() {
3037
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
3138
local level

tests/testing/environment.bats

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,9 @@ teardown() {
8787
[ -x "$TEST_GO_SCRIPTS_DIR/foo.d/bar.d/xyzzy" ]
8888
[ -x "$TEST_GO_SCRIPTS_DIR/foo.d/bar.d/plugh" ]
8989
}
90+
91+
@test "$SUITE: run TEST_GO_SCRIPT via test-go" {
92+
create_test_go_script 'printf "_GO_CMD: %s\n" "$_GO_CMD"'
93+
run test-go
94+
assert_success '_GO_CMD: test-go'
95+
}

tests/testing/log.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ teardown() {
1111
remove_test_go_rootdir
1212
}
1313

14+
@test "$SUITE: create_log_script and run_log_script without log file" {
15+
export TEST_LOG_FILE="$TEST_GO_ROOTDIR/test-script.log"
16+
TEST_LOG_FILE= run_log_script '@go.log INFO Hello, World!'
17+
assert_success
18+
assert_output_matches '^INFO +Hello, World!$'
19+
[ ! -e "$TEST_LOG_FILE" ]
20+
}
21+
22+
@test "$SUITE: create_log_script and run_log_script with log file" {
23+
export TEST_LOG_FILE="$TEST_GO_ROOTDIR/test-script.log"
24+
run_log_script '@go.log INFO Hello, World!'
25+
assert_success
26+
assert_output_matches '^INFO +Hello, World!$'
27+
[ -e "$TEST_LOG_FILE" ]
28+
assert_file_matches "$TEST_LOG_FILE" '^INFO +Hello, World!$'
29+
}
30+
1431
@test "$SUITE: set_log_command_stack_trace_items" {
1532
assert_equal '' "${LOG_COMMAND_STACK_TRACE_ITEMS[*]}"
1633
set_log_command_stack_trace_items

0 commit comments

Comments
 (0)