-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy patheditor_test.go
121 lines (112 loc) · 2.37 KB
/
editor_test.go
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
package notes
import (
"github.com/kballard/go-shellquote"
"github.com/rhysd/go-fakeio"
"os/exec"
"strings"
"testing"
)
func TestOpenEditor(t *testing.T) {
echo, err := exec.LookPath("echo")
panicIfErr(err)
for _, tc := range []struct {
what string
editor string
want string
}{
{
what: "executable",
editor: "echo",
want: "foo bar\n",
},
{
what: "executable path",
editor: shellquote.Join(echo), // On Windows, path would contain 'Program Files'
want: "foo bar\n",
},
{
what: "multiple args",
editor: "echo 'aaa' bbb",
want: "aaa bbb foo bar\n",
},
{
what: "arg contains white space",
editor: "echo 'aaa bbb' 'ccc\tddd'",
want: "aaa bbb ccc\tddd foo bar\n",
},
{
what: "arg contains multiple white space",
editor: "echo aaa bbb ",
want: "aaa bbb foo bar\n",
},
} {
t.Run(tc.what, func(t *testing.T) {
fake := fakeio.Stdout()
defer fake.Restore()
cfg := &Config{
EditorCmd: tc.editor,
HomePath: ".",
}
if err := openEditor(cfg, "foo", "bar"); err != nil {
t.Fatal(err)
}
have, err := fake.String()
panicIfErr(err)
if have != tc.want {
t.Fatalf("Output from %q is unexpected. Wanted %q but have %q", tc.editor, tc.want, have)
}
})
}
}
func TestEditorInvalidEditor(t *testing.T) {
exe, err := exec.LookPath("false")
panicIfErr(err)
exe = shellquote.Join(exe)
for _, tc := range []struct {
what string
value string
want string
}{
{
what: "empty",
value: "",
want: "Editor is not set",
},
{
what: "empty command",
value: "''",
want: "did not exit successfully",
},
{
what: "unterminated single quote",
value: "vim '-g",
want: "Cannot parse editor command line",
},
{
what: "unterminated double quote",
value: "vim \"-g",
want: "Cannot parse editor command line",
},
{
what: "executable exit failure",
value: exe,
want: "did not exit successfully",
},
{
what: "executable does not exist",
value: "unknown-command-which-does-not-exist",
want: "did not exit successfully",
},
} {
t.Run(tc.want, func(t *testing.T) {
cfg := &Config{EditorCmd: tc.value}
err := openEditor(cfg)
if err == nil {
t.Fatal("Error did not occur")
}
if !strings.Contains(err.Error(), tc.want) {
t.Fatal("Unexpected error:", err)
}
})
}
}