Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 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_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.º 7
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.º 8
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.º 9
0
def test_find_ambiguous_command(app: Application):
    app.add(FooCommand())

    with pytest.raises(
        CommandNotFoundException,
        match=r'The command "foo b" does not exist\.\n\nDid you mean this\?\n    foo bar',
    ):
        app.find("foo b")
Exemplo n.º 10
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.º 11
0
def test_all(app: Application):
    commands = app.all()

    assert isinstance(commands["help"], Command)

    app.add(FooCommand())

    assert len(app.all("foo")) == 1
Exemplo n.º 12
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.º 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_find_ambiguous_namespace(app: Application):
    app.add(FooCommand())
    app.add(Foo2Command())

    with pytest.raises(
        NamespaceNotFoundException,
        match=r'There are no commands in the "f" namespace\.\n\nDid you mean one of these\?\n    foo\n    foo1',
    ):
        app.find_namespace("f")
Exemplo n.º 15
0
def test_find_invalid_namespace(app: Application):
    app.add(FooCommand())
    app.add(Foo2Command())

    with pytest.raises(
        NamespaceNotFoundException,
        match=r'There are no commands in the "bar" namespace\.',
    ):
        app.find_namespace("bar")
Exemplo n.º 16
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.º 17
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.º 18
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.º 19
0
def test_find_ambiguous_command_hidden(app: Application):
    foo = FooCommand()
    foo.hidden = True
    app.add(foo)

    with pytest.raises(
        CommandNotFoundException,
        match=r'The command "foo b" does not exist\.$',
    ):
        app.find("foo b")
Exemplo n.º 20
0
    def test_find_unique_name_but_namespace_name(self):
        """
        Application.find() should raise an error when command is missing
        """
        application = Application()
        application.add(FooCommand())
        application.add(Foo1Command())
        application.add(Foo2Command())

        self.assertRaisesRegexp(Exception, 'Command "foo1" is not defined', application.find, "foo1")
Exemplo n.º 21
0
def test_find_unique_name_but_namespace_name(app: Application):
    app.add(FooCommand())
    app.add(Foo1Command())
    app.add(Foo2Command())

    with pytest.raises(
        CommandNotFoundException,
        match=r'The command "foo1" does not exist\.',
    ):
        app.find("foo1")
Exemplo n.º 22
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.º 23
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.º 24
0
    def test_find_unique_name_but_namespace_name(self):
        """
        Application.find() should raise an error when command is missing
        """
        application = Application()
        application.add(FooCommand())
        application.add(Foo1Command())
        application.add(Foo2Command())

        self.assertRaisesRegexp(Exception, 'Command "foo1" is not defined',
                                application.find, 'foo1')
Exemplo n.º 25
0
    def test_get_namespaces(self):
        """
        Application.get_namespaces() should return registered namespaces
        """
        application = Application()
        application.add(FooCommand())
        application.add(Foo1Command())

        self.assertEqual(
            ["foo"], application.get_namespaces(), msg=".get_namespaces() returns an array of unique used namespaces"
        )
Exemplo n.º 26
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
Exemplo n.º 27
0
    def test_find_alternative_exception_message_single(self):
        """
        Application.find() raises an exception when an alternative has been found
        """
        data = ["foo3:baR", "foO3:bar"]

        application = Application()
        application.add(Foo3Command())

        for d in data:
            self.assertRaisesRegexp(Exception, "Did you mean this", application.find, d)
Exemplo n.º 28
0
    def test_as_text(self):
        """
        Application.as_text() returns a text representation of the application.
        """
        application = Application()
        application.add(FooCommand())

        self.ensure_static_command_help(application)

        self.assertEqual(self.open_fixture("application_astext1.txt"), application.as_text())
        self.assertEqual(self.open_fixture("application_astext2.txt"), application.as_text("foo"))
Exemplo n.º 29
0
    def test_find_alternative_exception_message_single(self):
        """
        Application.find() raises an exception when an alternative has been found
        """
        data = ['foo3:baR', 'foO3:bar']

        application = Application()
        application.add(Foo3Command())

        for d in data:
            self.assertRaisesRegexp(Exception, 'Did you mean this',
                                    application.find, d)
