-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: exec binary from within tea.Program
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |