forked from charmbracelet/bubbletea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_test.go
88 lines (76 loc) · 1.45 KB
/
exec_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
package tea
import (
"bytes"
"os/exec"
"runtime"
"testing"
)
type execFinishedMsg struct{ err error }
type testExecModel struct {
cmd string
err error
}
func (m testExecModel) Init() Cmd {
c := exec.Command(m.cmd) //nolint:gosec
return ExecProcess(c, func(err error) Msg {
return execFinishedMsg{err}
})
}
func (m *testExecModel) Update(msg Msg) (Model, Cmd) {
switch msg := msg.(type) {
case execFinishedMsg:
if msg.err != nil {
m.err = msg.err
}
return m, Quit
}
return m, nil
}
func (m *testExecModel) View() string {
return "\n"
}
func TestTeaExec(t *testing.T) {
type test struct {
name string
cmd string
expectErr bool
}
tests := []test{
{
name: "invalid command",
cmd: "invalid",
expectErr: true,
},
}
if runtime.GOOS != "windows" {
tests = append(tests, []test{
{
name: "true",
cmd: "true",
expectErr: false,
},
{
name: "false",
cmd: "false",
expectErr: true,
},
}...)
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var buf bytes.Buffer
var in bytes.Buffer
m := &testExecModel{cmd: test.cmd}
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
if _, err := p.Run(); err != nil {
t.Error(err)
}
if m.err != nil && !test.expectErr {
t.Errorf("expected no error, got %v", m.err)
}
if m.err == nil && test.expectErr {
t.Error("expected error, got nil")
}
})
}
}