<s> """<STR_LIT>""" <EOL> import errno <EOL> import os <EOL> import subprocess <EOL> import sys <EOL> import time <EOL> import pytest <EOL> from colorclass . codes import ANSICodeMapping , BASE_CODES , list_tags <EOL> from colorclass . windows import IS_WINDOWS <EOL> def test_ansi_code_mapping_whitelist ( ) : <EOL> """<STR_LIT>""" <EOL> auto_codes = ANSICodeMapping ( '<STR_LIT>' ) <EOL> with pytest . raises ( KeyError ) : <EOL> assert not auto_codes [ '<STR_LIT>' ] <EOL> assert auto_codes [ '<STR_LIT>' ] == <NUM_LIT:32> <EOL> assert sorted ( auto_codes ) == [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert len ( auto_codes ) == <NUM_LIT:3> <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:none>' ] ) <EOL> def test_auto_toggles ( toggle ) : <EOL> """<STR_LIT>""" <EOL> if toggle == '<STR_LIT>' : <EOL> ANSICodeMapping . enable_all_colors ( ) <EOL> ANSICodeMapping . set_light_background ( ) <EOL> assert ANSICodeMapping . DISABLE_COLORS is False <EOL> assert ANSICodeMapping . LIGHT_BACKGROUND is True <EOL> elif toggle == '<STR_LIT>' : <EOL> ANSICodeMapping . enable_all_colors ( ) <EOL> ANSICodeMapping . set_dark_background ( ) <EOL> assert ANSICodeMapping . DISABLE_COLORS is False <EOL> assert ANSICodeMapping . LIGHT_BACKGROUND is False <EOL> else : <EOL> ANSICodeMapping . disable_all_colors ( ) <EOL> assert ANSICodeMapping . DISABLE_COLORS is True <EOL> assert ANSICodeMapping . LIGHT_BACKGROUND is False <EOL> auto_codes = ANSICodeMapping ( '<STR_LIT>' . join ( [ '<STR_LIT>' ] + list ( BASE_CODES ) + [ '<STR_LIT>' ] ) ) <EOL> count = <NUM_LIT:0> <EOL> for k , v in auto_codes . items ( ) : <EOL> count += <NUM_LIT:1> <EOL> assert str ( k ) == k <EOL> assert v is None or int ( v ) == v <EOL> assert len ( auto_codes ) == count <EOL> key_fg = '<STR_LIT>' <EOL> actual = key_fg . format ( ** auto_codes ) <EOL> if toggle == '<STR_LIT>' : <EOL> assert actual == '<STR_LIT>' <EOL> elif toggle == '<STR_LIT>' : <EOL> assert actual == '<STR_LIT>' <EOL> else : <EOL> assert actual == '<STR_LIT>' <EOL> key_fg = '<STR_LIT>' <EOL> actual = key_fg . format ( ** auto_codes ) <EOL> if toggle == '<STR_LIT>' : <EOL> assert actual == '<STR_LIT>' <EOL> elif toggle == '<STR_LIT>' : <EOL> assert actual == '<STR_LIT>' <EOL> else : <EOL> assert actual == '<STR_LIT>' <EOL> def test_list_tags ( ) : <EOL> """<STR_LIT>""" <EOL> actual = list_tags ( ) <EOL> assert ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) in actual <EOL> assert sorted ( t for i in actual for t in i [ : <NUM_LIT:2> ] if t is not None ) == sorted ( BASE_CODES ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ False , True ] ) <EOL> def test_disable_colors_piped ( tty ) : <EOL> """<STR_LIT>""" <EOL> assert_statement = '<STR_LIT>' <EOL> command_colors_enabled = [ sys . executable , '<STR_LIT:-c>' , assert_statement . format ( bool = '<STR_LIT:False>' ) ] <EOL> command_colors_disabled = [ sys . executable , '<STR_LIT:-c>' , assert_statement . format ( bool = '<STR_LIT:True>' ) ] <EOL> if not tty : <EOL> proc = subprocess . Popen ( command_colors_disabled , stderr = subprocess . STDOUT , stdout = subprocess . PIPE ) <EOL> output = proc . communicate ( ) <EOL> assert not output [ <NUM_LIT:0> ] <EOL> assert not output [ <NUM_LIT:1> ] <EOL> assert proc . poll ( ) == <NUM_LIT:0> <EOL> return <EOL> if IS_WINDOWS : <EOL> c_flags = subprocess . CREATE_NEW_CONSOLE <EOL> proc = subprocess . Popen ( command_colors_enabled , close_fds = True , creationflags = c_flags ) <EOL> proc . communicate ( ) <EOL> assert proc . poll ( ) == <NUM_LIT:0> <EOL> return <EOL> master , slave = __import__ ( '<STR_LIT>' ) . openpty ( ) <EOL> proc = subprocess . Popen ( command_colors_enabled , stderr = subprocess . STDOUT , stdout = slave , close_fds = True ) <EOL> os . close ( slave ) <EOL> output = '<STR_LIT>' <EOL> while True : <EOL> try : <EOL> data = os . read ( master , <NUM_LIT> ) . decode ( ) <EOL> except OSError as exc : <EOL> if exc . errno != errno . EIO : <EOL> raise <EOL> data = None <EOL> if data : <EOL> output += data <EOL> elif proc . poll ( ) is None : <EOL> time . sleep ( <NUM_LIT> ) <EOL> else : <EOL> break <EOL> os . close ( master ) <EOL> assert not output <EOL> assert proc . poll ( ) == <NUM_LIT:0> </s>
