-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_system.go
59 lines (50 loc) · 1.1 KB
/
test_system.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
package cli
import (
"bytes"
"log"
"testing"
"time"
"github.com/Netflix/go-expect"
"golang.org/x/crypto/ssh/terminal"
)
type TestSystem struct {
*BaseSystem
Console *expect.Console
}
type TestOutput struct {
STDOUT *bytes.Buffer
STDERR *bytes.Buffer
}
func NewTestSystem(
t *testing.T, arguments []string, environment map[string]string,
) (*TestSystem, *TestOutput) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
console, err := expect.NewTestConsole(t,
expect.WithDefaultTimeout(5*time.Second),
expect.WithStdout(stdout),
)
if err != nil {
t.Fatal(err)
}
if environment == nil {
environment = map[string]string{}
}
return &TestSystem{
BaseSystem: &BaseSystem{
In: console.Tty(),
Out: console.Tty(),
Logger: log.New(stderr, "", log.LstdFlags),
Environment: environment,
Arguments: arguments,
},
Console: console,
}, &TestOutput{stdout, stderr}
}
func (ts *TestSystem) ReadPassword() (string, error) {
cloaked, err := terminal.ReadPassword(int(ts.Console.Tty().Fd()))
if err != nil {
return "", err
}
return string(cloaked), nil
}