-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhelp.bats
217 lines (181 loc) · 6.6 KB
/
help.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#! /usr/bin/env bats
load environment
setup() {
test_filter
@go.create_test_go_script '@go "$@"'
}
teardown() {
@go.remove_test_go_rootdir
}
@test "$SUITE: do not hijack -h/--help by default" {
@go.create_test_command_script 'foo' '# Does foo stuff' 'echo foo'
run "$TEST_GO_SCRIPT" foo -h
assert_success
assert_output_matches 'foo'
}
@test "$SUITE: hijack -h/--help when _GO_HELP_HIJACK is enabled" {
@go.create_test_command_script 'foo' '# Does foo stuff' 'echo foo'
export _GO_HELP_HIJACK=true
run "$TEST_GO_SCRIPT" foo -h
assert_success
assert_output_matches 'Does foo stuff'
run "$TEST_GO_SCRIPT" foo --help
assert_success
assert_output_matches 'Does foo stuff'
}
@test "$SUITE: tab completion" {
local subcommands=('plugh' 'quux' 'xyzzy')
@go.create_parent_and_subcommands foo "${subcommands[@]}"
run "$TEST_GO_SCRIPT" complete 1 help 'foo'
assert_success 'foo '
run "$TEST_GO_SCRIPT" complete 2 help 'foo' ''
assert_success "${subcommands[@]}"
run "$TEST_GO_SCRIPT" complete 2 help 'foo' 'q'
assert_success 'quux '
}
@test "$SUITE: produce message with successful return for help command" {
@go.create_test_command_script 'foo' '# Does foo stuff'
@go.create_test_command_script 'bar' '# Does bar stuff'
@go.create_test_command_script 'plugins/baz/bin/baz' '# Does baz stuff'
run "$TEST_GO_SCRIPT" help
assert_success
assert_line_equals 0 "Usage: $TEST_GO_SCRIPT <command> [arguments...]"
assert_output_matches ' foo Does foo stuff'
assert_output_matches ' bar Does bar stuff'
assert_output_matches ' baz Does baz stuff'
assert_output_matches "Use \"$TEST_GO_SCRIPT help builtins\" for help on "
}
@test "$SUITE: only show top-level commands when _GO_STANDALONE is set" {
@go.create_test_command_script 'foo' '# Does foo stuff'
@go.create_test_command_script 'bar' '# Does bar stuff'
@go.create_test_command_script 'plugins/baz/bin/baz' '# Does baz stuff'
cd "$HOME"
_GO_STANDALONE='true' run "$TEST_GO_SCRIPT" help
assert_success
assert_line_equals 0 "Usage: $TEST_GO_SCRIPT <command> [arguments...]"
assert_output_matches ' foo Does foo stuff'
assert_output_matches ' bar Does bar stuff'
fail_if output_matches ' baz Does baz stuff'
fail_if output_matches "Use \"$TEST_GO_SCRIPT help builtins\" for help on "
}
@test "$SUITE: produce usage message when error retrieving command summaries" {
run "$TEST_GO_SCRIPT" help
assert_failure
assert_line_equals 0 "Usage: $TEST_GO_SCRIPT <command> [arguments...]"
local expected_err="<No commands found in or error retrieving summaries "
expected_err+="from: $TEST_GO_SCRIPTS_DIR>"
assert_output_matches "$expected_err"
}
@test "$SUITE: accept -h, -help, and --help as synonyms" {
# Create a command to ensure a sucessful exit status.
@go.create_test_command_script 'foo' '# Does foo stuff'
run "$TEST_GO_SCRIPT" help
assert_success
local help_output="$output"
run "$TEST_GO_SCRIPT" -h
assert_success "$help_output"
run "$TEST_GO_SCRIPT" -help
assert_success "$help_output"
run "$TEST_GO_SCRIPT" --help
assert_success "$help_output"
}
@test "$SUITE: produce message for alias" {
run "$TEST_GO_SCRIPT" help ls foo
assert_success
assert_line_equals 0 \
"$TEST_GO_SCRIPT ls - Shell alias that will execute in $TEST_GO_ROOTDIR"
}
@test "$SUITE: error if command doesn't exist" {
run "$TEST_GO_SCRIPT" help foobar
assert_failure
assert_line_equals 0 'Unknown command: foobar'
assert_line_equals 1 'Available commands are:'
assert_line_equals 2 ' aliases'
assert_line_equals -1 ' vars'
}
@test "$SUITE: error if parsing description fails" {
# As noted in the code, this _shouldn't_ ever happen. We're doing something
# tricky to make sure we handle it if it ever does.
local go_script=(
'. "$_GO_CORE_DIR/lib/internal/path"'
'[email protected]_command_path_and_argv() {'
" __go_cmd_path='bogus/path/to/nowhere'"
'}'
'[email protected]_builtin "help" "$@"')
@go.create_test_go_script "${go_script[@]}"
run "$TEST_GO_SCRIPT" 'bogus' 'command'
local expected_errors=(
'ERROR: command script "bogus/path/to/nowhere" does not exist'
'ERROR: failed to parse description from bogus/path/to/nowhere')
assert_failure "${expected_errors[@]}"
}
@test "$SUITE: parse description from command script" {
local cmd_script=(
'#'
'# Does foo stuff in {{root}}'
'#'
'# Usage: {{go}} {{cmd}} <argument>'
''
'echo "rm -rf /"')
@go.create_test_command_script 'foo' "${cmd_script[@]}"
run "$TEST_GO_SCRIPT" help foo
local expected=(
"$TEST_GO_SCRIPT foo - Does foo stuff in $TEST_GO_ROOTDIR"
''
"Usage: $TEST_GO_SCRIPT foo <argument>")
assert_success "${expected[@]}"
}
@test "$SUITE: call help filter on command script" {
# Interesting difference between bash 3.2 and 4.x that led to hoisting out the
# `replacement` variable in the help filter: If instead we used
# `"${FOO_VALID_ARGS[*]}"` directly in the pattern substitution, bash 3.2 will
# include the double quotes. If we remove them, bash 4.x will ignore `IFS` and
# use spaces instead.
#
# It's clearer this way anyway. `aliases` and `builtins` have been updated to
# hoist the `pattern` and `replacement` variables in this fashion.
local cmd_script=(
'#'
'# Does foo stuff'
'#'
'# Usage: {{go}} {{cmd}} <{{FOO_VALID_ARGS}}>'
''
'declare -r FOO_VALID_ARGS=("bar" "baz" "quux")'
''
'if [[ "$1" == "--help-filter" ]]; then'
' # Help filter'
' IFS="|"'
' declare -r pattern="{{FOO_VALID_ARGS}}"'
' declare -r replacement="${FOO_VALID_ARGS[*]}"'
' echo "${2//$pattern/$replacement}"'
'fi')
@go.create_test_command_script 'foo' "${cmd_script[@]}"
run "$TEST_GO_SCRIPT" help foo
local expected=(
"$TEST_GO_SCRIPT foo - Does foo stuff"
''
"Usage: $TEST_GO_SCRIPT foo <bar|baz|quux>")
assert_success "${expected[@]}"
}
@test "$SUITE: add subcommand summaries" {
local cmd_template=$'# Does {{CMD}} stuff\n'
cmd_template+=$'#\n'
cmd_template+=$'# Usage: {{go}} {{cmd}}\n'
@go.create_test_command_script 'foo' "${cmd_template/\{\{CMD\}\}/foo}"
mkdir "$TEST_GO_SCRIPTS_DIR/foo.d"
@go.create_test_command_script 'foo.d/bar' "${cmd_template/\{\{CMD\}\}/bar}"
@go.create_test_command_script 'foo.d/baz' "${cmd_template/\{\{CMD\}\}/baz}"
@go.create_test_command_script 'foo.d/quux' "${cmd_template/\{\{CMD\}\}/quux}"
run "$TEST_GO_SCRIPT" help foo
local expected=(
"$TEST_GO_SCRIPT foo - Does foo stuff"
''
"Usage: $TEST_GO_SCRIPT foo"
''
'Subcommands:'
''
' bar Does bar stuff'
' baz Does baz stuff'
' quux Does quux stuff')
assert_success "${expected[@]}"
}