Esempio n. 1
0
    def test_has_get(self):
        """
        Application.has() and Application.get() should determine and get commands
        """
        application = Application()

        self.assertTrue(application.has('list'),
                        msg='.has() returns true if a command is registered')
        self.assertFalse(
            application.has('afoobar'),
            msg='.has() returns false if a command is not registered')

        foo = FooCommand()
        application.add(foo)

        self.assertTrue(application.has('afoobar'),
                        msg='.has() returns true if an alias is registered')
        self.assertEqual(foo,
                         application.get('foo:bar'),
                         msg='.get() returns a command by name')
        self.assertEqual(foo,
                         application.get('afoobar'),
                         msg='.get() returns a command by alias')

        application = Application()
        application.add(foo)
        # Simulate help
        application._want_helps = True
        self.assertTrue(
            isinstance(application.get('foo:bar'), HelpCommand),
            msg=
            '.get() returns the help command if --help is provided as the input'
        )
Esempio n. 2
0
    def test_combined_decorators(self):
        """
        Combining decorators should register a command with arguments and options
        """
        application = Application()

        @application.command('decorated_foo', description='Foo command')
        @application.argument('foo', description='Foo argument', required=True, is_list=True)
        @application.option('bar', 'b', description='Bar option', value_required=True, is_list=True)
        def decorated_foo_code(i, o):
            """Foo Description"""
            o.writeln('called')

        self.assertTrue(application.has('decorated_foo'))

        command = application.get('decorated_foo')
        self.assertEqual(command._code, decorated_foo_code)
        self.assertEqual(command.get_description(), 'Foo command')
        self.assertTrue('decorated_foo_code' in command.get_aliases())

        argument = command.get_definition().get_argument('foo')
        self.assertEqual('Foo argument', argument.get_description())
        self.assertTrue(argument.is_required())
        self.assertTrue(argument.is_list())

        option = command.get_definition().get_option('bar')
        self.assertEqual('b', option.get_shortcut())
        self.assertEqual('Bar option', option.get_description())
        self.assertTrue(option.is_value_required())
        self.assertTrue(option.is_list())
Esempio n. 3
0
    def test_combined_decorators(self):
        """
        Combining decorators should register a command with arguments and options
        """
        application = Application()

        @application.command("decorated_foo", description="Foo command")
        @application.argument("foo", description="Foo argument", required=True, is_list=True)
        @application.option("bar", "b", description="Bar option", value_required=True, is_list=True)
        def decorated_foo_code(i, o):
            """Foo Description"""
            o.writeln("called")

        self.assertTrue(application.has("decorated_foo"))

        command = application.get("decorated_foo")
        self.assertEqual(command._code, decorated_foo_code)
        self.assertEqual(command.get_description(), "Foo command")
        self.assertTrue("decorated_foo_code" in command.get_aliases())

        argument = command.get_definition().get_argument("foo")
        self.assertEqual("Foo argument", argument.get_description())
        self.assertTrue(argument.is_required())
        self.assertTrue(argument.is_list())

        option = command.get_definition().get_option("bar")
        self.assertEqual("b", option.get_shortcut())
        self.assertEqual("Bar option", option.get_description())
        self.assertTrue(option.is_value_required())
        self.assertTrue(option.is_list())
Esempio n. 4
0
 def test_execute_for_application_command(self):
     application = Application()
     tester = CommandTester(application.get('help'))
     tester.execute([('command_name', 'list')])
     self.assertRegex(
         tester.get_display(),
         'list \[--raw\] \[namespace\]'
     )
Esempio n. 5
0
def test_has_get(app: Application):
    assert app.has("list")
    assert not app.has("afoobar")

    foo = FooCommand()
    app.add(foo)

    assert app.has("foo bar")
    assert app.has("afoobar")
    assert app.get("foo bar") == foo
    assert app.get("afoobar") == foo
