...

Source file src/github.com/cli/go-gh/v2/pkg/term/env_test.go

Documentation: github.com/cli/go-gh/v2/pkg/term

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

View as plain text