Skip to content

Commit

Permalink
fix exit_code for click.testing.CliRunner.invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
timfeirg committed Feb 23, 2017
1 parent 8d9dd46 commit 1e8c8af
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ build
docs/_build
click.egg-info
.tox
.cache
16 changes: 11 additions & 5 deletions click/testing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import contextlib
import os
import sys
import shlex
import shutil
import sys
import tempfile
import contextlib
import shlex

from ._compat import iteritems, PY2, string_types

Expand Down Expand Up @@ -197,6 +197,7 @@ def _getchar(echo):
return char

default_color = color

def should_strip_ansi(stream=None, color=None):
if color is None:
return not default_color
Expand Down Expand Up @@ -292,14 +293,19 @@ def invoke(self, cli, args=None, input=None, env=None,

exit_code = e.code
if not isinstance(exit_code, int):
if exit_code is None:
exit_code = 0
else:
exit_code = 1

sys.stdout.write(str(exit_code))
sys.stdout.write('\n')
exit_code = 1

except Exception as e:
if not catch_exceptions:
raise
exception = e
exit_code = -1
exit_code = 1
exc_info = sys.exc_info()
finally:
sys.stdout.flush()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ def cli_no_error():

result = runner.invoke(cli_string)
assert result.exit_code == 1
assert result.output == 'hello world\nerror\n'
assert result.output == 'hello world\n1\n'

result = runner.invoke(cli_int)
assert result.exit_code == 1
assert result.output == 'hello world\n'

result = runner.invoke(cli_float)
assert result.exit_code == 1
assert result.output == 'hello world\n1.0\n'
assert result.output == 'hello world\n1\n'

result = runner.invoke(cli_no_error)
assert result.exit_code == 0
Expand Down

0 comments on commit 1e8c8af

Please sign in to comment.