Example #1
0
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'
    app.options = mock.Mock()
    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
    expected = ('  one            Test command.\n'
                '  three word command  Test command.\n')
    assert expected in help_text
Example #2
0
def test_fuzzy_no_prefix():
    # search by distance, no common prefix with any command
    cmd_mgr = CommandManager('cliff.fuzzy')
    app = App('test', '1.0', cmd_mgr)
    cmd_mgr.add_command('user', utils.TestCommand)
    matches = app.get_fuzzy_matches('uesr')
    assert matches == ['user']
Example #3
0
def test_fuzzy_no_prefix():
    # search by distance, no common prefix with any command
    cmd_mgr = CommandManager('cliff.fuzzy')
    app = App('test', '1.0', cmd_mgr)
    cmd_mgr.add_command('user', utils.TestCommand)
    matches = app.get_fuzzy_matches('uesr')
    assert matches == ['user']
Example #4
0
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'
    app.options = mock.Mock()
    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
    expected = (
        '  one            Test command.\n'
        '  three word command  Test command.\n'
    )
    assert expected in help_text
Example #5
0
def test_fuzzy_same_distance():
    # searched string has the same distance to all commands
    cmd_mgr = CommandManager('cliff.fuzzy')
    app = App('test', '1.0', cmd_mgr)
    cmd_mgr.add_command('user', utils.TestCommand)
    for cmd in cmd_mgr.commands.keys():
        assert damerau_levenshtein('node', cmd, COST) == 8
    matches = app.get_fuzzy_matches('node')
    assert matches == ['complete', 'help', 'user']
Example #6
0
def test_fuzzy_common_prefix():
    # searched string is a prefix of all commands
    cmd_mgr = CommandManager('cliff.fuzzy')
    app = App('test', '1.0', cmd_mgr)
    cmd_mgr.commands = {}
    cmd_mgr.add_command('user list', utils.TestCommand)
    cmd_mgr.add_command('user show', utils.TestCommand)
    matches = app.get_fuzzy_matches('user')
    assert matches == ['user list', 'user show']
Example #7
0
 def __init__(self):
     App.__init__(
         self,
         description='panoramix client',
         version='0.1',
         command_manager=MyCommandManager('panoramix.commands',
                                          convert_underscores=True),
         deferred_help=True,
     )
Example #8
0
def test_fuzzy_common_prefix():
    # searched string is a prefix of all commands
    cmd_mgr = CommandManager('cliff.fuzzy')
    app = App('test', '1.0', cmd_mgr)
    cmd_mgr.commands = {}
    cmd_mgr.add_command('user list', utils.TestCommand)
    cmd_mgr.add_command('user show', utils.TestCommand)
    matches = app.get_fuzzy_matches('user')
    assert matches == ['user list', 'user show']
Example #9
0
def test_fuzzy_same_distance():
    # searched string has the same distance to all commands
    cmd_mgr = CommandManager('cliff.fuzzy')
    app = App('test', '1.0', cmd_mgr)
    cmd_mgr.add_command('user', utils.TestCommand)
    for cmd in cmd_mgr.commands.keys():
        assert damerau_levenshtein('node', cmd, COST) == 8
    matches = app.get_fuzzy_matches('node')
    assert matches == ['complete', 'help', 'user']
Example #10
0
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
Example #11
0
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
Example #12
0
def test_show_help_for_command():
    # 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', TestCommandManager('cliff.test'), stdout=stdout)
    app.NAME = 'test'
    help_cmd = HelpCommand(app, mock.Mock())
    parser = help_cmd.get_parser('test')
    parsed_args = parser.parse_args(['one'])
    try:
        help_cmd.run(parsed_args)
    except SystemExit:
        pass
    assert stdout.getvalue() == 'TestParser'
Example #13
0
def make_app(**kwargs):
    cmd_mgr = CommandManager('cliff.tests')

    # Register a command that succeeds
    command = mock.MagicMock(spec=Command)
    command_inst = mock.MagicMock(spec=Command)
    command_inst.run.return_value = 0
    command.return_value = command_inst
    cmd_mgr.add_command('mock', command)

    # Register a command that fails
    err_command = mock.Mock(name='err_command', spec=Command)
    err_command_inst = mock.Mock(spec=Command)
    err_command_inst.run = mock.Mock(
        side_effect=RuntimeError('test exception'))
    err_command.return_value = err_command_inst
    cmd_mgr.add_command('error', err_command)

    app = App(
        'testing interactive mode',
        '1',
        cmd_mgr,
        stderr=mock.Mock(),  # suppress warning messages
        **kwargs)
    return app, command
