Example #1
0
    def _describe_command(self, command: Command, **options: Any) -> None:
        command.merge_application_definition(False)

        description = command.description
        if description:
            self._write("<b>Description:</b>")
            self._write("\n")
            self._write("  " + description)
            self._write("\n\n")

        self._write("<b>Usage:</b>")
        for usage in [command.synopsis(True)
                      ] + command.aliases + command.usages:
            self._write("\n")
            self._write("  " + Formatter.escape(usage))

        self._write("\n")

        definition = command.definition
        if definition.options or definition.arguments:
            self._write("\n")
            self._describe_definition(definition, **options)
            self._write("\n")

        help_text = command.processed_help
        if help_text and help_text != description:
            self._write("\n")
            self._write("<b>Help:</b>")
            self._write("\n")
            self._write("  " + help_text.replace("\n", "\n  "))
            self._write("\n")
Example #2
0
    def test_init(self):
        """
        Command.__init__() behaves properly
        """
        self.assertRaises(Exception, Command)

        command = Command('foo:bar')
        self.assertEqual(
            'foo:bar',
            command.get_name(),
            msg='__init__() takes the command name as its first argument'
        )
Example #3
0
    def test_init(self):
        """
        Command.__init__() behaves properly
        """
        self.assertRaises(Exception, Command)

        command = Command('foo:bar')
        self.assertEqual(
            'foo:bar',
            command.get_name(),
            msg='__init__() takes the command name as its first argument'
        )
Example #4
0
    def setUp(self):
        self.command = Command('foo')
        self.command.add_argument('command')
        self.command.add_argument('foo')
        self.command.set_code(lambda c: c.line('foo'))

        self.tester = CommandTester(self.command)
        self.tester.execute(
            [('foo', 'bar')], {
                'interactive': False,
                'decorated': False,
                'verbosity': Output.VERBOSITY_VERBOSE
            })
Example #5
0
    def test_from_dict_with_plain_dict(self):
        def foo(input_, output_):
            output_.writeln('foo')

        command_dict = {
            'name': 'foo',
            'description': 'The foo command.',
            'aliases': ['foobar'],
            'help': 'This is help.',
            'arguments': [{
                'name': 'bar',
                'description': 'The bar argument.',
                'required': True,
                'list': True
            }],
            'options': [{
                'name': 'baz',
                'shortcut': 'b',
                'description': 'The baz option.',
                'value_required': False,
                'list': True,
                'default': ['default']
            }],
            'code': foo
        }

        command = Command.from_dict(command_dict)

        self.assertTrue(isinstance(command, Command))
        self.assertEqual('foo', command.get_name())
        self.assertEqual(foo, command._code)
        self.assertEqual(['foobar'], command.get_aliases())
        self.assertTrue(command.get_definition().has_argument('bar'))
        self.assertTrue(command.get_definition().has_option('baz'))
Example #6
0
 def test_execute_method_needs_to_be_overwridden(self):
     command = Command('foo')
     self.assertRaises(
         NotImplementedError,
         command.run,
         ListInput([]),
         NullOutput()
     )
Example #7
0
    def setUp(self):
        self.command = Command('foo')
        self.command.add_argument('command')
        self.command.add_argument('foo')
        self.command.set_code(lambda input_, output_: output_.writeln('foo'))

        self.tester = CommandTester(self.command)
        self.tester.execute([('foo', 'bar')],
                            {'interactive': False,
                             'decorated': False,
                             'verbosity': Output.VERBOSITY_VERBOSE})
Example #8
0
    def test_command_with_inputs(self):
        questions = [
            'What\'s your name?',
            'How are you?',
            'Where do you come from?',
        ]

        def code(c):
            c.ask(questions[0])
            c.ask(questions[1])
            c.ask(questions[2])

        command = Command('foo')
        command.set_code(code)

        tester = CommandTester(command)
        tester.set_inputs(['Bobby', 'Fine', 'France'])
        tester.execute([])

        self.assertEqual(0, tester.status_code)
        self.assertEqual('\n' + ' \n\n\n'.join(questions) + ' \n',
                         tester.get_display(True))
Example #9
0
class TestCommandTester(TestCase):
    def setUp(self):
        self.command = Command('foo')
        self.command.add_argument('command')
        self.command.add_argument('foo')
        self.command.set_code(lambda input_, output_: output_.writeln('foo'))

        self.tester = CommandTester(self.command)
        self.tester.execute(
            [('foo', 'bar')], {
                'interactive': False,
                'decorated': False,
                'verbosity': Output.VERBOSITY_VERBOSE
            })

    def tearDown(self):
        self.command = None
        self.tester = None

    def test_execute(self):
        """
        CommandTester.execute() behaves properly
        """
        self.assertFalse(self.tester.get_input().is_interactive(),
                         msg='.execute() takes an interactive option.')
        self.assertFalse(self.tester.get_output().is_decorated(),
                         msg='.execute() takes a decorated option.')
        self.assertEqual(Output.VERBOSITY_VERBOSE,
                         self.tester.get_output().get_verbosity(),
                         msg='.execute() takes an interactive option.')

    def test_get_input(self):
        """
        CommandTester.get_input() behaves properly
        """
        self.assertEqual(
            'bar',
            self.tester.get_input().get_argument('foo'),
            msg='.get_input() returns the current input instance.')

    def test_get_output(self):
        """
        CommandTester.get_output() behaves properly
        """
        self.tester.get_output().get_stream().seek(0)
        self.assertEqual(
            'foo\n',
            self.tester.get_output().get_stream().read().decode(),
            msg='.get_output() returns the current output instance.')

    def test_get_display(self):
        """
        CommandTester.get_display() behaves properly
        """
        self.assertEqual(
            'foo\n',
            self.tester.get_display(),
            msg='.get_display() returns the display of the last execution.')
