Skip to content

Commit b9152d2

Browse files
committed
cmd-desc: Make from-file test faster
The reason this test suite was so slow before was because the test cases made direct calls to the functions under test instead of wrapping them inside `TEST_GO_SCRIPT`. The functions would then call other functions, then call other functions in loops, and so on, causing Bats to do a lot of work building stack traces with every call. A sample time before this change on my macOS MacBoook Pro: 6 tests, 0 failures real 0m6.554s user 0m2.820s sys 0m3.487s A sample time after: 6 tests, 0 failures real 0m2.316s user 0m1.010s sys 0m1.204s I expect the difference to be even more dramatic on Windows.
1 parent 95f714a commit b9152d2

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

tests/command-descriptions/from-file.bats

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ load ../environment
55
TEST_COMMAND_SCRIPT_PATH="$TEST_GO_SCRIPTS_DIR/test-command"
66

77
setup() {
8-
. 'lib/internal/command_descriptions'
8+
test_filter
9+
10+
@go.create_test_go_script \
11+
". '$_GO_CORE_DIR/lib/internal/command_descriptions'" \
12+
'declare __go_cmd_desc=""' \
13+
'"$@"' \
14+
'declare result="$?"' \
15+
'printf "%s\n" "$__go_cmd_desc"' \
16+
'exit "$result"'
17+
}
918

19+
create_script_with_description() {
1020
local script='#
1121
# Command that does something in {{root}}
1222
#
@@ -51,14 +61,14 @@ teardown() {
5161

5262
@test "$SUITE: return error if there's an error reading" {
5363
skip_if_cannot_trigger_file_permission_failure
64+
printf '\n' >"$TEST_COMMAND_SCRIPT_PATH"
5465
chmod ugo-r "$TEST_COMMAND_SCRIPT_PATH"
5566

56-
run _@go.command_summary "$TEST_COMMAND_SCRIPT_PATH"
67+
run "$TEST_GO_SCRIPT" _@go.command_summary "$TEST_COMMAND_SCRIPT_PATH"
5768
assert_failure
5869
assert_output_matches "ERROR: problem reading $TEST_COMMAND_SCRIPT_PATH\$"
5970

60-
output=''
61-
run _@go.command_description "$TEST_COMMAND_SCRIPT_PATH"
71+
run "$TEST_GO_SCRIPT" _@go.command_description "$TEST_COMMAND_SCRIPT_PATH"
6272
assert_failure
6373
assert_output_matches "ERROR: problem reading $TEST_COMMAND_SCRIPT_PATH\$"
6474
}
@@ -67,39 +77,32 @@ teardown() {
6777
@go.create_test_command_script 'test-command' \
6878
'echo "This script has no description"'
6979

70-
local __go_cmd_desc=''
71-
_@go.command_summary "$TEST_COMMAND_SCRIPT_PATH"
72-
assert_equal 'No description available' "$__go_cmd_desc" 'command summary'
80+
run "$TEST_GO_SCRIPT" _@go.command_summary "$TEST_COMMAND_SCRIPT_PATH"
81+
assert_success 'No description available'
7382

7483
__go_cmd_desc=''
75-
_@go.command_description "$TEST_COMMAND_SCRIPT_PATH"
76-
assert_equal 'No description available' "$__go_cmd_desc" 'command description'
84+
run "$TEST_GO_SCRIPT" _@go.command_description "$TEST_COMMAND_SCRIPT_PATH"
85+
assert_success 'No description available'
7786
}
7887

7988
@test "$SUITE: parse summary from command script" {
80-
_GO_ROOTDIR='/foo/bar'
81-
_@go.command_summary "$TEST_COMMAND_SCRIPT_PATH"
82-
assert_equal 'Command that does something in /foo/bar' "$__go_cmd_desc" \
83-
'command summary'
89+
create_script_with_description
90+
run "$TEST_GO_SCRIPT" _@go.command_summary "$TEST_COMMAND_SCRIPT_PATH"
91+
assert_success "Command that does something in $TEST_GO_ROOTDIR"
8492
}
8593

8694
@test "$SUITE: one-line description from command script has no trailing space" {
8795
echo '# Command that does something in {{root}}' > "$TEST_COMMAND_SCRIPT_PATH"
88-
_GO_ROOTDIR='/foo/bar'
89-
COLUMNS=40
90-
91-
_@go.command_description "$TEST_COMMAND_SCRIPT_PATH"
92-
assert_equal 'Command that does something in /foo/bar' "$__go_cmd_desc" \
93-
'one-line command description'
96+
COLUMNS=40 run "$TEST_GO_SCRIPT" _@go.command_description \
97+
"$TEST_COMMAND_SCRIPT_PATH"
98+
assert_success "Command that does something in $TEST_GO_ROOTDIR"
9499
}
95100

96101
@test "$SUITE: parse description from command script" {
97-
_GO_CMD='test-go'
98-
_GO_ROOTDIR='/foo/bar'
99-
COLUMNS=40
100-
_@go.command_description "$TEST_COMMAND_SCRIPT_PATH"
102+
create_script_with_description
103+
COLUMNS=40 run test-go _@go.command_description "$TEST_COMMAND_SCRIPT_PATH"
101104

102-
local expected='Command that does something in /foo/bar
105+
local expected='Command that does something in TEST_GO_ROOTDIR
103106
104107
Usage: test-go test-command [args...]
105108
@@ -125,13 +128,12 @@ Indented lines that look like tables (there are two or more adjacent spaces afte
125128
xyzzy all work and no play makes
126129
mike a Dull Boy.
127130
plugh all werk and no play makes
128-
mike a dull Boy
129-
'
131+
mike a dull Boy'
130132

131133
# With this test, I learned that you _do_ have to quote strings even inside of
132134
# '[[' and ']]' in case the strings themselves contain '[' or ']', as with
133135
# '[args...]' above.
134-
assert_equal "$expected" "$__go_cmd_desc" 'command description'
136+
assert_success "${expected/TEST_GO_ROOTDIR/$TEST_GO_ROOTDIR}"
135137
}
136138

137139
@test "$SUITE: format subcommand description" {
@@ -144,15 +146,9 @@ Indented lines that look like tables (there are two or more adjacent spaces afte
144146
echo The command script starts now.
145147
'
146148

147-
local _GO_CMD='test-go'
148-
local expected=("Leaf command that does something in $_GO_ROOTDIR"
149-
''
150-
"Usage: $_GO_CMD root-command node-command leaf-command [args...]"
151-
'')
152-
local __go_cmd_desc
153-
_@go.command_description \
149+
run test-go _@go.command_description \
154150
"$TEST_GO_SCRIPTS_DIR/root-command.d/node-command.d/leaf-command"
155-
156-
local IFS=$'\n'
157-
assert_equal "${expected[*]}" "$__go_cmd_desc"
151+
assert_success "Leaf command that does something in $TEST_GO_ROOTDIR" \
152+
'' \
153+
"Usage: test-go root-command node-command leaf-command [args...]"
158154
}

0 commit comments

Comments
 (0)