Exemplo n.º 30
0
    def test_get_namespaces(self):
        """
        Application.get_namespaces() should return registered namespaces
        """
        application = Application()
        application.add(FooCommand())
        application.add(Foo1Command())

        self.assertEqual(
            ['foo'],
            application.get_namespaces(),
            msg='.get_namespaces() returns an array of unique used namespaces')
Exemplo n.º 31
0
def test_execute():
    config = {
        "scripts": {
            "e": ["python", "-c", "print(1)"],
        }
    }

    application = Application()
    application.add(CustomCommand(config))
    command = application.find("do")
    command_tester = CommandTester(command)
    command_tester.execute("e")
Exemplo n.º 32
0
def test_add(app: Application):
    foo = FooCommand()
    app.add(foo)
    commands = app.all()

    assert [commands["foo bar"]] == [foo]

    foo1 = Foo1Command()
    app.add(foo1)

    commands = app.all()

    assert [commands["foo bar"], commands["foo bar1"]] == [foo, foo1]
Exemplo n.º 33
0
def test_run(app: Application, argv):
    app.catch_exceptions(False)
    app.auto_exits(False)
    command = Foo1Command()
    app.add(command)

    sys.argv = ["console", "foo bar1"]
    app.run()

    assert isinstance(command.io, IO)
    assert isinstance(command.io.output, StreamOutput)
    assert isinstance(command.io.error_output, StreamOutput)
    assert command.io.output.stream == sys.stdout
    assert command.io.error_output.stream == sys.stderr
Exemplo n.º 34
0
    def test_all(self):
        """
        Application.get_all() returns all comands of the application
        """
        application = Application()
        commands = application.all()

        self.assertEqual(
            "HelpCommand", commands["help"].__class__.__name__, msg=".all() returns the registered commands"
        )

        application.add(FooCommand())

        self.assertEqual(1, len(application.all("foo")), msg=".all() take a namespace as first argument")
Exemplo n.º 35
0
    def test_set_run_custom_single_command(self):
        command = FooCommand()

        application = Application()
        application.set_auto_exit(False)
        application.add(command)
        application.set_default_command(command.get_name(), True)

        tester = ApplicationTester(application)

        tester.run([])
        self.assertIn('called', tester.get_display())

        tester.run([('--help', True)])
        self.assertIn('The foo:bar command', tester.get_display())
Exemplo n.º 36
0
    def test_find_command_equal_namesapce(self):
        """
        Application.find() returns a command if it has a namespace with the same name
        """
        application = Application()
        application.add(Foo3Command())
        application.add(Foo4Command())

        self.assertTrue(
            isinstance(application.find('foo3:bar'), Foo3Command),
            msg='.find() returns the good command even if a namespace has same name'
        )
        self.assertTrue(
            isinstance(application.find('foo3:bar:toh'), Foo4Command),
            msg='.find() returns a command even if its namespace equals another command name'
        )
Exemplo n.º 37
0
    def test_find_command_equal_namesapce(self):
        """
        Application.find() returns a command if it has a namespace with the same name
        """
        application = Application()
        application.add(Foo3Command())
        application.add(Foo4Command())

        self.assertTrue(
            isinstance(application.find("foo3:bar"), Foo3Command),
            msg=".find() returns the good command even if a namespace has same name",
        )
        self.assertTrue(
            isinstance(application.find("foo3:bar:toh"), Foo4Command),
            msg=".find() returns a command even if its namespace equals another command name",
        )
Exemplo n.º 38
0
    def test_find_alternative_commands_with_question(self):
        application = Application()
        application.set_auto_exit(False)
        os.environ['COLUMNS'] = '120'
        os.environ['SHELL_INTERACTIVE'] = '1'
        application.add(FooCommand())
        application.add(Foo1Command())
        application.add(Foo2Command())

        tester = ApplicationTester(application)
        tester.set_inputs(['1\n'])
        tester.run([('command', 'f:b')], {'interactive': True})

        self.assertEqual(
            self.open_fixture('application_unknown_command_question.txt'),
            tester.get_display(True))
