forked from moby/term
-
Notifications
You must be signed in to change notification settings - Fork 0
/
term_test.go
114 lines (100 loc) · 2.69 KB
/
term_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// +build !windows
package term
import (
"io/ioutil"
"os"
"testing"
"github.com/creack/pty"
"github.com/google/go-cmp/cmp"
"gotest.tools/assert"
)
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)
}
return pty, tty
}
func newTempFile() (*os.File, error) {
return ioutil.TempFile(os.TempDir(), "temp")
}
func TestGetWinsize(t *testing.T) {
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
winSize, err := GetWinsize(tty.Fd())
assert.NilError(t, err)
assert.Assert(t, winSize != nil)
newSize := Winsize{Width: 200, Height: 200, x: winSize.x, y: winSize.y}
err = SetWinsize(tty.Fd(), &newSize)
assert.NilError(t, err)
winSize, err = GetWinsize(tty.Fd())
assert.NilError(t, err)
assert.DeepEqual(t, *winSize, newSize, cmpWinsize)
}
var cmpWinsize = cmp.AllowUnexported(Winsize{})
func TestSetWinsize(t *testing.T) {
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
winSize, err := GetWinsize(tty.Fd())
assert.NilError(t, err)
assert.Assert(t, winSize != nil)
newSize := Winsize{Width: 200, Height: 200, x: winSize.x, y: winSize.y}
err = SetWinsize(tty.Fd(), &newSize)
assert.NilError(t, err)
winSize, err = GetWinsize(tty.Fd())
assert.NilError(t, err)
assert.DeepEqual(t, *winSize, newSize, cmpWinsize)
}
func TestGetFdInfo(t *testing.T) {
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
inFd, isTerminal := GetFdInfo(tty)
assert.Equal(t, inFd, tty.Fd())
assert.Equal(t, isTerminal, true)
tmpFile, err := newTempFile()
assert.NilError(t, err)
defer tmpFile.Close()
inFd, isTerminal = GetFdInfo(tmpFile)
assert.Equal(t, inFd, tmpFile.Fd())
assert.Equal(t, isTerminal, false)
}
func TestIsTerminal(t *testing.T) {
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
isTerminal := IsTerminal(tty.Fd())
assert.Equal(t, isTerminal, true)
tmpFile, err := newTempFile()
assert.NilError(t, err)
defer tmpFile.Close()
isTerminal = IsTerminal(tmpFile.Fd())
assert.Equal(t, isTerminal, false)
}
func TestSaveState(t *testing.T) {
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
state, err := SaveState(tty.Fd())
assert.NilError(t, err)
assert.Assert(t, state != nil)
pty, tty = newTtyForTest(t)
defer pty.Close()
defer tty.Close()
err = RestoreTerminal(tty.Fd(), state)
assert.NilError(t, err)
}
func TestDisableEcho(t *testing.T) {
pty, tty := newTtyForTest(t)
defer pty.Close()
defer tty.Close()
state, err := SetRawTerminal(tty.Fd())
defer RestoreTerminal(tty.Fd(), state)
assert.NilError(t, err)
assert.Assert(t, state != nil)
err = DisableEcho(tty.Fd(), state)
assert.NilError(t, err)
}