Esempio n. 6
0
    def test_argument_decorator_order(self):
        application = Application()

        @application.command("decorated_foo", description="Foo command")
        @application.argument("foo", description="Foo argument", required=True)
        @application.argument("bar", description="Bar argument", required=True)
        def decorated_foo_code(i, o):
            """Foo Description"""
            o.writeln("called")

        command = application.get("decorated_foo")
        self.assertEqual(["bar", "foo"], list(map(lambda a: a.get_name(), command.get_definition().get_arguments())))
Esempio n. 7
0
    def test_argument_decorator_order(self):
        application = Application()

        @application.command('decorated_foo', description='Foo command')
        @application.argument('foo', description='Foo argument', required=True)
        @application.argument('bar', description='Bar argument', required=True)
        def decorated_foo_code(i, o):
            """Foo Description"""
            o.writeln('called')

        command = application.get('decorated_foo')
        self.assertEqual(
            ['bar', 'foo'],
            list(map(lambda a: a.get_name(), command.get_definition().get_arguments()))
        )
Esempio n. 8
0
    def test_has_get(self):
        """
        Application.has() and Application.get() should determine and get commands
        """
        application = Application()

        self.assertTrue(application.has("list"), msg=".has() returns true if a command is registered")
        self.assertFalse(application.has("afoobar"), msg=".has() returns false if a command is not registered")

        foo = FooCommand()
        application.add(foo)

        self.assertTrue(application.has("afoobar"), msg=".has() returns true if an alias is registered")
        self.assertEqual(foo, application.get("foo:bar"), msg=".get() returns a command by name")
        self.assertEqual(foo, application.get("afoobar"), msg=".get() returns a command by alias")

        application = Application()
        application.add(foo)
        # Simulate help
        application._want_helps = True
        self.assertTrue(
            isinstance(application.get("foo:bar"), HelpCommand),
            msg=".get() returns the help command if --help is provided as the input",
        )
Esempio n. 9
0
    def test_command_decorator(self):
        """
        @Application.command decorator should register a command
        """
        application = Application()

        @application.command('decorated_foo', description='Foo')
        def decorated_foo_code(i, o):
            o.writeln('called')

        self.assertTrue(application.has('decorated_foo'))

        command = application.get('decorated_foo')
        self.assertEqual(command._code, decorated_foo_code)
        self.assertEqual(command.get_description(), 'Foo')
        self.assertTrue('decorated_foo_code' in command.get_aliases())
Esempio n. 10
0
    def test_command_decorator(self):
        """
        @Application.command decorator should register a command
        """
        application = Application()

        @application.command("decorated_foo", description="Foo")
        def decorated_foo_code(i, o):
            o.writeln("called")

        self.assertTrue(application.has("decorated_foo"))

        command = application.get("decorated_foo")
        self.assertEqual(command._code, decorated_foo_code)
        self.assertEqual(command.get_description(), "Foo")
        self.assertTrue("decorated_foo_code" in command.get_aliases())
Esempio n. 11
0
    def test_execute(self):
        """
        ListCommand.execute() behaves properly
        """
        application = Application()

        command = application.get('list')

        command_tester = CommandTester(command)
        command_tester.execute([('command', command.get_name())], {'decorated': False})
        self.assertTrue(re.match('(?s).*help   Displays help for a command.*', command_tester.get_display()) is not None,
                        msg='.execute() returns a list of available commands')

        command_tester.execute([('command', command.get_name()), ('--raw', True)])
        output = """help   Displays help for a command
list   Lists commands"""
        self.assertEqual(output, command_tester.get_display())
Esempio n. 12
0
    def test_execute(self):
        """
        ListCommand.execute() behaves properly
        """
        application = Application()

        command = application.get('list')

        command_tester = CommandTester(command)
        command_tester.execute([('command', command.get_name())],
                               {'decorated': False})
        self.assertTrue(re.match('(?s).*help   Displays help for a command.*',
                                 command_tester.get_display()) is not None,
                        msg='.execute() returns a list of available commands')

        command_tester.execute([('command', command.get_name()),
                                ('--raw', True)])
        output = """help   Displays help for a command
list   Lists commands"""
        self.assertEqual(output, command_tester.get_display())
