From 5c8eb85b69a90e05262b73d135582fe4091736b1 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 23 Sep 2022 01:27:11 +0200 Subject: [PATCH] test: exec binary from within tea.Program --- exec_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 exec_test.go diff --git a/exec_test.go b/exec_test.go new file mode 100644 index 0000000..37de3f5 --- /dev/null +++ b/exec_test.go @@ -0,0 +1,48 @@ +package tea + +import ( + "bytes" + "os/exec" + "testing" +) + +type execFinishedMsg struct{ err error } +type testExecModel struct{ err error } + +func (m testExecModel) Init() Cmd { + c := exec.Command("true") //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) { + var buf bytes.Buffer + var in bytes.Buffer + + m := &testExecModel{} + p := NewProgram(m, WithInput(&in), WithOutput(&buf)) + if err := p.Start(); err != nil { + t.Fatal(err) + } + + if m.err != nil { + t.Fatal(m.err) + } +}