test_install_vcs_git.py 4.3 KB
Newer Older
1 2
import pytest

3
from mock import patch
4

5
from pip.vcs.git import Git
6
from tests.lib import _create_test_package
7
from tests.lib.git_submodule_helpers import (
8 9 10
    _change_test_package_submodule,
    _pull_in_submodule_changes_to_module,
    _create_test_package_with_submodule,
D
Donald Stufft 已提交
11
)
12 13


14
@pytest.mark.network
15
def test_get_short_refs_should_return_tag_name_and_commit_pair(script):
D
Donald Stufft 已提交
16 17 18
    version_pkg_path = _create_test_package(script)
    script.run('git', 'tag', '0.1', cwd=version_pkg_path)
    script.run('git', 'tag', '0.2', cwd=version_pkg_path)
19 20 21 22
    commit = script.run(
        'git', 'rev-parse', 'HEAD',
        cwd=version_pkg_path
    ).stdout.strip()
23
    git = Git()
24
    result = git.get_short_refs(version_pkg_path)
25 26
    assert result['0.1'] == commit, result
    assert result['0.2'] == commit, result
27 28


29
@pytest.mark.network
30
def test_get_short_refs_should_return_branch_name_and_commit_pair(script):
D
Donald Stufft 已提交
31 32
    version_pkg_path = _create_test_package(script)
    script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
33 34 35 36
    commit = script.run(
        'git', 'rev-parse', 'HEAD',
        cwd=version_pkg_path
    ).stdout.strip()
37
    git = Git()
38
    result = git.get_short_refs(version_pkg_path)
39 40
    assert result['master'] == commit, result
    assert result['branch0.1'] == commit, result
41 42


43
@pytest.mark.network
44
def test_get_short_refs_should_ignore_no_branch(script):
D
Donald Stufft 已提交
45 46
    version_pkg_path = _create_test_package(script)
    script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
47 48 49 50
    commit = script.run(
        'git', 'rev-parse', 'HEAD',
        cwd=version_pkg_path
    ).stdout.strip()
51
    # current branch here is "* (nobranch)"
52 53 54 55 56
    script.run(
        'git', 'checkout', commit,
        cwd=version_pkg_path,
        expect_stderr=True,
    )
57
    git = Git()
58
    result = git.get_short_refs(version_pkg_path)
59 60
    assert result['master'] == commit, result
    assert result['branch0.1'] == commit, result
61 62


63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
@pytest.mark.network
def test_check_version(script):
    version_pkg_path = _create_test_package(script)
    script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
    commit = script.run(
        'git', 'rev-parse', 'HEAD',
        cwd=version_pkg_path
    ).stdout.strip()
    git = Git()
    assert git.check_version(version_pkg_path, [commit])
    assert git.check_version(version_pkg_path, [commit[:7]])
    assert not git.check_version(version_pkg_path, ['branch0.1'])
    assert not git.check_version(version_pkg_path, ['abc123'])


@patch('pip.vcs.git.Git.get_short_refs')
79 80
def test_check_rev_options_should_handle_branch_name(get_refs_mock):
    get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
81 82 83 84 85 86
    git = Git()

    result = git.check_rev_options('master', '.', [])
    assert result == ['123456']


87
@patch('pip.vcs.git.Git.get_short_refs')
88 89
def test_check_rev_options_should_handle_tag_name(get_refs_mock):
    get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
90 91 92 93 94 95
    git = Git()

    result = git.check_rev_options('0.1', '.', [])
    assert result == ['123456']


96
@patch('pip.vcs.git.Git.get_short_refs')
97 98
def test_check_rev_options_should_handle_ambiguous_commit(get_refs_mock):
    get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
99 100 101 102
    git = Git()

    result = git.check_rev_options('0.1', '.', [])
    assert result == ['123456'], result
F
fin 已提交
103 104


105 106
# TODO(pnasrat) fix all helpers to do right things with paths on windows.
@pytest.mark.skipif("sys.platform == 'win32'")
107
@pytest.mark.network
108
def test_check_submodule_addition(script):
F
fin 已提交
109
    """
110
    Submodules are pulled in on install and updated on upgrade.
F
fin 已提交
111
    """
D
Donald Stufft 已提交
112
    module_path, submodule_path = _create_test_package_with_submodule(script)
F
fin 已提交
113

114
    install_result = script.pip(
D
Donald Stufft 已提交
115
        'install', '-e', 'git+' + module_path + '#egg=version_pkg'
116 117
    )
    assert (
D
Donald Stufft 已提交
118
        script.venv / 'src/version-pkg/testpkg/static/testfile'
119 120
        in install_result.files_created
    )
F
fin 已提交
121

D
Donald Stufft 已提交
122 123
    _change_test_package_submodule(script, submodule_path)
    _pull_in_submodule_changes_to_module(script, module_path)
F
fin 已提交
124

125
    # expect error because git may write to stderr
126
    update_result = script.pip(
D
Donald Stufft 已提交
127 128
        'install', '-e', 'git+' + module_path + '#egg=version_pkg',
        '--upgrade',
129 130
        expect_error=True,
    )
F
fin 已提交
131

132
    assert (
D
Donald Stufft 已提交
133
        script.venv / 'src/version-pkg/testpkg/static/testfile2'
134 135
        in update_result.files_created
    )