Esempio n. 1
0
def make_app(**kwargs):
    cmd_mgr = commandmanager.CommandManager('cliff.tests')

    # Register a command that succeeds
    command = mock.MagicMock(spec=c_cmd.Command)
    command_inst = mock.MagicMock(spec=c_cmd.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=c_cmd.Command)
    err_command_inst = mock.Mock(spec=c_cmd.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 = application.App('testing interactive mode',
                          '1',
                          cmd_mgr,
                          stderr=mock.Mock(),  # suppress warning messages
                          **kwargs
                          )
    return app, command
Esempio n. 2
0
 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)
Esempio n. 3
0
 def test_fuzzy_no_prefix(self):
     # search by distance, no common prefix with any command
     cmd_mgr = commandmanager.CommandManager('cliff.fuzzy')
     app = application.App('test', '1.0', cmd_mgr)
     cmd_mgr.add_command('user', test_utils.TestCommand)
     matches = app.get_fuzzy_matches('uesr')
     self.assertEqual(['user'], matches)
Esempio n. 4
0
def main(argv=sys.argv[1:]):
    myapp = app.App(
        description='',
        version='',
        command_manager=commandmanager.CommandManager('manager'),
        deferred_help=True,
    )
    return myapp.run(argv)
Esempio n. 5
0
 def test_fuzzy_common_prefix(self):
     # searched string is a prefix of all commands
     cmd_mgr = commandmanager.CommandManager('cliff.fuzzy')
     app = application.App('test', '1.0', cmd_mgr)
     cmd_mgr.commands = {}
     cmd_mgr.add_command('user list', test_utils.TestCommand)
     cmd_mgr.add_command('user show', test_utils.TestCommand)
     matches = app.get_fuzzy_matches('user')
     self.assertEqual(['user list', 'user show'], matches)
Esempio n. 6
0
def test_fuzzy_same_distance():
    # searched string has the same distance to all commands
    cmd_mgr = commandmanager.CommandManager('cliff.fuzzy')
    app = application.App('test', '1.0', cmd_mgr)
    cmd_mgr.add_command('user', test_utils.TestCommand)
    for cmd in cmd_mgr.commands.keys():
        assert utils.damerau_levenshtein('node', cmd, utils.COST) == 8
    matches = app.get_fuzzy_matches('node')
    assert matches == ['complete', 'help', 'user']
