def test_get_corrected_commands(mocker): command = Command("test", "test", "test") rules = [ Rule(match=lambda _: False), Rule(match=lambda _: True, get_new_command=lambda x: x.script + "!", priority=100), Rule(match=lambda _: True, get_new_command=lambda x: [x.script + "@", x.script + ";"], priority=60), ] mocker.patch("thefuck.corrector.get_rules", return_value=rules) assert [cmd.script for cmd in get_corrected_commands(command)] == ["test!", "test@", "test;"]
def test_get_corrected_commands(mocker): command = Command('test', 'test', 'test') rules = [Rule(match=lambda *_: False), Rule(match=lambda *_: True, get_new_command=lambda x, _: x.script + '!', priority=100), Rule(match=lambda *_: True, get_new_command=lambda x, _: [x.script + '@', x.script + ';'], priority=60)] mocker.patch('thefuck.corrector.get_rules', return_value=rules) assert [cmd.script for cmd in get_corrected_commands(command, None, Mock(debug=False))] \ == ['test@', 'test!', 'test;']
def test_get_corrected_commands(mocker): command = Command('test', 'test', 'test') rules = [Rule(match=lambda _: False), Rule(match=lambda _: True, get_new_command=lambda x: x.script + '!', priority=100), Rule(match=lambda _: True, get_new_command=lambda x: [x.script + '@', x.script + ';'], priority=60)] mocker.patch('thefuck.corrector.get_rules', return_value=rules) assert [cmd.script for cmd in get_corrected_commands(command)] \ == ['test!', 'test@', 'test;']
def process_event(self, event, *args, **kwargs): has_failed = kwargs.get('has_failed') if has_failed: _input = self.get_input(event) exp = shell.from_shell(_input) output = get_output(_input, exp) cmd = Command(exp, output) corrected_cmds = [x.script for x in get_corrected_commands(cmd)] if corrected_cmds: self.logger.msg( eventid='cowrie.command.misspell', input=_input, corrected_commands=corrected_cmds, format= 'Misspelled command: %(input)s | Found corrections: %(corrected_commands)s' )
def post_execute(self, state: State) -> Action: if state.result_code == '0': return Action(suggested_command=state.command) cmd = str(state.command) stderr = str(state.stderr) try: # Get corrected command from `thefuck` bot settings.init() cmd = Command(cmd, stderr) cmd_corrected = get_corrected_commands(cmd) cmd_to_run = next(cmd_corrected).script except Exception: return Action(suggested_command=state.command, confidence=0.1) else: return Action(description=Colorize().info().append( "Maybe you want to try: {}".format(cmd_to_run)).to_console(), confidence=0.8)
def receive_command(command, connection_socket): corrected_commands = get_corrected_commands(command) send_to_client(connection_socket, corrected_commands)
def test_no_commands_found(command): corrected_commands = list(get_corrected_commands(command)) assert len(corrected_commands) == 0
def test_get_correct_command(command, uid, result): setUID(uid) corrected_commands = list(get_corrected_commands(command)) assert len(corrected_commands) > 0 assert (corrected_commands[0].script == result)
def test_none_root_commands(command, result): setUID(1234) corrected_commands = list(get_corrected_commands(command)) assert len(corrected_commands) > 0 assert (result in [corrected.script for corrected in corrected_commands])