Skip to content

Commit 3c1aa6e

Browse files
authored
Merge pull request #157 from mbland/improvements
Small improvements, add `@go.test_compgen`, make `cmd-desc/from-file` test faster
2 parents abe6a54 + b9152d2 commit 3c1aa6e

File tree

14 files changed

+94
-69
lines changed

14 files changed

+94
-69
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
.*.swp
2-
.DS_Store
1+
# Are your editor's temp files, OS metadata files, etc. showing up in `git
2+
# status`? Try adding them to `$HOME/.config/git/ignore` instead.
33
tests/bats/
44
tests/coverage/
55
tests/kcov/

go-core.bash

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ declare _GO_IMPORTED_MODULE_CALLERS=()
112112
# Path to the project's script directory
113113
declare _GO_SCRIPTS_DIR=
114114

115+
# Directory containing Bats tests, relative to `_GO_ROOTDIR`
116+
declare -r -x _GO_TEST_DIR="${_GO_TEST_DIR:-tests}"
117+
115118
# Path to the main ./go script in the project's root directory
116119
declare -r -x _GO_SCRIPT="$_GO_ROOTDIR/${0##*/}"
117120

lib/bats-main

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040
# make the Bats repository a submodule if so desired. The Bats version so cloned
4141
# may be set by overriding `_GO_BATS_VERSION`.
4242

43-
# Directory containing Bats tests, relative to `_GO_ROOTDIR`
44-
export _GO_TEST_DIR="${_GO_TEST_DIR:-tests}"
45-
4643
# Directory in which kcov will be built
4744
export _GO_KCOV_DIR="${_GO_KCOV_DIR:-$_GO_TEST_DIR/kcov}"
4845

lib/complete

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# `-f` or `-d` options to generate file or directory paths.
1616
#
1717
# Arguments:
18-
# ...: Arguments passed directly ot the builtin `compgen`
18+
# ...: Arguments passed directly to the builtin `compgen`
1919
@go.compgen() {
2020
local add_slashes
2121
local compreply=()

lib/testing/environment

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,19 @@ test-go() {
108108
@go.create_test_command_script "$parent.d/$subcommand"
109109
done
110110
}
111+
112+
# Assigns the results of `@go.compgen` to a caller-defined test data array
113+
#
114+
# While more compact than writing a `while` loop by hand, it's also faster
115+
# thanks to the fact that it disables the Bats function tracing mechanism.
116+
#
117+
# Arguments:
118+
# result: Name of the caller-declared output array
119+
# ...: Arguments passed directly to `@go.compgen`
120+
@go.test_compgen() {
121+
. "$_GO_CORE_DIR/lib/bats/assertions"
122+
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
123+
. "$_GO_USE_MODULES" 'complete' 'strings'
124+
@go.split $'\n' "$(@go.compgen "${@:2}")" "$1"
125+
return_from_bats_assertion
126+
}

lib/testing/stack-trace-item

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
# DO NOT USE THIS FILE DIRECTLY! Source `_GO_CORE_DIR/lib/testing/stack-trace`
66
# and use the `stack_trace_item` function instead.
77

8-
. "$_GO_CORE_DIR/lib/bats/helpers"
9-
108
__@go.stack_trace_item() {
119
local filepath="$1"
1210
local function_name="$2"

scripts/test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
# Passes all arguments through to `@go.bats_main` from `lib/bats-main`.
2727
_test_main() {
2828
local _GO_BATS_COVERAGE_INCLUDE
29-
_GO_BATS_COVERAGE_INCLUDE=('go' 'go-core.bash' 'lib/' 'libexec/' 'scripts/')
29+
_GO_BATS_COVERAGE_INCLUDE=('go' 'go-core.bash' 'go-template'
30+
'lib/' 'libexec/' 'scripts/')
3031
local _GO_COVERALLS_URL='https://coveralls.io/github/mbland/go-script-bash'
3132

3233
. "$_GO_USE_MODULES" 'bats-main'

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
}

tests/complete.bats

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ teardown() {
7070

7171
while IFS= read -r item; do
7272
expected+=("${item#$TEST_GO_ROOTDIR/}")
73-
done<<<"$(@go.compgen -d "$TEST_GO_SCRIPTS_DIR/")"
73+
done < <(@go.compgen -d "$TEST_GO_SCRIPTS_DIR/")
7474

7575
run "$TEST_GO_SCRIPT" complete 1 cd 'scripts/'
7676
assert_success "${expected[@]}"
@@ -85,16 +85,12 @@ teardown() {
8585
touch "${files[@]/#/$TEST_GO_SCRIPTS_DIR/}"
8686

8787
local top_level=()
88-
local all_scripts_entries=()
89-
local item
90-
91-
while IFS= read -r item; do
92-
top_level+=("${item#$TEST_GO_ROOTDIR/}")
93-
done <<<"$(@go.compgen -f "$TEST_GO_ROOTDIR/")"
88+
@go.test_compgen top_level -f "$TEST_GO_ROOTDIR/"
89+
top_level=("${top_level[@]#$TEST_GO_ROOTDIR/}")
9490

95-
while IFS= read -r item; do
96-
all_scripts_entries+=("${item#$TEST_GO_ROOTDIR/}")
97-
done <<<"$(@go.compgen -f "$TEST_GO_SCRIPTS_DIR/")"
91+
local all_scripts_entries=()
92+
@go.test_compgen all_scripts_entries -f "$TEST_GO_SCRIPTS_DIR/"
93+
all_scripts_entries=("${all_scripts_entries[@]#$TEST_GO_ROOTDIR/}")
9894

9995
run "$TEST_GO_SCRIPT" complete 1 edit ''
10096
assert_success "${top_level[@]}"

tests/get/file.bats

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ teardown() {
2626
assert_success '-f'
2727

2828
local expected=()
29-
local item
30-
31-
. "$_GO_USE_MODULES" 'complete'
32-
while IFS= read -r item; do
33-
expected+=("$item")
34-
done <<<"$(@go.compgen -f -- "$TEST_GO_ROOTDIR/")"
29+
@go.test_compgen expected -f -- "$TEST_GO_ROOTDIR/"
3530

3631
run "$TEST_GO_SCRIPT" get file --complete 1 -f
3732
assert_success "${expected[@]#$TEST_GO_ROOTDIR/}"

0 commit comments

Comments
 (0)