Exemplo n.º 39
0
    def test_all(self):
        """
        Application.get_all() returns all comands of the application
        """
        application = Application()
        commands = application.all()

        self.assertEqual('HelpCommand',
                         commands['help'].__class__.__name__,
                         msg='.all() returns the registered commands')

        application.add(FooCommand())

        self.assertEqual(1,
                         len(application.all('foo')),
                         msg='.all() take a namespace as first argument')
Exemplo n.º 40
0
    def test_find_with_ambiguous_abbreviations(self):
        """
        Application.find() should raise an error when there is ambiguosity
        """
        data = [
            ["f", 'Command "f" is not defined\.'],
            ["a", 'Command "a" is ambiguous \(afoobar, afoobar1 and 1 more\)\.'],
            ["foo:b", 'Command "foo:b" is ambiguous \(foo1:bar, foo:bar and 1 more\)\.'],
        ]

        application = Application()
        application.add(FooCommand())
        application.add(Foo1Command())
        application.add(Foo2Command())

        for d in data:
            self.assertRaisesRegexp(Exception, d[1], application.find, d[0])
Exemplo n.º 41
0
    def test_as_text(self):
        """
        Application.as_text() returns a text representation of the application.
        """
        application = Application()
        application.add(FooCommand())

        self.ensure_static_command_help(application)

        self.assertEqual(
            self.open_fixture('application_astext1.txt'),
            application.as_text()
        )
        self.assertEqual(
            self.open_fixture('application_astext2.txt'),
            application.as_text('foo')
        )
Exemplo n.º 42
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.º 43
0
    def test_add(self):
        """
        Application.add() and .addCommands() register commands
        """
        application = Application()
        foo = FooCommand()
        application.add(foo)

        self.assertEqual(foo, application.all()["foo:bar"], msg=".add() registers a command")

        application = Application()
        foo, foo1 = FooCommand(), Foo1Command()
        application.add_commands([foo, foo1])
        commands = application.all()

        self.assertEqual(
            [foo, foo1], [commands["foo:bar"], commands["foo:bar1"]], msg=".add_commands() registers a list of commands"
        )
Exemplo n.º 44
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.º 45
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.º 46
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.º 47
0
    def test_find_alternative_exception_message_multiple(self):
        """
        Application.find() raises an exception when alternatives have been found
        """
        application = Application()
        application.add(FooCommand())
        application.add(Foo1Command())
        application.add(Foo2Command())

        try:
            application.find("foo:baR")
            self.fail(".find() raises an Exception if command does not exist, with alternatives")
        except Exception as e:
            self.assertRegexpMatches(str(e), "Did you mean one of these")
            self.assertRegexpMatches(str(e), "foo1:bar")
            self.assertRegexpMatches(str(e), "foo:bar")

        try:
            application.find("foo2:baR")
            self.fail(".find() raises an Exception if command does not exist, with alternatives")
        except Exception as e:
            self.assertRegexpMatches(str(e), "Did you mean one of these")
            self.assertRegexpMatches(str(e), "foo1")

        application.add(Foo3Command())
        application.add(Foo4Command())

        try:
            application.find("foo3:")
            self.fail(".find() raises an Exception if command does not exist, with alternatives")
        except Exception as e:
            self.assertRegexpMatches(str(e), "foo3:bar")
            self.assertRegexpMatches(str(e), "foo3:bar:toh")
