diff --git a/pubs/pubs_cmd.py b/pubs/pubs_cmd.py index e849f69..e9c8b46 100644 --- a/pubs/pubs_cmd.py +++ b/pubs/pubs_cmd.py @@ -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): diff --git a/tests/test_usecase.py b/tests/test_usecase.py index ce870cf..7ce85fc 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -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):