ronantakizawa/codereview-bench huggingface dataset:
Repository: nvbn/thefuck
File: tests/rules/test_remove_shell_prompt_literal.py (Python)

Code before:
import pytest
from thefuck.rules.remove_shell_prompt_literal import match, get_new_command
from thefuck.types import Command


@pytest.mark.parametrize(
    "command",
    [
        Command("$ cd newdir", "$: command not found"),
        Command(" $ cd newdir", "$: command not found"),
    ],
)
def test_match(command):
    assert match(command)


@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


Review comment (suggestion):
```suggestion
    assert match(Command(script, "$: command not found"))
```

Code after:
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
Repository: nvbn/thefuck
File: thefuck/rules/remove_shell_prompt_literal.py (Python)

Code before:
"""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


Review comment (suggestion):
This can be omitted as [`requires_output`'s default value is `True`](https://github.com/nvbn/thefuck/blob/70b414aca2acbbac2cd3c75bc4c3e2dd489b3144/thefuck/types.py#L147).

Code after:
"""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()
Repository: nvbn/thefuck
File: thefuck/rules/choco_install.py (Python)

Code before:
def match(command):
    return (('choco install' in command.script_parts or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    cmdList = command.script.split(' ')
    packageName = ""
    # Find the argument that is the package name
    for i in cmdList:
        print(i)
        if "choco" in i:
            continue
        if "cinst" in i:
            continue
        if "install" in i:
            continue
        if i.startswith('-'):   # Some parameters start with hyphens; some packages contain them though
            continue
        if '=' in i:            # Some paramaters contain '='
            continue
        if '/' in i:            # Some parameters contain slashes
            continue
        else:
            packageName = i
    # Find the name of the broken package, and append metapackage names
    if not packageName:
        return False
    return(command.script.replace(packageName, packageName + ".install"))


Review comment (suggestion):
`command.script_parts` should be used instead.
```suggestion
```
See below.

Code after:
from thefuck.utils import for_app, which


@for_app("choco", "cinst")
def match(command):
    return ((command.script.startswith('choco install') or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    # Find the argument that is the package name
    for script_part in command.script_parts:
        if (
            script_part not in ["choco", "cinst", "install"]
            # Need exact match (bc chocolatey is a package)
            and not script_part.startswith('-')
            # Leading hyphens are parameters; some packages contain them though
            and '=' not in script_part and '/' not in script_part
            # These are certainly parameters
        ):
            return command.script.replace(script_part, script_part + ".install")
    return []


enabled_by_default = bool(which("choco")) or bool(which("cinst"))
Repository: nvbn/thefuck
File: thefuck/rules/choco_install.py (Python)

Code before:
def match(command):
    return (('choco install' in command.script_parts or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    cmdList = command.script.split(' ')
    packageName = ""
    # Find the argument that is the package name
    for i in cmdList:
        print(i)
        if "choco" in i:
            continue
        if "cinst" in i:
            continue
        if "install" in i:
            continue
        if i.startswith('-'):   # Some parameters start with hyphens; some packages contain them though
            continue
        if '=' in i:            # Some paramaters contain '='
            continue
        if '/' in i:            # Some parameters contain slashes
            continue
        else:
            packageName = i
    # Find the name of the broken package, and append metapackage names
    if not packageName:
        return False
    return(command.script.replace(packageName, packageName + ".install"))


Review comment (suggestion):
`command.script_parts` should be used instead.

```suggestion
    for script_part in command.script_parts:
```

Code after:
from thefuck.utils import for_app, which


@for_app("choco", "cinst")
def match(command):
    return ((command.script.startswith('choco install') or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    # Find the argument that is the package name
    for script_part in command.script_parts:
        if (
            script_part not in ["choco", "cinst", "install"]
            # Need exact match (bc chocolatey is a package)
            and not script_part.startswith('-')
            # Leading hyphens are parameters; some packages contain them though
            and '=' not in script_part and '/' not in script_part
            # These are certainly parameters
        ):
            return command.script.replace(script_part, script_part + ".install")
    return []


enabled_by_default = bool(which("choco")) or bool(which("cinst"))
Repository: nvbn/thefuck
File: thefuck/rules/choco_install.py (Python)

Code before:
def match(command):
    return (('choco install' in command.script_parts or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    cmdList = command.script.split(' ')
    packageName = ""
    # Find the argument that is the package name
    for i in cmdList:
        print(i)
        if "choco" in i:
            continue
        if "cinst" in i:
            continue
        if "install" in i:
            continue
        if i.startswith('-'):   # Some parameters start with hyphens; some packages contain them though
            continue
        if '=' in i:            # Some paramaters contain '='
            continue
        if '/' in i:            # Some parameters contain slashes
            continue
        else:
            packageName = i
    # Find the name of the broken package, and append metapackage names
    if not packageName:
        return False
    return(command.script.replace(packageName, packageName + ".install"))


Review comment (suggestion):
This `if` statement sequence should be rewritten with all expressions grouped together with `and` operators.

Code after:
from thefuck.utils import for_app, which


@for_app("choco", "cinst")
def match(command):
    return ((command.script.startswith('choco install') or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    # Find the argument that is the package name
    for script_part in command.script_parts:
        if (
            script_part not in ["choco", "cinst", "install"]
            # Need exact match (bc chocolatey is a package)
            and not script_part.startswith('-')
            # Leading hyphens are parameters; some packages contain them though
            and '=' not in script_part and '/' not in script_part
            # These are certainly parameters
        ):
            return command.script.replace(script_part, script_part + ".install")
    return []


enabled_by_default = bool(which("choco")) or bool(which("cinst"))
Repository: nvbn/thefuck
File: thefuck/rules/choco_install.py (Python)

Code before:
def match(command):
    return (('choco install' in command.script_parts or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    cmdList = command.script.split(' ')
    packageName = ""
    # Find the argument that is the package name
    for i in cmdList:
        print(i)
        if "choco" in i:
            continue
        if "cinst" in i:
            continue
        if "install" in i:
            continue
        if i.startswith('-'):   # Some parameters start with hyphens; some packages contain them though
            continue
        if '=' in i:            # Some paramaters contain '='
            continue
        if '/' in i:            # Some parameters contain slashes
            continue
        else:
            packageName = i
    # Find the name of the broken package, and append metapackage names
    if not packageName:
        return False
    return(command.script.replace(packageName, packageName + ".install"))


Review comment (suggestion):
```suggestion
from thefuck.utils import for_app


@for_app("choco", "cinst")
def match(command):
```

Code after:
from thefuck.utils import for_app, which


@for_app("choco", "cinst")
def match(command):
    return ((command.script.startswith('choco install') or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    # Find the argument that is the package name
    for script_part in command.script_parts:
        if (
            script_part not in ["choco", "cinst", "install"]
            # Need exact match (bc chocolatey is a package)
            and not script_part.startswith('-')
            # Leading hyphens are parameters; some packages contain them though
            and '=' not in script_part and '/' not in script_part
            # These are certainly parameters
        ):
            return command.script.replace(script_part, script_part + ".install")
    return []


enabled_by_default = bool(which("choco")) or bool(which("cinst"))
Repository: nvbn/thefuck
File: thefuck/rules/choco_install.py (Python)

Code before:
def match(command):
    return (('choco install' in command.script_parts or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    cmdList = command.script.split(' ')
    packageName = ""
    # Find the argument that is the package name
    for i in cmdList:
        print(i)
        if "choco" in i:
            continue
        if "cinst" in i:
            continue
        if "install" in i:
            continue
        if i.startswith('-'):   # Some parameters start with hyphens; some packages contain them though
            continue
        if '=' in i:            # Some paramaters contain '='
            continue
        if '/' in i:            # Some parameters contain slashes
            continue
        else:
            packageName = i
    # Find the name of the broken package, and append metapackage names
    if not packageName:

Review comment (suggestion):
```suggestion
    return ((command.script.startswith('choco install') or 'cinst' in command.script_parts)
```

Also, the outer parenthesis can be removed.

Code after:
from thefuck.utils import for_app, which


@for_app("choco", "cinst")
def match(command):
    return ((command.script.startswith('choco install') or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    # Find the argument that is the package name
    for script_part in command.script_parts:
        if (
            script_part not in ["choco", "cinst", "install"]
            # Need exact match (bc chocolatey is a package)
            and not script_part.startswith('-')
            # Leading hyphens are parameters; some packages contain them though
            and '=' not in script_part and '/' not in script_part
            # These are certainly parameters
        ):
            return command.script.replace(script_part, script_part + ".install")
    return []


enabled_by_default = bool(which("choco")) or bool(which("cinst"))
Repository: nvbn/thefuck
File: thefuck/rules/choco_install.py (Python)

Code before:


def get_new_command(command):
    cmdList = command.script.split(' ')
    packageName = ""
    # Find the argument that is the package name
    for i in cmdList:
        print(i)
        if "choco" in i:
            continue
        if "cinst" in i:
            continue
        if "install" in i:
            continue
        if i.startswith('-'):   # Some parameters start with hyphens; some packages contain them though
            continue
        if '=' in i:            # Some paramaters contain '='
            continue
        if '/' in i:            # Some parameters contain slashes
            continue
        else:
            packageName = i
    # Find the name of the broken package, and append metapackage names
    if not packageName:
        return False
    return(command.script.replace(packageName, packageName + ".install"))


Review comment (suggestion):
Should be disabled when those commands are not available.
```suggestion
    return(command.script.replace(packageName, packageName + ".install"))


enabled_by_default = bool(which("choco")) or bool(which("cinst"))
```

Code after:
from thefuck.utils import for_app, which


@for_app("choco", "cinst")
def match(command):
    return ((command.script.startswith('choco install') or 'cinst' in command.script_parts)
            and 'Installing the following packages' in command.output)


def get_new_command(command):
    # Find the argument that is the package name
    for script_part in command.script_parts:
        if (
            script_part not in ["choco", "cinst", "install"]
            # Need exact match (bc chocolatey is a package)
            and not script_part.startswith('-')
            # Leading hyphens are parameters; some packages contain them though
            and '=' not in script_part and '/' not in script_part
            # These are certainly parameters
        ):
            return command.script.replace(script_part, script_part + ".install")
    return []


enabled_by_default = bool(which("choco")) or bool(which("cinst"))
Repository: nvbn/thefuck
File: tests/rules/test_git_lfs_mistype.py (Python)

Code before:
    err_response = """
Error: unknown command "evn" for "git-lfs"

Did you mean this?
        env
        ext

Run 'git-lfs --help' for usage.
    """
    result = get_new_command(Command('git lfs evn', err_response))
    expected = 'git lfs env'
    assert result == expected


def test_get_another_new_command():
    err_response = """
Error: unknown command "chekout" for "git-lfs"

Did you mean this?
        checkout

Run 'git-lfs --help' for usage.
    """
    result = get_new_command(Command('git lfs chekout', err_response))
    expected = 'git lfs checkout'
    assert result == expected


Review comment (suggestion):
I recommend reading on pytest fixtures and trying to replace the repeated `err_response` definitions with a single fixture. There are examples in other test files.

Code after:
import pytest

from thefuck.rules.git_lfs_mistype import match, get_new_command
from thefuck.types import Command


@pytest.fixture
def mistype_response():
    return """
Error: unknown command "evn" for "git-lfs"

Did you mean this?
        env
        ext

Run 'git-lfs --help' for usage.
    """


def test_match(mistype_response):
    assert match(Command('git lfs evn', mistype_response))
    err_response = 'bash: git: command not found'
    assert not match(Command('git lfs env', err_response))
    assert not match(Command('docker lfs env', mistype_response))


def test_get_new_command(mistype_response):
    assert (get_new_command(Command('git lfs evn', mistype_response))
            == ['git lfs env', 'git lfs ext'])
Repository: nvbn/thefuck
File: thefuck/rules/git_lfs_mistype.py (Python)

Code before:
from thefuck.shells import shell
from thefuck.specific.git import git_support


@git_support
def match(command):
    '''
    Match a mistyped command
    '''
    return 'lfs' in command.script and 'Did you mean this?' in command.output


@git_support
def get_new_command(command):
    new = command.script.split(' ')
    recommended = command.output.split('\n')[4].strip()
    new[2] = recommended
    return shell.and_(' '.join(new))


Review comment (suggestion):
I suggest you take a look at how `thefuck/rules/git_not_command.py` is implemented, as both are very similar. For instance, check how using a regular expression can make the code more robust. Also, the use of both `get_all_matched_commands` and `replace_command` can make the code more flexible and assertive.

Code after:
import re
from thefuck.utils import get_all_matched_commands, replace_command
from thefuck.specific.git import git_support


@git_support
def match(command):
    '''
    Match a mistyped command
    '''
    return 'lfs' in command.script and 'Did you mean this?' in command.output


@git_support
def get_new_command(command):
    broken_cmd = re.findall(r'Error: unknown command "([^"]*)" for "git-lfs"', command.output)[0]
    matched = get_all_matched_commands(command.output, ['Did you mean', ' for usage.'])
    return replace_command(command, broken_cmd, matched)
Repository: nvbn/thefuck
File: thefuck/rules/git_lfs_mistype.py (Python)

Code before:
import re
from thefuck.utils import get_all_matched_commands, replace_command
from thefuck.specific.git import git_support


@git_support
def match(command):
    '''
    Match a mistyped command
    '''
    return 'lfs' in command.script and 'Did you mean this?' in command.output


@git_support
def get_new_command(command):
    broken_cmd = re.findall(r'Error: unknown command "([^"]*)" for "git-lfs"', command.output)[0]
    matched = get_all_matched_commands(command.output, ['Did you mean'])
    return replace_command(command, broken_cmd, matched)


Review comment (bug):
If you place a breakpoint right after line 17 (e.g `__import__("ipdb").set_trace()` after installing ipdb with `pip install ipdb`) you can see that `matched` contains some undesired content:

```
ipdb> l
     16 def get_new_command(command):
     17     broken_cmd = re.findall(r'Error: unknown command "([^"]+)" for "git-lfs"',
     18                             command.output)[0]
     19     matched = get_all_matched_commands(command.output, 'Did you mean')
     20     __import__("ipdb").set_trace()  # FIXME: BREAKPOINT!!!
---> 21     return replace_command(command, broken_cmd, matched)

ipdb> matched
['env', 'ext', "Run 'git-lfs --help' for usage."]
```

To get the debugger taking input, I ran TheFuck directly, instead of via `fuck`, like this: `thefuck git lfs evn`

Checking `get_all_matched_commands` source code:

https://github.com/nvbn/thefuck/blob/3c542a5b8cde085126fd9197b1ca6967c7adab1e/thefuck/utils.py#L148-L159

You can see that it supports `separator` being a list of strings. So, how about adding:

```
' for usage.'
```

to the list of separators passed to `get_all_matched_commands`? This way, `matched` will be equal to `['env', 'ext']`.

Code after:
import re
from thefuck.utils import get_all_matched_commands, replace_command
from thefuck.specific.git import git_support


@git_support
def match(command):
    '''
    Match a mistyped command
    '''
    return 'lfs' in command.script and 'Did you mean this?' in command.output


@git_support
def get_new_command(command):
    broken_cmd = re.findall(r'Error: unknown command "([^"]*)" for "git-lfs"', command.output)[0]
    matched = get_all_matched_commands(command.output, ['Did you mean', ' for usage.'])
    return replace_command(command, broken_cmd, matched)
Repository: nvbn/thefuck
File: tests/rules/test_fix_file.py (Python)

Code before:
# -*- coding: utf-8 -*-

import pytest
import os
from collections import namedtuple
from thefuck.rules.fix_file import match, get_new_command
from thefuck.types import Command

FileFixTest = namedtuple('FileFixTest', ['script', 'file', 'line', 'col', 'output'])

tests = (
    FileFixTest('gcc a.c', 'a.c', 3, 1, """
a.c: In function 'main':
a.c:3:1: error: expected expression before '}' token
 }
  ^
"""),

    FileFixTest('clang a.c', 'a.c', 3, 1, """
a.c:3:1: error: expected expression
}
^
"""),

    FileFixTest('perl a.pl', 'a.pl', 3, None, """
syntax error at a.pl line 3, at EOF
Execution of a.pl aborted due to compilation errors.
"""),

    FileFixTest('perl a.pl', 'a.pl', 2, None, """
Search pattern not terminated at a.pl line 2.
"""),

    FileFixTest('sh a.sh', 'a.sh', 2, None, """

Review comment (suggestion):
Nit:
```suggestion
FixFileTest = namedtuple('FixFileTest', ['script', 'file', 'line', 'col', 'output'])
```

Code after:
# -*- coding: utf-8 -*-

import pytest
import os
from collections import namedtuple
from thefuck.rules.fix_file import match, get_new_command
from thefuck.types import Command

FixFileTest = namedtuple('FixFileTest', ['script', 'file', 'line', 'col', 'output'])

tests = (
    FixFileTest('gcc a.c', 'a.c', 3, 1, """
a.c: In function 'main':
a.c:3:1: error: expected expression before '}' token
 }
  ^
"""),

    FixFileTest('clang a.c', 'a.c', 3, 1, """
a.c:3:1: error: expected expression
}
^
"""),

    FixFileTest('perl a.pl', 'a.pl', 3, None, """
syntax error at a.pl line 3, at EOF
Execution of a.pl aborted due to compilation errors.
"""),

    FixFileTest('perl a.pl', 'a.pl', 2, None, """
Search pattern not terminated at a.pl line 2.
"""),

    FixFileTest('sh a.sh', 'a.sh', 2, None, """