Repository navigation
Expand file tree
/
Copy pathshell_test.go
More file actions
125 lines (108 loc) · 3.75 KB
/
Copy pathshell_test.go
File metadata and controls
125 lines (108 loc) · 3.75 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
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
package climux
import (
"context"
"strings"
"testing"
)
// TestCompletionEnvVarDerivation pins how EnableCompletion derives the
// environment variable Run consults from the root command's name.
func TestCompletionEnvVarDerivation(t *testing.T) {
for _, tt := range []struct{ name, want string }{
{"app", "APP_COMPLETE"},
{"my-app", "MY_APP_COMPLETE"},
{"my.app_2", "MY_APP_2_COMPLETE"},
} {
if got := completionEnvVar(tt.name); got != tt.want {
t.Errorf("completionEnvVar(%q) = %q, want %q", tt.name, got, tt.want)
}
}
}
func TestRunCompletionBashSource(t *testing.T) {
cmd := NewCommand("app", "").EnableCompletion()
t.Setenv("APP_COMPLETE", "bash_source")
code, stdout, stderr := runCaptured(cmd)
if got, want := code, ExitCodeSuccess; got != want {
t.Errorf("code = %d, want %d", got, want)
}
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
if !strings.Contains(stdout, "_app_complete()") {
t.Errorf("stdout does not look like a bash completion script:\n%s", stdout)
}
if !strings.Contains(stdout, "APP_COMPLETE=bash_complete") {
t.Errorf("stdout does not re-invoke with bash_complete:\n%s", stdout)
}
}
func TestRunCompletionZshSource(t *testing.T) {
cmd := NewCommand("app", "").EnableCompletion()
t.Setenv("APP_COMPLETE", "zsh_source")
code, stdout, _ := runCaptured(cmd)
if got, want := code, ExitCodeSuccess; got != want {
t.Errorf("code = %d, want %d", got, want)
}
if !strings.HasPrefix(stdout, "#compdef app") {
t.Errorf("stdout does not start with #compdef app:\n%s", stdout)
}
}
// TestRunCompletionReply asserts the wire format end to end: COMP_WORDS
// and COMP_CWORD in, "plain," and "nofiles," lines out.
func TestRunCompletionReply(t *testing.T) {
cmd := NewCommand("app", "").EnableCompletion().Flags(
Bool("verbose", "").Aliases("v"),
)
t.Setenv("APP_COMPLETE", "bash_complete")
t.Setenv("COMP_WORDS", "app\n--v")
t.Setenv("COMP_CWORD", "1")
code, stdout, stderr := runCaptured(cmd)
if got, want := code, ExitCodeSuccess; got != want {
t.Errorf("code = %d, want %d", got, want)
}
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
if got, want := stdout, "plain,--verbose\nnofiles,\n"; got != want {
t.Errorf("stdout = %q, want %q", got, want)
}
}
// TestRunCompletionUnknownValueFallsThrough asserts that a value the
// protocol does not recognize leaves Run's ordinary behavior untouched --
// EnableCompletion never hijacks a command line it does not understand.
func TestRunCompletionUnknownValueFallsThrough(t *testing.T) {
handlerCalled := false
cmd := NewCommand("app", "").EnableCompletion().
HandleFunc(func(ctx context.Context, inv *Invocation) error {
handlerCalled = true
return nil
})
t.Setenv("APP_COMPLETE", "something_else")
code, _, _ := runCaptured(cmd)
if got, want := code, ExitCodeSuccess; got != want {
t.Errorf("code = %d, want %d", got, want)
}
if !handlerCalled {
t.Error("handler was not called; Run did not fall through")
}
}
// TestRunCompletionMalformedTreeFallsThrough asserts that a tree that does
// not compile answers no completion request and falls through to Run's
// ordinary reporting, rather than answering from a tree whose shape is
// exactly what failed to validate.
func TestRunCompletionMalformedTreeFallsThrough(t *testing.T) {
cmd := NewCommand("app", "").EnableCompletion().Flags(
String("foo", ""),
String("foo", ""),
)
t.Setenv("APP_COMPLETE", "bash_source")
code, stdout, stderr := runCaptured(cmd)
if got, want := code, ExitCodeUsage; got != want {
t.Errorf("code = %d, want %d", got, want)
}
if stdout != "" {
t.Errorf("stdout = %q, want empty", stdout)
}
want := "Program error: app: flag declared more than once: --foo\n"
if got := stderr; got != want {
t.Errorf("stderr = %q, want %q", got, want)
}
}