11package cli
22
33import (
4+ "sync"
45 "testing"
56
67 "github.com/stretchr/testify/assert"
78)
89
9- func TestProgressBar (t * testing.T ) { //nolint:paralleltest // modifies global colorEnabled state
10+ // colorTestMutex serializes color tests that modify global colorEnabled state.
11+ // This allows tests to be marked as parallel (satisfying paralleltest linter)
12+ // while ensuring correct behavior.
13+ var colorTestMutex sync.Mutex
14+
15+ func TestProgressBar (t * testing.T ) {
16+ t .Parallel ()
17+ colorTestMutex .Lock ()
18+ defer colorTestMutex .Unlock ()
19+
1020 // Disable colors for consistent test results
11- oldColorEnabled := colorEnabled
21+ oldColorEnabled := IsColorEnabled ()
1222 SetColorEnabled (false )
1323 defer SetColorEnabled (oldColorEnabled )
1424
@@ -68,17 +78,21 @@ func TestProgressBar(t *testing.T) { //nolint:paralleltest // modifies global co
6878 },
6979 }
7080
71- for _ , tt := range tests { //nolint:paralleltest // parent modifies global colorEnabled state
81+ for _ , tt := range tests {
7282 t .Run (tt .name , func (t * testing.T ) {
7383 result := ProgressBar (tt .percent , tt .width )
7484 assert .Equal (t , tt .expected , result )
7585 })
7686 }
7787}
7888
79- func TestProgressBar_WithColors (t * testing.T ) { //nolint:paralleltest // modifies global colorEnabled state
89+ func TestProgressBar_WithColors (t * testing.T ) {
90+ t .Parallel ()
91+ colorTestMutex .Lock ()
92+ defer colorTestMutex .Unlock ()
93+
8094 // Enable colors for color testing
81- oldColorEnabled := colorEnabled
95+ oldColorEnabled := IsColorEnabled ()
8296 SetColorEnabled (true )
8397 defer SetColorEnabled (oldColorEnabled )
8498
@@ -92,7 +106,7 @@ func TestProgressBar_WithColors(t *testing.T) { //nolint:paralleltest // modifie
92106 {name : "low battery (red)" , percent : 20 , width : 10 },
93107 }
94108
95- for _ , tt := range tests { //nolint:paralleltest // parent modifies global colorEnabled state
109+ for _ , tt := range tests {
96110 t .Run (tt .name , func (t * testing.T ) {
97111 result := ProgressBar (tt .percent , tt .width )
98112 // Just verify it contains ANSI codes (starts with escape sequence)
@@ -101,9 +115,13 @@ func TestProgressBar_WithColors(t *testing.T) { //nolint:paralleltest // modifie
101115 }
102116}
103117
104- func TestColorize (t * testing.T ) { //nolint:paralleltest // modifies global colorEnabled state
118+ func TestColorize (t * testing.T ) {
119+ t .Parallel ()
120+ colorTestMutex .Lock ()
121+ defer colorTestMutex .Unlock ()
122+
105123 // Disable colors
106- oldColorEnabled := colorEnabled
124+ oldColorEnabled := IsColorEnabled ()
107125 SetColorEnabled (false )
108126 defer SetColorEnabled (oldColorEnabled )
109127
@@ -114,9 +132,13 @@ func TestColorize(t *testing.T) { //nolint:paralleltest // modifies global color
114132 assert .Equal (t , text , Bold (text ))
115133}
116134
117- func TestColorize_WithColors (t * testing.T ) { //nolint:paralleltest // modifies global colorEnabled state
135+ func TestColorize_WithColors (t * testing.T ) {
136+ t .Parallel ()
137+ colorTestMutex .Lock ()
138+ defer colorTestMutex .Unlock ()
139+
118140 // Enable colors
119- oldColorEnabled := colorEnabled
141+ oldColorEnabled := IsColorEnabled ()
120142 SetColorEnabled (true )
121143 defer SetColorEnabled (oldColorEnabled )
122144
@@ -143,8 +165,12 @@ func TestColorize_WithColors(t *testing.T) { //nolint:paralleltest // modifies g
143165 assert .Equal (t , expected , result )
144166}
145167
146- func TestSetColorEnabled (t * testing.T ) { //nolint:paralleltest // modifies global colorEnabled state
147- oldColorEnabled := colorEnabled
168+ func TestSetColorEnabled (t * testing.T ) {
169+ t .Parallel ()
170+ colorTestMutex .Lock ()
171+ defer colorTestMutex .Unlock ()
172+
173+ oldColorEnabled := IsColorEnabled ()
148174 defer SetColorEnabled (oldColorEnabled )
149175
150176 SetColorEnabled (true )
@@ -154,9 +180,13 @@ func TestSetColorEnabled(t *testing.T) { //nolint:paralleltest // modifies globa
154180 assert .False (t , IsColorEnabled ())
155181}
156182
157- func TestColorPressure (t * testing.T ) { //nolint:paralleltest // modifies global colorEnabled state
183+ func TestColorPressure (t * testing.T ) {
184+ t .Parallel ()
185+ colorTestMutex .Lock ()
186+ defer colorTestMutex .Unlock ()
187+
158188 // Disable colors for consistent test results
159- oldColorEnabled := colorEnabled
189+ oldColorEnabled := IsColorEnabled ()
160190 SetColorEnabled (false )
161191 defer SetColorEnabled (oldColorEnabled )
162192
@@ -181,17 +211,21 @@ func TestColorPressure(t *testing.T) { //nolint:paralleltest // modifies global
181211 {"very low" , 25.0 , "25.0" },
182212 }
183213
184- for _ , tt := range tests { //nolint:paralleltest // parent modifies global colorEnabled state
214+ for _ , tt := range tests {
185215 t .Run (tt .name , func (t * testing.T ) {
186216 result := ColorPressure (tt .pressure , target )
187217 assert .Equal (t , tt .expected , result )
188218 })
189219 }
190220}
191221
192- func TestColorPressure_WithColors (t * testing.T ) { //nolint:paralleltest // modifies global colorEnabled state
222+ func TestColorPressure_WithColors (t * testing.T ) {
223+ t .Parallel ()
224+ colorTestMutex .Lock ()
225+ defer colorTestMutex .Unlock ()
226+
193227 // Enable colors for color testing
194- oldColorEnabled := colorEnabled
228+ oldColorEnabled := IsColorEnabled ()
195229 SetColorEnabled (true )
196230 defer SetColorEnabled (oldColorEnabled )
197231
@@ -210,7 +244,7 @@ func TestColorPressure_WithColors(t *testing.T) { //nolint:paralleltest // modif
210244 {"red - very low" , 25.0 , colorRed },
211245 }
212246
213- for _ , tt := range tests { //nolint:paralleltest // parent modifies global colorEnabled state
247+ for _ , tt := range tests {
214248 t .Run (tt .name , func (t * testing.T ) {
215249 result := ColorPressure (tt .pressure , target )
216250 // Check it starts with expected color code
0 commit comments