@@ -207,171 +207,34 @@ def test_echo_via_pager(monkeypatch, capfd, cat, test):
207207 assert out == expected_output
208208
209209
210- """
211- These variables are used for the next three tests.
212-
213- _text_str is guranteeing that each color test is using the same
214- string.
215-
216- _styled_text_str is running _text_str through click.style with fg
217- set to red. This gurantees that each test is getting the same styled text.
218-
219- expected_stripped_text is using an fstring on _text_str to add a newline chracter
220- to the end of it.
221-
222- expected_unstripped_text is using an fstring on_styled_text_str to add a newline
223- character to the end of it.
224-
225- Each expected variable is what is being used in the asserts to gurantee the output
226- is correct.
227-
228- The unstripped version is for when an the ANSI style should stay applied.
229- The stripped version is for when the ANSI style should be removed from the text.
230-
231- These can be removed if you would rather have the parametrize values be explict
232- in the following tests. The variables could also be extended and the tests truncated
233- by adding isatty and jupyter_kernel variables here. Expanding the parametrize
234- matrix to use those within the test instead of it being explict based on tests.
235- """
236- _text_str = "test"
237- _styled_text_str = click .style (_text_str , fg = "red" )
238- expected_stripped_text = f"{ _text_str } \n "
239- expected_unstripped_text = f"{ _styled_text_str } \n "
240-
241-
242- @pytest .mark .parametrize (
243- "color, text, stylized_text, expected" ,
244- [
245- (False , _text_str , _styled_text_str , expected_stripped_text ),
246- (True , _text_str , _styled_text_str , expected_unstripped_text ),
247- (None , _text_str , _styled_text_str , expected_unstripped_text ),
248- ],
249- )
250- def test_echo_color_flag_atty_and_not_jupyter_kernel (
251- monkeypatch , capfd , color , text , stylized_text , expected
252- ):
253- """
254- This is the test for echo color when the stream is a tty and
255- not a jupyter kernel output.
256-
257- If color is set to False, then the text should strip the ANSI values regardless
258- of operating system.
259-
260- If color is set to True, then we are forcing click to not care about
261- the type of stream or operating system. This means ANSI style codes will be applied
262- even if the output would not support them. Instances of this would be piping an
263- echo to a file.
264-
265- If color is set to None / not set, then click will check if the stream is a tty.
266- This test is forcing that to always be true, and will then mean that the output
267- should keep the ANSI style.
268-
269- The mocking of _is_jupyter_kernel_output is not needed, but was added to keep it
270- consistent with the other tests.
271- """
272- isatty = True
273- monkeypatch .setattr (click ._compat , "isatty" , lambda x : isatty )
274-
275- jupyter_kernel = False
276- monkeypatch .setattr (
277- click ._compat , "_is_jupyter_kernel_output" , lambda x : jupyter_kernel
278- )
210+ def test_echo_color_flag (monkeypatch , capfd ):
211+ should_strip_ansi = False
212+ monkeypatch .setattr (click ._compat , "should_strip_ansi" , lambda x : should_strip_ansi )
279213
214+ text = "foo"
280215 styled_text = click .style (text , fg = "red" )
281- assert styled_text == stylized_text
216+ assert styled_text == " \x1b [31mfoo \x1b [0m"
282217
283- stream = StringIO ()
284- click .echo (styled_text , color = color , file = stream )
218+ click .echo (styled_text , color = False )
285219 out , err = capfd .readouterr ()
286- assert stream . getvalue () == expected
220+ assert out == f" { text } \n "
287221
288-
289- @pytest .mark .parametrize (
290- "color, text, stylized_text, expected" ,
291- [
292- (False , _text_str , _styled_text_str , expected_stripped_text ),
293- (True , _text_str , _styled_text_str , expected_unstripped_text ),
294- (None , _text_str , _styled_text_str , expected_unstripped_text ),
295- ],
296- )
297- def test_echo_color_flag_jupyter_kernel_and_not_atty (
298- monkeypatch , capfd , color , text , stylized_text , expected
299- ):
300- """
301- This is the test for echo color when the stream is not a tty and
302- is a jupyter kernel output.
303-
304- If color is set to False, then the text should strip the ANSI values regardless
305- of operating system.
306-
307- If color is set to True, then we are forcing click to not care about
308- the type of stream or operating system. This means ANSI style codes will be applied
309- even if the output would not support them. Instances of this would be piping an
310- echo to a file.
311-
312- If color is set to None / not set, then click will check if the stream is a tty.
313- Which is being forced to be False so, the strip function will next check if
314- the stream is a jupyter kernel output which is being set as True. This means
315- that the ANSI style should not be stripped.
316- """
317- jupyter_kernel = True
318- monkeypatch .setattr (
319- click ._compat , "_is_jupyter_kernel_output" , lambda x : jupyter_kernel
320- )
321- isatty = False
322- monkeypatch .setattr (click ._compat , "isatty" , lambda x : isatty )
323-
324- styled_text = click .style (text , fg = "red" )
325- assert styled_text == stylized_text
326-
327- click .echo (styled_text , color = color )
222+ click .echo (styled_text , color = True )
328223 out , err = capfd .readouterr ()
329- assert out == expected
224+ assert out == f" { styled_text } \n "
330225
226+ should_strip_ansi = False
227+ click .echo (styled_text )
228+ out , err = capfd .readouterr ()
229+ assert out == f"{ styled_text } \n "
331230
332- @pytest .mark .parametrize (
333- "color, text, stylized_text, expected" ,
334- [
335- (False , _text_str , _styled_text_str , expected_stripped_text ),
336- (True , _text_str , _styled_text_str , expected_unstripped_text ),
337- (None , _text_str , _styled_text_str , expected_stripped_text ),
338- ],
339- )
340- def test_echo_color_flag_not_atty_and_not_jupyter_kernel (
341- monkeypatch , capfd , color , text , stylized_text , expected
342- ):
343- """
344- This is the test for echo color when the stream is not a tty nor a jupyter kernel
345- output.
346-
347- If color is set to False, then the text should strip the ANSI values regardless
348- of operating system.
349-
350- If color is set to True, then we are forcing click to not care about
351- the type of stream or operating system. This means ANSI style codes will be applied
352- even if the output would not support them. Instances of this would be piping an
353- echo to a file.
354-
355- If color is set to None / not set, then click will check that the stream is neither
356- a tty nor a jupyter notebook. This means the styling should be stripped regardless
357- of the operating system.
358- """
359- isatty = False
231+ should_strip_ansi = True
360232 # Faking isatty() is not enough on Windows;
361233 # the implementation caches the colorama wrapped stream
362234 # so we have to use a new stream for each test
363235 stream = StringIO ()
364- monkeypatch .setattr (click ._compat , "isatty" , lambda x : isatty )
365- jupyter_kernel = False
366- monkeypatch .setattr (
367- click ._compat , "_is_jupyter_kernel_output" , lambda x : jupyter_kernel
368- )
369-
370- styled_text = click .style (text , fg = "red" )
371- assert styled_text == stylized_text
372-
373- click .echo (styled_text , color = color , file = stream )
374- assert stream .getvalue () == expected
236+ click .echo (styled_text , file = stream )
237+ assert stream .getvalue () == f"{ text } \n "
375238
376239 stream = StringIO ()
377240 click .echo (styled_text , file = stream , color = True )
0 commit comments