MCPcopy Index your code
hub / github.com/coder/coder / TestPtytest

Function TestPtytest

pty/ptytest/ptytest_test.go:16–77  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestPtytest(t *testing.T) {
17 t.Parallel()
18 t.Run("Echo", func(t *testing.T) {
19 t.Parallel()
20 pty := ptytest.New(t)
21 pty.Output().Write([]byte("write"))
22 pty.ExpectMatch("write")
23 pty.WriteLine("read")
24 })
25
26 t.Run("ReadLine", func(t *testing.T) {
27 t.Parallel()
28 if runtime.GOOS == "windows" {
29 t.Skip("ReadLine is glitchy on windows when it comes to the final line of output it seems")
30 }
31
32 ctx := testutil.Context(t, testutil.WaitLong)
33 pty := ptytest.New(t)
34
35 // The PTY expands these to \r\n (even on linux).
36 pty.Output().Write([]byte("line 1\nline 2\nline 3\nline 4\nline 5"))
37 require.Equal(t, "line 1", pty.ReadLine(ctx))
38 require.Equal(t, "line 2", pty.ReadLine(ctx))
39 require.Equal(t, "line 3", pty.ReadLine(ctx))
40 require.Equal(t, "line 4", pty.ReadLine(ctx))
41 require.Equal(t, "line 5", pty.ExpectMatch("5"))
42 })
43
44 // See https://github.com/coder/coder/issues/2122 for the motivation
45 // behind this test.
46 t.Run("Ptytest should not hang when output is not consumed", func(t *testing.T) {
47 t.Parallel()
48
49 tests := []struct {
50 name string
51 output string
52 isPlatformBug bool
53 }{
54 {name: "1024 is safe (does not exceed macOS buffer)", output: strings.Repeat(".", 1024)},
55 {name: "1025 exceeds macOS buffer (must not hang)", output: strings.Repeat(".", 1025)},
56 {name: "10241 large output", output: strings.Repeat(".", 10241)}, // 1024 * 10 + 1
57 }
58 for _, tt := range tests {
59 // nolint:paralleltest // Avoid parallel test to more easily identify the issue.
60 t.Run(tt.name, func(t *testing.T) {
61 cmd := &serpent.Command{
62 Use: "test",
63 Handler: func(inv *serpent.Invocation) error {
64 fmt.Fprint(inv.Stdout, tt.output)
65 return nil
66 },
67 }
68
69 inv := cmd.Invoke()
70 pty := ptytest.New(t)
71 pty.Attach(inv)
72 err := inv.Run()
73 require.NoError(t, err)

Callers

nothing calls this directly

Calls 12

NewFunction · 0.92
ContextFunction · 0.92
WriteLineMethod · 0.80
SkipMethod · 0.80
RunMethod · 0.65
WriteMethod · 0.65
OutputMethod · 0.65
AttachMethod · 0.65
ExpectMatchMethod · 0.45
EqualMethod · 0.45
ReadLineMethod · 0.45
InvokeMethod · 0.45

Tested by

no test coverage detected