Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ following rules are enabled by default:
* `quotation_marks` – fixes uneven usage of `'` and `"` when containing args';
* `path_from_history` – replaces not found path with similar absolute path from history;
* `react_native_command_unrecognized` – fixes unrecognized `react-native` commands;
* `remove_shell_prompt_literal` – remove leading shell prompt symbol `$`, common when copying commands from documentations;
* `remove_trailing_cedilla` – remove trailling cedillas `ç`, a common typo for european keyboard layouts;
* `rm_dir` – adds `-rf` when you try to remove a directory;
* `scm_correction` – corrects wrong scm like `hg log` to `git log`;
Expand Down
33 changes: 33 additions & 0 deletions tests/rules/test_remove_shell_prompt_literal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from thefuck.rules.remove_shell_prompt_literal import match, get_new_command
from thefuck.types import Command


@pytest.mark.parametrize(
"command",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about parametrizing only script (as string) and declare a Command instance with "$: command not found" as output?

Suggested change
"command",
"script",

That would make all test tables more readable.

[
Command("$ cd newdir", "$: command not found"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Command("$ cd newdir", "$: command not found"),
"$ cd newdir",

Command(" $ cd newdir", "$: command not found"),
],
)
def test_match(command):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_match(command):
def test_match(script):

assert match(command)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert match(command)
assert match(Command(script, "$: command not found"))



@pytest.mark.parametrize(
"command",
[Command("$", ""), Command("$?", ""), Command(" $?", ""), Command("", "")],
)
def test_not_match(command):
assert not match(command)


@pytest.mark.parametrize(
"command, new_command",
[
(Command("$ cd newdir", ""), "cd newdir"),
(Command("$ python3 -m virtualenv env", ""), "python3 -m virtualenv env"),
],
)
def test_get_new_command(command, new_command):
assert get_new_command(command) == new_command
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

25 changes: 25 additions & 0 deletions thefuck/rules/remove_shell_prompt_literal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""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()


requires_output = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be omitted as requires_output's default value is True.