Skip to content
Myr

test: exec returning an error for invalid and failing commands

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Muehlhaeuser · committed by Christian Muehlhaeuser · parent 77619d2 · Browse files · 1 file+44 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+44 added−11 removed

  • other:+44 added−11 removed

1 file+44 added−11 removed

modified:exec_test.go+44 added-11 removed
5 unmodified lines
6 "testing"7)89type execFinishedMsg struct{ err error }10type testExecModel struct{ err error }1011type testExecModel struct {12 cmd string13 err error14}1516func (m testExecModel) Init() Cmd {13 c := exec.Command("true") //nolint:gosec17 c := exec.Command(m.cmd) //nolint:gosec18 return ExecProcess(c, func(err error) Msg {19 return execFinishedMsg{err}20 })21}
14 unmodified lines
36 return "\n"37}3839func TestTeaExec(t *testing.T) {36 var buf bytes.Buffer37 var in bytes.Buffer3839 m := &testExecModel{}40 p := NewProgram(m, WithInput(&in), WithOutput(&buf))41 if _, err := p.Run(); err != nil {42 t.Fatal(err)40 tests := []struct {41 name string42 cmd string43 expectErr bool44 }{45 {46 name: "true",47 cmd: "true",48 expectErr: false,49 },50 {51 name: "false",52 cmd: "false",53 expectErr: true,54 },55 {56 name: "invalid command",57 cmd: "invalid",58 expectErr: true,59 },60 }6145 if m.err != nil {46 t.Fatal(m.err)62 for _, test := range tests {63 t.Run(test.name, func(t *testing.T) {64 var buf bytes.Buffer65 var in bytes.Buffer6667 m := &testExecModel{cmd: test.cmd}68 p := NewProgram(m, WithInput(&in), WithOutput(&buf))69 if _, err := p.Run(); err != nil {70 t.Error(err)71 }7273 if m.err != nil && !test.expectErr {74 t.Errorf("expected no error, got %v", m.err)75 }76 if m.err == nil && test.expectErr {77 t.Error("expected error, got nil")78 }79 })80 }81}