mirror of
https://github.com/nvbn/thefuck.git
synced 2025-11-04 17:12:05 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54d82f9528 | ||
|
|
888756d519 | ||
|
|
d5b4bddc4c | ||
|
|
d09238a6e8 | ||
|
|
c6c3756caf | ||
|
|
275574beae | ||
|
|
de4b774134 | ||
|
|
3af5c80d29 | ||
|
|
bd5f5045aa | ||
|
|
798928b5ad | ||
|
|
82e2c89472 |
2
setup.py
2
setup.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|||||||
|
|
||||||
|
|
||||||
setup(name='thefuck',
|
setup(name='thefuck',
|
||||||
version=1.20,
|
version="1.23",
|
||||||
description="Magnificent app which corrects your previous console command",
|
description="Magnificent app which corrects your previous console command",
|
||||||
author='Vladimir Iakovlev',
|
author='Vladimir Iakovlev',
|
||||||
author_email='nvbn.rm@gmail.com',
|
author_email='nvbn.rm@gmail.com',
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ def test_load_rule():
|
|||||||
return_value=Mock(
|
return_value=Mock(
|
||||||
match=match,
|
match=match,
|
||||||
get_new_command=get_new_command)) as load_source:
|
get_new_command=get_new_command)) as load_source:
|
||||||
assert main.load_rule(Path('/rules/bash.py')) == main.Rule(match, get_new_command)
|
assert main.load_rule(Path('/rules/bash.py')) == main.Rule('bash', match, get_new_command)
|
||||||
load_source.assert_called_once_with('bash', '/rules/bash.py')
|
load_source.assert_called_once_with('bash', '/rules/bash.py')
|
||||||
|
|
||||||
|
|
||||||
@@ -35,14 +35,14 @@ def test_get_rules():
|
|||||||
glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
|
glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
|
||||||
assert main.get_rules(
|
assert main.get_rules(
|
||||||
Path('~'),
|
Path('~'),
|
||||||
Mock(rules=None)) == [main.Rule('bash', 'bash'),
|
Mock(rules=None)) == [main.Rule('bash', 'bash', 'bash'),
|
||||||
main.Rule('lisp', 'lisp'),
|
main.Rule('lisp', 'lisp', 'lisp'),
|
||||||
main.Rule('bash', 'bash'),
|
main.Rule('bash', 'bash', 'bash'),
|
||||||
main.Rule('lisp', 'lisp')]
|
main.Rule('lisp', 'lisp', 'lisp')]
|
||||||
assert main.get_rules(
|
assert main.get_rules(
|
||||||
Path('~'),
|
Path('~'),
|
||||||
Mock(rules=['bash'])) == [main.Rule('bash', 'bash'),
|
Mock(rules=['bash'])) == [main.Rule('bash', 'bash', 'bash'),
|
||||||
main.Rule('bash', 'bash')]
|
main.Rule('bash', 'bash', 'bash')]
|
||||||
|
|
||||||
|
|
||||||
def test_get_command():
|
def test_get_command():
|
||||||
@@ -61,24 +61,28 @@ def test_get_command():
|
|||||||
stdout=PIPE,
|
stdout=PIPE,
|
||||||
stderr=PIPE,
|
stderr=PIPE,
|
||||||
env={'LANG': 'C'})
|
env={'LANG': 'C'})
|
||||||
|
assert main.get_command(Mock(), ['']) is None
|
||||||
|
|
||||||
|
|
||||||
def test_get_matched_rule():
|
def test_get_matched_rule(capsys):
|
||||||
rules = [main.Rule(lambda x, _: x.script == 'cd ..', None),
|
rules = [main.Rule('', lambda x, _: x.script == 'cd ..', None),
|
||||||
main.Rule(lambda *_: False, None)]
|
main.Rule('', lambda *_: False, None),
|
||||||
|
main.Rule('rule', Mock(side_effect=OSError('Denied')), None)]
|
||||||
assert main.get_matched_rule(main.Command('ls', '', ''),
|
assert main.get_matched_rule(main.Command('ls', '', ''),
|
||||||
rules, None) is None
|
rules, None) is None
|
||||||
assert main.get_matched_rule(main.Command('cd ..', '', ''),
|
assert main.get_matched_rule(main.Command('cd ..', '', ''),
|
||||||
rules, None) == rules[0]
|
rules, None) == rules[0]
|
||||||
|
assert capsys.readouterr()[1].split('\n')[0]\
|
||||||
|
== '[WARN] rule: Traceback (most recent call last):'
|
||||||
|
|
||||||
|
|
||||||
def test_run_rule(capsys):
|
def test_run_rule(capsys):
|
||||||
with patch('thefuck.main.confirm', return_value=True):
|
with patch('thefuck.main.confirm', return_value=True):
|
||||||
main.run_rule(main.Rule(None, lambda *_: 'new-command'),
|
main.run_rule(main.Rule('', None, lambda *_: 'new-command'),
|
||||||
None, None)
|
None, None)
|
||||||
assert capsys.readouterr() == ('new-command\n', '')
|
assert capsys.readouterr() == ('new-command\n', '')
|
||||||
with patch('thefuck.main.confirm', return_value=False):
|
with patch('thefuck.main.confirm', return_value=False):
|
||||||
main.run_rule(main.Rule(None, lambda *_: 'new-command'),
|
main.run_rule(main.Rule('', None, lambda *_: 'new-command'),
|
||||||
None, None)
|
None, None)
|
||||||
assert capsys.readouterr() == ('', '')
|
assert capsys.readouterr() == ('', '')
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ from os.path import expanduser
|
|||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from traceback import format_exception
|
||||||
from psutil import Process, TimeoutExpired
|
from psutil import Process, TimeoutExpired
|
||||||
|
|
||||||
|
|
||||||
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
||||||
Rule = namedtuple('Rule', ('match', 'get_new_command'))
|
Rule = namedtuple('Rule', ('name', 'match', 'get_new_command'))
|
||||||
|
|
||||||
|
|
||||||
def setup_user_dir():
|
def setup_user_dir():
|
||||||
@@ -43,7 +44,8 @@ def is_rule_enabled(settings, rule):
|
|||||||
def load_rule(rule):
|
def load_rule(rule):
|
||||||
"""Imports rule module and returns it."""
|
"""Imports rule module and returns it."""
|
||||||
rule_module = load_source(rule.name[:-3], str(rule))
|
rule_module = load_source(rule.name[:-3], str(rule))
|
||||||
return Rule(rule_module.match, rule_module.get_new_command)
|
return Rule(rule.name[:-3], rule_module.match,
|
||||||
|
rule_module.get_new_command)
|
||||||
|
|
||||||
|
|
||||||
def get_rules(user_dir, settings):
|
def get_rules(user_dir, settings):
|
||||||
@@ -80,6 +82,10 @@ def get_command(settings, args):
|
|||||||
script = ' '.join(arg.decode('utf-8') for arg in args[1:])
|
script = ' '.join(arg.decode('utf-8') for arg in args[1:])
|
||||||
else:
|
else:
|
||||||
script = ' '.join(args[1:])
|
script = ' '.join(args[1:])
|
||||||
|
|
||||||
|
if not script:
|
||||||
|
return
|
||||||
|
|
||||||
result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE,
|
result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE,
|
||||||
env=dict(os.environ, LANG='C'))
|
env=dict(os.environ, LANG='C'))
|
||||||
if wait_output(settings, result):
|
if wait_output(settings, result):
|
||||||
@@ -90,8 +96,12 @@ def get_command(settings, args):
|
|||||||
def get_matched_rule(command, rules, settings):
|
def get_matched_rule(command, rules, settings):
|
||||||
"""Returns first matched rule for command."""
|
"""Returns first matched rule for command."""
|
||||||
for rule in rules:
|
for rule in rules:
|
||||||
if rule.match(command, settings):
|
try:
|
||||||
return rule
|
if rule.match(command, settings):
|
||||||
|
return rule
|
||||||
|
except Exception:
|
||||||
|
sys.stderr.write(u'[WARN] {}: {}---------------------\n\n'.format(
|
||||||
|
rule.name, ''.join(format_exception(*sys.exc_info()))))
|
||||||
|
|
||||||
|
|
||||||
def confirm(new_command, settings):
|
def confirm(new_command, settings):
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ def _safe(fn, fallback):
|
|||||||
|
|
||||||
def _get_all_bins():
|
def _get_all_bins():
|
||||||
return [exe.name
|
return [exe.name
|
||||||
for path in os.environ['PATH'].split(':')
|
for path in os.environ.get('PATH', '').split(':')
|
||||||
for exe in _safe(Path(path).iterdir, [])
|
for exe in _safe(lambda: list(Path(path).iterdir()), [])
|
||||||
if not _safe(exe.is_dir, True)]
|
if not _safe(exe.is_dir, True)]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ patterns = ['permission denied',
|
|||||||
'pkg: Insufficient privileges',
|
'pkg: Insufficient privileges',
|
||||||
'you cannot perform this operation unless you are root',
|
'you cannot perform this operation unless you are root',
|
||||||
'non-root users cannot',
|
'non-root users cannot',
|
||||||
'Operation not permitted']
|
'Operation not permitted',
|
||||||
|
'root privilege',
|
||||||
|
'This command has to be run under the root user.']
|
||||||
|
|
||||||
|
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
|
|||||||
Reference in New Issue
Block a user