github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/output/color_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 output 18 19 import ( 20 "bytes" 21 "context" 22 "testing" 23 ) 24 25 func compareText(t *testing.T, expected, actual string) { 26 t.Helper() 27 if actual != expected { 28 t.Errorf("Formatting not applied to text. Expected %q but got %q", expected, actual) 29 } 30 } 31 32 func TestFprintln(t *testing.T) { 33 defer func() { SetupColors(context.Background(), nil, DefaultColorCode, false) }() 34 var b bytes.Buffer 35 36 cw := SetupColors(context.Background(), &b, 0, true) 37 Green.Fprintln(cw, "2", "less", "chars!") 38 39 compareText(t, "\033[32m2 less chars!\033[0m\n", b.String()) 40 } 41 42 func TestFprintf(t *testing.T) { 43 defer func() { SetupColors(context.Background(), nil, DefaultColorCode, false) }() 44 var b bytes.Buffer 45 46 cw := SetupColors(context.Background(), &b, 0, true) 47 Green.Fprintf(cw, "It's been %d %s", 1, "week") 48 49 compareText(t, "\033[32mIt's been 1 week\033[0m", b.String()) 50 } 51 52 func TestFprintlnNoTTY(t *testing.T) { 53 var b bytes.Buffer 54 55 cw := SetupColors(context.Background(), &b, 0, false) 56 Green.Fprintln(cw, "2", "less", "chars!") 57 58 compareText(t, "2 less chars!\n", b.String()) 59 } 60 61 func TestFprintfNoTTY(t *testing.T) { 62 var b bytes.Buffer 63 64 cw := SetupColors(context.Background(), &b, 0, false) 65 Green.Fprintf(cw, "It's been %d %s", 1, "week") 66 67 compareText(t, "It's been 1 week", b.String()) 68 } 69 70 func TestFprintlnDefaultColor(t *testing.T) { 71 var b bytes.Buffer 72 73 cw := SetupColors(context.Background(), &b, 34, true) 74 Default.Fprintln(cw, "2", "less", "chars!") 75 compareText(t, "\033[34m2 less chars!\033[0m\n", b.String()) 76 } 77 78 func TestFprintlnChangeDefaultToNone(t *testing.T) { 79 var b bytes.Buffer 80 81 cw := SetupColors(context.Background(), &b, 0, true) 82 Default.Fprintln(cw, "2", "less", "chars!") 83 compareText(t, "2 less chars!\n", b.String()) 84 } 85 86 func TestFprintlnChangeDefaultToUnknown(t *testing.T) { 87 var b bytes.Buffer 88 89 cw := SetupColors(context.Background(), &b, -1, true) 90 Default.Fprintln(cw, "2", "less", "chars!") 91 compareText(t, "2 less chars!\n", b.String()) 92 } 93 94 func TestSprintf(t *testing.T) { 95 // Set Default to original blue as it gets modified by other tests 96 Default = Blue 97 98 tests := []struct { 99 name string 100 color Color 101 params []interface{} 102 expected string 103 }{ 104 { 105 name: "default color", 106 color: Default, 107 params: []interface{}{"a", "few", "words"}, 108 expected: "\u001B[34ma few words\u001B[0m", 109 }, 110 { 111 name: "red color", 112 color: Red, 113 params: []interface{}{"a", "few", "words"}, 114 expected: "\u001B[31ma few words\u001B[0m", 115 }, 116 { 117 name: "nil color", 118 color: Color{ 119 color: nil, 120 }, 121 params: []interface{}{"a", "few", "words"}, 122 expected: "a few words", 123 }, 124 } 125 126 for _, test := range tests { 127 t.Run(test.name, func(t *testing.T) { 128 got := test.color.Sprintf("%s %s %s", test.params...) 129 compareText(t, test.expected, got) 130 }) 131 } 132 }