Debian Codegraph @

pkg/color/util_test.go

raw
1 package color
2
3 import (
4 "os"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9 )
10
11 func Test_boolPtr(t *testing.T) {
12 t.Parallel()
13 tests := []struct {
14 name string
15 input bool
16 checkPtr func(*testing.T, *bool)
17 }{
18 {
19 name: "returns pointer to true",
20 input: true,
21 checkPtr: func(t *testing.T, p *bool) {
22 t.Helper()
23 require.NotNil(t, p)
24 assert.True(t, *p)
25 },
26 },
27 {
28 name: "returns pointer to false",
29 input: false,
30 checkPtr: func(t *testing.T, p *bool) {
31 t.Helper()
32 require.NotNil(t, p)
33 assert.False(t, *p)
34 },
35 },
36 }
37
38 for _, tt := range tests {
39 t.Run(tt.name, func(t *testing.T) {
40 t.Parallel()
41
42 result := boolPtr(tt.input)
43 tt.checkPtr(t, result)
44 })
45 }
46 }
47
48 //nolint:paralleltest // This test cannot run in parallel because it manipulates environment variables.
49 func Test_noColorIsSet(t *testing.T) {
50 tests := []struct {
51 name string
52 setEnv bool // whether to set the environment variable
53 envValue string // the value to set (only used if setEnv is true)
54 expectSet bool
55 }{
56 {
57 name: "returns false when NO_COLOR is not set",
58 setEnv: false,
59 envValue: "",
60 expectSet: false,
61 },
62 {
63 name: "returns true when NO_COLOR is set to empty string",
64 setEnv: true,
65 envValue: "",
66 expectSet: true,
67 },
68 {
69 name: "returns true when NO_COLOR is set to 0",
70 setEnv: true,
71 envValue: "0",
72 expectSet: true,
73 },
74 {
75 name: "returns true when NO_COLOR is set to false",
76 setEnv: true,
77 envValue: "false",
78 expectSet: true,
79 },
80 {
81 name: "returns true when NO_COLOR is set to any value",
82 setEnv: true,
83 envValue: "1",
84 expectSet: true,
85 },
86 }
87
88 for _, tt := range tests {
89 t.Run(tt.name, func(t *testing.T) {
90 // Save original environment before each subtest
91 originalEnv, hadOriginal := os.LookupEnv("NO_COLOR")
92
93 // Set or unset environment variable
94 if tt.setEnv {
95 os.Setenv("NO_COLOR", tt.envValue)
96 } else {
97 os.Unsetenv("NO_COLOR")
98 }
99
100 // Defer cleanup to restore original state
101 defer func() {
102 if hadOriginal {
103 os.Setenv("NO_COLOR", originalEnv)
104 } else {
105 os.Unsetenv("NO_COLOR")
106 }
107 }()
108
109 result := noColorIsSet()
110 assert.Equal(t, tt.expectSet, result)
111 })
112 }
113 }
114
115 func Test_sprintln(t *testing.T) {
116 t.Parallel()
117
118 tests := []struct {
119 name string
120 input []any
121 expected string
122 }{
123 {
124 name: "empty input returns empty string",
125 input: []any{},
126 expected: "",
127 },
128 {
129 name: "single string removes trailing newline",
130 input: []any{"hello"},
131 expected: "hello",
132 },
133 {
134 name: "single integer",
135 input: []any{42},
136 expected: "42",
137 },
138 {
139 name: "multiple arguments",
140 input: []any{"hello", "world"},
141 expected: "hello world",
142 },
143 {
144 name: "mixed types",
145 input: []any{"count:", 123, "pi:", 3.14},
146 expected: "count: 123 pi: 3.14",
147 },
148 {
149 name: "string with spaces",
150 input: []any{"hello world"},
151 expected: "hello world",
152 },
153 {
154 name: "empty string",
155 input: []any{""},
156 expected: "",
157 },
158 }
159
160 for _, tt := range tests {
161 t.Run(tt.name, func(t *testing.T) {
162 t.Parallel()
163
164 result := sprintln(tt.input...)
165 assert.Equal(t, tt.expected, result)
166 })
167 }
168 }