test_install_vcs_git.py 5.4 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
@pytest.mark.network
62
def test_is_commit_id_equal(script):
63
    """
64
    Test Git.is_commit_id_equal().
65
    """
66 67 68 69 70 71 72
    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()
73 74 75 76
    assert git.is_commit_id_equal(version_pkg_path, commit)
    assert not git.is_commit_id_equal(version_pkg_path, commit[:7])
    assert not git.is_commit_id_equal(version_pkg_path, 'branch0.1')
    assert not git.is_commit_id_equal(version_pkg_path, 'abc123')
77
    # Also check passing a None value.
78
    assert not git.is_commit_id_equal(version_pkg_path, None)
79 80


81
@patch('pip._internal.vcs.git.Git.get_short_refs')
82
def test_check_rev_options_should_handle_branch_name(get_refs_mock):
C
Chris Jerdonek 已提交
83
    get_refs_mock.return_value = {'master': '123456', '0.1': 'abc123'}
84
    git = Git()
C
Chris Jerdonek 已提交
85
    rev_options = git.make_rev_options('master')
86

C
Chris Jerdonek 已提交
87 88
    new_options = git.check_rev_options('.', rev_options)
    assert new_options.rev == '123456'
89 90


91
@patch('pip._internal.vcs.git.Git.get_short_refs')
92
def test_check_rev_options_should_handle_tag_name(get_refs_mock):
C
Chris Jerdonek 已提交
93
    get_refs_mock.return_value = {'master': '123456', '0.1': 'abc123'}
94
    git = Git()
C
Chris Jerdonek 已提交
95
    rev_options = git.make_rev_options('0.1')
96

C
Chris Jerdonek 已提交
97 98
    new_options = git.check_rev_options('.', rev_options)
    assert new_options.rev == 'abc123'
99 100


101
@patch('pip._internal.vcs.git.Git.get_short_refs')
102 103
def test_check_rev_options_should_handle_ambiguous_commit(get_refs_mock):
    get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
104
    git = Git()
C
Chris Jerdonek 已提交
105
    rev_options = git.make_rev_options('0.1')
106

C
Chris Jerdonek 已提交
107 108
    new_options = git.check_rev_options('.', rev_options)
    assert new_options.rev == '123456'
F
fin 已提交
109 110


111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
@patch('pip._internal.vcs.git.Git.get_short_refs')
def test_check_rev_options_not_found_warning(get_refs_mock, caplog):
    get_refs_mock.return_value = {}
    git = Git()

    sha = 40 * 'a'
    rev_options = git.make_rev_options(sha)
    new_options = git.check_rev_options('.', rev_options)
    assert new_options.rev == sha

    rev_options = git.make_rev_options(sha[:6])
    new_options = git.check_rev_options('.', rev_options)
    assert new_options.rev == 'aaaaaa'

    # Check that a warning got logged only for the abbreviated hash.
    messages = [r.getMessage() for r in caplog.records]
    messages = [msg for msg in messages if msg.startswith('Did not find ')]
    assert messages == [
        "Did not find branch or tag 'aaaaaa', assuming ref or revision."
    ]


133 134
# TODO(pnasrat) fix all helpers to do right things with paths on windows.
@pytest.mark.skipif("sys.platform == 'win32'")
135
@pytest.mark.network
136
def test_check_submodule_addition(script):
F
fin 已提交
137
    """
138
    Submodules are pulled in on install and updated on upgrade.
F
fin 已提交
139
    """
D
Donald Stufft 已提交
140
    module_path, submodule_path = _create_test_package_with_submodule(script)
F
fin 已提交
141

142
    install_result = script.pip(
D
Donald Stufft 已提交
143
        'install', '-e', 'git+' + module_path + '#egg=version_pkg'
144 145
    )
    assert (
D
Donald Stufft 已提交
146
        script.venv / 'src/version-pkg/testpkg/static/testfile'
147 148
        in install_result.files_created
    )
F
fin 已提交
149

D
Donald Stufft 已提交
150 151
    _change_test_package_submodule(script, submodule_path)
    _pull_in_submodule_changes_to_module(script, module_path)
F
fin 已提交
152

153
    # expect error because git may write to stderr
154
    update_result = script.pip(
D
Donald Stufft 已提交
155 156
        'install', '-e', 'git+' + module_path + '#egg=version_pkg',
        '--upgrade',
157 158
        expect_error=True,
    )
F
fin 已提交
159

160
    assert (
D
Donald Stufft 已提交
161
        script.venv / 'src/version-pkg/testpkg/static/testfile2'
162 163
        in update_result.files_created
    )