2015-05-14 17:34:40 -03:00
|
|
|
import pytest
|
2016-01-31 18:09:03 -02:00
|
|
|
from io import BytesIO
|
|
|
|
from thefuck.rules.git_checkout import match, get_branches, get_new_command
|
2015-05-14 17:34:40 -03:00
|
|
|
from tests.utils import Command
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def did_not_match(target, did_you_forget=False):
|
|
|
|
error = ("error: pathspec '{}' did not match any "
|
|
|
|
"file(s) known to git.".format(target))
|
|
|
|
if did_you_forget:
|
|
|
|
error = ("{}\nDid you forget to 'git add'?'".format(error))
|
|
|
|
return error
|
|
|
|
|
|
|
|
|
2015-07-20 19:25:29 +03:00
|
|
|
@pytest.fixture
|
2016-01-31 18:09:03 -02:00
|
|
|
def git_branch(mocker, branches):
|
|
|
|
mock = mocker.patch('subprocess.Popen')
|
|
|
|
mock.return_value.stdout = BytesIO(branches)
|
|
|
|
return mock
|
2015-07-20 19:25:29 +03:00
|
|
|
|
|
|
|
|
2015-05-14 17:34:40 -03:00
|
|
|
@pytest.mark.parametrize('command', [
|
|
|
|
Command(script='git checkout unknown', stderr=did_not_match('unknown')),
|
|
|
|
Command(script='git commit unknown', stderr=did_not_match('unknown'))])
|
|
|
|
def test_match(command):
|
2015-09-07 13:00:29 +03:00
|
|
|
assert match(command)
|
2015-05-14 17:34:40 -03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('command', [
|
|
|
|
Command(script='git submodule update unknown',
|
|
|
|
stderr=did_not_match('unknown', True)),
|
|
|
|
Command(script='git checkout known', stderr=('')),
|
|
|
|
Command(script='git commit known', stderr=(''))])
|
|
|
|
def test_not_match(command):
|
2015-09-07 13:00:29 +03:00
|
|
|
assert not match(command)
|
2015-05-14 17:34:40 -03:00
|
|
|
|
|
|
|
|
2016-01-31 18:09:03 -02:00
|
|
|
@pytest.mark.parametrize('branches, branch_list', [
|
|
|
|
(b'', []),
|
|
|
|
(b'* master', ['master']),
|
|
|
|
(b' remotes/origin/master', ['master']),
|
|
|
|
(b' just-another-branch', ['just-another-branch']),
|
|
|
|
(b'* master\n just-another-branch', ['master', 'just-another-branch']),
|
|
|
|
(b'* master\n remotes/origin/master\n just-another-branch',
|
|
|
|
['master', 'master', 'just-another-branch'])])
|
|
|
|
def test_get_branches(branches, branch_list, git_branch):
|
|
|
|
git_branch(branches)
|
|
|
|
assert list(get_branches()) == branch_list
|
|
|
|
|
|
|
|
|
2015-07-20 19:25:29 +03:00
|
|
|
@pytest.mark.parametrize('branches, command, new_command', [
|
2016-01-31 18:09:03 -02:00
|
|
|
(b'',
|
2015-07-20 19:25:29 +03:00
|
|
|
Command(script='git checkout unknown', stderr=did_not_match('unknown')),
|
2015-05-14 17:34:40 -03:00
|
|
|
'git branch unknown && git checkout unknown'),
|
2016-01-31 18:09:03 -02:00
|
|
|
(b'',
|
2015-07-20 19:25:29 +03:00
|
|
|
Command('git commit unknown', stderr=did_not_match('unknown')),
|
|
|
|
'git branch unknown && git commit unknown'),
|
2016-01-31 18:09:03 -02:00
|
|
|
(b' test-random-branch-123',
|
2015-07-22 04:52:52 +03:00
|
|
|
Command(script='git checkout tst-rdm-brnch-123',
|
|
|
|
stderr=did_not_match('tst-rdm-brnch-123')),
|
|
|
|
'git checkout test-random-branch-123'),
|
2016-01-31 18:09:03 -02:00
|
|
|
(b' test-random-branch-123',
|
2015-07-22 04:52:52 +03:00
|
|
|
Command(script='git commit tst-rdm-brnch-123',
|
|
|
|
stderr=did_not_match('tst-rdm-brnch-123')),
|
|
|
|
'git commit test-random-branch-123')])
|
2016-01-31 18:09:03 -02:00
|
|
|
def test_get_new_command(branches, command, new_command, git_branch):
|
|
|
|
git_branch(branches)
|
2015-09-07 13:00:29 +03:00
|
|
|
assert get_new_command(command) == new_command
|