Merge pull request #363 from torfuspolymorphus/fix_362
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix issue #362.
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 8056132, 807c5df · Browse files · 3 files·+52 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+52 added−1 removed
- tests:+43 added−0 removed
- other:+9 added−1 removed
Provenance
- Commit
189d616- Tree
83f5a3e- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
8056132- Parent 2
807c5df
3 files changed+52 added−1 removed
modified:click/testing.py+7 added-1 removed
268 unmodified lines269 exc_info = None270 with self.isolation(input=input, env=env, color=color) as out:271 exception = None272 exit_code = 0273+ error_message = ''274 275 try:276 cli.main(args=args or (),277 prog_name=self.get_default_prog_name(cli), **extra)278 except SystemExit as e:279 if e.code != 0:280 exception = e281 exit_code = e.code282+ if not isinstance(exit_code, int):283+ error_message = '%s\n' % exit_code284+ exit_code = 1285 exc_info = sys.exc_info()286 except Exception as e:287 if not catch_exceptions:288 raise1 unmodified line290 exit_code = -1291 exc_info = sys.exc_info()292 finally:293 sys.stdout.flush()294+ if not PY2:295+ error_message = error_message.encode(self.charset)290- output = out.getvalue()296+ output = out.getvalue() + error_message297 298 return Result(runner=self,299 output_bytes=output,300 exit_code=exit_code,19 unmodified linesmodified:tests/test_testing.py+43 added
1+import sys2+3 import pytest4 import click5 6 from click.testing import CliRunner134 unmodified lines141 runner = CliRunner()142 result = runner.invoke(cli, color=True)143 assert not result.exception144 assert result.output == ''145+146+147+def test_exit_code_and_output_from_sys_exit():148+ # See issue #362149+ @click.command()150+ def cli_string():151+ click.echo('hello world')152+ sys.exit('error')153+154+ @click.command()155+ def cli_int():156+ click.echo('hello world')157+ sys.exit(1)158+159+ @click.command()160+ def cli_float():161+ click.echo('hello world')162+ sys.exit(1.0)163+164+ @click.command()165+ def cli_no_error():166+ click.echo('hello world')167+168+ runner = CliRunner()169+170+ result = runner.invoke(cli_string)171+ assert result.exit_code == 1172+ assert result.output == 'hello world\nerror\n'173+174+ result = runner.invoke(cli_int)175+ assert result.exit_code == 1176+ assert result.output == 'hello world\n'177+178+ result = runner.invoke(cli_float)179+ assert result.exit_code == 1180+ assert result.output == 'hello world\n1.0\n'181+182+ result = runner.invoke(cli_no_error)183+ assert result.exit_code == 0184+ assert result.output == 'hello world\n'185+modified:CHANGES+2 added
10 unmodified lines11 - Removed various deprecated functionality.12 - Atomic files now only accept the `w` mode.13 - Change the usage part of help output for very long commands to wrap14 their arguments onto the next line, indented by 4 spaces.15+- Fix a bug where return code and error messages were incorrect when16+ using ``CliRunner``.17 18 Version 4.119 -----------20 219 unmodified lines