Ejemplo n.º 1
0
def test_register_command_sets_up_class_at_runtime(registrar):
    inner_function = Mock()

    context = Mock()
    context.cwd = "."

    # Inside the following class, we test that the virtualenv is set up properly
    # dynamically on the instance that actually runs the command.
    @CommandProvider
    class CommandFoo(MachCommandBase):
        @Command("cmd_foo", category="testing", virtualenv_name="env_foo")
        def run_foo(self):
            assert (os.path.basename(
                self.virtualenv_manager.virtualenv_root) == "env_foo")
            inner_function("foo")

        @Command("cmd_bar", category="testing", virtualenv_name="env_bar")
        def run_bar(self):
            assert (os.path.basename(
                self.virtualenv_manager.virtualenv_root) == "env_bar")
            inner_function("bar")

    registrar.dispatch("cmd_foo", context)
    inner_function.assert_called_with("foo")
    registrar.dispatch("cmd_bar", context)
    inner_function.assert_called_with("bar")
Ejemplo n.º 2
0
def test_register_command_with_metrics_path(registrar):
    context = Mock()
    context.cwd = "."

    metrics_path = "metrics/path"
    metrics_mock = Mock()
    context.telemetry.metrics.return_value = metrics_mock

    @CommandProvider
    class CommandFoo(MachCommandBase):
        @Command("cmd_foo", category="testing", metrics_path=metrics_path)
        def run_foo(self):
            assert self.metrics == metrics_mock

        @SubCommand("cmd_foo", "sub_foo", metrics_path=metrics_path + "2")
        def run_subfoo(self):
            assert self.metrics == metrics_mock

    registrar.dispatch("cmd_foo", context)

    context.telemetry.metrics.assert_called_with(metrics_path)
    assert context.handler.metrics_path == metrics_path

    registrar.dispatch("cmd_foo", context, subcommand="sub_foo")
    assert context.handler.metrics_path == metrics_path + "2"
Ejemplo n.º 3
0
def test_register_command_with_argument(registrar):
    inner_function = Mock()
    context = Mock()
    context.cwd = "."

    @Command("cmd_foo", category="testing")
    @CommandArgument("--arg", default=None, help="Argument help.")
    def run_foo(command_context, arg):
        inner_function(arg)

    registrar.dispatch("cmd_foo", context, arg="argument")

    inner_function.assert_called_with("argument")
Ejemplo n.º 4
0
def test_register_command_sets_up_class_at_runtime(registrar):
    inner_function = Mock()

    context = Mock()
    context.cwd = "."

    # We test that the virtualenv is set up properly dynamically on
    # the instance that actually runs the command.
    @Command("cmd_foo", category="testing", virtualenv_name="env_foo")
    def run_foo(command_context):
        assert (os.path.basename(
            command_context.virtualenv_manager.virtualenv_root) == "env_foo")
        inner_function("foo")

    @Command("cmd_bar", category="testing", virtualenv_name="env_bar")
    def run_bar(command_context):
        assert (os.path.basename(
            command_context.virtualenv_manager.virtualenv_root) == "env_bar")
        inner_function("bar")

    registrar.dispatch("cmd_foo", context)
    inner_function.assert_called_with("foo")
    registrar.dispatch("cmd_bar", context)
    inner_function.assert_called_with("bar")