Added unittest to cover new behavior pubs
Unittest just checks that both `pubs` and `pubs --help` raise a `SystemExit` exception with error code 0. Due to how argparse handles the `--help` keyword, this is the best way I could think to provide test coverage without heavily modifying the parser structure or the unittest infrastructure. To ensure the `pubs` matches the behavior of `pubs --help`, it now raises the same `SystemExit(0)` exception via `sys.exit(0)`. And in order to catch it in the unittest, I had to modify the `FakeSystemExit` behavior slightly.
This commit is contained in:
parent
ac3a8d8bf2
commit
66c90c5d43
@ -83,13 +83,17 @@ def execute(raw_args=sys.argv):
|
|||||||
|
|
||||||
# Eventually autocomplete
|
# Eventually autocomplete
|
||||||
autocomplete(parser)
|
autocomplete(parser)
|
||||||
|
|
||||||
# Parse and run appropriate command
|
# Parse and run appropriate command
|
||||||
|
# if no command, print help and exit peacefully (as '--help' does)
|
||||||
args = parser.parse_args(remaining_args)
|
args = parser.parse_args(remaining_args)
|
||||||
|
|
||||||
if not args.command:
|
if not args.command:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
else:
|
sys.exit(0)
|
||||||
args.prog = "pubs" # FIXME?
|
|
||||||
args.func(conf, args)
|
args.prog = "pubs" # FIXME?
|
||||||
|
args.func(conf, args)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if not uis.get_ui().handle_exception(e):
|
if not uis.get_ui().handle_exception(e):
|
||||||
|
@ -29,7 +29,7 @@ PRINT_OUTPUT = False
|
|||||||
CAPTURE_OUTPUT = True
|
CAPTURE_OUTPUT = True
|
||||||
|
|
||||||
|
|
||||||
class FakeSystemExit(Exception):
|
class FakeSystemExit(SystemExit):
|
||||||
"""\
|
"""\
|
||||||
SystemExit exceptions are replaced by FakeSystemExit in the execute_cmds()
|
SystemExit exceptions are replaced by FakeSystemExit in the execute_cmds()
|
||||||
function, so they can be catched by ExpectedFailure tests in Python 2.x.
|
function, so they can be catched by ExpectedFailure tests in Python 2.x.
|
||||||
@ -131,9 +131,9 @@ class CommandTestCase(fake_env.TestFakeFs):
|
|||||||
exc_class, exc, tb = sys.exc_info()
|
exc_class, exc, tb = sys.exc_info()
|
||||||
if sys.version_info.major == 2:
|
if sys.version_info.major == 2:
|
||||||
# using six to avoid a SyntaxError in Python 3.x
|
# using six to avoid a SyntaxError in Python 3.x
|
||||||
six.reraise(FakeSystemExit, exc, tb)
|
six.reraise(FakeSystemExit, *exc.args, exc_traceback=tb)
|
||||||
else:
|
else:
|
||||||
raise FakeSystemExit(exc).with_traceback(tb)
|
raise FakeSystemExit(*exc.args).with_traceback(tb)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
pass
|
pass
|
||||||
@ -187,6 +187,26 @@ class URLContentTestCase(DataCommandTestCase):
|
|||||||
|
|
||||||
# Actual tests
|
# Actual tests
|
||||||
|
|
||||||
|
class TestAlone(CommandTestCase):
|
||||||
|
|
||||||
|
def test_alone_prints_help(self):
|
||||||
|
# capturing the output of `pubs --help` is difficult because argparse
|
||||||
|
# raises as SystemExit(0) after calling `print_help`, and this gets
|
||||||
|
# caught so no output is captured. so comparing outputs of `pubs` and
|
||||||
|
# `pubs --help` isn't too easy unless substantially reorganization of
|
||||||
|
# the parser and testing context is made. instead, the exit codes of
|
||||||
|
# the two usecases are compared.
|
||||||
|
self.execute_cmds(['pubs init'])
|
||||||
|
|
||||||
|
with self.assertRaises(FakeSystemExit) as cm1:
|
||||||
|
self.execute_cmds(['pubs'])
|
||||||
|
|
||||||
|
with self.assertRaises(FakeSystemExit) as cm2:
|
||||||
|
self.execute_cmds(['pubs --help'])
|
||||||
|
|
||||||
|
self.assertEqual(cm1.exception.code, cm2.exception.code, 0)
|
||||||
|
|
||||||
|
|
||||||
class TestInit(CommandTestCase):
|
class TestInit(CommandTestCase):
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user