Exemplo n.º 1
0
 def test_post_register(self):
     manager = ExecutionManager()
     with mock.patch.object(CompletionCommand, 'get_env_var_name') as m:
         m.return_value = 'FOO_AUTO_COMPLETE'
         manager.register('completion', CompletionCommand)
     self.assertTrue(manager.completion)
     self.assertEqual(manager.completion_env_var_name, 'FOO_AUTO_COMPLETE')
Exemplo n.º 2
0
 def test_post_register(self):
     manager = ExecutionManager()
     with mock.patch.object(CompletionCommand, 'get_env_var_name') as m:
         m.return_value = 'FOO_AUTO_COMPLETE'
         manager.register('completion', CompletionCommand)
     self.assertTrue(manager.completion)
     self.assertEqual(manager.completion_env_var_name, 'FOO_AUTO_COMPLETE')
Exemplo n.º 3
0
    def test_post_register_hooks(self):
        Command = type('Command', (BaseCommand,), {'args': ['foo', 'bar']})

        class Command(BaseCommand):
            def post_register(self, manager):
                manager.completion = True

        manager = ExecutionManager()
        self.assertFalse(manager.completion)
        manager.register('completion', Command)
        self.assertTrue(manager.completion)
Exemplo n.º 4
0
    def test_post_register_hooks(self):
        Command = type('Command', (BaseCommand, ), {'args': ['foo', 'bar']})

        class Command(BaseCommand):
            def post_register(self, manager):
                manager.completion = True

        manager = ExecutionManager()
        self.assertFalse(manager.completion)
        manager.register('completion', Command)
        self.assertTrue(manager.completion)
Exemplo n.º 5
0
class TestAutocomplete(unittest.TestCase):

    def setUp(self):
        self.stream = io.StringIO()
        self.stdout = io.StringIO()
        self.manager = ExecutionManager(stderr=self.stream, stdout=self.stdout)
        self.manager
        Command = type('Command', (BaseCommand,), {})
        CompCommand = type('CompCommand', (CompletionCommand,), {})

        class AddCommand(BaseCommand):
            args = BaseCommand.args + [
                arg('-f', '--force', action='store_true', default=False),
            ]

        self.manager.register('add', AddCommand)
        self.manager.register('annotate', Command)
        self.manager.register('init', Command)
        self.manager.register('completion', CompCommand)
        self.manager.completion = True
        self.manager.completion_env_var_name = 'PROG_AUTO_COMPLETE'

    @mock.patch.object(sys, 'argv', ['prog', 'a'])
    def test_autocomplete_returns_none_if_env_var_not_defined(self):
        with mock.patch.object(os, 'environ', {}):
            self.assertEqual(self.manager.autocomplete(), None)

    @mock.patch.object(sys, 'argv', ['prog', 'a'])
    def test_manager_execute_calls_autocomplete(self):
        self.manager.autocomplete = mock.Mock()
        stream = io.StringIO()
        with mock.patch.object(sys, 'stderr', stream):
            with self.assertRaises(SystemExit):
                self.manager.execute()
        self.manager.autocomplete.assert_called_once_with()

    @mock.patch.object(sys, 'argv', ['prog', 'a'])
    def test_autocomplete_returns_completes_for_subcommands(self):
        os.environ['COMP_WORDS'] = 'prog a'
        os.environ['COMP_CWORD'] = '1'
        stream = io.StringIO()
        with mock.patch.object(sys, 'stderr', stream):
            with self.assertRaises(SystemExit):
                self.manager.execute()
        self.assertEqual(self.stdout.getvalue(), 'add annotate')

    @mock.patch.object(sys, 'argv', ['prog', 'a'])
    def test_autocomplete_returns_none_if_wrong_COMP_CWORD_set(self):
        os.environ['COMP_WORDS'] = 'prog a'
        os.environ['COMP_CWORD'] = '3'
        stream = io.StringIO()
        with mock.patch.object(sys, 'stderr', stream):
            with self.assertRaises(SystemExit):
                self.manager.execute()
        self.assertEqual(self.stdout.getvalue(), '')
