github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/opts/commands_test.go (about) 1 // +build unit 2 3 package opts_test 4 5 import ( 6 "bytes" 7 "testing" 8 9 expect "github.com/Netflix/go-expect" 10 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 11 "github.com/olli-ai/jx/v2/pkg/tests" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestExecuteCommand(t *testing.T) { 16 t.Parallel() 17 o := opts.CommonOptions{} 18 err := o.RunCommand("echo", "foo") 19 assert.Nil(t, err) 20 } 21 22 func TestCommandError(t *testing.T) { 23 t.Parallel() 24 o := opts.CommonOptions{} 25 err := o.RunCommand("noSuchCommand") 26 assert.NotNil(t, err) 27 } 28 29 func TestVerboseOutput(t *testing.T) { 30 tests.SkipForWindows(t, "go-expect does not work on windows") 31 t.Parallel() 32 buf := new(bytes.Buffer) 33 c, err := expect.NewConsole(expect.WithStdout(buf)) 34 assert.NoError(t, err, "Should not error") 35 defer c.Close() 36 out := c.Tty() 37 o := opts.CommonOptions{Verbose: true, Out: out} 38 donec := make(chan struct{}) 39 go func() { 40 defer close(donec) 41 c.ExpectEOF() 42 }() 43 44 commandResult := o.RunCommand("echo", "foo") 45 46 // Close the worker end of the pty, and read the remaining bytes from the master end. 47 out.Close() 48 <-donec 49 50 assert.NoError(t, commandResult, "Should not error") 51 assert.Equal(t, "foo\r", expect.StripTrailingEmptyLines(buf.String())) 52 } 53 54 func TestNonVerboseOutput(t *testing.T) { 55 tests.SkipForWindows(t, "go-expect does not work on windows") 56 t.Parallel() 57 console := tests.NewTerminal(t, nil) 58 defer console.Close() 59 defer console.Cleanup() 60 o := opts.CommonOptions{Out: console.Out} 61 err := o.RunCommand("echo", "foo") 62 assert.NoError(t, err, "Should not error") 63 assert.Empty(t, expect.StripTrailingEmptyLines(console.CurrentState())) 64 }