Esempio n. 13
0
    def test_argument_decorator(self):
        """
        @Application.argument decorator should register a command with a specific argument
        """
        application = Application()

        @application.argument('foo', description='Foo', required=True, is_list=True)
        def decorated_foo_code(i, o):
            """Foo Description"""
            o.writeln('called')

        self.assertTrue(application.has('decorated_foo_code'))

        command = application.get('decorated_foo_code')
        self.assertEqual(command._code, decorated_foo_code)
        self.assertEqual(command.get_description(), 'Foo Description')

        argument = command.get_definition().get_argument('foo')
        self.assertEqual('Foo', argument.get_description())
        self.assertTrue(argument.is_required())
        self.assertTrue(argument.is_list())
Esempio n. 14
0
    def test_execute(self):
        """
        ListCommand.execute() behaves properly
        """
        application = Application()

        command = application.get('list')

        command_tester = CommandTester(command)
        command_tester.execute([('command', command.get_name())],
                               {'decorated': False})
        self.assertRegex(command_tester.get_display(),
                         'help\s{2,}Displays help for a command')

        command_tester.execute([('command', command.get_name()),
                                ('--raw', True)])
        output = """help   Displays help for a command
list   Lists commands

"""
        self.assertEqual(output, command_tester.get_display())
Esempio n. 15
0
    def test_argument_decorator(self):
        """
        @Application.argument decorator should register a command with a specific argument
        """
        application = Application()

        @application.argument("foo", description="Foo", required=True, is_list=True)
        def decorated_foo_code(i, o):
            """Foo Description"""
            o.writeln("called")

        self.assertTrue(application.has("decorated_foo_code"))

        command = application.get("decorated_foo_code")
        self.assertEqual(command._code, decorated_foo_code)
        self.assertEqual(command.get_description(), "Foo Description")

        argument = command.get_definition().get_argument("foo")
        self.assertEqual("Foo", argument.get_description())
        self.assertTrue(argument.is_required())
        self.assertTrue(argument.is_list())
Esempio n. 16
0
    def test_option_decorator(self):
        """
        @Application.option decorator should register a command with a specific option
        """
        application = Application()

        @application.option('foo', 'f', description='Foo', value_required=True, is_list=True)
        def decorated_foo_code(i, o):
            """Foo Description"""
            o.writeln('called')

        self.assertTrue(application.has('decorated_foo_code'))

        command = application.get('decorated_foo_code')
        self.assertEqual(command._code, decorated_foo_code)
        self.assertEqual(command.get_description(), 'Foo Description')

        option = command.get_definition().get_option('foo')
        self.assertEqual('f', option.get_shortcut())
        self.assertEqual('Foo', option.get_description())
        self.assertTrue(option.is_value_required())
        self.assertTrue(option.is_list())
Esempio n. 17
0
    def test_option_decorator(self):
        """
        @Application.option decorator should register a command with a specific option
        """
        application = Application()

        @application.option("foo", "f", description="Foo", value_required=True, is_list=True)
        def decorated_foo_code(i, o):
            """Foo Description"""
            o.writeln("called")

        self.assertTrue(application.has("decorated_foo_code"))

        command = application.get("decorated_foo_code")
        self.assertEqual(command._code, decorated_foo_code)
        self.assertEqual(command.get_description(), "Foo Description")

        option = command.get_definition().get_option("foo")
        self.assertEqual("f", option.get_shortcut())
        self.assertEqual("Foo", option.get_description())
        self.assertTrue(option.is_value_required())
        self.assertTrue(option.is_list())
Esempio n. 18
0
 def test_execute_for_application_command(self):
     application = Application()
     tester = CommandTester(application.get('help'))
     tester.execute([('command_name', 'list')])
     self.assertRegex(tester.get_display(), 'list \[--raw\] \[namespace\]')
Esempio n. 19
0
 def test_execute_for_application_command(self):
     application = Application()
     tester = CommandTester(application.get('help'))
     tester.execute([('command_name', 'list')])
     self.assertIn('list [options] [--] [<namespace>]',
                   tester.get_display())