@@ -2,7 +2,7 @@ package ptytest_test
2
2
3
3
import (
4
4
"fmt"
5
- "os/exec "
5
+ "strings "
6
6
"testing"
7
7
8
8
"github.com/spf13/cobra"
@@ -21,32 +21,36 @@ func TestPtytest(t *testing.T) {
21
21
pty .WriteLine ("read" )
22
22
})
23
23
24
- // nolint:paralleltest
25
- t .Run ("Do not hang on Intel macOS" , func (t * testing.T ) {
26
- cmd := exec .Command ("sh" , "-c" , "echo hi, I will cause a hang" )
27
- pty := ptytest .New (t )
28
- cmd .Stdin = pty .Input ()
29
- cmd .Stdout = pty .Output ()
30
- err := cmd .Run ()
31
- require .NoError (t , err )
32
- })
24
+ t .Run ("Cobra ptytest should not hang when output is not consumed" , func (t * testing.T ) {
25
+ t .Parallel ()
33
26
34
- // nolint:paralleltest
35
- t .Run ("CobraCommandWorksLinux" , func (t * testing.T ) {
36
- // Example with cobra command instead of exec. More abstractions, but
37
- // for some reason works on linux.
38
- cmd := cobra.Command {
39
- Use : "test" ,
40
- RunE : func (cmd * cobra.Command , args []string ) error {
41
- fmt .Println ("Hello world" )
42
- return nil
43
- },
27
+ tests := []struct {
28
+ name string
29
+ output string
30
+ }{
31
+ {name : "1024 is safe (does not exceed macOS buffer)" , output : strings .Repeat ("." , 1024 )},
32
+ {name : "1025 exceeds macOS buffer (must not hang)" , output : strings .Repeat ("." , 1025 )},
44
33
}
34
+ for _ , tt := range tests {
35
+ tt := tt
36
+ // nolint:paralleltest // Avoid parallel test to more easily identify the issue.
37
+ t .Run (tt .name , func (t * testing.T ) {
38
+ // Example with cobra command instead of exec. More abstractions, but
39
+ // for some reason works on linux.
40
+ cmd := cobra.Command {
41
+ Use : "test" ,
42
+ RunE : func (cmd * cobra.Command , args []string ) error {
43
+ fmt .Fprint (cmd .OutOrStdout (), tt .output )
44
+ return nil
45
+ },
46
+ }
45
47
46
- pty := ptytest .New (t )
47
- cmd .SetIn (pty .Input ())
48
- cmd .SetOut (pty .Output ())
49
- err := cmd .Execute ()
50
- require .NoError (t , err )
48
+ pty := ptytest .New (t )
49
+ cmd .SetIn (pty .Input ())
50
+ cmd .SetOut (pty .Output ())
51
+ err := cmd .Execute ()
52
+ require .NoError (t , err )
53
+ })
54
+ }
51
55
})
52
56
}
0 commit comments