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] ")
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] ")
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)
def get_result(self, interactive=False, interactive_default=True): if interactive is False and self.attributes['interactive'] is True: return self.STATUS_ACTION_SKIPPED if self.triggered and not self.has_been_triggered: LOG.debug(_("skipping {} because it wasn't triggered").format(self.id)) return self.STATUS_ACTION_SKIPPED if self.unless: unless_result = self.bundle.node.run( self.unless, may_fail=True, ) if unless_result.return_code == 0: LOG.debug(_("{node}:action:{name}: failed 'unless', not running").format( name=self.name, node=self.bundle.node.name, )) return self.STATUS_ACTION_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_ACTION_SKIPPED try: self.run(interactive=interactive) return self.STATUS_ACTION_OK except ActionFailure: return self.STATUS_ACTION_FAILED
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: status_before = self.get_status() if self.unless and not status_before.correct: unless_result = self.node.run(self.unless, may_fail=True) if unless_result.return_code == 0: LOG.debug(_("'unless' for {} succeeded, not fixing").format(self.id)) status_code = self.STATUS_SKIPPED if status_code is None: 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