Skip to content

Commit 16ff004

Browse files
committed
help: Restore IFS, handle ./go commands error
This appeared after trying to use the script with Bash 3.2.57(1)-release (OS X 10.11.6 default) in my blog repo. The problem did not manifest when using Bash 4.3.46(1)-release. A seemingly related issue: apparently "temporary" assignments to `IFS` were not meant to affect word expansions and redirections, an issue apparently fixed in Bash 4.3: https://lists.gnu.org/archive/html/bug-bash/2014-03/msg00065.html Perhaps the "fix" prevents called functions from inheriting the value of `IFS`. Certainly adheres to the principle of least surprise considering the ubiquity and importance of `IFS`, but violates the principle of expecting called functions to inherit variable values from their callers. In a way, both versions were doing the "right" thing, but in the case of 3.2, the "right" thing exposed a latent bug.
1 parent 9925809 commit 16ff004

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

libexec/help

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,30 @@
5656

5757
_@go.usage() {
5858
local cmd_paths=("${_GO_PLUGINS_PATHS[@]}" "$_GO_SCRIPTS_DIR")
59+
local origIFS="$IFS"
5960
local IFS=':'
61+
local summaries
62+
local summaries_status=0
63+
64+
cmd_paths="${cmd_paths[*]}"
65+
IFS="$origIFS"
66+
summaries="$(_@go.source_builtin 'commands' --summaries "$cmd_paths")"
67+
68+
if [[ "$?" -ne '0' ]]; then
69+
summaries="<No commands found in or error retrieving summaries from: "
70+
summaries+="$cmd_paths>"
71+
summaries_status=1
72+
fi
6073

6174
@go.printf "%s\n\n%s\n\n%s\n\n%s %s\n\n%s %s\n\n" \
6275
"Usage: $_GO_CMD <command> [arguments...]" \
6376
"Where <command> is one of:" \
64-
"$(_@go.source_builtin 'commands' --summaries "${cmd_paths[*]}")" \
77+
"$summaries" \
6578
"Use \"$_GO_CMD help <command>\" for more information about that command," \
6679
"if available." \
6780
"Use \"$_GO_CMD help builtins\" for help on builtin commands," \
6881
"and \"$_GO_CMD help aliases\" for information on shell alias commands."
82+
[[ "$?" -eq '0' && "$summaries_status" -eq '0' ]]
6983
}
7084

7185
_@go.help_message_for_command() {

tests/help.bats

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,32 @@ teardown() {
2727
}
2828

2929
@test "$SUITE: produce message with successful return for help command" {
30+
create_test_command_script 'foo' '# Does foo stuff'
31+
create_test_command_script 'bar' '# Does bar stuff'
32+
create_test_command_script 'baz' '# Does baz stuff'
3033
run "$TEST_GO_SCRIPT" help
34+
3135
assert_success
3236
assert_line_equals 0 "Usage: $TEST_GO_SCRIPT <command> [arguments...]"
37+
assert_output_matches ' foo Does foo stuff'
38+
assert_output_matches ' bar Does bar stuff'
39+
assert_output_matches ' baz Does baz stuff'
40+
}
41+
42+
@test "$SUITE: produce usage message when error retrieving command summaries" {
43+
run "$TEST_GO_SCRIPT" help
44+
45+
assert_failure
46+
assert_line_equals 0 "Usage: $TEST_GO_SCRIPT <command> [arguments...]"
47+
48+
local expected_err="<No commands found in or error retrieving summaries "
49+
expected_err+="from: $TEST_GO_SCRIPTS_DIR>"
50+
assert_output_matches "$expected_err"
3351
}
3452

3553
@test "$SUITE: accept -h, -help, and --help as synonyms" {
54+
# Create a command to ensure a sucessful exit status.
55+
create_test_command_script 'foo' '# Does foo stuff'
3656
run "$TEST_GO_SCRIPT" help
3757
assert_success
3858

0 commit comments

Comments
 (0)