11package ptytest_test
22
33import (
4+ "fmt"
5+ "runtime"
6+ "strings"
47 "testing"
58
9+ "github.com/spf13/cobra"
10+ "github.com/stretchr/testify/require"
11+
612 "github.com/coder/coder/pty/ptytest"
713)
814
@@ -15,4 +21,41 @@ func TestPtytest(t *testing.T) {
1521 pty .ExpectMatch ("write" )
1622 pty .WriteLine ("read" )
1723 })
24+
25+ t .Run ("Cobra ptytest should not hang when output is not consumed" , func (t * testing.T ) {
26+ t .Parallel ()
27+
28+ tests := []struct {
29+ name string
30+ output string
31+ isPlatformBug bool // See https://github.com/coder/coder/issues/2122 for more info.
32+ }{
33+ {name : "1024 is safe (does not exceed macOS buffer)" , output : strings .Repeat ("." , 1024 )},
34+ {name : "1025 exceeds macOS buffer (must not hang)" , output : strings .Repeat ("." , 1025 ), isPlatformBug : true },
35+ {name : "10241 large output" , output : strings .Repeat ("." , 10241 ), isPlatformBug : true }, // 1024 * 10 + 1
36+ }
37+ for _ , tt := range tests {
38+ tt := tt
39+ // nolint:paralleltest // Avoid parallel test to more easily identify the issue.
40+ t .Run (tt .name , func (t * testing.T ) {
41+ if tt .isPlatformBug && (runtime .GOOS == "darwin" || runtime .GOOS == "windows" ) {
42+ t .Skip ("This test hangs on macOS and Windows, see https://github.com/coder/coder/issues/2122" )
43+ }
44+
45+ cmd := cobra.Command {
46+ Use : "test" ,
47+ RunE : func (cmd * cobra.Command , args []string ) error {
48+ fmt .Fprint (cmd .OutOrStdout (), tt .output )
49+ return nil
50+ },
51+ }
52+
53+ pty := ptytest .New (t )
54+ cmd .SetIn (pty .Input ())
55+ cmd .SetOut (pty .Output ())
56+ err := cmd .Execute ()
57+ require .NoError (t , err )
58+ })
59+ }
60+ })
1861}
0 commit comments