Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7692c5d

Browse files
committed
Create proper test to fully reproduce the issue on macOS
1 parent 638acb6 commit 7692c5d

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

pty/ptytest/ptytest_test.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package ptytest_test
22

33
import (
44
"fmt"
5-
"os/exec"
5+
"strings"
66
"testing"
77

88
"github.com/spf13/cobra"
@@ -21,32 +21,36 @@ func TestPtytest(t *testing.T) {
2121
pty.WriteLine("read")
2222
})
2323

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()
3326

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)},
4433
}
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+
}
4547

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+
}
5155
})
5256
}

0 commit comments

Comments
 (0)