From 3e8db28a736443e29b50caf8c4078fc7c4e5cf87 Mon Sep 17 00:00:00 2001 From: nvbn Date: Wed, 9 Sep 2015 10:53:56 +0300 Subject: [PATCH] #355 Add `touch` rule --- README.md | 1 + tests/test_touch.py | 29 +++++++++++++++++++++++++++++ thefuck/rules/touch.py | 13 +++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 tests/test_touch.py create mode 100644 thefuck/rules/touch.py diff --git a/README.md b/README.md index ebb12c1b..199f37b8 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `switch_lang` – switches command from your local layout to en; * `systemctl` – correctly orders parameters of confusing `systemctl`; * `test.py` – runs `py.test` instead of `test.py`; +* `touch` – creates missing directories before "touching"; * `tsuru_login` – runs `tsuru login` if not authenticated or session expired; * `tsuru_not_command` – fixes wrong `tsuru` commands like `tsuru shell`; * `tmux` – fixes `tmux` commands; diff --git a/tests/test_touch.py b/tests/test_touch.py new file mode 100644 index 00000000..6de74085 --- /dev/null +++ b/tests/test_touch.py @@ -0,0 +1,29 @@ +import pytest +from thefuck.rules.touch import match, get_new_command +from tests.utils import Command + + +@pytest.fixture +def stderr(): + return "touch: cannot touch '/a/b/c':" \ + " No such file or directory" + + +def test_match(stderr): + command = Command( + 'touch /a/b/c', stderr=stderr) + assert match(command) + + +@pytest.mark.parametrize('command', [ + Command('touch /a/b/c'), + Command('touch /a/b/c', stdout=stderr()), + Command('ls /a/b/c', stderr=stderr())]) +def test_not_match(command): + assert not match(command) + + +def test_get_new_command(stderr): + command = Command('touch /a/b/c', stderr=stderr) + fixed_command = get_new_command(command) + assert fixed_command == 'mkdir -p /a/b && touch /a/b/c' diff --git a/thefuck/rules/touch.py b/thefuck/rules/touch.py new file mode 100644 index 00000000..bede8130 --- /dev/null +++ b/thefuck/rules/touch.py @@ -0,0 +1,13 @@ +import re +from thefuck.utils import for_app +from thefuck.shells import and_ + + +@for_app('touch') +def match(command): + return 'No such file or directory' in command.stderr + + +def get_new_command(command): + path = re.findall(r"touch: cannot touch '(.+)/.+':", command.stderr)[0] + return and_(u'mkdir -p {}'.format(path), command.script)