Skip to content

Commit 0701f8a

Browse files
committed
Add tests for ForceTerminal
1 parent 321fd98 commit 0701f8a

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

pkg/iostreams/iostreams.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package iostreams
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
"io"
78
"io/ioutil"
@@ -41,6 +42,7 @@ type IOStreams struct {
4142
stderrTTYOverride bool
4243
stderrIsTTY bool
4344
termWidthOverride int
45+
ttySize func() (int, int, error)
4446

4547
pagerCommand string
4648
pagerProcess *os.Process
@@ -273,7 +275,7 @@ func (s *IOStreams) ForceTerminal(spec string) {
273275
return
274276
}
275277

276-
ttyWidth, _, err := ttySize()
278+
ttyWidth, _, err := s.ttySize()
277279
if err != nil {
278280
return
279281
}
@@ -284,7 +286,6 @@ func (s *IOStreams) ForceTerminal(spec string) {
284286
s.termWidthOverride = int(float64(s.termWidthOverride) * (float64(p) / 100))
285287
}
286288
}
287-
288289
}
289290

290291
func (s *IOStreams) ColorScheme() *ColorScheme {
@@ -325,6 +326,7 @@ func System() *IOStreams {
325326
colorEnabled: EnvColorForced() || (!EnvColorDisabled() && stdoutIsTTY),
326327
is256enabled: Is256ColorSupported(),
327328
pagerCommand: os.Getenv("PAGER"),
329+
ttySize: ttySize,
328330
}
329331

330332
if stdoutIsTTY && stderrIsTTY {
@@ -345,6 +347,9 @@ func Test() (*IOStreams, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {
345347
In: ioutil.NopCloser(in),
346348
Out: out,
347349
ErrOut: errOut,
350+
ttySize: func() (int, int, error) {
351+
return -1, -1, errors.New("ttySize not implemented in tests")
352+
},
348353
}, in, out, errOut
349354
}
350355

@@ -359,6 +364,7 @@ func isCygwinTerminal(w io.Writer) bool {
359364
return false
360365
}
361366

367+
// terminalSize measures the viewport of the terminal that the output stream is connected to
362368
func terminalSize(w io.Writer) (int, int, error) {
363369
if f, isFile := w.(*os.File); isFile {
364370
return term.GetSize(int(f.Fd()))

pkg/iostreams/iostreams_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package iostreams
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
func TestIOStreams_ForceTerminal(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
iostreams *IOStreams
12+
arg string
13+
wantTTY bool
14+
wantWidth int
15+
}{
16+
{
17+
name: "explicit width",
18+
iostreams: &IOStreams{},
19+
arg: "72",
20+
wantTTY: true,
21+
wantWidth: 72,
22+
},
23+
{
24+
name: "measure width",
25+
iostreams: &IOStreams{
26+
ttySize: func() (int, int, error) {
27+
return 72, 0, nil
28+
},
29+
},
30+
arg: "true",
31+
wantTTY: true,
32+
wantWidth: 72,
33+
},
34+
{
35+
name: "measure width fails",
36+
iostreams: &IOStreams{
37+
ttySize: func() (int, int, error) {
38+
return -1, -1, errors.New("ttySize sabotage!")
39+
},
40+
},
41+
arg: "true",
42+
wantTTY: true,
43+
wantWidth: 80,
44+
},
45+
{
46+
name: "apply percentage",
47+
iostreams: &IOStreams{
48+
ttySize: func() (int, int, error) {
49+
return 72, 0, nil
50+
},
51+
},
52+
arg: "50%",
53+
wantTTY: true,
54+
wantWidth: 36,
55+
},
56+
}
57+
for _, tt := range tests {
58+
t.Run(tt.name, func(t *testing.T) {
59+
tt.iostreams.ForceTerminal(tt.arg)
60+
if isTTY := tt.iostreams.IsStdoutTTY(); isTTY != tt.wantTTY {
61+
t.Errorf("IOStreams.IsStdoutTTY() = %v, want %v", isTTY, tt.wantTTY)
62+
}
63+
if tw := tt.iostreams.TerminalWidth(); tw != tt.wantWidth {
64+
t.Errorf("IOStreams.TerminalWidth() = %v, want %v", tw, tt.wantWidth)
65+
}
66+
})
67+
}
68+
}

pkg/iostreams/tty_size.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"golang.org/x/term"
99
)
1010

11+
// ttySize measures the size of the controlling terminal for the current process
1112
func ttySize() (int, int, error) {
1213
f, err := os.Open("/dev/tty")
1314
if err != nil {

0 commit comments

Comments
 (0)