Skip to content
Myr

exec_test.go

This file

Path

exec_test.go

81 lines · 1.3 KiB · Go

Last changed by

test: exec returning an error for invalid and failing commandsChristian Muehlhaeuserbefore adoption13cdfd2

History of this file

Take it

test: exec binary from within tea.ProgramChristian Muehlhaeuserbefore adoption5c8eb85

lines 1–10why, one step back
1package tea
23import (4 "bytes"5 "os/exec"6 "testing"7)89type execFinishedMsg struct{ err error }10

test: exec returning an error for invalid and failing commandsChristian Muehlhaeuserbefore adoption13cdfd2

lines 11–15why, one step back
11type testExecModel struct {
12 cmd string13 err error14}15

test: exec binary from within tea.ProgramChristian Muehlhaeuserbefore adoption5c8eb85

line 16why, one step back
16func (m testExecModel) Init() Cmd {

test: exec returning an error for invalid and failing commandsChristian Muehlhaeuserbefore adoption13cdfd2

line 17why, one step back
17 c := exec.Command(m.cmd) //nolint:gosec

test: exec binary from within tea.ProgramChristian Muehlhaeuserbefore adoption5c8eb85

lines 18–39why, one step back
18 return ExecProcess(c, func(err error) Msg {
19 return execFinishedMsg{err}20 })21}2223func (m *testExecModel) Update(msg Msg) (Model, Cmd) {24 switch msg := msg.(type) {25 case execFinishedMsg:26 if msg.err != nil {27 m.err = msg.err28 }29 return m, Quit30 }3132 return m, nil33}3435func (m *testExecModel) View() string {36 return "\n"37}3839func TestTeaExec(t *testing.T) {

test: exec returning an error for invalid and failing commandsChristian Muehlhaeuserbefore adoption13cdfd2

lines 40–59why, one step back
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 },

test: exec binary from within tea.ProgramChristian Muehlhaeuserbefore adoption5c8eb85

lines 60–61why, one step back
60 }
61

test: exec returning an error for invalid and failing commandsChristian Muehlhaeuserbefore adoption13cdfd2

lines 62–79why, one step back
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 })

test: exec binary from within tea.ProgramChristian Muehlhaeuserbefore adoption5c8eb85

lines 80–81why, one step back
80 }
81}