1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
// Package term provides information about the terminal that the current process is connected to (if any),
// for example measuring the dimensions of the terminal and inspecting whether it's safe to output color.
package term
import (
"testing"
)
func TestFromEnv(t *testing.T) {
tests := []struct {
name string
env map[string]string
wantTerminal bool
wantColor bool
want256Color bool
wantTrueColor bool
}{
{
name: "default",
env: map[string]string{
"GH_FORCE_TTY": "",
"CLICOLOR": "",
"CLICOLOR_FORCE": "",
"NO_COLOR": "",
"TERM": "",
"COLORTERM": "",
},
wantTerminal: false,
wantColor: false,
want256Color: false,
wantTrueColor: false,
},
{
name: "force color",
env: map[string]string{
"GH_FORCE_TTY": "",
"CLICOLOR": "",
"CLICOLOR_FORCE": "1",
"NO_COLOR": "",
"TERM": "",
"COLORTERM": "",
},
wantTerminal: false,
wantColor: true,
want256Color: false,
wantTrueColor: false,
},
{
name: "force tty",
env: map[string]string{
"GH_FORCE_TTY": "true",
"CLICOLOR": "",
"CLICOLOR_FORCE": "",
"NO_COLOR": "",
"TERM": "",
"COLORTERM": "",
},
wantTerminal: true,
wantColor: true,
want256Color: false,
wantTrueColor: false,
},
{
name: "has 256-color support",
env: map[string]string{
"GH_FORCE_TTY": "true",
"CLICOLOR": "",
"CLICOLOR_FORCE": "",
"NO_COLOR": "",
"TERM": "256-color",
"COLORTERM": "",
},
wantTerminal: true,
wantColor: true,
want256Color: true,
wantTrueColor: false,
},
{
name: "has truecolor support",
env: map[string]string{
"GH_FORCE_TTY": "true",
"CLICOLOR": "",
"CLICOLOR_FORCE": "",
"NO_COLOR": "",
"TERM": "truecolor",
"COLORTERM": "",
},
wantTerminal: true,
wantColor: true,
want256Color: true,
wantTrueColor: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for key, value := range tt.env {
t.Setenv(key, value)
}
terminal := FromEnv()
if got := terminal.IsTerminalOutput(); got != tt.wantTerminal {
t.Errorf("expected terminal %v, got %v", tt.wantTerminal, got)
}
if got := terminal.IsColorEnabled(); got != tt.wantColor {
t.Errorf("expected color %v, got %v", tt.wantColor, got)
}
if got := terminal.Is256ColorSupported(); got != tt.want256Color {
t.Errorf("expected 256-color %v, got %v", tt.want256Color, got)
}
if got := terminal.IsTrueColorSupported(); got != tt.wantTrueColor {
t.Errorf("expected truecolor %v, got %v", tt.wantTrueColor, got)
}
})
}
}
|