Exemplo n.º 6
0
class TestAutocomplete(unittest.TestCase):
    def setUp(self):
        self.stream = io.StringIO()
        self.stdout = io.StringIO()
        self.manager = ExecutionManager(stderr=self.stream, stdout=self.stdout)
        self.manager
        Command = type('Command', (BaseCommand, ), {})
        CompCommand = type('CompCommand', (CompletionCommand, ), {})

        class AddCommand(BaseCommand):
            args = BaseCommand.args + [
                arg('-f', '--force', action='store_true', default=False),
            ]

        self.manager.register('add', AddCommand)
        self.manager.register('annotate', Command)
        self.manager.register('init', Command)
        self.manager.register('completion', CompCommand)
        self.manager.completion = True
        self.manager.completion_env_var_name = 'PROG_AUTO_COMPLETE'

    @mock.patch.object(sys, 'argv', ['prog', 'a'])
    def test_autocomplete_returns_none_if_env_var_not_defined(self):
        with mock.patch.object(os, 'environ', {}):
            self.assertEqual(self.manager.autocomplete(), None)

    @mock.patch.object(sys, 'argv', ['prog', 'a'])
    def test_manager_execute_calls_autocomplete(self):
        self.manager.autocomplete = mock.Mock()
        stream = io.StringIO()
        with mock.patch.object(sys, 'stderr', stream):
            with self.assertRaises(SystemExit):
                self.manager.execute()
        self.manager.autocomplete.assert_called_once_with()

    @mock.patch.object(sys, 'argv', ['prog', 'a'])
    def test_autocomplete_returns_completes_for_subcommands(self):
        os.environ['COMP_WORDS'] = 'prog a'
        os.environ['COMP_CWORD'] = '1'
        stream = io.StringIO()
        with mock.patch.object(sys, 'stderr', stream):
            with self.assertRaises(SystemExit):
                self.manager.execute()
        self.assertEqual(self.stdout.getvalue(), 'add annotate')

    @mock.patch.object(sys, 'argv', ['prog', 'a'])
    def test_autocomplete_returns_none_if_wrong_COMP_CWORD_set(self):
        os.environ['COMP_WORDS'] = 'prog a'
        os.environ['COMP_CWORD'] = '3'
        stream = io.StringIO()
        with mock.patch.object(sys, 'stderr', stream):
            with self.assertRaises(SystemExit):
                self.manager.execute()
        self.assertEqual(self.stdout.getvalue(), '')
