Add tests for ForceTerminal

This commit is contained in:
Mislav Marohnić 2021-08-23 16:09:08 +02:00
parent 321fd98f82
commit 0701f8aa82
3 changed files with 77 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package iostreams
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
@ -41,6 +42,7 @@ type IOStreams struct {
stderrTTYOverride bool
stderrIsTTY bool
termWidthOverride int
ttySize func() (int, int, error)
pagerCommand string
pagerProcess *os.Process
@ -273,7 +275,7 @@ func (s *IOStreams) ForceTerminal(spec string) {
return
}
ttyWidth, _, err := ttySize()
ttyWidth, _, err := s.ttySize()
if err != nil {
return
}
@ -284,7 +286,6 @@ func (s *IOStreams) ForceTerminal(spec string) {
s.termWidthOverride = int(float64(s.termWidthOverride) * (float64(p) / 100))
}
}
}
func (s *IOStreams) ColorScheme() *ColorScheme {
@ -325,6 +326,7 @@ func System() *IOStreams {
colorEnabled: EnvColorForced() || (!EnvColorDisabled() && stdoutIsTTY),
is256enabled: Is256ColorSupported(),
pagerCommand: os.Getenv("PAGER"),
ttySize: ttySize,
}
if stdoutIsTTY && stderrIsTTY {
@ -345,6 +347,9 @@ func Test() (*IOStreams, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {
In: ioutil.NopCloser(in),
Out: out,
ErrOut: errOut,
ttySize: func() (int, int, error) {
return -1, -1, errors.New("ttySize not implemented in tests")
},
}, in, out, errOut
}
@ -359,6 +364,7 @@ func isCygwinTerminal(w io.Writer) bool {
return false
}
// terminalSize measures the viewport of the terminal that the output stream is connected to
func terminalSize(w io.Writer) (int, int, error) {
if f, isFile := w.(*os.File); isFile {
return term.GetSize(int(f.Fd()))

View file

@ -0,0 +1,68 @@
package iostreams
import (
"errors"
"testing"
)
func TestIOStreams_ForceTerminal(t *testing.T) {
tests := []struct {
name string
iostreams *IOStreams
arg string
wantTTY bool
wantWidth int
}{
{
name: "explicit width",
iostreams: &IOStreams{},
arg: "72",
wantTTY: true,
wantWidth: 72,
},
{
name: "measure width",
iostreams: &IOStreams{
ttySize: func() (int, int, error) {
return 72, 0, nil
},
},
arg: "true",
wantTTY: true,
wantWidth: 72,
},
{
name: "measure width fails",
iostreams: &IOStreams{
ttySize: func() (int, int, error) {
return -1, -1, errors.New("ttySize sabotage!")
},
},
arg: "true",
wantTTY: true,
wantWidth: 80,
},
{
name: "apply percentage",
iostreams: &IOStreams{
ttySize: func() (int, int, error) {
return 72, 0, nil
},
},
arg: "50%",
wantTTY: true,
wantWidth: 36,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.iostreams.ForceTerminal(tt.arg)
if isTTY := tt.iostreams.IsStdoutTTY(); isTTY != tt.wantTTY {
t.Errorf("IOStreams.IsStdoutTTY() = %v, want %v", isTTY, tt.wantTTY)
}
if tw := tt.iostreams.TerminalWidth(); tw != tt.wantWidth {
t.Errorf("IOStreams.TerminalWidth() = %v, want %v", tw, tt.wantWidth)
}
})
}
}

View file

@ -8,6 +8,7 @@ import (
"golang.org/x/term"
)
// ttySize measures the size of the controlling terminal for the current process
func ttySize() (int, int, error) {
f, err := os.Open("/dev/tty")
if err != nil {