Example #10
0
class TestCommandTester(TestCase):

    def setUp(self):
        self.command = Command('foo')
        self.command.add_argument('command')
        self.command.add_argument('foo')
        self.command.set_code(lambda input_, output_: output_.writeln('foo'))

        self.tester = CommandTester(self.command)
        self.tester.execute([('foo', 'bar')],
                            {'interactive': False,
                             'decorated': False,
                             'verbosity': Output.VERBOSITY_VERBOSE})

    def tearDown(self):
        self.command = None
        self.tester = None

    def test_execute(self):
        """
        CommandTester.execute() behaves properly
        """
        self.assertFalse(self.tester.get_input().is_interactive(),
                         msg='.execute() takes an interactive option.')
        self.assertFalse(self.tester.get_output().is_decorated(),
                         msg='.execute() takes a decorated option.')
        self.assertEqual(Output.VERBOSITY_VERBOSE, self.tester.get_output().get_verbosity(),
                         msg='.execute() takes an interactive option.')

    def test_get_input(self):
        """
        CommandTester.get_input() behaves properly
        """
        self.assertEqual('bar', self.tester.get_input().get_argument('foo'),
                         msg='.get_input() returns the current input instance.')

    def test_get_output(self):
        """
        CommandTester.get_output() behaves properly
        """
        self.tester.get_output().get_stream().seek(0)
        self.assertEqual('foo\n', self.tester.get_output().get_stream().read().decode(),
                         msg='.get_output() returns the current output instance.')

    def test_get_display(self):
        """
        CommandTester.get_display() behaves properly
        """
        self.assertEqual('foo\n', self.tester.get_display(),
                         msg='.get_display() returns the display of the last execution.')
Example #11
0
    def test_from_dict(self):
        def foo(input_, output_):
            output_.writeln('foo')

        command_dict = {
            'foo': {
                'description': 'The foo command.',
                'aliases': ['foobar'],
                'help': 'This is help.',
                'arguments': [{
                    'bar': {
                        'description': 'The bar argument.',
                        'required': True,
                        'list': True
                    }
                }],
                'options': [{
                    'baz': {
                        'shortcut': 'b',
                        'description': 'The baz option.',
                        'value_required': False,
                        'list': True,
                        'default': ['default']
                    }
                }],
                'code': foo
            }
        }

        command = Command.from_dict(command_dict)

        self.assertTrue(isinstance(command, Command))
        self.assertEqual('foo', command.get_name())
        self.assertEqual(foo, command._code)
        self.assertEqual(['foobar'], command.get_aliases())
        self.assertTrue(command.get_definition().has_argument('bar'))
        self.assertTrue(command.get_definition().has_option('baz'))
Example #12
0
def test_set_application():
    application = Application()
    command = Command()
    command.set_application(application)

    assert application == command.application
Example #13
0
class TestCommandTester(TestCase):
    def setUp(self):
        self.command = Command('foo')
        self.command.add_argument('command')
        self.command.add_argument('foo')
        self.command.set_code(lambda c: c.line('foo'))

        self.tester = CommandTester(self.command)
        self.tester.execute(
            [('foo', 'bar')], {
                'interactive': False,
                'decorated': False,
                'verbosity': Output.VERBOSITY_VERBOSE
            })

    def tearDown(self):
        self.command = None
        self.tester = None

    def test_execute(self):
        """
        CommandTester.execute() behaves properly
        """
        self.assertFalse(self.tester.get_input().is_interactive(),
                         msg='.execute() takes an interactive option.')
        self.assertFalse(self.tester.get_output().is_decorated(),
                         msg='.execute() takes a decorated option.')
        self.assertEqual(Output.VERBOSITY_VERBOSE,
                         self.tester.get_output().get_verbosity(),
                         msg='.execute() takes an interactive option.')

    def test_get_input(self):
        """
        CommandTester.get_input() behaves properly
        """
        self.assertEqual(
            'bar',
            self.tester.get_input().get_argument('foo'),
            msg='.get_input() returns the current input instance.')

    def test_get_output(self):
        """
        CommandTester.get_output() behaves properly
        """
        self.tester.get_output().get_stream().seek(0)
        self.assertEqual(
            'foo\n',
            self.tester.get_output().get_stream().read().decode(),
            msg='.get_output() returns the current output instance.')

    def test_get_display(self):
        """
        CommandTester.get_display() behaves properly
        """
        self.assertEqual(
            'foo\n',
            self.tester.get_display(),
            msg='.get_display() returns the display of the last execution.')

    def test_command_with_inputs(self):
        questions = [
            'What\'s your name?',
            'How are you?',
            'Where do you come from?',
        ]

        def code(c):
            c.ask(questions[0])
            c.ask(questions[1])
            c.ask(questions[2])

        command = Command('foo')
        command.set_code(code)

        tester = CommandTester(command)
        tester.set_inputs(['Bobby', 'Fine', 'France'])
        tester.execute([])

        self.assertEqual(0, tester.status_code)
        self.assertEqual('\n' + ' \n\n\n'.join(questions) + ' \n',
                         tester.get_display(True))
Example #14
0
def command(name: str) -> Command:
    command_ = Command()
    command_.name = name

    return command_
Example #15
0
def test_set_application():
    application = Application()
    command = Command()
    command.set_application(application)

    assert application == command.application