Exemplo n.º 7
0
class TestExecutionManager(unittest.TestCase):

    def assertRegistryClassesEqual(self, actual, expected):
        self.assertEqual(list(sorted(actual)), list(sorted(expected)))
        for key in actual:
            self.assertEqual(actual[key].__class__, expected[key],
                "Command class don't match for %r (it's %r but "
                "expected %r)" % (key, actual[key].__class__,
                expected[key]))

    def setUp(self):
        self.manager = ExecutionManager(['foobar'], stderr=StringIO())

    def test_init_prog_name(self):
        self.assertEqual(self.manager.prog_name, 'foobar')

    def test_init_stderr(self):
        manager = ExecutionManager()
        self.assertEqual(manager.stderr, sys.stderr)

    def test_default_argv(self):
        with mock.patch.object(sys, 'argv', ['vcs', 'foo', 'bar']):
            manager = ExecutionManager()
            self.assertEqual(manager.argv, ['foo', 'bar'])

    def test_get_usage(self):
        self.manager.usage = 'foobar baz'
        self.assertEqual(self.manager.get_usage(), 'foobar baz')

    def test_get_parser(self):
        self.manager.usage = 'foo bar'
        parser = self.manager.get_parser()
        self.assertIsInstance(parser, argparse.ArgumentParser)
        self.assertEqual(parser.prog, 'foobar') # argv[0]
        self.assertEqual(parser.usage, 'foo bar')
        self.assertEqual(parser.stream, self.manager.stderr)

    def test_get_parser_calls_setup_parser(self):
        class DummyCommand(BaseCommand):
            pass

        self.manager.register('foo', DummyCommand)
        with mock.patch.object(DummyCommand, 'setup_parser') as setup_parser:
            self.manager.get_parser()
            self.assertTrue(setup_parser.called)

    def test_register(self):
        Command = type('Command', (BaseCommand,), {})
        self.manager.register('foo', Command)
        self.assertRegistryClassesEqual(self.manager.registry, {'foo': Command})
        command = self.manager.registry['foo']
        self.assertEqual(command.manager, self.manager)

    def test_register_raise_if_command_with_same_name_registered(self):
        Command = type('Command', (BaseCommand,), {})
        self.manager.register('foobar', Command)
        with self.assertRaises(AlreadyRegistered):
            self.manager.register('foobar', Command)

    def test_register_respects_force_argument(self):
        Command1 = type('Command', (BaseCommand,), {})
        Command2 = type('Command', (BaseCommand,), {})
        self.manager.register('foobar', Command1)
        self.manager.register('foobar', Command2, force=True)
        self.assertRegistryClassesEqual(self.manager.registry, {
            'foobar': Command2})

    def test_get_commands(self):
        FooCommand = type('FooCommand', (BaseCommand,), {})
        BarCommand = type('BarCommand', (BaseCommand,), {})
        self.manager.register('foo', FooCommand)
        self.manager.register('bar', BarCommand)
        self.assertEqual(list(self.manager.get_commands().keys()), ['bar', 'foo'])
        self.assertRegistryClassesEqual(self.manager.get_commands(), {
            'foo': FooCommand,
            'bar': BarCommand,
        })

    def test_get_commands_to_register(self):
        FooCommand = type('FooCommand', (BaseCommand,), {})
        BarCommand = type('BarCommand', (BaseCommand,), {})

        class Manager(ExecutionManager):

            def get_commands_to_register(self):
                return {
                    'foo': FooCommand,
                    'bar': BarCommand,
                }

        manager = Manager(['foobar'])
        self.assertRegistryClassesEqual(manager.registry, {
            'foo': FooCommand,
            'bar': BarCommand,
        })

    def test_call_command(self):

        class Command(BaseCommand):
            name = 'init'
            handle = mock.Mock()

        self.manager.register('init', Command)
        self.manager.call_command('init')
        self.assertTrue(Command.handle.called)

    def test_called_command_has_prog_name_properly_set(self):

        prog_names = []

        class Command(BaseCommand):
            name = 'init'
            def handle(self, namespace):
                prog_names.append(self.prog_name)

        self.manager.register('init', Command)
        self.manager.call_command('init')
        self.assertEqual(prog_names, ['foobar'])

    def test_call_command_with_args(self):

        class Command(BaseCommand):
            args = [
                arg('-f', '--force', action='store_true', default=False),
            ]
            name = 'add'
            handle = mock.Mock()

        self.manager.register('add', Command)
        self.manager.call_command('add', '-f')
        self.assertTrue(Command.handle.called)
        namespace = Command.handle.call_args[0][0]
        self.assertTrue(namespace.force)

    @mock.patch('monolith.cli.base.sys.stderr')
    def test_call_command_fails(self, stderr):

        class Command(BaseCommand):
            args = [
                arg('-f', '--force', action='store_true', default=False),
            ]
            name = 'add'

            def handle(self, namespace):
                raise CommandError('foo bar baz', 92)

        self.manager.register('add', Command)
        with self.assertRaises(SystemExit):
            self.manager.call_command('add', '-f')

        stderr.write.assert_called_once_with('ERROR: foo bar baz\n')

    def test_execute_calls_handle_command(self):

        class Command(BaseCommand):
            args = [
                arg('-f', '--force', action='store_true', default=False),
            ]
            name = 'add'
            handle = mock.Mock()

        self.manager.register('add', Command)
        with mock.patch.object(sys, 'argv', ['prog', 'add', '-f']):
            self.manager.execute()
        namespace = Command.handle.call_args[0][0]
        Command.handle.assert_called_once_with(namespace)
