test_install_vcs_git.py 4.3 KB
Newer Older
1
import pytest
2
from mock import patch
3

4
from pip._internal.vcs.git import Git
5
from tests.lib import _create_test_package
6
from tests.lib.git_submodule_helpers import (
P
Pradyun S. Gedam 已提交
7 8
    _change_test_package_submodule, _create_test_package_with_submodule,
    _pull_in_submodule_changes_to_module
D
Donald Stufft 已提交
9
)
10 11


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


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


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


61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
@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'])


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

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


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

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


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

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


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

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

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

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

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