<s> import sys <EOL> import pytest <EOL> import click <EOL> from click . testing import CliRunner <EOL> from click . _compat import PY2 , WIN <EOL> if PY2 : <EOL> from cStringIO import StringIO as ReasonableBytesIO <EOL> else : <EOL> from io import BytesIO as ReasonableBytesIO <EOL> def test_runner ( ) : <EOL> @ click . command ( ) <EOL> def test ( ) : <EOL> i = click . get_binary_stream ( '<STR_LIT>' ) <EOL> o = click . get_binary_stream ( '<STR_LIT>' ) <EOL> while <NUM_LIT:1> : <EOL> chunk = i . read ( <NUM_LIT> ) <EOL> if not chunk : <EOL> break <EOL> o . write ( chunk ) <EOL> o . flush ( ) <EOL> runner = CliRunner ( ) <EOL> result = runner . invoke ( test , input = '<STR_LIT>' ) <EOL> assert not result . exception <EOL> assert result . output == '<STR_LIT>' <EOL> runner = CliRunner ( echo_stdin = True ) <EOL> result = runner . invoke ( test , input = '<STR_LIT>' ) <EOL> assert not result . exception <EOL> assert result . output == '<STR_LIT>' <EOL> def test_runner_with_stream ( ) : <EOL> @ click . command ( ) <EOL> def test ( ) : <EOL> i = click . get_binary_stream ( '<STR_LIT>' ) <EOL> o = click . get_binary_stream ( '<STR_LIT>' ) <EOL> while <NUM_LIT:1> : <EOL> chunk = i . read ( <NUM_LIT> ) <EOL> if not chunk : <EOL> break <EOL> o . write ( chunk ) <EOL> o . flush ( ) <EOL> runner = CliRunner ( ) <EOL> result = runner . invoke ( test , input = ReasonableBytesIO ( b'<STR_LIT>' ) ) <EOL> assert not result . exception <EOL> assert result . output == '<STR_LIT>' <EOL> runner = CliRunner ( echo_stdin = True ) <EOL> result = runner . invoke ( test , input = ReasonableBytesIO ( b'<STR_LIT>' ) ) <EOL> assert not result . exception <EOL> assert result . output == '<STR_LIT>' <EOL> def test_prompts ( ) : <EOL> @ click . command ( ) <EOL> @ click . option ( '<STR_LIT>' , prompt = True ) <EOL> def test ( foo ) : <EOL> click . echo ( '<STR_LIT>' % foo ) <EOL> runner = CliRunner ( ) <EOL> result = runner . invoke ( test , input = '<STR_LIT>' ) <EOL> assert not result . exception <EOL> assert result . output == '<STR_LIT>' <EOL> @ click . command ( ) <EOL> @ click . option ( '<STR_LIT>' , prompt = True , hide_input = True ) <EOL> def test ( foo ) : <EOL> click . echo ( '<STR_LIT>' % foo ) <EOL> runner = CliRunner ( ) <EOL> result = runner . invoke ( test , input = '<STR_LIT>' ) <EOL> assert not result . exception <EOL> assert result . output == '<STR_LIT>' <EOL> def test_getchar ( ) : <EOL> @ click . command ( ) <EOL> def continue_it ( ) : <EOL> click . echo ( click . getchar ( ) ) <EOL> runner = CliRunner ( ) <EOL> result = runner . invoke ( continue_it , input = '<STR_LIT:y>' ) <EOL> assert not result . exception <EOL> assert result . output == '<STR_LIT>' <EOL> def test_catch_exceptions ( ) : <EOL> class CustomError ( Exception ) : <EOL> pass <EOL> @ click . command ( ) <EOL> def cli ( ) : <EOL> raise CustomError ( <NUM_LIT:1> ) <EOL> runner = CliRunner ( ) <EOL> result = runner . invoke ( cli ) <EOL> assert isinstance ( result . exception , CustomError ) <EOL> assert type ( result . exc_info ) is tuple <EOL> assert len ( result . exc_info ) == <NUM_LIT:3> <EOL> with pytest . raises ( CustomError ) : <EOL> runner . invoke ( cli , catch_exceptions = False ) <EOL> CustomError = SystemExit <EOL> result = runner . invoke ( cli ) <EOL> assert result . exit_code == <NUM_LIT:1> <EOL> @ pytest . mark . skipif ( WIN , reason = '<STR_LIT>' ) <EOL> def test_with_color ( ) : <EOL> @ click . command ( ) <EOL> def cli ( ) : <EOL> click . secho ( '<STR_LIT>' , fg = '<STR_LIT>' ) <EOL> runner = CliRunner ( ) <EOL> result = runner . invoke ( cli ) <EOL> assert result . output == '<STR_LIT>' <EOL> assert not result . exception <EOL> result = runner . invoke ( cli , color = True ) <EOL> assert result . output == click . style ( '<STR_LIT>' , fg = '<STR_LIT>' ) + '<STR_LIT:\n>' <EOL> assert not result . exception <EOL> def test_with_color_but_pause_not_blocking ( ) : <EOL> @ click . command ( ) <EOL> def cli ( ) : <EOL> click . pause ( ) <EOL> runner = CliRunner ( ) <EOL> result = runner . invoke ( cli , color = True ) <EOL> assert not result . exception <EOL> assert result . output == '<STR_LIT>' <EOL> def test_exit_code_and_output_from_sys_exit ( ) : <EOL> @ click . command ( ) <EOL> def cli_string ( ) : <EOL> click . echo ( '<STR_LIT>' ) <EOL> sys . exit ( '<STR_LIT:error>' ) <EOL> @ click . command ( ) <EOL> def cli_int ( ) : <EOL> click . echo ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> @ click . command ( ) <EOL> def cli_float ( ) : <EOL> click . echo ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1.0> ) <EOL> @ click . command ( ) <EOL> def cli_no_error ( ) : <EOL> click . echo ( '<STR_LIT>' ) <EOL> runner = CliRunner ( ) <EOL> result = runner . invoke ( cli_string ) <EOL> assert result . exit_code == <NUM_LIT:1> <EOL> assert result . output == '<STR_LIT>' <EOL> result = runner . invoke ( cli_int ) <EOL> assert result . exit_code == <NUM_LIT:1> <EOL> assert result . output == '<STR_LIT>' <EOL> result = runner . invoke ( cli_float ) <EOL> assert result . exit_code == <NUM_LIT:1> <EOL> assert result . output == '<STR_LIT>' <EOL> result = runner . invoke ( cli_no_error ) <EOL> assert result . exit_code == <NUM_LIT:0> <EOL> assert result . output == '<STR_LIT>' </s>