Exemplo n.º 8
0
class TestExecutionManager(unittest.TestCase):
    def assertRegistryClassesEqual(self, actual, expected):
        self.assertEqual(list(sorted(actual)), list(sorted(expected)))
        for key in actual:
            self.assertEqual(
                actual[key].__class__, expected[key],
                "Command class don't match for %r (it's %r but "
                "expected %r)" % (key, actual[key].__class__, expected[key]))

    def setUp(self):
        self.manager = ExecutionManager(['foobar'], stderr=StringIO())

    def test_init_prog_name(self):
        self.assertEqual(self.manager.prog_name, 'foobar')

    def test_init_stderr(self):
        manager = ExecutionManager()
        self.assertEqual(manager.stderr, sys.stderr)

    def test_default_argv(self):
        with mock.patch.object(sys, 'argv', ['vcs', 'foo', 'bar']):
            manager = ExecutionManager()
            self.assertEqual(manager.argv, ['foo', 'bar'])

    def test_get_usage(self):
        self.manager.usage = 'foobar baz'
        self.assertEqual(self.manager.get_usage(), 'foobar baz')

    def test_get_parser(self):
        self.manager.usage = 'foo bar'
        parser = self.manager.get_parser()
        self.assertIsInstance(parser, argparse.ArgumentParser)
        self.assertEqual(parser.prog, 'foobar')  # argv[0]
        self.assertEqual(parser.usage, 'foo bar')
        self.assertEqual(parser.stream, self.manager.stderr)

    def test_get_parser_calls_setup_parser(self):
        class DummyCommand(BaseCommand):
            pass

        self.manager.register('foo', DummyCommand)
        with mock.patch.object(DummyCommand, 'setup_parser') as setup_parser:
            self.manager.get_parser()
            self.assertTrue(setup_parser.called)

    def test_register(self):
        Command = type('Command', (BaseCommand, ), {})
        self.manager.register('foo', Command)
        self.assertRegistryClassesEqual(self.manager.registry,
                                        {'foo': Command})
        command = self.manager.registry['foo']
        self.assertEqual(command.manager, self.manager)

    def test_register_raise_if_command_with_same_name_registered(self):
        Command = type('Command', (BaseCommand, ), {})
        self.manager.register('foobar', Command)
        with self.assertRaises(AlreadyRegistered):
            self.manager.register('foobar', Command)

    def test_register_respects_force_argument(self):
        Command1 = type('Command', (BaseCommand, ), {})
        Command2 = type('Command', (BaseCommand, ), {})
        self.manager.register('foobar', Command1)
        self.manager.register('foobar', Command2, force=True)
        self.assertRegistryClassesEqual(self.manager.registry,
                                        {'foobar': Command2})

    def test_get_commands(self):
        FooCommand = type('FooCommand', (BaseCommand, ), {})
        BarCommand = type('BarCommand', (BaseCommand, ), {})
        self.manager.register('foo', FooCommand)
        self.manager.register('bar', BarCommand)
        self.assertEqual(list(self.manager.get_commands().keys()),
                         ['bar', 'foo'])
        self.assertRegistryClassesEqual(self.manager.get_commands(), {
            'foo': FooCommand,
            'bar': BarCommand,
        })

    def test_get_commands_to_register(self):
        FooCommand = type('FooCommand', (BaseCommand, ), {})
        BarCommand = type('BarCommand', (BaseCommand, ), {})

        class Manager(ExecutionManager):
            def get_commands_to_register(self):
                return {
                    'foo': FooCommand,
                    'bar': BarCommand,
                }

        manager = Manager(['foobar'])
        self.assertRegistryClassesEqual(manager.registry, {
            'foo': FooCommand,
            'bar': BarCommand,
        })

    def test_call_command(self):
        class Command(BaseCommand):
            name = 'init'
            handle = mock.Mock()

        self.manager.register('init', Command)
        self.manager.call_command('init')
        self.assertTrue(Command.handle.called)

    def test_called_command_has_prog_name_properly_set(self):

        prog_names = []

        class Command(BaseCommand):
            name = 'init'

            def handle(self, namespace):
                prog_names.append(self.prog_name)

        self.manager.register('init', Command)
        self.manager.call_command('init')
        self.assertEqual(prog_names, ['foobar'])

    def test_call_command_with_args(self):
        class Command(BaseCommand):
            args = [
                arg('-f', '--force', action='store_true', default=False),
            ]
            name = 'add'
            handle = mock.Mock()

        self.manager.register('add', Command)
        self.manager.call_command('add', '-f')
        self.assertTrue(Command.handle.called)
        namespace = Command.handle.call_args[0][0]
        self.assertTrue(namespace.force)

    @mock.patch('monolith.cli.base.sys.stderr')
    def test_call_command_fails(self, stderr):
        class Command(BaseCommand):
            args = [
                arg('-f', '--force', action='store_true', default=False),
            ]
            name = 'add'

            def handle(self, namespace):
                raise CommandError('foo bar baz', 92)

        self.manager.register('add', Command)
        with self.assertRaises(SystemExit):
            self.manager.call_command('add', '-f')

        stderr.write.assert_called_once_with('ERROR: foo bar baz\n')

    def test_execute_calls_handle_command(self):
        class Command(BaseCommand):
            args = [
                arg('-f', '--force', action='store_true', default=False),
            ]
            name = 'add'
            handle = mock.Mock()

        self.manager.register('add', Command)
        with mock.patch.object(sys, 'argv', ['prog', 'add', '-f']):
            self.manager.execute()
        namespace = Command.handle.call_args[0][0]
        Command.handle.assert_called_once_with(namespace)