@@ -19,6 +19,8 @@ pub(super) enum StreamSelector {
19
19
Stderr ,
20
20
#[ cfg( feature = "test" ) ]
21
21
TestWriter ( TestWriter ) ,
22
+ #[ cfg( feature = "test" ) ]
23
+ TestTtyWriter ( TestWriter ) ,
22
24
}
23
25
24
26
impl StreamSelector {
@@ -36,6 +38,8 @@ impl StreamSelector {
36
38
} ,
37
39
#[ cfg( feature = "test" ) ]
38
40
StreamSelector :: TestWriter ( _) => false ,
41
+ #[ cfg( feature = "test" ) ]
42
+ StreamSelector :: TestTtyWriter ( _) => true ,
39
43
}
40
44
}
41
45
}
@@ -55,7 +59,7 @@ pub struct ColorableTerminal {
55
59
enum TerminalInner {
56
60
StandardStream ( StandardStream , ColorSpec ) ,
57
61
#[ cfg( feature = "test" ) ]
58
- TestWriter ( TestWriter ) ,
62
+ TestWriter ( TestWriter , ColorChoice ) ,
59
63
}
60
64
61
65
pub struct ColorableTerminalLocked {
@@ -94,7 +98,9 @@ impl ColorableTerminal {
94
98
TerminalInner :: StandardStream ( StandardStream :: stderr ( choice) , ColorSpec :: new ( ) )
95
99
}
96
100
#[ cfg( feature = "test" ) ]
97
- StreamSelector :: TestWriter ( w) => TerminalInner :: TestWriter ( w) ,
101
+ StreamSelector :: TestWriter ( w) | StreamSelector :: TestTtyWriter ( w) => {
102
+ TerminalInner :: TestWriter ( w, choice)
103
+ }
98
104
} ;
99
105
ColorableTerminal {
100
106
inner : Arc :: new ( Mutex :: new ( inner) ) ,
@@ -122,7 +128,7 @@ impl ColorableTerminal {
122
128
TerminalInnerLocked :: StandardStream ( locked)
123
129
}
124
130
#[ cfg( feature = "test" ) ]
125
- TerminalInner :: TestWriter ( w) => TerminalInnerLocked :: TestWriter ( w. lock ( ) ) ,
131
+ TerminalInner :: TestWriter ( w, _ ) => TerminalInnerLocked :: TestWriter ( w. lock ( ) ) ,
126
132
} ) ;
127
133
// ColorableTerminalLocked { inner, guard, locked }
128
134
uninit. assume_init ( )
@@ -136,7 +142,7 @@ impl ColorableTerminal {
136
142
s. set_color ( spec)
137
143
}
138
144
#[ cfg( feature = "test" ) ]
139
- TerminalInner :: TestWriter ( _) => Ok ( ( ) ) ,
145
+ TerminalInner :: TestWriter ( _, _ ) => Ok ( ( ) ) ,
140
146
}
141
147
}
142
148
@@ -147,7 +153,7 @@ impl ColorableTerminal {
147
153
s. set_color ( spec)
148
154
}
149
155
#[ cfg( feature = "test" ) ]
150
- TerminalInner :: TestWriter ( _) => Ok ( ( ) ) ,
156
+ TerminalInner :: TestWriter ( _, _ ) => Ok ( ( ) ) ,
151
157
}
152
158
}
153
159
@@ -161,23 +167,23 @@ impl ColorableTerminal {
161
167
s. set_color ( spec)
162
168
}
163
169
#[ cfg( feature = "test" ) ]
164
- TerminalInner :: TestWriter ( _) => Ok ( ( ) ) ,
170
+ TerminalInner :: TestWriter ( _, _ ) => Ok ( ( ) ) ,
165
171
}
166
172
}
167
173
168
174
pub fn reset ( & mut self ) -> io:: Result < ( ) > {
169
175
match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
170
176
TerminalInner :: StandardStream ( s, _color) => s. reset ( ) ,
171
177
#[ cfg( feature = "test" ) ]
172
- TerminalInner :: TestWriter ( _) => Ok ( ( ) ) ,
178
+ TerminalInner :: TestWriter ( _, _ ) => Ok ( ( ) ) ,
173
179
}
174
180
}
175
181
176
182
pub fn carriage_return ( & mut self ) -> io:: Result < ( ) > {
177
183
match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
178
184
TerminalInner :: StandardStream ( s, _color) => s. write ( b"\r " ) ?,
179
185
#[ cfg( feature = "test" ) ]
180
- TerminalInner :: TestWriter ( w) => w. write ( b"\r " ) ?,
186
+ TerminalInner :: TestWriter ( w, _ ) => w. write ( b"\r " ) ?,
181
187
} ;
182
188
Ok ( ( ) )
183
189
}
@@ -194,15 +200,15 @@ impl io::Write for ColorableTerminal {
194
200
match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
195
201
TerminalInner :: StandardStream ( s, _) => s. write ( buf) ,
196
202
#[ cfg( feature = "test" ) ]
197
- TerminalInner :: TestWriter ( w) => w. write ( buf) ,
203
+ TerminalInner :: TestWriter ( w, _ ) => w. write ( buf) ,
198
204
}
199
205
}
200
206
201
207
fn flush ( & mut self ) -> std:: result:: Result < ( ) , io:: Error > {
202
208
match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
203
209
TerminalInner :: StandardStream ( s, _) => s. flush ( ) ,
204
210
#[ cfg( feature = "test" ) ]
205
- TerminalInner :: TestWriter ( w) => w. flush ( ) ,
211
+ TerminalInner :: TestWriter ( w, _ ) => w. flush ( ) ,
206
212
}
207
213
}
208
214
}
@@ -224,3 +230,56 @@ impl io::Write for ColorableTerminalLocked {
224
230
}
225
231
}
226
232
}
233
+
234
+ #[ cfg( test) ]
235
+ mod tests {
236
+ use std:: collections:: HashMap ;
237
+
238
+ use rustup_macros:: unit_test as test;
239
+
240
+ use super :: * ;
241
+ use crate :: { currentprocess, test:: Env } ;
242
+
243
+ #[ test]
244
+ fn term_color_choice ( ) {
245
+ fn assert_color_choice ( env_val : & str , stream : StreamSelector , color_choice : ColorChoice ) {
246
+ let mut vars = HashMap :: new ( ) ;
247
+ vars. env ( "RUSTUP_TERM_COLOR" , env_val) ;
248
+ let tp = currentprocess:: TestProcess {
249
+ vars,
250
+ ..Default :: default ( )
251
+ } ;
252
+ currentprocess:: with ( tp. into ( ) , || {
253
+ let term = ColorableTerminal :: new ( stream) ;
254
+ let inner = term. inner . lock ( ) . unwrap ( ) ;
255
+ assert ! ( matches!(
256
+ & * inner,
257
+ & TerminalInner :: TestWriter ( _, choice) if choice == color_choice
258
+ ) ) ;
259
+ } ) ;
260
+ }
261
+
262
+ assert_color_choice (
263
+ "always" ,
264
+ StreamSelector :: TestWriter ( Default :: default ( ) ) ,
265
+ ColorChoice :: Always ,
266
+ ) ;
267
+ assert_color_choice (
268
+ "never" ,
269
+ StreamSelector :: TestWriter ( Default :: default ( ) ) ,
270
+ ColorChoice :: Never ,
271
+ ) ;
272
+ // tty + `auto` enables the colors.
273
+ assert_color_choice (
274
+ "auto" ,
275
+ StreamSelector :: TestTtyWriter ( Default :: default ( ) ) ,
276
+ ColorChoice :: Auto ,
277
+ ) ;
278
+ // non-tty + `auto` does not enable the colors.
279
+ assert_color_choice (
280
+ "auto" ,
281
+ StreamSelector :: TestWriter ( Default :: default ( ) ) ,
282
+ ColorChoice :: Never ,
283
+ ) ;
284
+ }
285
+ }
0 commit comments