1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-18 20:11:17 +00:00

#N/A: Add grep_arguments_order rule

This commit is contained in:
nvbn 2016-02-08 11:04:19 +03:00
parent bbfd53d718
commit 4f95b3365a
3 changed files with 65 additions and 1 deletions

View File

@ -168,7 +168,8 @@ using the matched rule and runs it. Rules enabled by default are as follows:
* `git_push_pull` – runs `git pull` when `push` was rejected;
* `git_stash` – stashes you local modifications before rebasing or switching branch;
* `git_two_dashes` – adds a missing dash to commands like `git commit -amend` or `git rebase -continue`;
* `go_run` – appends `.go` extension when compiling/running Go programs
* `go_run` – appends `.go` extension when compiling/running Go programs;
* `grep_arguments_order` – fixes grep arguments order for situations like `grep -lir . test`;
* `grep_recursive` – adds `-r` when you trying to `grep` directory;
* `gulp_not_task` – fixes misspelled `gulp` tasks;
* `has_exists_script` – prepends `./` when script/binary exists;

View File

@ -0,0 +1,40 @@
import pytest
from thefuck.rules.grep_arguments_order import get_new_command, match
from tests.utils import Command
stderr = 'grep: {}: No such file or directory'.format
@pytest.fixture(autouse=True)
def os_path(monkeypatch):
monkeypatch.setattr('os.path.isfile', lambda x: not x.startswith('-'))
@pytest.mark.parametrize('script, file', [
('grep test.py test', 'test'),
('grep -lir . test', 'test'),
('egrep test.py test', 'test'),
('egrep -lir . test', 'test')])
def test_match(script, file):
assert match(Command(script, stderr=stderr(file)))
@pytest.mark.parametrize('script, stderr', [
('cat test.py', stderr('test')),
('grep test test.py', ''),
('grep -lir test .', ''),
('egrep test test.py', ''),
('egrep -lir test .', '')])
def test_not_match(script, stderr):
assert not match(Command(script, stderr=stderr))
@pytest.mark.parametrize('script, stderr, result', [
('grep test.py test', stderr('test'), 'grep test test.py'),
('grep -lir . test', stderr('test'), 'grep -lir test .'),
('grep . test -lir', stderr('test'), 'grep test -lir .'),
('egrep test.py test', stderr('test'), 'egrep test test.py'),
('egrep -lir . test', stderr('test'), 'egrep -lir test .'),
('egrep . test -lir', stderr('test'), 'egrep test -lir .')])
def test_get_new_command(script, stderr, result):
assert get_new_command(Command(script, stderr=stderr)) == result

View File

@ -0,0 +1,23 @@
import os
from thefuck.utils import for_app
def _get_actual_file(parts):
for part in parts[1:]:
if os.path.isfile(part) or os.path.isdir(part):
return part
@for_app('grep', 'egrep')
def match(command):
return ': No such file or directory' in command.stderr \
and _get_actual_file(command.script_parts)
def get_new_command(command):
actual_file = _get_actual_file(command.script_parts)
parts = command.script_parts[::]
# Moves file to the end of the script:
parts.remove(actual_file)
parts.append(actual_file)
return ' '.join(parts)