Exemplo 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'
        )
Exemplo n.º 2
0
    def test_render_exception(self):
        """
        Application.render_exception() displays formatted exception.
        """
        application = Application()
        application.set_auto_exit(False)

        application.get_terminal_width = self.mock().MagicMock(return_value=120)
        tester = ApplicationTester(application)

        tester.run([('command', 'foo')], {'decorated': False})
        self.assertEqual(
            self.open_fixture('application_renderexception1.txt'),
            tester.get_display()
        )

        tester.run([('command', 'foo')],
                   {'decorated': False, 'verbosity': Output.VERBOSITY_VERBOSE})
        self.assertRegex(
            tester.get_display(),
            'Exception trace'
        )

        tester.run([('command', 'list'), ('--foo', True)], {'decorated': False})
        self.assertEqual(
            self.open_fixture('application_renderexception2.txt'),
            tester.get_display()
        )

        application.add(Foo3Command())
        tester = ApplicationTester(application)
        tester.run([('command', 'foo3:bar')], {'decorated': False})
        self.assertEqual(
            self.open_fixture('application_renderexception3.txt'),
            tester.get_display()
        )
        tester = ApplicationTester(application)
        tester.run([('command', 'foo3:bar')], {'decorated': True})
        self.assertEqual(
            self.open_fixture('application_renderexception3decorated.txt'),
            tester.get_display()
        )


        application = Application()
        application.set_auto_exit(False)

        application.get_terminal_width = self.mock().MagicMock(return_value=31)
        tester = ApplicationTester(application)

        tester.run([('command', 'foo')], {'decorated': False})
        self.assertEqual(
            self.open_fixture('application_renderexception4.txt'),
            tester.get_display()
        )
Exemplo n.º 3
0
    def test_merge_application_definition(self):
        """
        Command.merge_application_definition() merges command and application.
        """
        application1 = Application()
        application1.get_definition().add_arguments([InputArgument('foo')])
        application1.get_definition().add_options([InputOption('bar')])
        command = SomeCommand()
        command.set_application(application1)
        command.set_definition(
            InputDefinition([
                InputArgument('bar'),
                InputOption('foo')
            ])
        )

        command.merge_application_definition()
        self.assertTrue(command.get_definition().has_argument('foo'))
        self.assertTrue(command.get_definition().has_option('foo'))
        self.assertTrue(command.get_definition().has_argument('bar'))
        self.assertTrue(command.get_definition().has_option('bar'))

        # It should not merge the definitions twice
        command.merge_application_definition()
        self.assertEqual(3, command.get_definition().get_argument_count())
Exemplo n.º 4
0
    def test_set_run_custom_default_command(self):
        """
        Application calls the default command.
        """
        application = Application()
        application.set_auto_exit(False)
        command = FooCommand()
        application.add(command)
        application.set_default_command(command.get_name())

        tester = ApplicationTester(application)
        tester.run([])
        self.assertEqual(
            'interact called\ncalled\n',
            tester.get_display()
        )

        application = CustomDefaultCommandApplication()
        application.set_auto_exit(False)

        tester = ApplicationTester(application)
        tester.run([])
        self.assertEqual(
            'interact called\ncalled\n',
            tester.get_display()
        )
Exemplo n.º 5
0
    def test_find_namespace(self):
        """
        Application.find_namespace() should return a namespace
        """
        application = Application()
        application.add(FooCommand())

        self.assertEqual(
            'foo',
            application.find_namespace('foo'),
            msg='.find_namespace() returns the given namespace if it exists'
        )
        self.assertEqual(
            'foo',
            application.find_namespace('f'),
            msg='.find_namespace() finds a namespace given an abbreviation'
        )

        application.add(Foo2Command())

        self.assertEqual(
            'foo',
            application.find_namespace('foo'),
            msg='.find_namespace() returns the given namespace if it exists'
        )
Exemplo n.º 6
0
def test_execute_hello_world_command_no_params():
    application = Application()
    application.add(HelloWorldCommand())
    command = application.find('greet')
    command_tester = CommandTester(command)
    command_tester.execute()
    assert "Hello" == command_tester.io.fetch_output().rstrip()
