test_install_vcs_git.py 3.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
    _change_test_package_submodule, _create_test_package_with_submodule,
P
Pradyun Gedam 已提交
8
    _pull_in_submodule_changes_to_module,
D
Donald Stufft 已提交
9
)
10 11


12
@pytest.mark.network
13
def test_is_commit_id_equal(script):
14
    """
15
    Test Git.is_commit_id_equal().
16
    """
17 18 19 20 21 22 23
    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()
24 25 26 27
    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')
28
    # Also check passing a None value.
29
    assert not git.is_commit_id_equal(version_pkg_path, None)
30 31


32 33 34
@patch('pip._internal.vcs.git.Git.get_revision_sha')
def test_check_rev_options_ref_exists(get_sha_mock):
    get_sha_mock.return_value = '123456'
35
    git = Git()
36
    rev_options = git.make_rev_options('develop')
37

C
Chris Jerdonek 已提交
38 39
    new_options = git.check_rev_options('.', rev_options)
    assert new_options.rev == '123456'
40 41


42 43 44
@patch('pip._internal.vcs.git.Git.get_revision_sha')
def test_check_rev_options_ref_not_found(get_sha_mock):
    get_sha_mock.return_value = None
45
    git = Git()
46
    rev_options = git.make_rev_options('develop')
47

C
Chris Jerdonek 已提交
48
    new_options = git.check_rev_options('.', rev_options)
49
    assert new_options.rev == 'develop'
F
fin 已提交
50 51


52 53 54
@patch('pip._internal.vcs.git.Git.get_revision_sha')
def test_check_rev_options_not_found_warning(get_sha_mock, caplog):
    get_sha_mock.return_value = None
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    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 == [
70
        "Did not find branch or tag 'aaaaaa', assuming revision or ref."
71 72 73
    ]


74 75
# TODO(pnasrat) fix all helpers to do right things with paths on windows.
@pytest.mark.skipif("sys.platform == 'win32'")
76
@pytest.mark.network
77
def test_check_submodule_addition(script):
F
fin 已提交
78
    """
79
    Submodules are pulled in on install and updated on upgrade.
F
fin 已提交
80
    """
D
Donald Stufft 已提交
81
    module_path, submodule_path = _create_test_package_with_submodule(script)
F
fin 已提交
82

83
    install_result = script.pip(
D
Donald Stufft 已提交
84
        'install', '-e', 'git+' + module_path + '#egg=version_pkg'
85 86
    )
    assert (
D
Donald Stufft 已提交
87
        script.venv / 'src/version-pkg/testpkg/static/testfile'
88 89
        in install_result.files_created
    )
F
fin 已提交
90

D
Donald Stufft 已提交
91 92
    _change_test_package_submodule(script, submodule_path)
    _pull_in_submodule_changes_to_module(script, module_path)
F
fin 已提交
93

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

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