mirror of
https://github.com/nvbn/thefuck.git
synced 2025-03-20 09:39:01 +00:00
Merge 04ae5e37848a7518c9ad88c7b2cc1c4cc47bf1f2 into 6975d30818792f1b37de702fc93c66023c4c50d5
This commit is contained in:
commit
9138131d30
@ -1,6 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from thefuck.rules.brew_unknown_command import match, get_new_command
|
from thefuck.rules.brew_unknown_command import match, get_new_command
|
||||||
from thefuck.rules.brew_unknown_command import _brew_commands
|
from thefuck.specific.brew import all_brew_commands
|
||||||
from thefuck.types import Command
|
from thefuck.types import Command
|
||||||
|
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ def brew_unknown_cmd2():
|
|||||||
|
|
||||||
def test_match(brew_unknown_cmd):
|
def test_match(brew_unknown_cmd):
|
||||||
assert match(Command('brew inst', brew_unknown_cmd))
|
assert match(Command('brew inst', brew_unknown_cmd))
|
||||||
for command in _brew_commands():
|
for command in all_brew_commands():
|
||||||
assert not match(Command('brew ' + command, ''))
|
assert not match(Command('brew ' + command, ''))
|
||||||
|
|
||||||
|
|
||||||
|
13
tests/specific/test_brew.py
Normal file
13
tests/specific/test_brew.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import pytest
|
||||||
|
from thefuck.specific.brew import brew_available, get_brew_path_prefix, all_brew_commands
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.fixtures('no_memoize')
|
||||||
|
def test_get_brew_path_prefix():
|
||||||
|
assert get_brew_path_prefix() == "/usr/local" if brew_available else not get_brew_path_prefix()
|
||||||
|
|
||||||
|
|
||||||
|
def test_all_brew_commands():
|
||||||
|
assert all_brew_commands() == ['info', 'home', 'options', 'install', 'uninstall',
|
||||||
|
'search', 'list', 'update', 'upgrade', 'pin', 'unpin',
|
||||||
|
'doctor', 'create', 'edit'] if brew_available else not get_brew_path_prefix()
|
@ -1,70 +1,10 @@
|
|||||||
import os
|
|
||||||
import re
|
import re
|
||||||
from thefuck.utils import get_closest, replace_command
|
from thefuck.utils import get_closest, replace_command
|
||||||
from thefuck.specific.brew import get_brew_path_prefix, brew_available
|
from thefuck.specific.brew import brew_available, all_brew_commands
|
||||||
|
|
||||||
BREW_CMD_PATH = '/Library/Homebrew/cmd'
|
|
||||||
TAP_PATH = '/Library/Taps'
|
|
||||||
TAP_CMD_PATH = '/%s/%s/cmd'
|
|
||||||
|
|
||||||
enabled_by_default = brew_available
|
enabled_by_default = brew_available
|
||||||
|
|
||||||
|
|
||||||
def _get_brew_commands(brew_path_prefix):
|
|
||||||
"""To get brew default commands on local environment"""
|
|
||||||
brew_cmd_path = brew_path_prefix + BREW_CMD_PATH
|
|
||||||
|
|
||||||
return [name[:-3] for name in os.listdir(brew_cmd_path)
|
|
||||||
if name.endswith(('.rb', '.sh'))]
|
|
||||||
|
|
||||||
|
|
||||||
def _get_brew_tap_specific_commands(brew_path_prefix):
|
|
||||||
"""To get tap's specific commands
|
|
||||||
https://github.com/Homebrew/homebrew/blob/master/Library/brew.rb#L115"""
|
|
||||||
commands = []
|
|
||||||
brew_taps_path = brew_path_prefix + TAP_PATH
|
|
||||||
|
|
||||||
for user in _get_directory_names_only(brew_taps_path):
|
|
||||||
taps = _get_directory_names_only(brew_taps_path + '/%s' % user)
|
|
||||||
|
|
||||||
# Brew Taps's naming rule
|
|
||||||
# https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/brew-tap.md#naming-conventions-and-limitations
|
|
||||||
taps = (tap for tap in taps if tap.startswith('homebrew-'))
|
|
||||||
for tap in taps:
|
|
||||||
tap_cmd_path = brew_taps_path + TAP_CMD_PATH % (user, tap)
|
|
||||||
|
|
||||||
if os.path.isdir(tap_cmd_path):
|
|
||||||
commands += (name.replace('brew-', '').replace('.rb', '')
|
|
||||||
for name in os.listdir(tap_cmd_path)
|
|
||||||
if _is_brew_tap_cmd_naming(name))
|
|
||||||
|
|
||||||
return commands
|
|
||||||
|
|
||||||
|
|
||||||
def _is_brew_tap_cmd_naming(name):
|
|
||||||
return name.startswith('brew-') and name.endswith('.rb')
|
|
||||||
|
|
||||||
|
|
||||||
def _get_directory_names_only(path):
|
|
||||||
return [d for d in os.listdir(path)
|
|
||||||
if os.path.isdir(os.path.join(path, d))]
|
|
||||||
|
|
||||||
|
|
||||||
def _brew_commands():
|
|
||||||
brew_path_prefix = get_brew_path_prefix()
|
|
||||||
if brew_path_prefix:
|
|
||||||
try:
|
|
||||||
return (_get_brew_commands(brew_path_prefix)
|
|
||||||
+ _get_brew_tap_specific_commands(brew_path_prefix))
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Failback commands for testing (Based on Homebrew 0.9.5)
|
|
||||||
return ['info', 'home', 'options', 'install', 'uninstall',
|
|
||||||
'search', 'list', 'update', 'upgrade', 'pin', 'unpin',
|
|
||||||
'doctor', 'create', 'edit']
|
|
||||||
|
|
||||||
|
|
||||||
def match(command):
|
def match(command):
|
||||||
is_proper_command = ('brew' in command.script and
|
is_proper_command = ('brew' in command.script and
|
||||||
'Unknown command' in command.output)
|
'Unknown command' in command.output)
|
||||||
@ -72,11 +12,11 @@ def match(command):
|
|||||||
if is_proper_command:
|
if is_proper_command:
|
||||||
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
|
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
|
||||||
command.output)[0]
|
command.output)[0]
|
||||||
return bool(get_closest(broken_cmd, _brew_commands()))
|
return bool(get_closest(broken_cmd, all_brew_commands()))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_new_command(command):
|
def get_new_command(command):
|
||||||
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
|
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
|
||||||
command.output)[0]
|
command.output)[0]
|
||||||
return replace_command(command, broken_cmd, _brew_commands())
|
return replace_command(command, broken_cmd, all_brew_commands())
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from ..utils import memoize, which
|
from ..utils import memoize, which
|
||||||
|
|
||||||
|
BREW_CMD_PATH = '/Library/Homebrew/cmd'
|
||||||
|
TAP_PATH = '/Library/Taps'
|
||||||
|
TAP_CMD_PATH = '/%s/%s/cmd'
|
||||||
|
|
||||||
brew_available = bool(which('brew'))
|
brew_available = bool(which('brew'))
|
||||||
|
|
||||||
@ -13,3 +17,56 @@ def get_brew_path_prefix():
|
|||||||
universal_newlines=True).strip()
|
universal_newlines=True).strip()
|
||||||
except Exception:
|
except Exception:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_brew_commands(brew_path_prefix):
|
||||||
|
"""To get brew default commands on local environment"""
|
||||||
|
brew_cmd_path = brew_path_prefix + BREW_CMD_PATH
|
||||||
|
|
||||||
|
return [name[:-3] for name in os.listdir(brew_cmd_path)
|
||||||
|
if name.endswith(('.rb', '.sh'))]
|
||||||
|
|
||||||
|
|
||||||
|
def get_brew_tap_specific_commands(brew_path_prefix):
|
||||||
|
def _is_brew_tap_cmd_naming(name):
|
||||||
|
return name.startswith('brew-') and name.endswith('.rb')
|
||||||
|
|
||||||
|
def _get_directory_names_only(path):
|
||||||
|
return [d for d in os.listdir(path)
|
||||||
|
if os.path.isdir(os.path.join(path, d))]
|
||||||
|
|
||||||
|
"""To get tap's specific commands
|
||||||
|
https://github.com/Homebrew/homebrew/blob/master/Library/brew.rb#L115"""
|
||||||
|
commands = []
|
||||||
|
brew_taps_path = brew_path_prefix + TAP_PATH
|
||||||
|
|
||||||
|
for user in _get_directory_names_only(brew_taps_path):
|
||||||
|
taps = _get_directory_names_only(brew_taps_path + '/%s' % user)
|
||||||
|
|
||||||
|
# Brew Taps's naming rule
|
||||||
|
# https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/brew-tap.md#naming-conventions-and-limitations
|
||||||
|
taps = (tap for tap in taps if tap.startswith('homebrew-'))
|
||||||
|
for tap in taps:
|
||||||
|
tap_cmd_path = brew_taps_path + TAP_CMD_PATH % (user, tap)
|
||||||
|
|
||||||
|
if os.path.isdir(tap_cmd_path):
|
||||||
|
commands += (name.replace('brew-', '').replace('.rb', '')
|
||||||
|
for name in os.listdir(tap_cmd_path)
|
||||||
|
if _is_brew_tap_cmd_naming(name))
|
||||||
|
|
||||||
|
return commands
|
||||||
|
|
||||||
|
|
||||||
|
def all_brew_commands():
|
||||||
|
brew_path_prefix = get_brew_path_prefix()
|
||||||
|
if brew_path_prefix:
|
||||||
|
try:
|
||||||
|
return (get_brew_commands(brew_path_prefix)
|
||||||
|
+ get_brew_tap_specific_commands(brew_path_prefix))
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Failback commands for testing (Based on Homebrew 0.9.5)
|
||||||
|
return ['info', 'home', 'options', 'install', 'uninstall',
|
||||||
|
'search', 'list', 'update', 'upgrade', 'pin', 'unpin',
|
||||||
|
'doctor', 'create', 'edit']
|
||||||
|
Loading…
x
Reference in New Issue
Block a user