diff --git a/tests/rules/test_cd_correction.py b/tests/rules/test_cd_correction.py new file mode 100644 index 0000000..d257658 --- /dev/null +++ b/tests/rules/test_cd_correction.py @@ -0,0 +1,23 @@ +import pytest +from thefuck.rules.cd_correction import match +from thefuck.types import Command + + +@pytest.mark.parametrize('command', [ + Command('cd foo', 'cd: foo: No such file or directory'), + Command('cd foo/bar/baz', + 'cd: foo: No such file or directory'), + Command('cd foo/bar/baz', 'cd: can\'t cd to foo/bar/baz'), + Command('cd /foo/bar/', 'cd: The directory "/foo/bar/" does not exist')]) +def test_match(command): + assert match(command) + + +@pytest.mark.parametrize('command', [ + Command('cd foo', ''), Command('', '')]) +def test_not_match(command): + assert not match(command) + + +# Note that get_new_command uses local filesystem, so not testing it here. +# Instead, see the functional test `functional.test_cd_correction` diff --git a/tests/rules/test_cd_mkdir.py b/tests/rules/test_cd_mkdir.py index 61dc30e..aa3d82e 100644 --- a/tests/rules/test_cd_mkdir.py +++ b/tests/rules/test_cd_mkdir.py @@ -7,7 +7,8 @@ from thefuck.types import Command Command('cd foo', 'cd: foo: No such file or directory'), Command('cd foo/bar/baz', 'cd: foo: No such file or directory'), - Command('cd foo/bar/baz', 'cd: can\'t cd to foo/bar/baz')]) + Command('cd foo/bar/baz', 'cd: can\'t cd to foo/bar/baz'), + Command('cd /foo/bar/', 'cd: The directory "/foo/bar/" does not exist')]) def test_match(command): assert match(command) diff --git a/thefuck/rules/cd_correction.py b/thefuck/rules/cd_correction.py index ca3464b..376e51e 100644 --- a/thefuck/rules/cd_correction.py +++ b/thefuck/rules/cd_correction.py @@ -21,9 +21,12 @@ def _get_sub_dirs(parent): @for_app('cd') def match(command): """Match function copied from cd_mkdir.py""" - return (command.script.startswith('cd ') - and ('no such file or directory' in command.output.lower() - or 'cd: can\'t cd to' in command.output.lower())) + return ( + 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() + ))) @sudo_support @@ -37,7 +40,11 @@ def get_new_command(command): dest = command.script_parts[1].split(os.sep) if dest[-1] == '': dest = dest[:-1] - if six.PY2: + + if dest[0] == '': + cwd = os.sep + dest = dest[1:] + elif six.PY2: cwd = os.getcwdu() else: cwd = os.getcwd() diff --git a/thefuck/rules/cd_mkdir.py b/thefuck/rules/cd_mkdir.py index 09e3ab6..363b92d 100644 --- a/thefuck/rules/cd_mkdir.py +++ b/thefuck/rules/cd_mkdir.py @@ -8,10 +8,11 @@ from thefuck.shells import shell @for_app('cd') def match(command): return ( - 'no such file or directory' in command.output.lower() - or 'cd: can\'t cd to' in command.output.lower() - or 'the system cannot find the path specified.' in command.output.lower() - ) + 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() + ))) @sudo_support