@@ -19,6 +19,8 @@ pub(super) enum StreamSelector {
1919 Stderr ,
2020 #[ cfg( feature = "test" ) ]
2121 TestWriter ( TestWriter ) ,
22+ #[ cfg( feature = "test" ) ]
23+ TestTtyWriter ( TestWriter ) ,
2224}
2325
2426impl StreamSelector {
@@ -36,6 +38,8 @@ impl StreamSelector {
3638 } ,
3739 #[ cfg( feature = "test" ) ]
3840 StreamSelector :: TestWriter ( _) => false ,
41+ #[ cfg( feature = "test" ) ]
42+ StreamSelector :: TestTtyWriter ( _) => true ,
3943 }
4044 }
4145}
@@ -55,7 +59,7 @@ pub struct ColorableTerminal {
5559enum TerminalInner {
5660 StandardStream ( StandardStream , ColorSpec ) ,
5761 #[ cfg( feature = "test" ) ]
58- TestWriter ( TestWriter ) ,
62+ TestWriter ( TestWriter , ColorChoice ) ,
5963}
6064
6165pub struct ColorableTerminalLocked {
@@ -94,7 +98,9 @@ impl ColorableTerminal {
9498 TerminalInner :: StandardStream ( StandardStream :: stderr ( choice) , ColorSpec :: new ( ) )
9599 }
96100 #[ cfg( feature = "test" ) ]
97- StreamSelector :: TestWriter ( w) => TerminalInner :: TestWriter ( w) ,
101+ StreamSelector :: TestWriter ( w) | StreamSelector :: TestTtyWriter ( w) => {
102+ TerminalInner :: TestWriter ( w, choice)
103+ }
98104 } ;
99105 ColorableTerminal {
100106 inner : Arc :: new ( Mutex :: new ( inner) ) ,
@@ -122,7 +128,7 @@ impl ColorableTerminal {
122128 TerminalInnerLocked :: StandardStream ( locked)
123129 }
124130 #[ cfg( feature = "test" ) ]
125- TerminalInner :: TestWriter ( w) => TerminalInnerLocked :: TestWriter ( w. lock ( ) ) ,
131+ TerminalInner :: TestWriter ( w, _ ) => TerminalInnerLocked :: TestWriter ( w. lock ( ) ) ,
126132 } ) ;
127133 // ColorableTerminalLocked { inner, guard, locked }
128134 uninit. assume_init ( )
@@ -136,7 +142,7 @@ impl ColorableTerminal {
136142 s. set_color ( spec)
137143 }
138144 #[ cfg( feature = "test" ) ]
139- TerminalInner :: TestWriter ( _) => Ok ( ( ) ) ,
145+ TerminalInner :: TestWriter ( _, _ ) => Ok ( ( ) ) ,
140146 }
141147 }
142148
@@ -147,7 +153,7 @@ impl ColorableTerminal {
147153 s. set_color ( spec)
148154 }
149155 #[ cfg( feature = "test" ) ]
150- TerminalInner :: TestWriter ( _) => Ok ( ( ) ) ,
156+ TerminalInner :: TestWriter ( _, _ ) => Ok ( ( ) ) ,
151157 }
152158 }
153159
@@ -161,23 +167,23 @@ impl ColorableTerminal {
161167 s. set_color ( spec)
162168 }
163169 #[ cfg( feature = "test" ) ]
164- TerminalInner :: TestWriter ( _) => Ok ( ( ) ) ,
170+ TerminalInner :: TestWriter ( _, _ ) => Ok ( ( ) ) ,
165171 }
166172 }
167173
168174 pub fn reset ( & mut self ) -> io:: Result < ( ) > {
169175 match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
170176 TerminalInner :: StandardStream ( s, _color) => s. reset ( ) ,
171177 #[ cfg( feature = "test" ) ]
172- TerminalInner :: TestWriter ( _) => Ok ( ( ) ) ,
178+ TerminalInner :: TestWriter ( _, _ ) => Ok ( ( ) ) ,
173179 }
174180 }
175181
176182 pub fn carriage_return ( & mut self ) -> io:: Result < ( ) > {
177183 match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
178184 TerminalInner :: StandardStream ( s, _color) => s. write ( b"\r " ) ?,
179185 #[ cfg( feature = "test" ) ]
180- TerminalInner :: TestWriter ( w) => w. write ( b"\r " ) ?,
186+ TerminalInner :: TestWriter ( w, _ ) => w. write ( b"\r " ) ?,
181187 } ;
182188 Ok ( ( ) )
183189 }
@@ -194,15 +200,15 @@ impl io::Write for ColorableTerminal {
194200 match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
195201 TerminalInner :: StandardStream ( s, _) => s. write ( buf) ,
196202 #[ cfg( feature = "test" ) ]
197- TerminalInner :: TestWriter ( w) => w. write ( buf) ,
203+ TerminalInner :: TestWriter ( w, _ ) => w. write ( buf) ,
198204 }
199205 }
200206
201207 fn flush ( & mut self ) -> std:: result:: Result < ( ) , io:: Error > {
202208 match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
203209 TerminalInner :: StandardStream ( s, _) => s. flush ( ) ,
204210 #[ cfg( feature = "test" ) ]
205- TerminalInner :: TestWriter ( w) => w. flush ( ) ,
211+ TerminalInner :: TestWriter ( w, _ ) => w. flush ( ) ,
206212 }
207213 }
208214}
@@ -224,3 +230,56 @@ impl io::Write for ColorableTerminalLocked {
224230 }
225231 }
226232}
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