Esempio n. 1
0
 def test_yes(self):
     get_input = MagicMock(return_value="y")
     self.assertEqual(
         ui.ask_interactively("OHAI?", True, get_input=get_input),
         True,
     )
     get_input.assert_called_once_with("OHAI? [Y/n] ")
Esempio n. 2
0
 def test_default_no(self):
     get_input = MagicMock(return_value="")
     self.assertEqual(
         ui.ask_interactively("OHAI?", False, get_input=get_input),
         False,
     )
     get_input.assert_called_once_with("OHAI? [y/N] ")
Esempio n. 3
0
    def apply(self, interactive=False, interactive_default=True):
        self.node.repo.hooks.item_apply_start(
            self.node.repo,
            self.node,
            self,
        )
        status_code = None
        status_before = None
        status_after = None
        start_time = datetime.now()

        if self.triggered and not self.has_been_triggered:
            LOG.debug(_("skipping {} because it wasn't triggered").format(self.id))
            status_code = self.STATUS_SKIPPED

        if status_code is None and self.cached_unless_result:
            LOG.debug(_("'unless' for {} succeeded, not fixing").format(self.id))
            status_code = self.STATUS_SKIPPED

        if status_code is None:
            status_before = self.cached_status
            if status_before.correct:
                status_code = self.STATUS_OK

        if status_code is None:
            if not interactive:
                self.fix(status_before)
                status_after = self.get_status()
            else:
                question = wrap_question(
                    self.id,
                    self.ask(status_before),
                    _("Fix {}?").format(bold(self.id)),
                )
                if ask_interactively(question,
                                     interactive_default):
                    self.fix(status_before)
                    status_after = self.get_status()
                else:
                    status_code = self.STATUS_SKIPPED

        if status_code is None:
            if status_after.correct:
                status_code = self.STATUS_FIXED
            else:
                status_code = self.STATUS_FAILED

        self.node.repo.hooks.item_apply_end(
            self.node.repo,
            self.node,
            self,
            duration=datetime.now() - start_time,
            status_code=status_code,
            status_before=status_before,
            status_after=status_after,
        )

        return status_code
Esempio n. 4
0
    def test_invalid_input(self):
        answers = ["wat", "zomg", "\n", "y"]

        def side_effect(*args):
            return answers.pop(0)

        get_input = MagicMock(side_effect=side_effect)
        self.assertEqual(
            ui.ask_interactively("OHAI?", False, get_input=get_input),
            True,
        )
        self.assertEqual(get_input.call_count, 4)
Esempio n. 5
0
    def _get_result(self, interactive=False, interactive_default=True):
        if interactive is False and self.attributes['interactive'] is True:
            return self.STATUS_SKIPPED

        if self.triggered and not self.has_been_triggered:
            LOG.debug(_("skipping {} because it wasn't triggered").format(self.id))
            return self.STATUS_SKIPPED

        if self.unless:
            unless_result = self.bundle.node.run(
                self.unless,
                may_fail=True,
            )
            if unless_result.return_code == 0:
                LOG.debug(_("{node}:{bundle}:action:{name}: failed 'unless', not running").format(
                    bundle=self.bundle.name,
                    name=self.name,
                    node=self.bundle.node.name,
                ))
                return self.STATUS_SKIPPED

        if (
            interactive and
            self.attributes['interactive'] is not False
            and not ask_interactively(
                wrap_question(
                    self.id,
                    self.attributes['command'],
                    _("Run action {}?").format(
                        bold(self.name),
                    ),
                ),
                interactive_default,
            )
        ):
            return self.STATUS_SKIPPED
        try:
            self.run(interactive=interactive)
            return self.STATUS_ACTION_SUCCEEDED
        except ActionFailure:
            return self.STATUS_FAILED