From 0fcc35c227ae8abe59a9e86fdc9716173a7985de Mon Sep 17 00:00:00 2001 From: nvbn Date: Mon, 12 Oct 2015 17:52:44 +0800 Subject: [PATCH] #380 merge `cd_mkdir` and `cd_correction` rules --- README.md | 4 ++-- .../{test_cd_mkdir.py => test_cd_correct.py} | 2 +- thefuck/rules/cd_correction.py | 9 ++++++--- thefuck/rules/cd_mkdir.py | 17 ----------------- 4 files changed, 9 insertions(+), 23 deletions(-) rename tests/rules/{test_cd_mkdir.py => test_cd_correct.py} (92%) delete mode 100644 thefuck/rules/cd_mkdir.py diff --git a/README.md b/README.md index 199f37b8..e8491530 100644 --- a/README.md +++ b/README.md @@ -141,8 +141,8 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `cargo` – runs `cargo build` instead of `cargo`; * `cargo_no_command` – fixes wrongs commands like `cargo buid`; -* `cd_correction` – spellchecks and correct failed cd commands; -* `cd_mkdir` – creates directories before cd'ing into them; +* `cd_correction` – spellchecks and correct failed cd commands, when it's not possible +creates directories before cd'ing into them; * `cd_parent` – changes `cd..` to `cd ..`; * `composer_not_command` – fixes composer command name; * `cp_omitting_directory` – adds `-a` when you `cp` directory; diff --git a/tests/rules/test_cd_mkdir.py b/tests/rules/test_cd_correct.py similarity index 92% rename from tests/rules/test_cd_mkdir.py rename to tests/rules/test_cd_correct.py index 759ada36..12282fb9 100644 --- a/tests/rules/test_cd_mkdir.py +++ b/tests/rules/test_cd_correct.py @@ -1,5 +1,5 @@ import pytest -from thefuck.rules.cd_mkdir import match, get_new_command +from thefuck.rules.cd_correction import match, get_new_command from tests.utils import Command diff --git a/thefuck/rules/cd_correction.py b/thefuck/rules/cd_correction.py index 24c70067..eecf1a35 100644 --- a/thefuck/rules/cd_correction.py +++ b/thefuck/rules/cd_correction.py @@ -2,9 +2,10 @@ import os from difflib import get_close_matches +import re from thefuck.specific.sudo import sudo_support -from thefuck.rules import cd_mkdir from thefuck.utils import for_app +from thefuck import shells __author__ = "mmussomele" @@ -43,11 +44,13 @@ def get_new_command(command): elif directory == "..": cwd = os.path.split(cwd)[0] continue - best_matches = get_close_matches(directory, _get_sub_dirs(cwd), cutoff=MAX_ALLOWED_DIFF) + best_matches = get_close_matches( + directory, _get_sub_dirs(cwd), cutoff=MAX_ALLOWED_DIFF) if best_matches: cwd = os.path.join(cwd, best_matches[0]) else: - return cd_mkdir.get_new_command(command) + repl = shells.and_('mkdir -p \\1', 'cd \\1') + return re.sub(r'^cd (.*)', repl, command.script) return 'cd "{0}"'.format(cwd) diff --git a/thefuck/rules/cd_mkdir.py b/thefuck/rules/cd_mkdir.py deleted file mode 100644 index e47479e2..00000000 --- a/thefuck/rules/cd_mkdir.py +++ /dev/null @@ -1,17 +0,0 @@ -import re -from thefuck import shells -from thefuck.utils import for_app -from thefuck.specific.sudo import sudo_support - - -@sudo_support -@for_app('cd') -def match(command): - return (('no such file or directory' in command.stderr.lower() - or 'cd: can\'t cd to' in command.stderr.lower())) - - -@sudo_support -def get_new_command(command): - repl = shells.and_('mkdir -p \\1', 'cd \\1') - return re.sub(r'^cd (.*)', repl, command.script)