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.
main
Bill Flynn 7 years ago
parent ac3a8d8bf2
commit 66c90c5d43

@ -83,13 +83,17 @@ def execute(raw_args=sys.argv):
# Eventually autocomplete
autocomplete(parser)
# Parse and run appropriate command
# if no command, print help and exit peacefully (as '--help' does)
args = parser.parse_args(remaining_args)
if not args.command:
parser.print_help()
else:
args.prog = "pubs" # FIXME?
args.func(conf, args)
sys.exit(0)
args.prog = "pubs" # FIXME?
args.func(conf, args)
except Exception as e:
if not uis.get_ui().handle_exception(e):

@ -29,7 +29,7 @@ PRINT_OUTPUT = False
CAPTURE_OUTPUT = True
class FakeSystemExit(Exception):
class FakeSystemExit(SystemExit):
"""\
SystemExit exceptions are replaced by FakeSystemExit in the execute_cmds()
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()
if sys.version_info.major == 2:
# using six to avoid a SyntaxError in Python 3.x
six.reraise(FakeSystemExit, exc, tb)
six.reraise(FakeSystemExit, *exc.args, exc_traceback=tb)
else:
raise FakeSystemExit(exc).with_traceback(tb)
raise FakeSystemExit(*exc.args).with_traceback(tb)
def tearDown(self):
pass
@ -187,6 +187,26 @@ class URLContentTestCase(DataCommandTestCase):
# 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):
def test_init(self):

Loading…
Cancel
Save