diff --git a/README.md b/README.md index 4ff50cd23..c78b8713d 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,7 @@ following rules are enabled by default: * `chmod_x` – add execution bit; * `choco_install` – append common suffixes for chocolatey packages; * `composer_not_command` – fixes composer command name; +* `cp_create_destination` – creates a new directory when you attempt to `cp` or `mv` to a non existent one * `cp_omitting_directory` – adds `-a` when you `cp` directory; * `cpp11` – adds missing `-std=c++11` to `g++` or `clang++`; * `dirty_untar` – fixes `tar x` command that untarred in the current directory; diff --git a/tests/rules/test_cp_create_destination.py b/tests/rules/test_cp_create_destination.py new file mode 100644 index 000000000..1c4094662 --- /dev/null +++ b/tests/rules/test_cp_create_destination.py @@ -0,0 +1,30 @@ +import pytest +from thefuck.rules.cp_create_destination import match, get_new_command +from thefuck.types import Command + + +@pytest.mark.parametrize( + "script, output", + [("cp", "cp: directory foo does not exist\n"), ("mv", "No such file or directory")], +) +def test_match(script, output): + assert match(Command(script, output)) + + +@pytest.mark.parametrize( + "script, output", [("cp", ""), ("mv", ""), ("ls", "No such file or directory")] +) +def test_not_match(script, output): + assert not match(Command(script, output)) + + +@pytest.mark.parametrize( + "script, output, new_command", + [ + ("cp foo bar/", "cp: directory foo does not exist\n", "mkdir -p bar/ && cp foo bar/"), + ("mv foo bar/", "No such file or directory", "mkdir -p bar/ && mv foo bar/"), + ("cp foo bar/baz/", "cp: directory foo does not exist\n", "mkdir -p bar/baz/ && cp foo bar/baz/"), + ], +) +def test_get_new_command(script, output, new_command): + assert get_new_command(Command(script, output)) == new_command diff --git a/thefuck/rules/cp_create_destination.py b/thefuck/rules/cp_create_destination.py new file mode 100644 index 000000000..6a1fbc594 --- /dev/null +++ b/thefuck/rules/cp_create_destination.py @@ -0,0 +1,15 @@ +from thefuck.shells import shell +from thefuck.utils import for_app + + +@for_app("cp", "mv") +def match(command): + return ( + "No such file or directory" in command.output + or command.output.startswith("cp: directory") + and command.output.rstrip().endswith("does not exist") + ) + + +def get_new_command(command): + return shell.and_(u"mkdir -p {}".format(command.script_parts[-1]), command.script)