github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/util/term/term_test.go (about)

     1  /*
     2  Copyright 2020 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package term
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"errors"
    23  	"runtime"
    24  	"testing"
    25  
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    28  	"github.com/GoogleContainerTools/skaffold/testutil"
    29  )
    30  
    31  func TestIsNotTerminal(t *testing.T) {
    32  	var w bytes.Buffer
    33  
    34  	termFd, isTerm := IsTerminal(&w)
    35  
    36  	testutil.CheckDeepEqual(t, uintptr(0x00), termFd)
    37  	testutil.CheckDeepEqual(t, false, isTerm)
    38  }
    39  
    40  func TestSupportsColor(t *testing.T) {
    41  	tests := []struct {
    42  		description  string
    43  		colorsOutput string
    44  		shouldErr    bool
    45  		expected     bool
    46  	}{
    47  		{
    48  			description:  "Supports 256 colors",
    49  			colorsOutput: "256",
    50  			expected:     true,
    51  		},
    52  		{
    53  			description:  "Supports 0 colors",
    54  			colorsOutput: "0",
    55  			expected:     false,
    56  		},
    57  		{
    58  			description:  "tput returns -1",
    59  			colorsOutput: "-1",
    60  			expected:     false,
    61  		},
    62  		{
    63  			description:  "cmd run errors",
    64  			colorsOutput: "-1",
    65  			expected:     false,
    66  			shouldErr:    true,
    67  		},
    68  	}
    69  
    70  	for _, test := range tests {
    71  		testutil.Run(t, test.description, func(t *testutil.T) {
    72  			if test.shouldErr {
    73  				t.Override(&util.DefaultExecCommand, testutil.CmdRunOutErr("tput colors", test.colorsOutput, errors.New("error")))
    74  			} else {
    75  				t.Override(&util.DefaultExecCommand, testutil.CmdRunOut("tput colors", test.colorsOutput))
    76  			}
    77  			if runtime.GOOS == constants.Windows {
    78  				test.expected = true
    79  				test.shouldErr = false
    80  			}
    81  
    82  			supportsColors, err := SupportsColor(context.Background())
    83  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, supportsColors)
    84  		})
    85  	}
    86  }