Example #14
0
 def build_option_parser(self, description, version, argparse_kwargs=None):
     parser = CliffApp.build_option_parser(self, description, version, argparse_kwargs)
     for k, v in self.args_def.iteritems():
         parser.add_argument(
             *((['-{}'.format(v['short'])] if 'short' in v else []) + ['--{}'.format(k)]),
             **v.get('info', {})
         )
     return parser
Example #15
0
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
Example #16
0
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
Example #17
0
 def build_option_parser(self, description, version):
     parser = App.build_option_parser(self, description, version)
     parser.add_argument(
         '-c', '--control',
         dest='control',
         default='ipc://control.sock',
         help='Endpoint for nrv control.',
         )
     return parser
Example #18
0
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 = App('testing', '1', TestCommandManager('cliff.test'), stdout=stdout)
    app.NAME = 'test'
    help_cmd = 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'
Example #19
0
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', TestCommandManager('cliff.test'), 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
Example #20
0
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
Example #21
0
def test_show_help_print_exc_with_ep_load_fail(mock_load):
    stdout = StringIO()
    app = App('testing', '1',
              utils.TestCommandManager(utils.TEST_NAMESPACE),
              stdout=stdout)
    app.NAME = 'test'
    app.options = mock.Mock()
    app.options.debug = True
    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_output = stdout.getvalue()
    assert 'Commands:' in help_output
    assert 'Could not load' in help_output
    assert 'Exception: Could not load EntryPoint' in help_output
Example #22
0
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
Example #23
0
def test_show_help_print_exc_with_ep_load_fail(mock_load):
    stdout = StringIO()
    app = App('testing',
              '1',
              utils.TestCommandManager(utils.TEST_NAMESPACE),
              stdout=stdout)
    app.NAME = 'test'
    app.options = mock.Mock()
    app.options.debug = True
    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_output = stdout.getvalue()
    assert 'Commands:' in help_output
    assert 'Could not load' in help_output
    assert 'Exception: Could not load EntryPoint' in help_output
Example #24
0
    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument('name')
        return parser

    def take_action(self, args):
        print('Bye {}'.format(args.name))


# Here we create a CommandManager and App directly
# To customize the CLI you have to sub-class those!

command_manager = CommandManager('greet')
command_manager.add_command('hello', Hello)
command_manager.add_command('bye', Bye)

app = App(
    description='Twitter command line application',
    version='0.1',
    command_manager=command_manager,
)

# Application needs to be run with command line to parse.
if __name__ == '__main__':
    app = App(
        description='Command line interface for the greet package',
        version='0.1',
        command_manager=command_manager,
    )
    sys.exit(app.run(sys.argv[1:]))
def given_complete_command():
    cmd_mgr = CommandManager('cliff.tests')
    app = App('testing', '1', cmd_mgr, stdout=FakeStdout())
    sot = complete.CompleteCommand(app, mock.Mock())
    cmd_mgr.add_command('complete', complete.CompleteCommand)
    return sot, app, cmd_mgr
Example #26
0
def test_fuzzy_no_commands():
    cmd_mgr = CommandManager('cliff.fuzzy')
    app = App('test', '1.0', cmd_mgr)
    cmd_mgr.commands = {}
    matches = app.get_fuzzy_matches('foo')
    assert matches == []
Example #27
0
def main(argv=sys.argv[1:]):
    myapp = App()
    return myapp.run(argv)
Example #28
0
def test_fuzzy_no_commands():
    cmd_mgr = CommandManager('cliff.fuzzy')
    app = App('test', '1.0', cmd_mgr)
    cmd_mgr.commands = {}
    matches = app.get_fuzzy_matches('foo')
    assert matches == []
Example #29
0
def main(argv=sys.argv[2:]):
    app = App()
    return app.run(argv)
Example #30
0
def main(argv=None):
    configure_logger()
    return App(description="Shotgun CLI",
               version=shotgun.__version__,
               command_manager=CommandManager('shotgun')).run(argv)
Example #31
0
 def __init__(self, args_def=None, stdin=None, stdout=None, stderr=None,
              interactive_app_factory=InteractiveApp):
     self.__args_def = args_def or {}
     CliffApp.__init__(self, 'djehuty', __version__, CommandManager('djehuty.commands'),
                       stdin=stdin, stdout=stdout, stderr=stderr,
                       interactive_app_factory=interactive_app_factory)