Esempio n. 7
0
def test_list_matching_commands_no_match():
    stdout = StringIO()
    app = application.App('testing',
                          '1',
                          TestCommandManager(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'])
    with pytest.raises(ValueError):
        help_cmd.run(parsed_args)
Esempio n. 8
0
    def test_io_streams(self):
        cmd_mgr = commandmanager.CommandManager('cliff.tests')
        io = mock.Mock()

        app = application.App('no io streams', 1, cmd_mgr)
        self.assertIs(sys.stdin, app.stdin)
        self.assertIs(sys.stdout, app.stdout)
        self.assertIs(sys.stderr, app.stderr)

        app = application.App('with stdin io stream', 1, cmd_mgr, stdin=io)
        self.assertIs(io, app.stdin)
        self.assertIs(sys.stdout, app.stdout)
        self.assertIs(sys.stderr, app.stderr)

        app = application.App('with stdout io stream', 1, cmd_mgr, stdout=io)
        self.assertIs(sys.stdin, app.stdin)
        self.assertIs(io, app.stdout)
        self.assertIs(sys.stderr, app.stderr)

        app = application.App('with stderr io stream', 1, cmd_mgr, stderr=io)
        self.assertIs(sys.stdin, app.stdin)
        self.assertIs(sys.stdout, app.stdout)
        self.assertIs(io, app.stderr)
Esempio n. 9
0
def test_list_deprecated_commands():
    stdout = StringIO()
    app = application.App('testing',
                          '1',
                          TestCommandManager(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
Esempio n. 10
0
def test_list_matching_commands():
    stdout = StringIO()
    app = application.App('testing', '1',
                          test_utils.TestCommandManager(
                            test_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
Esempio n. 11
0
def test_show_help_for_command():
    stdout = StringIO()
    app = application.App('testing',
                          '1',
                          TestCommandManager(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 SystemExit:
        pass
    assert 'TestParser' == stdout.getvalue()
Esempio n. 12
0
 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)
Esempio n. 13
0
 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)
Esempio n. 14
0
def test_list_matching_commands():
    stdout = StringIO()
    app = application.App('testing',
                          '1',
                          TestCommandManager(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 SystemExit:
        pass
    help_output = stdout.getvalue()
    assert 'Command "t" matches:' in help_output
    assert 'three word command\n  two words\n' in help_output
Esempio n. 15
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())
Esempio n. 16
0
 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,
     )
Esempio n. 17
0
 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)
Esempio n. 18
0
 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)
Esempio n. 19
0
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
Esempio n. 20
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 = 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'
Esempio n. 21
0
def make_app(**kwargs):
    cmd_mgr = commandmanager.CommandManager('cliff.tests')

    # Register a command that succeeds
    command = mock.MagicMock(spec=c_cmd.Command)
    command_inst = mock.MagicMock(spec=c_cmd.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=c_cmd.Command)
    err_command_inst = mock.Mock(spec=c_cmd.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)

    # Register a command that is interrrupted
    interrupt_command = mock.Mock(name='interrupt_command', spec=c_cmd.Command)
    interrupt_command_inst = mock.Mock(spec=c_cmd.Command)
    interrupt_command_inst.run = mock.Mock(side_effect=KeyboardInterrupt)
    interrupt_command.return_value = interrupt_command_inst
    cmd_mgr.add_command('interrupt', interrupt_command)

    # Register a command that is interrrupted by a broken pipe
    pipeclose_command = mock.Mock(name='pipeclose_command', spec=c_cmd.Command)
    pipeclose_command_inst = mock.Mock(spec=c_cmd.Command)
    pipeclose_command_inst.run = mock.Mock(side_effect=BrokenPipeError)
    pipeclose_command.return_value = pipeclose_command_inst
    cmd_mgr.add_command('pipe-close', pipeclose_command)

    app = application.App(
        'testing interactive mode',
        '1',
        cmd_mgr,
        stderr=mock.Mock(),  # suppress warning messages
        **kwargs)
    return app, command
Esempio n. 22
0
def test_show_help_print_exc_with_ep_load_fail(mocker):
    stdout = StringIO()
    mocker.patch('cliff.commandmanager.EntryPointWrapper.load',
                 side_effect=Exception('Could not load EntryPoint'))
    app = application.App('testing',
                          '1',
                          TestCommandManager(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 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
Esempio n. 23
0
def test_show_help_for_help():
    stdout = StringIO()
    app = application.App('testing',
                          '1',
                          TestCommandManager(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]
    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
Esempio n. 24
0
    def test_io_streams(self):
        cmd_mgr = commandmanager.CommandManager('cliff.tests')
        io = mock.Mock()

        if six.PY2:
            stdin_save = sys.stdin
            stdout_save = sys.stdout
            stderr_save = sys.stderr
            encoding = locale.getpreferredencoding() or 'utf-8'

            app = application.App('no io streams', 1, cmd_mgr)
            self.assertIsInstance(app.stdin, codecs.StreamReader)
            self.assertIsInstance(app.stdout, codecs.StreamWriter)
            self.assertIsInstance(app.stderr, codecs.StreamWriter)

            app = application.App('with stdin io stream', 1, cmd_mgr, stdin=io)
            self.assertIs(io, app.stdin)
            self.assertIsInstance(app.stdout, codecs.StreamWriter)
            self.assertIsInstance(app.stderr, codecs.StreamWriter)

            app = application.App('with stdout io stream', 1, cmd_mgr,
                                  stdout=io)
            self.assertIsInstance(app.stdin, codecs.StreamReader)
            self.assertIs(io, app.stdout)
            self.assertIsInstance(app.stderr, codecs.StreamWriter)

            app = application.App('with stderr io stream', 1, cmd_mgr,
                                  stderr=io)
            self.assertIsInstance(app.stdin, codecs.StreamReader)
            self.assertIsInstance(app.stdout, codecs.StreamWriter)
            self.assertIs(io, app.stderr)

            try:
                sys.stdin = codecs.getreader(encoding)(sys.stdin)
                app = application.App(
                    'with wrapped sys.stdin io stream', 1, cmd_mgr)
                self.assertIs(sys.stdin, app.stdin)
                self.assertIsInstance(app.stdout, codecs.StreamWriter)
                self.assertIsInstance(app.stderr, codecs.StreamWriter)
            finally:
                sys.stdin = stdin_save

            try:
                sys.stdout = codecs.getwriter(encoding)(sys.stdout)
                app = application.App('with wrapped stdout io stream', 1,
                                      cmd_mgr)
                self.assertIsInstance(app.stdin, codecs.StreamReader)
                self.assertIs(sys.stdout, app.stdout)
                self.assertIsInstance(app.stderr, codecs.StreamWriter)
            finally:
                sys.stdout = stdout_save

            try:
                sys.stderr = codecs.getwriter(encoding)(sys.stderr)
                app = application.App('with wrapped stderr io stream', 1,
                                      cmd_mgr)
                self.assertIsInstance(app.stdin, codecs.StreamReader)
                self.assertIsInstance(app.stdout, codecs.StreamWriter)
                self.assertIs(sys.stderr, app.stderr)
            finally:
                sys.stderr = stderr_save

        else:
            app = application.App('no io streams', 1, cmd_mgr)
            self.assertIs(sys.stdin, app.stdin)
            self.assertIs(sys.stdout, app.stdout)
            self.assertIs(sys.stderr, app.stderr)

            app = application.App('with stdin io stream', 1, cmd_mgr, stdin=io)
            self.assertIs(io, app.stdin)
            self.assertIs(sys.stdout, app.stdout)
            self.assertIs(sys.stderr, app.stderr)

            app = application.App('with stdout io stream', 1, cmd_mgr,
                                  stdout=io)
            self.assertIs(sys.stdin, app.stdin)
            self.assertIs(io, app.stdout)
            self.assertIs(sys.stderr, app.stderr)

            app = application.App('with stderr io stream', 1, cmd_mgr,
                                  stderr=io)
            self.assertIs(sys.stdin, app.stdin)
            self.assertIs(sys.stdout, app.stdout)
            self.assertIs(io, app.stderr)
Esempio n. 25
0
 def test_fuzzy_no_commands(self):
     cmd_mgr = commandmanager.CommandManager('cliff.fuzzy')
     app = application.App('test', '1.0', cmd_mgr)
     cmd_mgr.commands = {}
     matches = app.get_fuzzy_matches('foo')
     self.assertEqual([], matches)
Esempio n. 26
0
 def given_complete_command(self):
     cmd_mgr = commandmanager.CommandManager('cliff.tests')
     app = application.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