Exemplo n.º 48
0
    def test_find_alternative_namespace(self):
        application = Application()

        application.add(FooCommand())
        application.add(Foo1Command())
        application.add(Foo2Command())
        application.add(Foo3Command())

        try:
            application.find("Unknown-namespace:Unknown-command")
            self.fail(".find() raises an Exception if namespace does not exist")
        except Exception as e:
            self.assertRegex(str(e), 'There are no commands defined in the "Unknown-namespace" namespace.')

        try:
            application.find("foo2:command")
            self.fail(".find() raises an tException if namespace does not exist")
        except Exception as e:
            self.assertRegex(str(e), 'There are no commands defined in the "foo2" namespace.')
            self.assertRegex(
                str(e), "foo", msg='.find() raises an tException if namespace does not exist, with alternative "foo"'
            )
            self.assertRegex(
                str(e), "foo1", msg='.find() raises an tException if namespace does not exist, with alternative "foo1"'
            )
            self.assertRegex(
                str(e), "foo3", msg='.find() raises an Exception if namespace does not exist, with alternative "foo2"'
            )
Exemplo n.º 49
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.º 50
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.º 51
0
    def test_find_ambiguous_namespace(self):
        """
        Application.find_namespace() should raise an error when namespace is ambiguous
        """
        application = Application()
        application.add(BarBucCommand())
        application.add(FooCommand())
        application.add(Foo2Command())

        self.assertRaisesRegexp(
            Exception, 'The namespace "f" is ambiguous \(foo, foo1\)\.', application.find_namespace, "f"
        )
Exemplo n.º 52
0
def app():
    app = Application()
    app.config.set_terminate_after_run(False)
    app.add(FooCommand())

    return app
Exemplo n.º 53
0
    def test_run(self):
        application = Application()
        application.set_auto_exit(False)
        application.set_catch_exceptions(False)
        command = Foo1Command()
        application.add(command)

        sys.argv = ["cli.py", "foo:bar1"]

        application.run()

        self.assertEqual("ArgvInput", command.input.__class__.__name__)
        self.assertEqual("ConsoleOutput", command.output.__class__.__name__)

        application = Application()
        application.set_auto_exit(False)
        application.set_catch_exceptions(False)

        self.ensure_static_command_help(application)
        tester = ApplicationTester(application)

        tester.run([], {"decorated": False})
        self.assertEqual(self.open_fixture("application_run1.txt"), tester.get_display())

        tester.run([("--help", True)], {"decorated": False})
        self.assertEqual(self.open_fixture("application_run2.txt"), tester.get_display())

        tester.run([("-h", True)], {"decorated": False})
        self.assertEqual(self.open_fixture("application_run2.txt"), tester.get_display())

        tester.run([("command", "list"), ("--help", True)], {"decorated": False})
        self.assertEqual(self.open_fixture("application_run3.txt"), tester.get_display())

        tester.run([("command", "list"), ("-h", True)], {"decorated": False})
        self.assertEqual(self.open_fixture("application_run3.txt"), tester.get_display())

        tester.run([("--ansi", True)])
        self.assertTrue(tester.get_output().is_decorated())

        tester.run([("--no-ansi", True)])
        self.assertFalse(tester.get_output().is_decorated())

        tester.run([("--version", True)], {"decorated": False})
        self.assertEqual(self.open_fixture("application_run4.txt"), tester.get_display())

        tester.run([("-V", True)], {"decorated": False})
        self.assertEqual(self.open_fixture("application_run4.txt"), tester.get_display())

        tester.run([("command", "list"), ("--quiet", True)])
        self.assertEqual("", tester.get_display())

        tester.run([("command", "list"), ("-q", True)])
        self.assertEqual("", tester.get_display())

        tester.run([("command", "list"), ("--verbose", True)])
        self.assertEqual(Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity())

        tester.run([("command", "list"), ("-v", True)])
        self.assertEqual(Output.VERBOSITY_VERBOSE, tester.get_output().get_verbosity())

        application = Application()
        application.set_auto_exit(False)
        application.set_catch_exceptions(False)
        application.add(FooCommand())
        tester = ApplicationTester(application)

        tester.run([("command", "foo:bar"), ("--no-interaction", True)], {"decorated": False})
        self.assertEqual("called\n", tester.get_display())

        tester.run([("command", "foo:bar"), ("-n", True)], {"decorated": False})
        self.assertEqual("called\n", tester.get_display())