Skip to content

Commit

Permalink
add a test for console I/O
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Dao <[email protected]>
  • Loading branch information
dqminh committed Jun 29, 2017
1 parent e0a2cdc commit f6ecb30
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

package console

import "testing"
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"sync"
"testing"
)

func TestWinSize(t *testing.T) {
c, _, err := NewPty()
Expand All @@ -29,3 +37,48 @@ func TestWinSize(t *testing.T) {
t.Errorf("height should be 10 but received %d", size.Height)
}
}

func TestConsolePty(t *testing.T) {
console, slavePath, err := NewPty()
if err != nil {
t.Fatal(err)
}
defer console.Close()

slave, err := os.OpenFile(slavePath, os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
defer slave.Close()

iteration := 10

cmd := exec.Command("sh", "-c", fmt.Sprintf("for x in `seq 1 %d`; do echo -n test; done", iteration))
cmd.Stdin = slave
cmd.Stdout = slave
cmd.Stderr = slave

var (
b bytes.Buffer
wg sync.WaitGroup
)
wg.Add(1)
go func() {
io.Copy(&b, console)
wg.Done()
}()

if err := cmd.Run(); err != nil {
t.Fatal(err)
}
slave.Close()
wg.Wait()

expectedOutput := ""
for i := 0; i < iteration; i++ {
expectedOutput += "test"
}
if out := b.String(); out != expectedOutput {
t.Errorf("unexpected output %q", out)
}
}

0 comments on commit f6ecb30

Please sign in to comment.