forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor_test.go
More file actions
284 lines (252 loc) · 6.34 KB
/
editor_test.go
File metadata and controls
284 lines (252 loc) · 6.34 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package surveyext
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
pseudotty "github.com/creack/pty"
"github.com/stretchr/testify/assert"
)
func testLookPath(s string) ([]string, []string, error) {
return []string{os.Args[0], "-test.run=TestHelperProcess", "--", s}, []string{"GH_WANT_HELPER_PROCESS=1"}, nil
}
func TestHelperProcess(t *testing.T) {
if os.Getenv("GH_WANT_HELPER_PROCESS") != "1" {
return
}
if err := func(args []string) error {
switch args[0] {
// "vim" appends a message to the file
case "vim":
f, err := os.OpenFile(args[1], os.O_APPEND|os.O_WRONLY, 0)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(" - added by vim")
return err
// "nano" truncates the contents of the file
case "nano":
f, err := os.OpenFile(args[1], os.O_TRUNC|os.O_WRONLY, 0)
if err != nil {
return err
}
return f.Close()
default:
return fmt.Errorf("unrecognized arguments: %#v\n", args)
}
}(os.Args[3:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
func Test_GhEditor_Prompt_skip(t *testing.T) {
pty := newTerminal(t)
e := &GhEditor{
BlankAllowed: true,
EditorCommand: "vim",
Editor: &survey.Editor{
Message: "Body",
FileName: "*.md",
Default: "initial value",
HideDefault: true,
AppendDefault: true,
},
lookPath: func(s string) ([]string, []string, error) {
return nil, nil, errors.New("no editor allowed")
},
}
e.WithStdio(pty.Stdio())
// wait until the prompt is rendered and send the Enter key
go func() {
pty.WaitForOutput("Body")
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch vim, enter to skip] \x1b[0m", normalizeANSI(pty.Output()))
pty.ResetOutput()
assert.NoError(t, pty.SendKey('\n'))
}()
res, err := e.Prompt(defaultPromptConfig())
assert.NoError(t, err)
assert.Equal(t, "initial value", res)
assert.Equal(t, "", normalizeANSI(pty.Output()))
}
func Test_GhEditor_Prompt_editorAppend(t *testing.T) {
pty := newTerminal(t)
e := &GhEditor{
BlankAllowed: true,
EditorCommand: "vim",
Editor: &survey.Editor{
Message: "Body",
FileName: "*.md",
Default: "initial value",
HideDefault: true,
AppendDefault: true,
},
lookPath: testLookPath,
}
e.WithStdio(pty.Stdio())
// wait until the prompt is rendered and send the 'e' key
go func() {
pty.WaitForOutput("Body")
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch vim, enter to skip] \x1b[0m", normalizeANSI(pty.Output()))
pty.ResetOutput()
assert.NoError(t, pty.SendKey('e'))
}()
res, err := e.Prompt(defaultPromptConfig())
assert.NoError(t, err)
assert.Equal(t, "initial value - added by vim", res)
assert.Equal(t, "", normalizeANSI(pty.Output()))
}
func Test_GhEditor_Prompt_editorTruncate(t *testing.T) {
pty := newTerminal(t)
e := &GhEditor{
BlankAllowed: true,
EditorCommand: "nano",
Editor: &survey.Editor{
Message: "Body",
FileName: "*.md",
Default: "initial value",
HideDefault: true,
AppendDefault: true,
},
lookPath: testLookPath,
}
e.WithStdio(pty.Stdio())
// wait until the prompt is rendered and send the 'e' key
go func() {
pty.WaitForOutput("Body")
assert.Equal(t, "\x1b[0G\x1b[2K\x1b[0;1;92m? \x1b[0m\x1b[0;1;99mBody \x1b[0m\x1b[0;36m[(e) to launch nano, enter to skip] \x1b[0m", normalizeANSI(pty.Output()))
pty.ResetOutput()
assert.NoError(t, pty.SendKey('e'))
}()
res, err := e.Prompt(defaultPromptConfig())
assert.NoError(t, err)
assert.Equal(t, "", res)
assert.Equal(t, "", normalizeANSI(pty.Output()))
}
// survey doesn't expose this
func defaultPromptConfig() *survey.PromptConfig {
return &survey.PromptConfig{
PageSize: 7,
HelpInput: "?",
SuggestInput: "tab",
Icons: survey.IconSet{
Error: survey.Icon{
Text: "X",
Format: "red",
},
Help: survey.Icon{
Text: "?",
Format: "cyan",
},
Question: survey.Icon{
Text: "?",
Format: "green+hb",
},
MarkedOption: survey.Icon{
Text: "[x]",
Format: "green",
},
UnmarkedOption: survey.Icon{
Text: "[ ]",
Format: "default+hb",
},
SelectFocus: survey.Icon{
Text: ">",
Format: "cyan+b",
},
},
Filter: func(filter string, value string, index int) (include bool) {
filter = strings.ToLower(filter)
return strings.Contains(strings.ToLower(value), filter)
},
KeepFilter: false,
}
}
type testTerminal struct {
pty *os.File
tty *os.File
stdout *teeWriter
stderr *teeWriter
}
func newTerminal(t *testing.T) *testTerminal {
t.Helper()
pty, tty, err := pseudotty.Open()
if errors.Is(err, pseudotty.ErrUnsupported) {
t.SkipNow()
return nil
}
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
pty.Close()
tty.Close()
})
if err := pseudotty.Setsize(tty, &pseudotty.Winsize{Cols: 72, Rows: 30}); err != nil {
t.Fatal(err)
}
return &testTerminal{pty: pty, tty: tty}
}
func (t *testTerminal) SendKey(c rune) error {
_, err := t.pty.WriteString(string(c))
return err
}
func (t *testTerminal) WaitForOutput(s string) {
for {
time.Sleep(time.Millisecond)
if strings.Contains(t.stdout.String(), s) {
return
}
}
}
func (t *testTerminal) Output() string {
return t.stdout.String()
}
func (t *testTerminal) ResetOutput() {
t.stdout.Reset()
}
func (t *testTerminal) Stdio() terminal.Stdio {
t.stdout = &teeWriter{File: t.tty}
t.stderr = t.stdout
return terminal.Stdio{
In: t.tty,
Out: t.stdout,
Err: t.stderr,
}
}
// teeWriter is a writer that duplicates all writes to a file into a buffer
type teeWriter struct {
*os.File
buf bytes.Buffer
mu sync.Mutex
}
func (f *teeWriter) Write(p []byte) (n int, err error) {
f.mu.Lock()
defer f.mu.Unlock()
_, _ = f.buf.Write(p)
return f.File.Write(p)
}
func (f *teeWriter) String() string {
f.mu.Lock()
s := f.buf.String()
f.mu.Unlock()
return s
}
func (f *teeWriter) Reset() {
f.mu.Lock()
f.buf.Reset()
f.mu.Unlock()
}
// strips some ANSI escape sequences that we do not want tests to be concerned with
func normalizeANSI(t string) string {
t = strings.ReplaceAll(t, "\x1b[?25h", "") // strip sequence that shows cursor
t = strings.ReplaceAll(t, "\x1b[?25l", "") // strip sequence that hides cursor
return t
}