def test_show_help_for_help(self): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() app = application.App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' app.options = mock.Mock() help_cmd = help.HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args([]) try: help_cmd.run(parsed_args) except SystemExit: pass help_text = stdout.getvalue() basecommand = os.path.split(sys.argv[0])[1] self.assertIn('usage: %s [--version]' % basecommand, help_text) self.assertIn('optional arguments:\n --version', help_text) expected = (' one Test command.\n' ' three word command Test command.\n') self.assertIn(expected, help_text)
def test_find_legacy(self): mgr = utils.TestCommandManager(None) mgr.add_command('new name', FauxCommand) mgr.add_legacy_command('old name', 'new name') cmd, name, remaining = mgr.find_command(['old', 'name']) self.assertIs(cmd, FauxCommand) self.assertEqual(name, 'old name')
def test_find_unknown_command(): mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) try: mgr.find_command(['a', 'b']) except ValueError as err: assert "['a', 'b']" in ('%s' % err) else: assert False, 'expected a failure'
def test_legacy_overrides_new(self): mgr = utils.TestCommandManager(None) mgr.add_command('cmd1', FauxCommand) mgr.add_command('cmd2', FauxCommand2) mgr.add_legacy_command('cmd2', 'cmd1') cmd, name, remaining = mgr.find_command(['cmd2']) self.assertIs(cmd, FauxCommand) self.assertEqual(name, 'cmd2')
def test_no_command(self): mgr = utils.TestCommandManager(None) mgr.add_legacy_command('cmd2', 'cmd1') self.assertRaises( ValueError, mgr.find_command, ['cmd2'], )
def test(self): mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) try: mgr.find_command(['a', 'b']) except ValueError as err: self.assertIn("['a', 'b']", str(err)) else: self.fail('expected a failure')
def test(self): mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) try: mgr.find_command(self.argv) except ValueError as err: # make sure err include 'a' when ['a', '-b'] self.assertIn(self.argv[0], str(err)) self.assertIn('-b', str(err)) else: self.fail('expected a failure')
def test_lookup_with_remainder(): def check(mgr, argv): cmd, name, remaining = mgr.find_command(argv) assert cmd assert remaining == ['--opt'] mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) for expected in [['one', '--opt'], ['two', 'words', '--opt'], ['three', 'word', 'command', '--opt'], ]: yield check, mgr, expected return
def test_lookup_and_find(): def check(mgr, argv): cmd, name, remaining = mgr.find_command(argv) assert cmd assert name == ' '.join(argv) assert not remaining mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) for expected in [['one'], ['two', 'words'], ['three', 'word', 'command'], ]: yield check, mgr, expected return
def test_intersected_commands(): def foo(arg): pass def foo_bar(): pass mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) mgr.add_command('foo', foo) mgr.add_command('foo bar', foo_bar) assert mgr.find_command(['foo', 'bar'])[0] is foo_bar assert mgr.find_command(['foo', 'arg0'])[0] is foo
def test_list_matching_commands(): stdout = StringIO() app = App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' try: assert app.run(['t']) == 2 except SystemExit: pass output = stdout.getvalue() assert "test: 't' is not a test command. See 'test --help'." in output assert 'Did you mean one of these?' in output assert 'three word command\n two words\n' in output
def test_list_matching_commands(self): stdout = StringIO() app = application.App('testing', '1', test_utils.TestCommandManager( test_utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' try: self.assertEqual(2, app.run(['t'])) except SystemExit: pass output = stdout.getvalue() self.assertIn("test: 't' is not a test command. See 'test --help'.", output) self.assertIn('Did you mean one of these?', output) self.assertIn('three word command\n two words\n', output)
def test_find_invalid_command(): mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) def check_one(argv): try: mgr.find_command(argv) except ValueError as err: assert '-b' in ('%s' % err) else: assert False, 'expected a failure' for argv in [ ['a', '-b'], ['-b'], ]: yield check_one, argv
def test_intersected_commands(self): def foo(arg): pass def foo_bar(): pass mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) mgr.add_command('foo', foo) mgr.add_command('foo bar', foo_bar) self.assertIs(foo_bar, mgr.find_command(['foo', 'bar'])[0]) self.assertIs( foo, mgr.find_command(['foo', 'arg0'])[0], )
def test_show_help_for_command(self): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = io.StringIO() app = application.App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' help_cmd = help.HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args(['one']) try: help_cmd.run(parsed_args) except help.HelpExit: pass self.assertEqual('TestParser', stdout.getvalue())
def test_list_deprecated_commands(self): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = io.StringIO() app = application.App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' try: app.run(['--help']) except help.HelpExit: pass help_output = stdout.getvalue() self.assertIn('two words', help_output) self.assertIn('three word command', help_output) self.assertNotIn('old cmd', help_output)
def test_list_matching_commands_no_match(self): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = io.StringIO() app = application.App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' help_cmd = help.HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args(['z']) self.assertRaises( ValueError, help_cmd.run, parsed_args, )
def test_list_deprecated_commands(): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() app = App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' try: app.run(['--help']) except SystemExit: pass help_output = stdout.getvalue() assert 'two words' in help_output assert 'three word command' in help_output assert 'old cmd' not in help_output
def test_list_matching_commands(self): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = io.StringIO() app = application.App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' help_cmd = help.HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args(['t']) try: help_cmd.run(parsed_args) except help.HelpExit: pass help_output = stdout.getvalue() self.assertIn('Command "t" matches:', help_output) self.assertIn('three word command\n two words\n', help_output)
def test_show_help_for_help(): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() app = App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' help_cmd = HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args([]) try: help_cmd.run(parsed_args) except SystemExit: pass help_text = stdout.getvalue() assert 'usage: test help [-h]' in help_text
def test_show_help_with_ep_load_fail(mock_load): stdout = StringIO() app = application.App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' app.options = mock.Mock() app.options.debug = False help_cmd = help.HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args([]) try: help_cmd.run(parsed_args) except SystemExit: pass help_output = stdout.getvalue() assert 'Commands:' in help_output assert 'Could not load' in help_output assert 'Exception: Could not load EntryPoint' not in help_output
def test_show_help_print_exc_with_ep_load_fail(self, mock_load): stdout = io.StringIO() app = application.App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' app.options = mock.Mock() app.options.debug = True help_cmd = help.HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args([]) try: help_cmd.run(parsed_args) except help.HelpExit: pass help_output = stdout.getvalue() self.assertIn('Commands:', help_output) self.assertIn('Could not load', help_output) self.assertIn('Exception: Could not load EntryPoint', help_output)
def test_list_matching_commands_no_match(): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() app = application.App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' help_cmd = help.HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args(['z']) try: help_cmd.run(parsed_args) except SystemExit: pass except ValueError: pass else: assert False, 'Should have seen a ValueError'
def test_list_matching_commands(): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() app = App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' help_cmd = HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args(['t']) try: help_cmd.run(parsed_args) except SystemExit: pass help_output = stdout.getvalue() assert 'Command "t" matches:' in help_output assert 'two' in help_output assert 'three' in help_output
def test_show_help_for_help(): # FIXME(dhellmann): Are commands tied too closely to the app? Or # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() app = App('testing', '1', utils.TestCommandManager(utils.TEST_NAMESPACE), stdout=stdout) app.NAME = 'test' help_cmd = HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') parsed_args = parser.parse_args([]) try: help_cmd.run(parsed_args) except SystemExit: pass help_text = stdout.getvalue() basecommand = os.path.split(sys.argv[0])[1] assert 'usage: %s [--version]' % basecommand in help_text assert 'optional arguments:\n --version' in help_text assert 'one \n three word command \n' in help_text
def test_add(self): mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) mock_cmd = mock.Mock() mgr.add_command('mock', mock_cmd) found_cmd, name, args = mgr.find_command(['mock']) self.assertIs(mock_cmd, found_cmd)
def test(self): mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) cmd, name, remaining = mgr.find_command(self.argv) self.assertTrue(cmd) self.assertEqual(['--opt'], remaining)
def test(self): mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) cmd, name, remaining = mgr.find_command(self.argv) self.assertTrue(cmd) self.assertEqual(' '.join(self.argv), name) self.assertFalse(remaining)