mirror of
https://github.com/nvbn/thefuck.git
synced 2025-03-19 00:58:56 +00:00
Fix smart rule tests based on string encoding over socket
This commit is contained in:
parent
c8fe0f771f
commit
72b6831485
@ -134,10 +134,9 @@ eval $(thefuck --alias)
|
|||||||
eval $(thefuck --alias FUCK)
|
eval $(thefuck --alias FUCK)
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to enable , add place these commands in your `.bash_profile`, `.bashrc`, `.zshrc` or other startup script:
|
If you want to enable the experimental smart rule, place this command in your `.bash_profile`, `.bashrc`, `.zshrc` or other startup script:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export SHELL_PATH=$(PWD)
|
|
||||||
eval $(thefuck --enable-experimental-shell-history)
|
eval $(thefuck --enable-experimental-shell-history)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from mock import patch, MagicMock
|
from mock import patch, MagicMock
|
||||||
from thefuck.rules.smart_rule import match, get_new_command
|
from thefuck.rules.smart_rule import match, get_new_command
|
||||||
|
from thefuck.types import Command
|
||||||
|
|
||||||
|
|
||||||
def test_match_simple():
|
def test_match_simple():
|
||||||
@ -19,7 +20,7 @@ def test_get_new_command(socket_mock, script, socket_response, new_command):
|
|||||||
recv_mock = MagicMock(side_effect=socket_response)
|
recv_mock = MagicMock(side_effect=socket_response)
|
||||||
socket_mock.socket.return_value = sock_mock
|
socket_mock.socket.return_value = sock_mock
|
||||||
sock_mock.recv = recv_mock
|
sock_mock.recv = recv_mock
|
||||||
returned_commands = get_new_command(script)
|
returned_commands = get_new_command(Command(script, None))
|
||||||
assert returned_commands == new_command
|
assert returned_commands == new_command
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,6 +29,8 @@ DEFAULT_RULES = [ALL_ENABLED]
|
|||||||
DEFAULT_PRIORITY = 1000
|
DEFAULT_PRIORITY = 1000
|
||||||
|
|
||||||
SHELL_LOGGER_SOCKET_ENV_VAR = '__SHELL_LOGGER_SOCKET'
|
SHELL_LOGGER_SOCKET_ENV_VAR = '__SHELL_LOGGER_SOCKET'
|
||||||
|
SHELL_LOGGER_DB_ENV_VAR = '__SHELL_LOGGER_DB_PATH'
|
||||||
|
SHELL_LOGGER_DB_FILENAME = 'my.db'
|
||||||
SHELL_LOGGER_SOCKET_PATH = '/tmp/tf_socket'
|
SHELL_LOGGER_SOCKET_PATH = '/tmp/tf_socket'
|
||||||
SHELL_LOGGER_BINARY_FILENAME = 'shell_logger'
|
SHELL_LOGGER_BINARY_FILENAME = 'shell_logger'
|
||||||
|
|
||||||
|
@ -8,7 +8,8 @@ import psutil
|
|||||||
from psutil import ZombieProcess
|
from psutil import ZombieProcess
|
||||||
|
|
||||||
from ..conf import settings
|
from ..conf import settings
|
||||||
from ..const import SHELL_LOGGER_SOCKET_ENV_VAR, SHELL_LOGGER_SOCKET_PATH, SHELL_LOGGER_BINARY_FILENAME
|
from ..const import SHELL_LOGGER_SOCKET_ENV_VAR, SHELL_LOGGER_SOCKET_PATH, \
|
||||||
|
SHELL_LOGGER_BINARY_FILENAME, SHELL_LOGGER_DB_FILENAME, SHELL_LOGGER_DB_ENV_VAR
|
||||||
from ..logs import warn, debug
|
from ..logs import warn, debug
|
||||||
from ..shells import shell
|
from ..shells import shell
|
||||||
from ..utils import which
|
from ..utils import which
|
||||||
@ -42,20 +43,25 @@ def print_experimental_shell_history(known_args):
|
|||||||
filename_suffix = sys.platform
|
filename_suffix = sys.platform
|
||||||
client_release = 'https://www.dropbox.com/s/m0jqp8i4c6woko5/client?dl=1'
|
client_release = 'https://www.dropbox.com/s/m0jqp8i4c6woko5/client?dl=1'
|
||||||
binary_path = '{}/{}'.format(settings.data_dir, SHELL_LOGGER_BINARY_FILENAME)
|
binary_path = '{}/{}'.format(settings.data_dir, SHELL_LOGGER_BINARY_FILENAME)
|
||||||
|
db_path = '{}/{}'.format(settings.data_dir, SHELL_LOGGER_DB_FILENAME)
|
||||||
|
|
||||||
debug('Downloading the shell_logger release and putting it in the path ... ')
|
debug('Downloading the shell_logger release and putting it in the path ... ')
|
||||||
urllib.request.urlretrieve(client_release, binary_path)
|
urllib.request.urlretrieve(client_release, binary_path)
|
||||||
|
|
||||||
subprocess.Popen(['chmod', '+x', binary_path])
|
subprocess.Popen(['chmod', '+x', binary_path])
|
||||||
|
|
||||||
proc = subprocess.Popen([binary_path, '-mode', 'configure'], stdout=subprocess.PIPE)
|
my_env = os.environ.copy()
|
||||||
|
my_env[SHELL_LOGGER_DB_ENV_VAR] = db_path
|
||||||
|
proc = subprocess.Popen([binary_path, '-mode', 'configure'], stdout=subprocess.PIPE,
|
||||||
|
env=my_env)
|
||||||
print(''.join([line.decode() for line in proc.stdout.readlines()]))
|
print(''.join([line.decode() for line in proc.stdout.readlines()]))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# If process is not running, start the process
|
# If process is not running, start the process
|
||||||
if SHELL_LOGGER_BINARY_FILENAME not in (p.name() for p in psutil.process_iter()):
|
if SHELL_LOGGER_BINARY_FILENAME not in (p.name() for p in psutil.process_iter()):
|
||||||
os.spawnve(os.P_NOWAIT, binary_path, [binary_path, '-mode', 'daemon'],
|
os.spawnve(os.P_NOWAIT, binary_path, [binary_path, '-mode', 'daemon'],
|
||||||
env={SHELL_LOGGER_SOCKET_ENV_VAR: SHELL_LOGGER_SOCKET_PATH})
|
env={SHELL_LOGGER_SOCKET_ENV_VAR: SHELL_LOGGER_SOCKET_PATH,
|
||||||
|
SHELL_LOGGER_DB_ENV_VAR: db_path})
|
||||||
except ZombieProcess as e:
|
except ZombieProcess as e:
|
||||||
warn("Zombie process is running. Please kill the running process " % e)
|
warn("Zombie process is running. Please kill the running process " % e)
|
||||||
|
|
||||||
|
@ -1,26 +1,26 @@
|
|||||||
import sys
|
import sys
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from thefuck.conf import settings
|
from thefuck.logs import debug
|
||||||
from thefuck.const import SHELL_LOGGER_SOCKET_PATH
|
from thefuck.const import SHELL_LOGGER_SOCKET_PATH
|
||||||
|
|
||||||
|
|
||||||
def match(command):
|
def match(command):
|
||||||
import ipdb; ipdb.set_trace()
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_new_command(command):
|
def get_new_command(command):
|
||||||
import ipdb; ipdb.set_trace()
|
|
||||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
list_of_commands = []
|
list_of_commands = []
|
||||||
try:
|
try:
|
||||||
sock.connect(settings.env.get(SHELL_LOGGER_SOCKET_PATH))
|
sock.connect(SHELL_LOGGER_SOCKET_PATH)
|
||||||
sock.sendall(command)
|
sock.send(command.script.encode())
|
||||||
number_of_strings = int.from_bytes(sock.recv(1), sys.byteorder)
|
number_of_strings = int.from_bytes(sock.recv(1), sys.byteorder)
|
||||||
|
debug("Number of strings {}".format(number_of_strings))
|
||||||
for i in range(number_of_strings):
|
for i in range(number_of_strings):
|
||||||
length_of_string = int.from_bytes(sock.recv(1), sys.byteorder)
|
length_of_string = int.from_bytes(sock.recv(1), sys.byteorder)
|
||||||
list_of_commands.append(sock.recv(length_of_string).decode('utf-8'))
|
debug("Length {}".format(length_of_string))
|
||||||
|
list_of_commands.append(sock.recv(length_of_string).decode())
|
||||||
finally:
|
finally:
|
||||||
sock.close()
|
sock.close()
|
||||||
return list_of_commands
|
return list_of_commands
|
||||||
|
Loading…
x
Reference in New Issue
Block a user