diff --git a/README.md b/README.md index c5e9cd25..861a7fa4 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ following rules are enabled by default: * `aws_cli` – fixes misspelled commands like `aws dynamdb scan`; * `cargo` – runs `cargo build` instead of `cargo`; * `cargo_no_command` – fixes wrongs commands like `cargo buid`; +* `cat_dir` – replaces `cat` with `ls` when you try to `cat` a directory * `cd_correction` – spellchecks and correct failed cd commands; * `cd_mkdir` – creates directories before cd'ing into them; * `cd_parent` – changes `cd..` to `cd ..`; diff --git a/tests/rules/test_cat_dir.py b/tests/rules/test_cat_dir.py new file mode 100644 index 00000000..c5332a93 --- /dev/null +++ b/tests/rules/test_cat_dir.py @@ -0,0 +1,29 @@ +import pytest +from thefuck.rules.cat_dir import match, get_new_command +from thefuck.types import Command + + +@pytest.mark.parametrize('command', [ + Command('cat foo', 'cat: foo: Is a directory\n'), + Command('cat /foo/bar/', 'cat: /foo/bar/: Is a directory\n'), + Command('cat cat/', 'cat: cat/: Is a directory\n'), +]) +def test_match(command): + assert match(command) + + +@pytest.mark.parametrize('command', [ + Command('cat foo', 'foo bar baz'), + Command('cat foo bar', 'foo bar baz'), +]) +def test_not_match(command): + assert not match(command) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('cat foo', 'cat: foo: Is a directory\n'), 'ls foo'), + (Command('cat /foo/bar/', 'cat: /foo/bar/: Is a directory\n'), 'ls /foo/bar/'), + (Command('cat cat', 'cat: cat: Is a directory\n'), 'ls cat'), +]) +def test_get_new_command(command, new_command): + assert get_new_command(command) == new_command diff --git a/thefuck/rules/cat_dir.py b/thefuck/rules/cat_dir.py new file mode 100644 index 00000000..39a158ad --- /dev/null +++ b/thefuck/rules/cat_dir.py @@ -0,0 +1,10 @@ +def match(command): + return ( + command.script.startswith('cat') and + command.output.startswith('cat: ') and + command.output.rstrip().endswith(': Is a directory') + ) + + +def get_new_command(command): + return command.script.replace('cat', 'ls', 1)