mirror of
https://github.com/nvbn/thefuck.git
synced 2025-03-19 00:58:56 +00:00
Implement smart_rule and tests
This commit is contained in:
parent
f8d6993c91
commit
1ac3842874
33
tests/rules/test_smart_rule.py
Normal file
33
tests/rules/test_smart_rule.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import pytest
|
||||||
|
from mock import patch, MagicMock
|
||||||
|
from thefuck.rules.smart_rule import match, get_new_command
|
||||||
|
|
||||||
|
|
||||||
|
def test_match_simple():
|
||||||
|
assert match('')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('script, socket_response, new_command', [
|
||||||
|
('git push', [b'\x03', b'\x25', b'git push --set-upstream origin master', b'\x16',
|
||||||
|
b'git push origin master', b'\x17', b'git push origin develop'],
|
||||||
|
['git push --set-upstream origin master', 'git push origin master', 'git push origin develop']),
|
||||||
|
('ls', [b'\x01', b'\x06', b'ls -la'], ['ls -la'])
|
||||||
|
])
|
||||||
|
@patch('thefuck.rules.smart_rule.socket')
|
||||||
|
def test_get_new_command(socket_mock, script, socket_response, new_command):
|
||||||
|
sock_mock = MagicMock()
|
||||||
|
recv_mock = MagicMock(side_effect=socket_response)
|
||||||
|
socket_mock.socket.return_value = sock_mock
|
||||||
|
sock_mock.recv = recv_mock
|
||||||
|
returned_commands = get_new_command(script)
|
||||||
|
assert returned_commands == new_command
|
||||||
|
|
||||||
|
|
||||||
|
@patch('thefuck.rules.smart_rule.socket')
|
||||||
|
def test_socket_open_close_connect(socket_mock):
|
||||||
|
sock_mock = MagicMock()
|
||||||
|
socket_mock.socket.return_value = sock_mock
|
||||||
|
get_new_command('')
|
||||||
|
socket_mock.socket.assert_called_once()
|
||||||
|
sock_mock.connect.assert_called_once()
|
||||||
|
sock_mock.close.assert_called_once()
|
29
thefuck/rules/smart_rule.py
Normal file
29
thefuck/rules/smart_rule.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import sys
|
||||||
|
import socket
|
||||||
|
|
||||||
|
from ..conf import settings
|
||||||
|
|
||||||
|
SHELL_LOGGER_SOCKET = '__SHELL_LOGGER_SOCKET'
|
||||||
|
|
||||||
|
|
||||||
|
def match(command):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def get_new_command(command):
|
||||||
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
|
list_of_commands = []
|
||||||
|
try:
|
||||||
|
sock.connect(settings.env.get(SHELL_LOGGER_SOCKET))
|
||||||
|
sock.sendall(command)
|
||||||
|
number_of_strings = int.from_bytes(sock.recv(1), sys.byteorder)
|
||||||
|
for i in range(number_of_strings):
|
||||||
|
length_of_string = int.from_bytes(sock.recv(1), sys.byteorder)
|
||||||
|
list_of_commands.append(sock.recv(length_of_string).decode('utf-8'))
|
||||||
|
finally:
|
||||||
|
sock.close()
|
||||||
|
return list_of_commands
|
||||||
|
|
||||||
|
|
||||||
|
# Make last priority
|
||||||
|
priority = 10000
|
Loading…
x
Reference in New Issue
Block a user