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

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create proper test to fully reproduce the issue on macOS
  • Loading branch information
mafredri committed Jun 7, 2022
commit 7692c5d249acffa8ae46f41f28e72201d17207b0
54 changes: 29 additions & 25 deletions pty/ptytest/ptytest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ptytest_test

import (
"fmt"
"os/exec"
"strings"
"testing"

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

// nolint:paralleltest
t.Run("Do not hang on Intel macOS", func(t *testing.T) {
cmd := exec.Command("sh", "-c", "echo hi, I will cause a hang")
pty := ptytest.New(t)
cmd.Stdin = pty.Input()
cmd.Stdout = pty.Output()
err := cmd.Run()
require.NoError(t, err)
})
t.Run("Cobra ptytest should not hang when output is not consumed", func(t *testing.T) {
t.Parallel()

// nolint:paralleltest
t.Run("CobraCommandWorksLinux", func(t *testing.T) {
// Example with cobra command instead of exec. More abstractions, but
// for some reason works on linux.
cmd := cobra.Command{
Use: "test",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Hello world")
return nil
},
tests := []struct {
name string
output string
}{
{name: "1024 is safe (does not exceed macOS buffer)", output: strings.Repeat(".", 1024)},
{name: "1025 exceeds macOS buffer (must not hang)", output: strings.Repeat(".", 1025)},
}
for _, tt := range tests {
tt := tt
// nolint:paralleltest // Avoid parallel test to more easily identify the issue.
t.Run(tt.name, func(t *testing.T) {
// Example with cobra command instead of exec. More abstractions, but
// for some reason works on linux.
cmd := cobra.Command{
Use: "test",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Fprint(cmd.OutOrStdout(), tt.output)
return nil
},
}

pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
err := cmd.Execute()
require.NoError(t, err)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
err := cmd.Execute()
require.NoError(t, err)
})
}
})
}