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

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

https_url = 'https://github.com/nvbn/thefuck.git'
http_url = 'http://github.com/nvbn/thefuck.git'
ssh_url = 'git@github.com:nvbn/thefuck.git'

# HELP WANTED: How can I make this independent of the shell?
https_output = """
output=/bin/sh: 1: https://github.com/nvbn/thefuck.git: not found
"""
http_output = """
output=/bin/sh: 1: http://github.com/nvbn/thefuck.git: not found
"""
ssh_output = """
/bin/sh: 1: git@github.com:nvbn/thefuck.git: not found
"""

@pytest.mark.parametrize(
    'cmd', [
        Command(https_url, https_output),
        Command(http_url, http_output),
        Command(ssh_url, ssh_output),
    ]
)
def test_match(cmd):
    assert match(cmd)

@pytest.mark.parametrize(
    'cmd', [
        Command('', ''),

Review comment (suggestion):
SSH cloning might also look like `ssh://git@github.com:nvbn/thefuck.git`, at which point I dont think your match rule will work as expected

Code after:
import pytest
from thefuck.rules.git_clone_missing import match, get_new_command
from thefuck.types import Command

valid_urls = [
    'https://github.com/nvbn/thefuck.git',
    'https://github.com/nvbn/thefuck',
    'http://github.com/nvbn/thefuck.git',
    'git@github.com:nvbn/thefuck.git',
    'git@github.com:nvbn/thefuck',
    'ssh://git@github.com:nvbn/thefuck.git',
]
invalid_urls = [
    '',  # No command
    'notacommand',  # Command not found
    'ssh git@github.com:nvbn/thefrick.git',  # ssh command, not a git clone
    'git clone foo',  # Valid clone
    'git clone https://github.com/nvbn/thefuck.git',  # Full command
    'github.com/nvbn/thefuck.git',  # Missing protocol
    'github.com:nvbn/thefuck.git',  # SSH missing username
    'git clone git clone ssh://git@github.com:nvbn/thefrick.git',  # 2x clone
    'https:/github.com/nvbn/thefuck.git'  # Bad protocol
]
outputs = [
    'No such file or directory',
    'not found',
    'is not recognised as',
]


@pytest.mark.parametrize('cmd', valid_urls)
@pytest.mark.parametrize('output', outputs)
Repository: nvbn/thefuck
File: thefuck/rules/git_clone_missing.py (Python)

Code before:
'''
Rule: git_clone_missing

Correct missing `git clone` command when pasting a git URL

```sh
& https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git

Author: Miguel Guthridge
'''
from urllib import parse

output_contains = [
    'not found',
    'no such file or directory',
    'is not recognized as',
]


def match(command):
    # Ignore capitalisation in output
    output = command.output.lower()
    script = command.script
    # Check for a command not found error
    if not any([search in output for search in output_contains]):
        return False
    # URLs can't have spaces
    if ' ' in command.script:
        return False
    url = parse.urlparse(script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not ('@' in script and ':' in script):
        return False

Review comment (suggestion):
We still support py2:

```suggestion
from six.moves.urllib import parse
```

Code after:
'''
Rule: git_clone_missing

Correct missing `git clone` command when pasting a git URL

```sh
>>> https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git
```

Author: Miguel Guthridge
'''
from six.moves.urllib import parse
from thefuck.utils import which


def match(command):
    # We want it to be a URL by itself
    if len(command.script_parts) != 1:
        return False
    # Ensure we got the error we expected
    if which(command.script_parts[0]) or not (
        'No such file or directory' in command.output
        or 'not found' in command.output
        or 'is not recognised as' in command.output
    ):
        return False
    url = parse.urlparse(command.script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not (
        '@' in command.script
        and ':' in command.script
    ):
        return False
Repository: nvbn/thefuck
File: thefuck/rules/git_clone_missing.py (Python)

Code before:
'''
Rule: git_clone_missing

Correct missing `git clone` command when pasting a git URL

```sh
& https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git

Author: Miguel Guthridge
'''
from urllib import parse

output_contains = [
    'not found',
    'no such file or directory',
    'is not recognized as',
]


def match(command):
    # Ignore capitalisation in output
    output = command.output.lower()
    script = command.script
    # Check for a command not found error
    if not any([search in output for search in output_contains]):
        return False
    # URLs can't have spaces
    if ' ' in command.script:
        return False
    url = parse.urlparse(script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not ('@' in script and ':' in script):
        return False
    return url.scheme in ['http', 'https', 'ssh']


def get_new_command(command):
    return 'git clone ' + command.script


Review comment (suggestion):
Let's not create a list just to iterate over it:

```suggestion
    if not any(search in output for search in output_contains):
```

Code after:
'''
Rule: git_clone_missing

Correct missing `git clone` command when pasting a git URL

```sh
>>> https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git
```

Author: Miguel Guthridge
'''
from six.moves.urllib import parse
from thefuck.utils import which


def match(command):
    # We want it to be a URL by itself
    if len(command.script_parts) != 1:
        return False
    # Ensure we got the error we expected
    if which(command.script_parts[0]) or not (
        'No such file or directory' in command.output
        or 'not found' in command.output
        or 'is not recognised as' in command.output
    ):
        return False
    url = parse.urlparse(command.script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not (
        '@' in command.script
        and ':' in command.script
    ):
        return False
    return url.scheme in ['http', 'https', 'ssh']


def get_new_command(command):
    return 'git clone ' + command.script
Repository: nvbn/thefuck
File: thefuck/rules/git_clone_missing.py (Python)

Code before:
Correct missing `git clone` command when pasting a git URL

```sh
& https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git

Author: Miguel Guthridge
'''
from urllib import parse

output_contains = [
    'not found',
    'no such file or directory',
    'is not recognized as',
]


def match(command):
    # Ignore capitalisation in output
    output = command.output.lower()
    script = command.script
    # Check for a command not found error
    if not any([search in output for search in output_contains]):
        return False
    # URLs can't have spaces
    if ' ' in command.script:
        return False
    url = parse.urlparse(script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not ('@' in script and ':' in script):
        return False
    return url.scheme in ['http', 'https', 'ssh']


def get_new_command(command):
    return 'git clone ' + command.script


Review comment (suggestion):
Let's avoid iterating over that string once again:
```suggestion
    if len(command.script_parts) > 1:
```

Code after:
Correct missing `git clone` command when pasting a git URL

```sh
>>> https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git
```

Author: Miguel Guthridge
'''
from six.moves.urllib import parse
from thefuck.utils import which


def match(command):
    # We want it to be a URL by itself
    if len(command.script_parts) != 1:
        return False
    # Ensure we got the error we expected
    if which(command.script_parts[0]) or not (
        'No such file or directory' in command.output
        or 'not found' in command.output
        or 'is not recognised as' in command.output
    ):
        return False
    url = parse.urlparse(command.script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not (
        '@' in command.script
        and ':' in command.script
    ):
        return False
    return url.scheme in ['http', 'https', 'ssh']


def get_new_command(command):
    return 'git clone ' + command.script
Repository: nvbn/thefuck
File: thefuck/rules/git_clone_missing.py (Python)

Code before:
'''
Rule: git_clone_missing

Correct missing `git clone` command when pasting a git URL

```sh
>>> https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git
```

Author: Miguel Guthridge
'''
from six.moves.urllib import parse


def match(command):
    script = command.script
    parts = command.script_parts
    # We want it to be a URL by itself
    if len(parts) > 1:
        return False
    url = parse.urlparse(script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not ('@' in script and ':' in script):
        return False
    return url.scheme in ['http', 'https', 'ssh']


def get_new_command(command):
    return 'git clone ' + command.script


Review comment (suggestion):
No need to care about Bash nor ZSH nor Fish, only `sh` and `cmd`/`pwsh`:
```suggestion
    # Check for a command not found error
    if which(command.script_parts[0]) or not (
        'not found' in command.output or 'is not recognized as' in command.output
    ):
        return False
    script = command.script
```
It's also marginally faster than using a generator with `any`.

Nit: declare variables only when necessary.

Code after:
'''
Rule: git_clone_missing

Correct missing `git clone` command when pasting a git URL

```sh
>>> https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git
```

Author: Miguel Guthridge
'''
from six.moves.urllib import parse
from thefuck.utils import which


def match(command):
    # We want it to be a URL by itself
    if len(command.script_parts) != 1:
        return False
    # Ensure we got the error we expected
    if which(command.script_parts[0]) or not (
        'No such file or directory' in command.output
        or 'not found' in command.output
        or 'is not recognised as' in command.output
    ):
        return False
    url = parse.urlparse(command.script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not (
        '@' in command.script
        and ':' in command.script
    ):
        return False
    return url.scheme in ['http', 'https', 'ssh']


def get_new_command(command):
    return 'git clone ' + command.script
Repository: nvbn/thefuck
File: thefuck/rules/git_clone_missing.py (Python)

Code before:
'''
Rule: git_clone_missing

Correct missing `git clone` command when pasting a git URL

```sh
>>> https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git
```

Author: Miguel Guthridge
'''
from six.moves.urllib import parse


def match(command):
    script = command.script
    parts = command.script_parts
    # We want it to be a URL by itself
    if len(parts) > 1:
        return False
    url = parse.urlparse(script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not ('@' in script and ':' in script):
        return False
    return url.scheme in ['http', 'https', 'ssh']


def get_new_command(command):
    return 'git clone ' + command.script


Review comment (suggestion):
No need to create a new variable, that's readable enough:
```suggestion
    if len(command.script_parts) > 1:
```

Code after:
'''
Rule: git_clone_missing

Correct missing `git clone` command when pasting a git URL

```sh
>>> https://github.com/nvbn/thefuck.git
git clone https://github.com/nvbn/thefuck.git
```

Author: Miguel Guthridge
'''
from six.moves.urllib import parse
from thefuck.utils import which


def match(command):
    # We want it to be a URL by itself
    if len(command.script_parts) != 1:
        return False
    # Ensure we got the error we expected
    if which(command.script_parts[0]) or not (
        'No such file or directory' in command.output
        or 'not found' in command.output
        or 'is not recognised as' in command.output
    ):
        return False
    url = parse.urlparse(command.script, scheme='ssh')
    # HTTP URLs need a network address
    if not url.netloc and url.scheme != 'ssh':
        return False
    # SSH needs a username and a splitter between the path
    if url.scheme == 'ssh' and not (
        '@' in command.script
        and ':' in command.script
    ):
        return False
    return url.scheme in ['http', 'https', 'ssh']


def get_new_command(command):
    return 'git clone ' + command.script
Repository: nvbn/thefuck
File: tests/rules/test_git_clone_missing.py (Python)

Code before:
    'https:/github.com/nvbn/thefuck.git'  # Bad protocol
]


@pytest.mark.parametrize(
    'cmd',
    [Command(c, 'not found') for c in valid_urls]
)
def test_match(cmd):
    assert match(cmd)


@pytest.mark.parametrize(
    'cmd',
    [Command(c, 'not found') for c in invalid_urls]
)
def test_not_match(cmd):
    assert not match(cmd)


@pytest.mark.parametrize(
    'cmd',
    [Command(c, 'not found') for c in valid_urls]
)
def test_get_new_command(cmd):
    assert get_new_command(cmd) == 'git clone ' + cmd.script


Review comment (suggestion):
Would be better if the `Command` instance was created in the test itself rather than in the parameterization. Then `script` could be used instead of `cmd.script`:

```suggestion
@pytest.mark.parametrize('script', valid_urls)
def test_get_new_command(script):
    command = Command(script, shell_errors[0])
    new_command = 'git clone ' + script
    assert get_new_command(command) == new_command
```

Code after:
    'https:/github.com/nvbn/thefuck.git'  # Bad protocol
]
outputs = [
    'No such file or directory',
    'not found',
    'is not recognised as',
]


@pytest.mark.parametrize('cmd', valid_urls)
@pytest.mark.parametrize('output', outputs)
def test_match(cmd, output):
    c = Command(cmd, output)
    assert match(c)


@pytest.mark.parametrize('cmd', invalid_urls)
@pytest.mark.parametrize('output', outputs + ["some other output"])
def test_not_match(cmd, output):
    c = Command(cmd, output)
    assert not match(c)


@pytest.mark.parametrize('script', valid_urls)
@pytest.mark.parametrize('output', outputs)
def test_get_new_command(script, output):
    command = Command(script, output)
    new_command = 'git clone ' + script
    assert get_new_command(command) == new_command
Repository: nvbn/thefuck
File: tests/rules/test_brew_install.py (Python)

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


@pytest.fixture
def brew_no_available_formula():
    return '''Warning: No available formula with the name "gitt". Did you mean git, gitg or gist?'''


@pytest.fixture
def brew_install_no_argument():
    return '''Install a formula or cask. Additional options specific to a formula may be'''


@pytest.fixture
def brew_already_installed():
    return '''Warning: git-2.3.5 already installed'''


def test_match(brew_no_available_formula, brew_already_installed,
               brew_install_no_argument):
    assert match(Command('brew install gitt',
                         brew_no_available_formula))
    assert not match(Command('brew install git',
                             brew_already_installed))
    assert not match(Command('brew install', brew_install_no_argument))


def test_get_new_command(brew_no_available_formula):
    assert get_new_command(Command('brew install gitt',
                                   brew_no_available_formula))\
        == 'brew install git'

    assert get_new_command(Command('brew install aa',
                                   brew_no_available_formula))\
        != 'brew install aha'


Review comment (suggestion):
I say we miss some possibly good extra suggestions here. How about extending the rule to suggesting those other too? e.g. for `brew install elsticsearch`, The Fuck would first recommend `brew install elasticsearch` then `brew install elasticsearch@6`.

Code after:
import pytest
from thefuck.rules.brew_install import match, get_new_command, _get_suggestions
from thefuck.types import Command


@pytest.fixture
def brew_no_available_formula_one():
    return '''Warning: No available formula with the name "giss". Did you mean gist?'''


@pytest.fixture
def brew_no_available_formula_two():
    return '''Warning: No available formula with the name "elasticserar". Did you mean elasticsearch or elasticsearch@6?'''


@pytest.fixture
def brew_no_available_formula_three():
    return '''Warning: No available formula with the name "gitt". Did you mean git, gitg or gist?'''


@pytest.fixture
def brew_install_no_argument():
    return '''Install a formula or cask. Additional options specific to a formula may be'''


@pytest.fixture
def brew_already_installed():
    return '''Warning: git-2.3.5 already installed'''


def test_suggestions():
    assert _get_suggestions("one") == ['one']
    assert _get_suggestions("one or two") == ['one', 'two']
    assert _get_suggestions("one, two or three") == ['one', 'two', 'three']


def test_match(brew_no_available_formula_one, brew_no_available_formula_two,
               brew_no_available_formula_three, brew_already_installed,
               brew_install_no_argument):
    assert match(Command('brew install giss',
                         brew_no_available_formula_one))
    assert match(Command('brew install elasticserar',
                         brew_no_available_formula_two))
Repository: nvbn/thefuck
File: tests/rules/test_terraform_no_command.py (Python)

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


@pytest.mark.parametrize('script, output', [
    ('terraform appyl', 'Terraform has no command named \"appyl\". Did you mean \"apply\"?)'),
    ('terraform destory', 'Terraform has no command named \"destory\". Did you mean \"destroy\"?)')])
def test_match(script, output):
    assert match(Command(script, output))


@pytest.mark.parametrize('script, output', [
    ('terraform --version', 'Terraform v0.12.2'),
    ('terraform plan', 'No changes. Infrastructure is up-to-date.'),
    ('terraform apply', 'Apply complete! Resources: 0 added, 0 changed, 0 destroyed.'),
])
def test_not_match(script, output):
    assert not match(Command(script, output=output))


@pytest.mark.parametrize('command, new_command', [
    (Command('terraform appyl', 'Terraform has no command named \"appyl\". Did you mean \"apply\"?)'), 'terraform apply'),
    (Command('terraform destory --some-other-option', 'Terraform has no command named \"destory\". Did you mean \"destroy\"?)'), 'terraform destroy --some-other-option'),
])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command


Review comment (suggestion):
```suggestion
    ('terraform appyl', 'Terraform has no command named "appyl". Did you mean "apply"?'),
    ('terraform destory', 'Terraform has no command named "destory". Did you mean "destroy"?')])
```

Code after:
import pytest
from thefuck.rules.terraform_no_command import match, get_new_command
from thefuck.types import Command


@pytest.mark.parametrize('script, output', [
    ('terraform appyl', 'Terraform has no command named "appyl". Did you mean "apply"?'),
    ('terraform destory', 'Terraform has no command named "destory". Did you mean "destroy"?')])
def test_match(script, output):
    assert match(Command(script, output))


@pytest.mark.parametrize('script, output', [
    ('terraform --version', 'Terraform v0.12.2'),
    ('terraform plan', 'No changes. Infrastructure is up-to-date.'),
    ('terraform apply', 'Apply complete! Resources: 0 added, 0 changed, 0 destroyed.'),
])
def test_not_match(script, output):
    assert not match(Command(script, output))


@pytest.mark.parametrize('script, output, new_command', [
    ('terraform appyl', 'Terraform has no command named "appyl". Did you mean "apply"?', 'terraform apply',),
    ('terraform destory --some-other-option', 'Terraform has no command named "destory". Did you mean "destroy"?', 'terraform destroy --some-other-option',),
])
def test_get_new_command(script, output, new_command):
    assert get_new_command(Command(script, output)) == new_command