-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule to remove leading shell prompt literal $ (#996)
* Add rule to remove shell prompt literals $ Rule added to handle cases where the $ symbol is used in the command, this usually happens when the command is copy pasted from a documentation that includes the shell prompt symbol in the code blocks. * Change files using black and flake8 style check * Refactor tests and rule - Refactor test for cleaner test tables - Removed unnecessary requires_output=True option
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pytest | ||
from thefuck.rules.remove_shell_prompt_literal import match, get_new_command | ||
from thefuck.types import Command | ||
|
||
|
||
@pytest.fixture | ||
def output(): | ||
return "$: command not found" | ||
|
||
|
||
@pytest.mark.parametrize("script", ["$ cd newdir", " $ cd newdir"]) | ||
def test_match(script, output): | ||
assert match(Command(script, output)) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"command", | ||
[ | ||
Command("$", "$: command not found"), | ||
Command(" $", "$: command not found"), | ||
Command("$?", "127: command not found"), | ||
Command(" $?", "127: command not found"), | ||
Command("", ""), | ||
], | ||
) | ||
def test_not_match(command): | ||
assert not match(command) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"script, new_command", | ||
[ | ||
("$ cd newdir", "cd newdir"), | ||
("$ python3 -m virtualenv env", "python3 -m virtualenv env"), | ||
], | ||
) | ||
def test_get_new_command(script, new_command, output): | ||
assert get_new_command(Command(script, output)) == new_command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Fixes error for commands containing the shell prompt symbol '$'. | ||
This usually happens when commands are copied from documentations | ||
including them in their code blocks. | ||
Example: | ||
> $ git clone https://github.com/nvbn/thefuck.git | ||
bash: $: command not found... | ||
""" | ||
|
||
import re | ||
|
||
|
||
def match(command): | ||
return ( | ||
"$: command not found" in command.output | ||
and re.search(r"^[\s]*\$ [\S]+", command.script) is not None | ||
) | ||
|
||
|
||
def get_new_command(command): | ||
return command.script.replace("$", "", 1).strip() |