mirror of
https://github.com/nvbn/thefuck.git
synced 2025-11-01 15:42:06 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0009fb0588 | ||
|
|
a9d3456e29 | ||
|
|
1e28671934 | ||
|
|
3134a60e27 | ||
|
|
03dd7eda04 | ||
|
|
d12a8bcdd8 | ||
|
|
58069f0a3e | ||
|
|
d0e02bc20c | ||
|
|
e554238996 | ||
|
|
fa465620ba | ||
|
|
294ba07ce1 | ||
|
|
1fa7827f1a | ||
|
|
93b6a623e1 |
@@ -156,6 +156,7 @@ using matched rule and run it. Rules enabled by default:
|
||||
|
||||
* `brew_unknown_command` – fixes wrong brew commands, for example `brew docto/brew doctor`;
|
||||
* `cd_parent` – changes `cd..` to `cd ..`;
|
||||
* `cd_mkdir` – creates directories before cd'ing into them;
|
||||
* `cp_omitting_directory` – adds `-a` when you `cp` directory;
|
||||
* `fix_alt_space` – replaces Alt+Space with Space character;
|
||||
* `git_no_command` – fixes wrong git commands like `git brnch`;
|
||||
|
||||
2
setup.py
2
setup.py
@@ -1,7 +1,7 @@
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
|
||||
VERSION = '1.29'
|
||||
VERSION = '1.30'
|
||||
|
||||
|
||||
setup(name='thefuck',
|
||||
|
||||
19
tests/rules/test_cd_mkdir.py
Normal file
19
tests/rules/test_cd_mkdir.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from mock import Mock
|
||||
from thefuck.rules.cd_mkdir import match, get_new_command
|
||||
|
||||
|
||||
def test_match():
|
||||
assert match(Mock(script='cd foo', stderr='cd: foo: No such file or directory'),
|
||||
None)
|
||||
assert match(Mock(script='cd foo/bar/baz', stderr='cd: foo: No such file or directory'),
|
||||
None)
|
||||
assert match(Mock(script='cd foo/bar/baz', stderr='cd: can\'t cd to foo/bar/baz'),
|
||||
None)
|
||||
assert not match(Mock(script='cd foo',
|
||||
stderr=''), None)
|
||||
assert not match(Mock(script='', stderr=''), None)
|
||||
|
||||
|
||||
def test_get_new_command():
|
||||
assert get_new_command(Mock(script='cd foo'), None) == 'mkdir -p foo && cd foo'
|
||||
assert get_new_command(Mock(script='cd foo/bar/baz'), None) == 'mkdir -p foo/bar/baz && cd foo/bar/baz'
|
||||
@@ -1,3 +1,4 @@
|
||||
import six
|
||||
from mock import patch, Mock
|
||||
from thefuck.types import Rule
|
||||
from thefuck import conf
|
||||
@@ -59,3 +60,30 @@ def test_settings_from_env_with_DEFAULT():
|
||||
patch('thefuck.conf.os.environ', new_callable=lambda: {'THEFUCK_RULES': 'DEFAULT_RULES:bash:lisp'}):
|
||||
settings = conf.get_settings(Mock())
|
||||
assert settings.rules == conf.DEFAULT_RULES + ['bash', 'lisp']
|
||||
|
||||
|
||||
def test_initialize_settings_file_ignore_if_exists():
|
||||
settings_path_mock = Mock(is_file=Mock(return_value=True), open=Mock())
|
||||
user_dir_mock = Mock(joinpath=Mock(return_value=settings_path_mock))
|
||||
conf.initialize_settings_file(user_dir_mock)
|
||||
assert settings_path_mock.is_file.call_count == 1
|
||||
assert not settings_path_mock.open.called
|
||||
|
||||
|
||||
def test_initialize_settings_file_create_if_exists_not():
|
||||
settings_file = six.StringIO()
|
||||
settings_path_mock = Mock(
|
||||
is_file=Mock(return_value=False),
|
||||
open=Mock(return_value=Mock(
|
||||
__exit__=lambda *args: None, __enter__=lambda *args: settings_file
|
||||
)),
|
||||
)
|
||||
user_dir_mock = Mock(joinpath=Mock(return_value=settings_path_mock))
|
||||
conf.initialize_settings_file(user_dir_mock)
|
||||
settings_file_contents = settings_file.getvalue()
|
||||
assert settings_path_mock.is_file.call_count == 1
|
||||
assert settings_path_mock.open.call_count == 1
|
||||
assert conf.SETTINGS_HEADER in settings_file_contents
|
||||
for setting in conf.DEFAULT_SETTINGS.items():
|
||||
assert '# {} = {}\n'.format(*setting) in settings_file_contents
|
||||
settings_file.close()
|
||||
|
||||
@@ -35,6 +35,19 @@ ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
|
||||
'THEFUCK_NO_COLORS': 'no_colors'}
|
||||
|
||||
|
||||
SETTINGS_HEADER = u"""# ~/.thefuck/settings.py: The Fuck settings file
|
||||
#
|
||||
# The rules are defined as in the example bellow:
|
||||
#
|
||||
# rules = ['cd_parent', 'git_push', 'python_command', 'sudo']
|
||||
#
|
||||
# The default values are as follows. Uncomment and change to fit your needs.
|
||||
# See https://github.com/nvbn/thefuck#settings for more information.
|
||||
#
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def _settings_from_file(user_dir):
|
||||
"""Loads settings from file."""
|
||||
settings = load_source('settings',
|
||||
@@ -92,3 +105,12 @@ def get_settings(user_dir):
|
||||
conf['rules'] = types.RulesNamesList(conf['rules'])
|
||||
|
||||
return types.Settings(conf)
|
||||
|
||||
|
||||
def initialize_settings_file(user_dir):
|
||||
settings_path = user_dir.joinpath('settings.py')
|
||||
if not settings_path.is_file():
|
||||
with settings_path.open(mode='w') as settings_file:
|
||||
settings_file.write(SETTINGS_HEADER)
|
||||
for setting in DEFAULT_SETTINGS.items():
|
||||
settings_file.write(u'# {} = {}\n'.format(*setting))
|
||||
|
||||
@@ -15,7 +15,7 @@ def setup_user_dir():
|
||||
rules_dir = user_dir.joinpath('rules')
|
||||
if not rules_dir.is_dir():
|
||||
rules_dir.mkdir(parents=True)
|
||||
user_dir.joinpath('settings.py').touch()
|
||||
conf.initialize_settings_file(user_dir)
|
||||
return user_dir
|
||||
|
||||
|
||||
|
||||
23
thefuck/rules/apt_get.py
Normal file
23
thefuck/rules/apt_get.py
Normal file
@@ -0,0 +1,23 @@
|
||||
try:
|
||||
import CommandNotFound
|
||||
except ImportError:
|
||||
enabled_by_default = False
|
||||
|
||||
|
||||
def match(command, settings):
|
||||
if 'not found' in command.stderr:
|
||||
try:
|
||||
c = CommandNotFound.CommandNotFound()
|
||||
pkgs = c.getPackages(command.script.split(" ")[0])
|
||||
name, _ = pkgs[0]
|
||||
return True
|
||||
except IndexError:
|
||||
# IndexError is thrown when no matching package is found
|
||||
return False
|
||||
|
||||
|
||||
def get_new_command(command, settings):
|
||||
c = CommandNotFound.CommandNotFound()
|
||||
pkgs = c.getPackages(command.script.split(" ")[0])
|
||||
name, _ = pkgs[0]
|
||||
return "sudo apt-get install {} && {}".format(name, command.script)
|
||||
14
thefuck/rules/cd_mkdir.py
Normal file
14
thefuck/rules/cd_mkdir.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import re
|
||||
from thefuck.utils import sudo_support
|
||||
|
||||
|
||||
@sudo_support
|
||||
def match(command, settings):
|
||||
return (command.script.startswith('cd ')
|
||||
and ('no such file or directory' in command.stderr.lower()
|
||||
or 'cd: can\'t cd to' in command.stderr.lower()))
|
||||
|
||||
|
||||
@sudo_support
|
||||
def get_new_command(command, settings):
|
||||
return re.sub(r'^cd (.*)', 'mkdir -p \\1 && cd \\1', command.script)
|
||||
Reference in New Issue
Block a user