Skip to content

Commit

Permalink
Merge pull request #7 from cpuguy83/use_pty
Browse files Browse the repository at this point in the history
Use pty for term tests
  • Loading branch information
thaJeztah committed Apr 29, 2020
2 parents 21c2d5e + c662d89 commit 129dac9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.13

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78
github.com/creack/pty v1.1.9
github.com/google/go-cmp v0.3.1
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
52 changes: 22 additions & 30 deletions term_linux_test.go → term_test.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
//+build linux
// +build !windows

package term

import (
"io/ioutil"
"os"
"strings"
"testing"

"github.com/creack/pty"
"github.com/google/go-cmp/cmp"
"gotest.tools/assert"
)

// RequiresRoot skips tests that require root, unless the test.root flag has
// been set
func RequiresRoot(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("skipping test that requires root")
return
func newTtyForTest(t *testing.T) (*os.File, *os.File) {
t.Helper()
pty, tty, err := pty.Open()
if err != nil {
t.Fatalf("error creating pty: %v", err)
}
}

func newTtyForTest(t *testing.T) (*os.File, error) {
RequiresRoot(t)
file, err := os.OpenFile("/dev/tty", os.O_RDWR, os.ModeDevice)
if err != nil && strings.Contains(err.Error(), "no such device or address") {
t.Skip("terminal missing, skipping test")
}
return file, err
return pty, tty
}

func newTempFile() (*os.File, error) {
return ioutil.TempFile(os.TempDir(), "temp")
}

func TestGetWinsize(t *testing.T) {
tty, err := newTtyForTest(t)
assert.NilError(t, err)
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
winSize, err := GetWinsize(tty.Fd())
assert.NilError(t, err)
Expand All @@ -53,8 +45,8 @@ func TestGetWinsize(t *testing.T) {
var cmpWinsize = cmp.AllowUnexported(Winsize{})

func TestSetWinsize(t *testing.T) {
tty, err := newTtyForTest(t)
assert.NilError(t, err)
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
winSize, err := GetWinsize(tty.Fd())
assert.NilError(t, err)
Expand All @@ -68,8 +60,8 @@ func TestSetWinsize(t *testing.T) {
}

func TestGetFdInfo(t *testing.T) {
tty, err := newTtyForTest(t)
assert.NilError(t, err)
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
inFd, isTerminal := GetFdInfo(tty)
assert.Equal(t, inFd, tty.Fd())
Expand All @@ -83,8 +75,8 @@ func TestGetFdInfo(t *testing.T) {
}

func TestIsTerminal(t *testing.T) {
tty, err := newTtyForTest(t)
assert.NilError(t, err)
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
isTerminal := IsTerminal(tty.Fd())
assert.Equal(t, isTerminal, true)
Expand All @@ -96,22 +88,22 @@ func TestIsTerminal(t *testing.T) {
}

func TestSaveState(t *testing.T) {
tty, err := newTtyForTest(t)
assert.NilError(t, err)
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
state, err := SaveState(tty.Fd())
assert.NilError(t, err)
assert.Assert(t, state != nil)
tty, err = newTtyForTest(t)
assert.NilError(t, err)
pty, tty = newTtyForTest(t)
defer pty.Close()
defer tty.Close()
err = RestoreTerminal(tty.Fd(), state)
assert.NilError(t, err)
}

func TestDisableEcho(t *testing.T) {
tty, err := newTtyForTest(t)
assert.NilError(t, err)
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
state, err := SetRawTerminal(tty.Fd())
defer RestoreTerminal(tty.Fd(), state)
Expand Down

0 comments on commit 129dac9

Please sign in to comment.