1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-05 18:31:10 +01:00

#N/A: Add workon_doesnt_exists rule

This commit is contained in:
Vladimir Iakovlev 2016-08-13 20:55:00 +03:00
parent ae3e231a5f
commit a2693bd737
3 changed files with 68 additions and 1 deletions

View File

@ -227,7 +227,8 @@ using the matched rule and runs it. Rules enabled by default are as follows:
* `tmux` – fixes `tmux` commands;
* `unknown_command` – fixes hadoop hdfs-style "unknown command", for example adds missing '-' to the command on `hdfs dfs ls`;
* `vagrant_up` – starts up the vagrant instance;
* `whois` – fixes `whois` command.
* `whois` – fixes `whois` command;
* `workon_doesnt_exists` – fixes `virtualenvwrapper` env name os suggests to create new.
Enabled by default only on specific platforms:

View File

@ -0,0 +1,30 @@
import pytest
from thefuck.rules.workon_doesnt_exists import match, get_new_command
from tests.utils import Command
@pytest.fixture(autouse=True)
def envs(mocker):
return mocker.patch(
'thefuck.rules.workon_doesnt_exists._get_all_environments',
return_value=['thefuck', 'code_view'])
@pytest.mark.parametrize('script', [
'workon tehfuck', 'workon code-view', 'workon new-env'])
def test_match(script):
assert match(Command(script))
@pytest.mark.parametrize('script', [
'workon thefuck', 'workon code_view', 'work on tehfuck'])
def test_not_match(script):
assert not match(Command(script))
@pytest.mark.parametrize('script, result', [
('workon tehfuck', 'workon thefuck'),
('workon code-view', 'workon code_view'),
('workon zzzz', 'mkvirtualenv zzzz')])
def test_get_new_command(script, result):
assert get_new_command(Command(script))[0] == result

View File

@ -0,0 +1,36 @@
try:
from pathlib import Path
except ImportError:
from pathlib2 import Path
from thefuck.utils import for_app, replace_command, eager, memoize
@memoize
@eager
def _get_all_environments():
root = Path('~/.virtualenvs').expanduser()
if not root.is_dir():
return
for child in root.iterdir():
if child.is_dir():
yield child.name
@for_app('workon')
def match(command):
return (len(command.script_parts) >= 2
and command.script_parts[1] not in _get_all_environments())
def get_new_command(command):
misspelled_env = command.script_parts[1]
create_new = u'mkvirtualenv {}'.format(misspelled_env)
available = _get_all_environments()
if available:
return replace_command(command, misspelled_env, available) \
+ [create_new]
else:
return create_new