github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/pkg/iostreams/color_test.go (about)

     1  package iostreams
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func TestEnvColorDisabled(t *testing.T) {
     9  	orig_NO_COLOR := os.Getenv("NO_COLOR")
    10  	orig_CLICOLOR := os.Getenv("CLICOLOR")
    11  	orig_CLICOLOR_FORCE := os.Getenv("CLICOLOR_FORCE")
    12  	t.Cleanup(func() {
    13  		os.Setenv("NO_COLOR", orig_NO_COLOR)
    14  		os.Setenv("CLICOLOR", orig_CLICOLOR)
    15  		os.Setenv("CLICOLOR_FORCE", orig_CLICOLOR_FORCE)
    16  	})
    17  
    18  	tests := []struct {
    19  		name           string
    20  		NO_COLOR       string
    21  		CLICOLOR       string
    22  		CLICOLOR_FORCE string
    23  		want           bool
    24  	}{
    25  		{
    26  			name:           "pristine env",
    27  			NO_COLOR:       "",
    28  			CLICOLOR:       "",
    29  			CLICOLOR_FORCE: "",
    30  			want:           false,
    31  		},
    32  		{
    33  			name:           "NO_COLOR enabled",
    34  			NO_COLOR:       "1",
    35  			CLICOLOR:       "",
    36  			CLICOLOR_FORCE: "",
    37  			want:           true,
    38  		},
    39  		{
    40  			name:           "CLICOLOR disabled",
    41  			NO_COLOR:       "",
    42  			CLICOLOR:       "0",
    43  			CLICOLOR_FORCE: "",
    44  			want:           true,
    45  		},
    46  		{
    47  			name:           "CLICOLOR enabled",
    48  			NO_COLOR:       "",
    49  			CLICOLOR:       "1",
    50  			CLICOLOR_FORCE: "",
    51  			want:           false,
    52  		},
    53  		{
    54  			name:           "CLICOLOR_FORCE has no effect",
    55  			NO_COLOR:       "",
    56  			CLICOLOR:       "",
    57  			CLICOLOR_FORCE: "1",
    58  			want:           false,
    59  		},
    60  	}
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			os.Setenv("NO_COLOR", tt.NO_COLOR)
    64  			os.Setenv("CLICOLOR", tt.CLICOLOR)
    65  			os.Setenv("CLICOLOR_FORCE", tt.CLICOLOR_FORCE)
    66  
    67  			if got := EnvColorDisabled(); got != tt.want {
    68  				t.Errorf("EnvColorDisabled(): want %v, got %v", tt.want, got)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestEnvColorForced(t *testing.T) {
    75  	orig_NO_COLOR := os.Getenv("NO_COLOR")
    76  	orig_CLICOLOR := os.Getenv("CLICOLOR")
    77  	orig_CLICOLOR_FORCE := os.Getenv("CLICOLOR_FORCE")
    78  	t.Cleanup(func() {
    79  		os.Setenv("NO_COLOR", orig_NO_COLOR)
    80  		os.Setenv("CLICOLOR", orig_CLICOLOR)
    81  		os.Setenv("CLICOLOR_FORCE", orig_CLICOLOR_FORCE)
    82  	})
    83  
    84  	tests := []struct {
    85  		name           string
    86  		NO_COLOR       string
    87  		CLICOLOR       string
    88  		CLICOLOR_FORCE string
    89  		want           bool
    90  	}{
    91  		{
    92  			name:           "pristine env",
    93  			NO_COLOR:       "",
    94  			CLICOLOR:       "",
    95  			CLICOLOR_FORCE: "",
    96  			want:           false,
    97  		},
    98  		{
    99  			name:           "NO_COLOR enabled",
   100  			NO_COLOR:       "1",
   101  			CLICOLOR:       "",
   102  			CLICOLOR_FORCE: "",
   103  			want:           false,
   104  		},
   105  		{
   106  			name:           "CLICOLOR disabled",
   107  			NO_COLOR:       "",
   108  			CLICOLOR:       "0",
   109  			CLICOLOR_FORCE: "",
   110  			want:           false,
   111  		},
   112  		{
   113  			name:           "CLICOLOR enabled",
   114  			NO_COLOR:       "",
   115  			CLICOLOR:       "1",
   116  			CLICOLOR_FORCE: "",
   117  			want:           false,
   118  		},
   119  		{
   120  			name:           "CLICOLOR_FORCE enabled",
   121  			NO_COLOR:       "",
   122  			CLICOLOR:       "",
   123  			CLICOLOR_FORCE: "1",
   124  			want:           true,
   125  		},
   126  		{
   127  			name:           "CLICOLOR_FORCE disabled",
   128  			NO_COLOR:       "",
   129  			CLICOLOR:       "",
   130  			CLICOLOR_FORCE: "0",
   131  			want:           false,
   132  		},
   133  	}
   134  	for _, tt := range tests {
   135  		t.Run(tt.name, func(t *testing.T) {
   136  			os.Setenv("NO_COLOR", tt.NO_COLOR)
   137  			os.Setenv("CLICOLOR", tt.CLICOLOR)
   138  			os.Setenv("CLICOLOR_FORCE", tt.CLICOLOR_FORCE)
   139  
   140  			if got := EnvColorForced(); got != tt.want {
   141  				t.Errorf("EnvColorForced(): want %v, got %v", tt.want, got)
   142  			}
   143  		})
   144  	}
   145  }