Skip to content

Commit

Permalink
#N/A: Add a new rule to create directory on cp or mv
Browse files Browse the repository at this point in the history
  • Loading branch information
Caplinja authored and scorphus committed Mar 1, 2020
1 parent 444908c commit 88db57b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions tests/rules/test_cp_create_destination.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions thefuck/rules/cp_create_destination.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 88db57b

Please sign in to comment.