From 3c4f9d50a945a13c813884c795c78e8634940cf7 Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 15 May 2015 18:03:17 +0200 Subject: [PATCH 1/3] Add a `no_such_file` rule --- README.md | 1 + thefuck/rules/no_such_file.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 thefuck/rules/no_such_file.py diff --git a/README.md b/README.md index 6b507ee..9cf7237 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `lein_not_task` – fixes wrong `lein` tasks like `lein rpl`; * `mkdir_p` – adds `-p` when you trying to create directory without parent; * `no_command` – fixes wrong console commands, for example `vom/vim`; +* `no_such_file` – creates missing directories with `mv` and `cp` commands; * `man_no_space` – fixes man commands without spaces, for example `mandiff`; * `pacman` – installs app with `pacman` or `yaourt` if it is not installed; * `pip_unknown_command` – fixes wrong pip commands, for example `pip instatl/pip install`; diff --git a/thefuck/rules/no_such_file.py b/thefuck/rules/no_such_file.py new file mode 100644 index 0000000..157a41a --- /dev/null +++ b/thefuck/rules/no_such_file.py @@ -0,0 +1,26 @@ +import re + + +patterns = ( + r"mv: cannot move '[^']*' to '([^']*)': No such file or directory", + r"cp: cannot create regular file '([^']*)': No such file or directory", +) + + +def match(command, settings): + for pattern in patterns: + if re.search(pattern, command.stderr): + return True + + return False + + +def get_new_command(command, settings): + for pattern in patterns: + file = re.findall(pattern, command.stderr) + + if file: + file = file[0] + dir = file[0:file.rfind('/')] + + return 'mkdir -p {} && {}'.format(dir, command.script) From 5504aa44a1b47d6533c65c0f5884f11eda06359b Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 15 May 2015 18:03:33 +0200 Subject: [PATCH 2/3] Add tests for the `no_such_file` rule --- tests/rules/test_no_such_file.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/rules/test_no_such_file.py diff --git a/tests/rules/test_no_such_file.py b/tests/rules/test_no_such_file.py new file mode 100644 index 0000000..ba35477 --- /dev/null +++ b/tests/rules/test_no_such_file.py @@ -0,0 +1,19 @@ +import pytest +from thefuck.rules.no_such_file import match, get_new_command +from tests.utils import Command + + +@pytest.mark.parametrize('command', [ + Command(script='mv foo bar/foo', stderr="mv: cannot move 'foo' to 'bar/foo': No such file or directory"), + Command(script='mv foo bar/', stderr="mv: cannot move 'foo' to 'bar/': No such file or directory"), + ]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command(script='mv foo bar/foo', stderr="mv: cannot move 'foo' to 'bar/foo': No such file or directory"), 'mkdir -p bar && mv foo bar/foo'), + (Command(script='mv foo bar/', stderr="mv: cannot move 'foo' to 'bar/': No such file or directory"), 'mkdir -p bar && mv foo bar/'), + ]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command From 08a2065119b00f57a12549affd242d7f2e18f9a3 Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 15 May 2015 18:08:43 +0200 Subject: [PATCH 3/3] Add missing cases for the `no_such_file` rule --- thefuck/rules/no_such_file.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/thefuck/rules/no_such_file.py b/thefuck/rules/no_such_file.py index 157a41a..9a0f3b4 100644 --- a/thefuck/rules/no_such_file.py +++ b/thefuck/rules/no_such_file.py @@ -3,7 +3,9 @@ import re patterns = ( r"mv: cannot move '[^']*' to '([^']*)': No such file or directory", + r"mv: cannot move '[^']*' to '([^']*)': Not a directory", r"cp: cannot create regular file '([^']*)': No such file or directory", + r"cp: cannot create regular file '([^']*)': Not a directory", )