test_install_vcs_git.py 3.6 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
def test_get_refs_should_return_tag_name_and_commit_pair(script):
D
Donald Stufft 已提交
15 16 17
    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)
18 19 20 21
    commit = script.run(
        'git', 'rev-parse', 'HEAD',
        cwd=version_pkg_path
    ).stdout.strip()
22
    git = Git()
23 24 25
    result = git.get_refs(version_pkg_path)
    assert result['0.1'] == commit, result
    assert result['0.2'] == commit, result
26 27


28
def test_get_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 37 38
    result = git.get_refs(version_pkg_path)
    assert result['master'] == commit, result
    assert result['branch0.1'] == commit, result
39 40


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


60 61 62
@patch('pip.vcs.git.Git.get_refs')
def test_check_rev_options_should_handle_branch_name(get_refs_mock):
    get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
63 64 65 66 67 68
    git = Git()

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


69 70 71
@patch('pip.vcs.git.Git.get_refs')
def test_check_rev_options_should_handle_tag_name(get_refs_mock):
    get_refs_mock.return_value = {'master': '123456', '0.1': '123456'}
72 73 74 75 76 77
    git = Git()

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


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

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


87 88
# TODO(pnasrat) fix all helpers to do right things with paths on windows.
@pytest.mark.skipif("sys.platform == 'win32'")
89
def test_check_submodule_addition(script):
F
fin 已提交
90
    """
91
    Submodules are pulled in on install and updated on upgrade.
F
fin 已提交
92
    """
D
Donald Stufft 已提交
93
    module_path, submodule_path = _create_test_package_with_submodule(script)
F
fin 已提交
94

95
    install_result = script.pip(
D
Donald Stufft 已提交
96
        'install', '-e', 'git+' + module_path + '#egg=version_pkg'
97 98
    )
    assert (
D
Donald Stufft 已提交
99
        script.venv / 'src/version-pkg/testpkg/static/testfile'
100 101
        in install_result.files_created
    )
F
fin 已提交
102

D
Donald Stufft 已提交
103 104
    _change_test_package_submodule(script, submodule_path)
    _pull_in_submodule_changes_to_module(script, module_path)
F
fin 已提交
105

106
    # expect error because git may write to stderr
107
    update_result = script.pip(
D
Donald Stufft 已提交
108 109
        'install', '-e', 'git+' + module_path + '#egg=version_pkg',
        '--upgrade',
110 111
        expect_error=True,
    )
F
fin 已提交
112

113
    assert (
D
Donald Stufft 已提交
114
        script.venv / 'src/version-pkg/testpkg/static/testfile2'
115 116
        in update_result.files_created
    )