1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-19 04:21:14 +00:00

Merge pull request #535 from scorphus/534-improve-open

#534: Improve open rule
This commit is contained in:
Vladimir Iakovlev 2016-08-13 20:20:12 +08:00 committed by GitHub
commit bc78c83224
3 changed files with 70 additions and 38 deletions

View File

@ -202,7 +202,7 @@ using the matched rule and runs it. Rules enabled by default are as follows:
* `npm_wrong_command` – fixes wrong npm commands like `npm urgrade`; * `npm_wrong_command` – fixes wrong npm commands like `npm urgrade`;
* `no_command` – fixes wrong console commands, for example `vom/vim`; * `no_command` – fixes wrong console commands, for example `vom/vim`;
* `no_such_file` – creates missing directories with `mv` and `cp` commands; * `no_such_file` – creates missing directories with `mv` and `cp` commands;
* `open` – prepends `http` to address passed to `open`; * `open` – either prepends `http://` to address passed to `open` or create a new file or directory and passes it to `open`;
* `pip_unknown_command` – fixes wrong `pip` commands, for example `pip instatl/pip install`; * `pip_unknown_command` – fixes wrong `pip` commands, for example `pip instatl/pip install`;
* `python_command` – prepends `python` when you trying to run not executable/without `./` python script; * `python_command` – prepends `python` when you trying to run not executable/without `./` python script;
* `python_execute` – appends missing `.py` when executing Python files; * `python_execute` – appends missing `.py` when executing Python files;

View File

@ -1,31 +1,49 @@
import pytest import pytest
from thefuck.rules.open import match, get_new_command from thefuck.rules.open import is_arg_url, match, get_new_command
from tests.utils import Command from tests.utils import Command
@pytest.mark.parametrize('command', [ @pytest.fixture
Command(script='open foo.com'), def stderr(script):
Command(script='open foo.ly'), return 'The file {} does not exist.\n'.format(script.split(' ', 1)[1])
Command(script='open foo.org'),
Command(script='open foo.net'),
Command(script='open foo.se'),
Command(script='open foo.io'),
Command(script='xdg-open foo.com'),
Command(script='gnome-open foo.com'),
Command(script='kde-open foo.com')])
def test_match(command):
assert match(command)
@pytest.mark.parametrize('command, new_command', [ @pytest.mark.parametrize('script', [
(Command('open foo.com'), 'open http://foo.com'), 'open foo.com',
(Command('open foo.ly'), 'open http://foo.ly'), 'open foo.edu',
(Command('open foo.org'), 'open http://foo.org'), 'open foo.info',
(Command('open foo.net'), 'open http://foo.net'), 'open foo.io',
(Command('open foo.se'), 'open http://foo.se'), 'open foo.ly',
(Command('open foo.io'), 'open http://foo.io'), 'open foo.me',
(Command('xdg-open foo.io'), 'xdg-open http://foo.io'), 'open foo.net',
(Command('gnome-open foo.io'), 'gnome-open http://foo.io'), 'open foo.org',
(Command('kde-open foo.io'), 'kde-open http://foo.io')]) 'open foo.se',
def test_get_new_command(command, new_command): 'open www.foo.ru'])
assert get_new_command(command) == new_command def test_is_arg_url(script):
assert is_arg_url(Command(script))
@pytest.mark.parametrize('script', ['open foo', 'open bar.txt', 'open egg.doc'])
def test_not_is_arg_url(script):
assert not is_arg_url(Command(script))
@pytest.mark.parametrize('script', [
'open foo.com',
'xdg-open foo.com',
'gnome-open foo.com',
'kde-open foo.com',
'open nonest'])
def test_match(script, stderr):
assert match(Command(script, stderr=stderr))
@pytest.mark.parametrize('script, new_command', [
('open foo.io', ['open http://foo.io']),
('xdg-open foo.io', ['xdg-open http://foo.io']),
('gnome-open foo.io', ['gnome-open http://foo.io']),
('kde-open foo.io', ['kde-open http://foo.io']),
('open nonest', ['touch nonest && open nonest',
'mkdir nonest && open nonest'])])
def test_get_new_command(script, new_command, stderr):
assert get_new_command(Command(script, stderr=stderr)) == new_command

View File

@ -5,22 +5,36 @@
# The file ~/github.com does not exist. # The file ~/github.com does not exist.
# Perhaps you meant 'http://github.com'? # Perhaps you meant 'http://github.com'?
# #
from thefuck.utils import for_app from thefuck.shells import shell
from thefuck.utils import eager, for_app
def is_arg_url(command):
return ('.com' in command.script or
'.edu' in command.script or
'.info' in command.script or
'.io' in command.script or
'.ly' in command.script or
'.me' in command.script or
'.net' in command.script or
'.org' in command.script or
'.se' in command.script or
'www.' in command.script)
@for_app('open', 'xdg-open', 'gnome-open', 'kde-open') @for_app('open', 'xdg-open', 'gnome-open', 'kde-open')
def match(command): def match(command):
return ('.com' in command.script return (is_arg_url(command) or
or '.net' in command.script command.stderr.strip().startswith('The file ') and
or '.org' in command.script command.stderr.strip().endswith(' does not exist.'))
or '.ly' in command.script
or '.io' in command.script
or '.se' in command.script
or '.edu' in command.script
or '.info' in command.script
or '.me' in command.script
or 'www.' in command.script)
@eager
def get_new_command(command): def get_new_command(command):
return command.script.replace('open ', 'open http://') stderr = command.stderr.strip()
if is_arg_url(command):
yield command.script.replace('open ', 'open http://')
elif stderr.startswith('The file ') and stderr.endswith(' does not exist.'):
arg = command.script.split(' ', 1)[1]
for option in ['touch', 'mkdir']:
yield shell.and_(u'{} {}'.format(option, arg), command.script)