-
Notifications
You must be signed in to change notification settings - Fork 242
/
prompt.go
76 lines (64 loc) · 2.21 KB
/
prompt.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
// Copyright © 2019 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prompt
import (
"bytes"
"testing"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
goexpect "github.com/Netflix/go-expect"
"github.com/tektoncd/cli/test/helper"
"gotest.tools/v3/assert"
)
// Prompt provides test utility for prompt test.
type Prompt struct {
CmdArgs []string
Procedure func(*goexpect.Console) error
}
// RunTest run the test cases of some prompt test.
// It will start given procedure in background and eval the test.
func (pt *Prompt) RunTest(t *testing.T, procedure func(*goexpect.Console) error, test func(terminal.Stdio) error) {
// Multiplex output to a buffer as well for the raw bytes.
buf := new(bytes.Buffer)
c, state, err := helper.NewVT10XConsole(goexpect.WithStdout(buf))
assert.NilError(t, err)
defer c.Close()
donec := make(chan struct{})
go func() {
defer close(donec)
if err := procedure(c); err != nil {
t.Logf("procedure failed: %v", err)
}
}()
assert.NilError(t, test(stdio(c)))
// Close the slave end of the pty, and read the remaining bytes from the master end.
_ = c.Tty().Close()
<-donec
t.Logf("Raw output: %q", buf.String())
// Dump the terminal's screen.
t.Logf("\n%s", goexpect.StripTrailingEmptyLines(state.String()))
}
// WithStdio helps to test interactive command
// by setting stdio for the ask function
func WithStdio(stdio terminal.Stdio) survey.AskOpt {
return func(options *survey.AskOptions) error {
options.Stdio.In = stdio.In
options.Stdio.Out = stdio.Out
options.Stdio.Err = stdio.Err
return nil
}
}
func stdio(c *goexpect.Console) terminal.Stdio {
return terminal.Stdio{In: c.Tty(), Out: c.Tty(), Err: c.Tty()}
}