Exemplo n.º 7
0
    def test_find(self):
        """
        Application.find() should return a command
        """
        application = Application()
        application.add(FooCommand())

        self.assertTrue(
            isinstance(application.find('foo:bar'), FooCommand),
            msg='.find() returns a command if its name exists'
        )
        self.assertTrue(
            isinstance(application.find('h'), HelpCommand),
            msg='.find() returns a command if its name exists'
        )
        self.assertTrue(
            isinstance(application.find('f:bar'), FooCommand),
            msg='.find() returns a command if the abbreviation for the namespace exists'
        )
        self.assertTrue(
            isinstance(application.find('f:b'), FooCommand),
            msg='.find() returns a command if the abbreviation for the namespace and the command name exist'
        )
        self.assertTrue(
            isinstance(application.find('a'), FooCommand),
            msg='.find() returns a command if the abbreviation exists for an alias'
        )
Exemplo n.º 8
0
    def test_find_alternative_commands(self):
        application = Application()
        application.add(FooCommand())
        application.add(Foo1Command())
        application.add(Foo2Command())

        command_name = 'Unknown command'
        try:
            application.find(command_name)
            self.fail('.find() raises an Exception if command does not exist')
        except Exception as e:
            self.assertEqual('Command "%s" is not defined.' % command_name,
                             str(e))

        command_name = 'bar1'
        try:
            application.find(command_name)
            self.fail('.find() raises an Exception if command does not exist')
        except Exception as e:
            self.assertRegexpMatches(
                str(e), 'Command "%s" is not defined.' % command_name)
            self.assertRegexpMatches(
                str(e),
                'afoobar1',
            )
            self.assertRegexpMatches(
                str(e),
                'foo:bar1',
            )
            self.assertNotRegex(str(e), 'foo:bar$')
Exemplo n.º 9
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())
Exemplo n.º 10
0
 def test_execute_for_command_alias(self):
     command = HelpCommand()
     command.set_application(Application())
     tester = CommandTester(command)
     tester.execute([('command_name', 'li')], {'decorated': False})
     self.assertIn('list [options] [--] [<namespace>]',
                   tester.get_display())
Exemplo n.º 11
0
    def test_help(self):
        """
        Application.get_help() returns the help message of the application
        """
        application = Application()

        self.assertEqual(self.open_fixture('application_gethelp.txt'),
                         application.get_help())
Exemplo n.º 12
0
    def test_get_long_version(self):
        """
        Application.get_long_version() returns the long version of the application
        """
        application = Application('foo', 'bar')

        self.assertEqual('foo <info>bar</info>',
                         application.get_long_version())
Exemplo n.º 13
0
    def test_find_command_with_missing_namespace(self):
        """
        Application.find() returns a command with missing namespace
        """
        application = Application()
        application.add(Foo4Command())

        self.assertTrue(isinstance(application.find('f::t'), Foo4Command))
Exemplo n.º 14
0
def test_execute_namespace_command():
    app = Application()
    app.add(FooBarCommand())
    tester = CommandTester(app.find("foo bar"))

    assert 0 == tester.execute()
    assert 0 == tester.status_code
    assert "foo bar called\n" == tester.io.fetch_output()
Exemplo n.º 15
0
 def test_set_application(self):
     """
     Command.set_application() sets the current application
     """
     application = Application()
     command = SomeCommand()
     command.set_application(application)
     self.assertEqual(application, command.get_application(), msg='.set_application() sets the current application')
Exemplo n.º 16
0
 def test_get_invalid_command(self):
     """
     Application.get() should raise an exception when command does not exist
     """
     application = Application()
     self.assertRaisesRegexp(Exception,
                             'The command "foofoo" does not exist',
                             application.get, 'foofoo')
Exemplo n.º 17
0
    def test_find_namespace_does_not_fail_on_deep_similar_namespaces(self):
        applicaton = Application()
        applicaton.get_namespaces = self.mock().MagicMock(return_value=['foo:sublong', 'bar:sub'])

        self.assertEqual(
            'foo:sublong',
            applicaton.find_namespace('f:sub')
        )
Exemplo n.º 18
0
    def test_render_exception(self):
        """
        Application.render_exception() displays formatted exception.
        """
        application = Application()
        application.set_auto_exit(False)

        os.environ['COLUMNS'] = '120'
        tester = ApplicationTester(application)

        tester.run([('command', 'foo')], {'decorated': False})
        self.assertEqual(self.open_fixture('application_renderexception1.txt'),
                         tester.get_display())

        tester.run([('command', 'foo')], {
            'decorated': False,
            'verbosity': Output.VERBOSITY_VERBOSE
        })
        self.assertRegex(tester.get_display(), 'Exception trace')

        tester.run([('command', 'list'), ('--foo', True)],
                   {'decorated': False})
        self.assertEqual(self.open_fixture('application_renderexception2.txt'),
                         tester.get_display())

        application.add(Foo3Command())
        tester = ApplicationTester(application)
        tester.run([('command', 'foo3:bar')], {'decorated': False})
        self.assertEqual(self.open_fixture('application_renderexception3.txt'),
                         tester.get_display())
        tester = ApplicationTester(application)
        tester.run([('command', 'foo3:bar')], {'decorated': True})
        self.assertEqual(
            self.open_fixture('application_renderexception3decorated.txt'),
            tester.get_display())

        application = Application()
        application.set_auto_exit(False)

        os.environ['COLUMNS'] = '31'
        tester = ApplicationTester(application)

        tester.run([('command', 'foo')], {'decorated': False})
        self.assertEqual(self.open_fixture('application_renderexception4.txt'),
                         tester.get_display())
