test_install_vcs_git.py 3.6 KB
Newer Older
P
Paul Nasrat 已提交
1
import sys
2 3 4

import pytest

5
from mock import patch
6

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


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


29
def test_get_refs_should_return_branch_name_and_commit_pair():
D
Donald Stufft 已提交
30 31 32 33
    script = reset_env()
    version_pkg_path = _create_test_package(script)
    script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
    commit = script.run('git', 'rev-parse', 'HEAD',
34 35
                     cwd=version_pkg_path).stdout.strip()
    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():
D
Donald Stufft 已提交
42 43 44 45
    script = reset_env()
    version_pkg_path = _create_test_package(script)
    script.run('git', 'branch', 'branch0.1', cwd=version_pkg_path)
    commit = script.run('git', 'rev-parse', 'HEAD',
46 47
                     cwd=version_pkg_path).stdout.strip()
    # current branch here is "* (nobranch)"
D
Donald Stufft 已提交
48
    script.run('git', 'checkout', commit,
49 50
            cwd=version_pkg_path, expect_stderr=True)
    git = Git()
51 52 53
    result = git.get_refs(version_pkg_path)
    assert result['master'] == commit, result
    assert result['branch0.1'] == commit, result
54 55


56 57 58
@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'}
59 60 61 62 63 64
    git = Git()

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


65 66 67
@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'}
68 69 70 71 72 73
    git = Git()

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


74 75 76
@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'}
77 78 79 80
    git = Git()

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


83 84
# TODO(pnasrat) fix all helpers to do right things with paths on windows.
@pytest.mark.skipif("sys.platform == 'win32'")
F
fin 已提交
85 86
def test_check_submodule_addition():
    """
87 88
    Submodules are pulled in on install and updated on upgrade.

F
fin 已提交
89
    """
D
Donald Stufft 已提交
90 91
    script = reset_env()
    module_path, submodule_path = _create_test_package_with_submodule(script)
F
fin 已提交
92

D
Donald Stufft 已提交
93
    install_result = script.pip('install', '-e', 'git+'+module_path+'#egg=version_pkg')
F
fin 已提交
94 95
    assert '.virtualenv/src/version-pkg/testpkg/static/testfile' in install_result.files_created

D
Donald Stufft 已提交
96 97
    _change_test_package_submodule(script, submodule_path)
    _pull_in_submodule_changes_to_module(script, module_path)
F
fin 已提交
98

99
    # expect error because git may write to stderr
D
Donald Stufft 已提交
100
    update_result = script.pip('install', '-e', 'git+'+module_path+'#egg=version_pkg', '--upgrade', expect_error=True)
F
fin 已提交
101

D
Donald Stufft 已提交
102
    assert script.venv/'src/version-pkg/testpkg/static/testfile2' in update_result.files_created