Skip to content

Commit

Permalink
refact(rules.no_command): do not add TF_ALIAS to the “callables” list
Browse files Browse the repository at this point in the history
  • Loading branch information
scorphus committed Jun 12, 2015
1 parent c7bbadc commit fa92f60
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
45 changes: 31 additions & 14 deletions tests/rules/test_no_command.py
@@ -1,19 +1,36 @@
from mock import patch, Mock
from thefuck.rules.no_command import match, get_new_command
from thefuck.rules.no_command import match, get_new_command, _get_all_callables


def test_match():
with patch('thefuck.rules.no_command._get_all_callables',
return_value=['vim', 'apt-get']):
assert match(Mock(stderr='vom: not found', script='vom file.py'), None)
assert not match(Mock(stderr='qweqwe: not found', script='qweqwe'), None)
assert not match(Mock(stderr='some text', script='vom file.py'), None)
@patch('thefuck.rules.no_command._safe', return_value=[])
@patch('thefuck.rules.no_command.get_aliases',
return_value=['vim', 'apt-get', 'fsck', 'fuck'])
def test_get_all_callables(*args):
all_callables = _get_all_callables()
assert 'vim' in all_callables
assert 'fsck' in all_callables
assert 'fuck' not in all_callables


def test_get_new_command():
with patch('thefuck.rules.no_command._get_all_callables',
return_value=['vim', 'apt-get']):
assert get_new_command(
Mock(stderr='vom: not found',
script='vom file.py'),
None) == 'vim file.py'
@patch('thefuck.rules.no_command._safe', return_value=[])
@patch('thefuck.rules.no_command.get_aliases',
return_value=['vim', 'apt-get', 'fsck', 'fuck'])
def test_match(*args):
assert match(Mock(stderr='vom: not found', script='vom file.py'), None)
assert match(Mock(stderr='fucck: not found', script='fucck'), None)
assert not match(Mock(stderr='qweqwe: not found', script='qweqwe'), None)
assert not match(Mock(stderr='some text', script='vom file.py'), None)


@patch('thefuck.rules.no_command._safe', return_value=[])
@patch('thefuck.rules.no_command.get_aliases',
return_value=['vim', 'apt-get', 'fsck', 'fuck'])
def test_get_new_command(*args):
assert get_new_command(
Mock(stderr='vom: not found',
script='vom file.py'),
None) == 'vim file.py'
assert get_new_command(
Mock(stderr='fucck: not found',
script='fucck'),
None) == 'fsck'
6 changes: 4 additions & 2 deletions thefuck/rules/no_command.py
Expand Up @@ -2,7 +2,7 @@
import os
from pathlib import Path
from thefuck.utils import sudo_support
from thefuck.shells import get_aliases
from thefuck.shells import thefuck_alias, get_aliases


def _safe(fn, fallback):
Expand All @@ -13,10 +13,12 @@ def _safe(fn, fallback):


def _get_all_callables():
tf_alias = thefuck_alias()
return [exe.name
for path in os.environ.get('PATH', '').split(':')
for exe in _safe(lambda: list(Path(path).iterdir()), [])
if not _safe(exe.is_dir, True)] + get_aliases()
if not _safe(exe.is_dir, True)] + [
alias for alias in get_aliases() if alias != tf_alias]


@sudo_support
Expand Down

0 comments on commit fa92f60

Please sign in to comment.