mirror of
https://github.com/nvbn/thefuck.git
synced 2025-03-14 06:38:32 +00:00
Merge pull request #16 from Comp-490-TR-Afternoon-Group-Four/cd_rules
Cd rules
This commit is contained in:
commit
8408c5f85c
@ -199,6 +199,7 @@ following rules are enabled by default:
|
||||
* `cd_cs` – changes `cs` to `cd`;
|
||||
* `cd_mkdir` – creates directories before cd'ing into them;
|
||||
* `cd_parent` – changes `cd..` to `cd ..`;
|
||||
* `chdir` – creates directories before changing into them using chdir;
|
||||
* `chmod_x` – add execution bit;
|
||||
* `choco_install` – append common suffixes for chocolatey packages;
|
||||
* `composer_not_command` – fixes composer command name;
|
||||
|
@ -20,7 +20,13 @@ def test_not_match(command):
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command, new_command', [
|
||||
(Command('cd foo', ''), 'mkdir -p foo && cd foo'),
|
||||
(Command('cd foo/bar/baz', ''), 'mkdir -p foo/bar/baz && cd foo/bar/baz')])
|
||||
(Command('cd foo', ''), 'mkdir -p foo; cd foo'),
|
||||
(Command('cd foo/bar/baz', ''), 'mkdir -p foo/bar/baz; cd foo/bar/baz')])
|
||||
def test_get_new_command(command, new_command):
|
||||
assert get_new_command(command) == new_command
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command', [
|
||||
Command('cd <', ''), Command('cd \0', '')])
|
||||
def test_bad_dir_name(command):
|
||||
assert not match(command)
|
||||
|
31
tests/rules/test_chdir.py
Normal file
31
tests/rules/test_chdir.py
Normal file
@ -0,0 +1,31 @@
|
||||
import pytest
|
||||
from thefuck.rules.chdir import match, get_new_command
|
||||
from thefuck.types import Command
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command', [
|
||||
Command('chdir foo', 'cannot find the path'),
|
||||
Command('chdir foo/bar', 'can\'t cd to'),
|
||||
Command('chdir foo/bar', 'cannot find path'),
|
||||
Command('chdir /foo/bar/', 'can\'t cd to')])
|
||||
def test_match(command):
|
||||
assert match(command)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command', [
|
||||
Command('chdir foo', ''), Command('', '')])
|
||||
def test_not_match(command):
|
||||
assert not match(command)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command, new_command', [
|
||||
(Command('chdirfoo', ''), 'mkdir -p foo; chdir foo'),
|
||||
(Command('chdirfoo/bar/baz', ''), 'mkdir -p foo/bar/baz; chdir foo/bar/baz')])
|
||||
def test_get_new_command(command, new_command):
|
||||
assert get_new_command(command) == new_command
|
||||
|
||||
|
||||
@pytest.mark.parametrize('command', [
|
||||
Command('chdir <', ''), Command('chdir \0', '')])
|
||||
def test_bad_dir_name(command):
|
||||
assert not match(command)
|
@ -58,7 +58,7 @@ def show_corrected_command(corrected_command):
|
||||
|
||||
def confirm_text(corrected_command):
|
||||
sys.stderr.write(
|
||||
(u'{prefix}{clear}{bold}{script}{reset}{side_effect} '
|
||||
(u'{prefix}{clear}{bold}{script}{reset}{side_effect}'
|
||||
u'[{green}enter{reset}/{blue}↑{reset}/{blue}↓{reset}'
|
||||
u'/{red}ctrl+c{reset}]').format(
|
||||
prefix=const.USER_COMMAND_MARK,
|
||||
|
@ -1,7 +1,6 @@
|
||||
import re
|
||||
from thefuck.utils import for_app
|
||||
from thefuck.specific.sudo import sudo_support
|
||||
from thefuck.shells import shell
|
||||
|
||||
|
||||
@sudo_support
|
||||
@ -11,11 +10,13 @@ def match(command):
|
||||
command.script.startswith('cd ') and any((
|
||||
'no such file or directory' in command.output.lower(),
|
||||
'cd: can\'t cd to' in command.output.lower(),
|
||||
'does not exist' in command.output.lower()
|
||||
'does not exist' in command.output.lower(),
|
||||
'cannot find the path' in command.output.lower(),
|
||||
'cannot find path' in command.output.lower()
|
||||
)))
|
||||
|
||||
|
||||
@sudo_support
|
||||
def get_new_command(command):
|
||||
repl = shell.and_('mkdir -p \\1', 'cd \\1')
|
||||
repl = 'mkdir -p \\1; cd \\1'
|
||||
return re.sub(r'^cd (.*)', repl, command.script)
|
||||
|
26
thefuck/rules/chdir.py
Normal file
26
thefuck/rules/chdir.py
Normal file
@ -0,0 +1,26 @@
|
||||
import re
|
||||
from thefuck.utils import for_app
|
||||
from thefuck.specific.sudo import sudo_support
|
||||
from thefuck.shells import shell
|
||||
|
||||
|
||||
@sudo_support
|
||||
@for_app('chdir')
|
||||
def match(command):
|
||||
return (
|
||||
command.script.startswith('chdir') and any((
|
||||
'cannot find the path' in command.output.lower(),
|
||||
'cannot find path' in command.output.lower(),
|
||||
'can\'t cd to' in command.output.lower()
|
||||
)))
|
||||
|
||||
|
||||
@sudo_support
|
||||
def get_new_command(command):
|
||||
shell_name = shell.info()
|
||||
if shell_name == 'pwsh' or shell_name == 'powershell':
|
||||
new_command = ('mkdir -p \\1' + '; chdir\\1')
|
||||
return re.sub(r'^chdir(.*)', new_command, command.script)
|
||||
else:
|
||||
repl = ('mkdir -p \\1; chdir \\1')
|
||||
return re.sub(r'^chdir(.*)', repl, command.script)
|
Loading…
x
Reference in New Issue
Block a user