-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcompletion_test.go
More file actions
91 lines (72 loc) · 1.88 KB
/
completion_test.go
File metadata and controls
91 lines (72 loc) · 1.88 KB
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
package cli
import (
"runtime"
"testing"
"github.com/kami-zh/go-capturer"
"github.com/stretchr/testify/require"
)
func TestCompletionCommand(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no completion command on windows")
}
require := require.New(t)
app := New("test", "0.1.0", "abcde", "test app")
var (
stdout, stderr string
err error
)
stdout = capturer.CaptureStdout(func() {
stderr = capturer.CaptureStderr(func() {
err = app.Run([]string{"test", "completion"})
})
})
require.NoError(err)
require.Empty(stderr)
require.Equal(
`# Save this file to /etc/bash_completion.d/test
#
# or add the following line to your .bashrc file:
# source <(test completion)
# running:
# echo "source <(test completion)" >> ~/.bashrc
_completion-test() {
# All arguments except the first one
args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
# Only split on newlines
local IFS=$'\n'
# Call completion (note that the first element of COMP_WORDS is
# the executable itself)
COMPREPLY=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}"))
return 0
}
complete -F _completion-test test
`, stdout)
}
func TestCompletionHelpCommand(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no completion command on windows")
}
require := require.New(t)
app := New("test", "0.1.0", "abcde", "test app")
var (
stdout, stderr string
err error
)
stdout = capturer.CaptureStdout(func() {
stderr = capturer.CaptureStderr(func() {
err = app.Run([]string{"test", "completion", "--help"})
})
})
require.NoError(err)
require.Empty(stderr)
require.Equal(
`Usage:
test [OPTIONS] completion
Print a bash completion script for test.
You can place it on /etc/bash_completion.d/test, or add it to your .bashrc
running:
echo "source <(test completion)" >> ~/.bashrc
Help Options:
-h, --help Show this help message
`, stdout)
}