Repository navigation
Expand file tree
/
Copy pathpipeconsole_test.go
More file actions
88 lines (75 loc) 路 2.69 KB
/
Copy pathpipeconsole_test.go
File metadata and controls
88 lines (75 loc) 路 2.69 KB
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
package uv
import (
"io"
"sync"
"testing"
"time"
"github.com/charmbracelet/x/term"
)
// pipeConsole is a Console over an io.Pipe: a terminal that is not there, so a
// test can feed the input loop bytes exactly as a terminal would. its input is
// not a File, so newPollReader takes the fallback reader on every platform.
type pipeConsole struct {
in *io.PipeReader
feed *io.PipeWriter
// ended is closed the first time a read of the input fails. the input
// loop returns on that error without touching the terminal again, so from
// then on Stop cannot race it.
ended chan struct{}
endedOnce sync.Once
}
var _ Console = (*pipeConsole)(nil)
func newPipeConsole() *pipeConsole {
r, w := io.Pipe()
return &pipeConsole{in: r, feed: w, ended: make(chan struct{})}
}
// send writes s as the terminal would. it returns once the input loop has
// read it, which is how a pipe works.
func (c *pipeConsole) send(s string) { _, _ = io.WriteString(c.feed, s) }
func (c *pipeConsole) Read(p []byte) (int, error) {
n, err := c.in.Read(p)
if err != nil {
c.endedOnce.Do(func() { close(c.ended) })
}
return n, err
}
func (c *pipeConsole) Write(p []byte) (int, error) { return io.Discard.Write(p) }
func (c *pipeConsole) Close() error { return c.in.Close() }
func (c *pipeConsole) Environ() []string { return []string{"TERM=xterm"} }
func (c *pipeConsole) Getenv(k string) string {
v, _ := c.LookupEnv(k)
return v
}
func (c *pipeConsole) LookupEnv(k string) (string, bool) {
if k == "TERM" {
return "xterm", true
}
return "", false
}
func (c *pipeConsole) Reader() io.Reader { return c }
func (c *pipeConsole) Writer() io.Writer { return io.Discard }
func (c *pipeConsole) MakeRaw() (*term.State, error) { return nil, nil }
func (c *pipeConsole) Restore() error { return nil }
func (c *pipeConsole) GetSize() (int, int, error) { return 80, 24, nil }
func (c *pipeConsole) GetWinsize() (*Winsize, error) { return &Winsize{Col: 80, Row: 24}, nil }
// stopAfterInputEnds ends the input and waits until the input loop has seen
// that before calling Stop, so a test that is not about Stop cannot trip over
// a Stop that races the input loop. events are drained meanwhile, since an
// event loop blocked on delivery would keep the input loop from reading.
func stopAfterInputEnds(t *testing.T, tm *Terminal, c *pipeConsole) {
t.Helper()
_ = c.feed.Close()
deadline := time.After(5 * time.Second)
for waiting := true; waiting; {
select {
case <-c.ended:
waiting = false
case <-tm.Events():
case <-deadline:
t.Fatal("the input loop never saw the end of its input")
}
}
if err := tm.Stop(); err != nil {
t.Errorf("Stop: %v", err)
}
}