Exemplo n.º 19
0
    def test_get_default_helper_set_returns_default_values(self):
        application = Application()
        application.set_auto_exit(False)
        application.set_catch_exceptions(False)

        helper_set = application.get_helper_set()

        self.assertTrue(helper_set.has('formatter'))
        self.assertTrue(helper_set.has('question'))
Exemplo n.º 20
0
    def test_add_with_dictionary(self):
        """
        Application.add() accepts a dictionary as argument.
        """
        application = Application()

        foo = application.add(foo_commmand)
        self.assertTrue(isinstance(foo, Command))
        self.assertEqual('foo:bar1', foo.get_name())
Exemplo n.º 21
0
    def test_find_command_with_ambiguous_namespace_but_unique_name(self):
        """
        Application.find() returns a command with ambiguous namespace
        """
        application = Application()
        application.add(FooCommand())
        application.add(FoobarCommand())

        self.assertTrue(isinstance(application.find('f:f'), FoobarCommand))
Exemplo n.º 22
0
    def find_invalid_namespace(self):
        """
        Application.find_namespace() should raise an error when finding missing namespace
        """
        application = Application()

        self.assertRaisesRegexp(
            Exception,
            'There are no commands defined in the "bar" namespace\.',
            application.find_namespace, 'bar')
Exemplo n.º 23
0
    def test_run_return_exit_code_one_for_exception_code_zero(self):
        exception = OSError(0, '')

        application = Application()
        application.set_auto_exit(False)
        application.do_run = self.mock().MagicMock(side_effect=exception)

        exit_code = application.run(ListInput([]), NullOutput())

        self.assertEqual(1, exit_code)
Exemplo n.º 24
0
    def test_set_get_version(self):
        """
        Application version accessors behave properly
        """
        application = Application()
        application.set_version('bar')

        self.assertEqual('bar',
                         application.get_version(),
                         msg='.set_version() sets the version of the application')
Exemplo n.º 25
0
    def find_alternative_commands_with_an_alias(self):
        foo_command = FooCommand()
        foo_command.set_aliases(['foo2'])

        application = Application()
        application.add(foo_command)

        result = application.find('foo')

        self.assertEqual(foo_command, result)
Exemplo n.º 26
0
    def test_set_get_name(self):
        """
        Application name accessors behave properly
        """
        application = Application()
        application.set_name('foo')

        self.assertEqual('foo',
                         application.get_name(),
                         msg='.set_name() sets the name of the application')
Exemplo n.º 27
0
    def test_as_text(self):
        command = TestCommand()
        command.set_application(Application())
        tester = CommandTester(command)
        tester.execute([('command', command.get_name())])

        self.assertEqual(
            self.open_fixture('command_astext.txt'),
            command.as_text()
        )
Exemplo n.º 28
0
    def test_register(self):
        """
        Application.register() registers a new command
        """
        application = Application()
        command = application.register('foo')

        self.assertEqual('foo',
                         command.get_name(),
                         msg='.register() registers a new command')
Exemplo n.º 29
0
    def test_get_helper(self):
        application = Application()
        command = SomeCommand()
        command.set_application(application)
        formatter_helper = FormatterHelper()

        self.assertEqual(
            formatter_helper.get_name(),
            command.get_helper('formatter').get_name()
        )
Exemplo n.º 30
0
    def test_terminal_dimensions(self):
        application = Application()
        original_dimensions = application.get_terminal_dimensions(StreamOutput(sys.stdout))
        self.assertEqual(2, len(original_dimensions))

        width = 80
        if original_dimensions[0] == width:
            width = 100

        application.set_terminal_dimensions(width, 80)
        self.assertEqual((width, 80), application.get_terminal_dimensions(StreamOutput(sys.stdout)))