test_install_vcs.py 10.5 KB
Newer Older
1
import pytest
2

P
Pradyun S. Gedam 已提交
3
from tests.lib import (
P
Pradyun Gedam 已提交
4
    _change_test_package_version, _create_test_package, pyversion,
P
Pradyun S. Gedam 已提交
5
)
6
from tests.lib.local_repos import local_checkout
7

8

9
def _install_version_pkg(script, path, rev=None):
10 11 12 13 14 15
    """
    Install the version_pkg package, and return the version installed.

    Args:
      path: a tests.lib.path.Path object pointing to a Git repository
        containing the package.
16
      rev: an optional revision to install like a branch name or tag.
17
    """
18 19 20 21
    path = path.abspath.replace('\\', '/')
    revision = '' if rev is None else '@{}'.format(rev)
    url = 'git+file://{}{}#egg=version_pkg'.format(path, revision)
    script.pip('install', '-e', url)
22 23 24 25 26 27
    result = script.run('version_pkg')
    version = result.stdout.strip()

    return version


28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
def test_git_install_again_after_changes(script):
    """
    Test installing a repository a second time without specifying a revision,
    and after updates to the remote repository.

    This test also checks that no warning message like the following gets
    logged on the update: "Did not find branch or tag ..., assuming ref or
    revision."
    """
    version_pkg_path = _create_test_package(script)
    version = _install_version_pkg(script, version_pkg_path)
    assert version == '0.1'

    _change_test_package_version(script, version_pkg_path)
    version = _install_version_pkg(script, version_pkg_path)
    assert version == 'some different version'


46 47 48 49 50 51
def test_git_install_branch_again_after_branch_changes(script):
    """
    Test installing a branch again after the branch is updated in the remote
    repository.
    """
    version_pkg_path = _create_test_package(script)
52
    version = _install_version_pkg(script, version_pkg_path, rev='master')
53 54 55
    assert version == '0.1'

    _change_test_package_version(script, version_pkg_path)
56
    version = _install_version_pkg(script, version_pkg_path, rev='master')
57 58 59
    assert version == 'some different version'


60
@pytest.mark.network
61
def test_install_editable_from_git_with_https(script, tmpdir):
J
Jannis Leidel 已提交
62 63 64
    """
    Test cloning from Git with https.
    """
65 66 67 68 69 70 71 72 73
    result = script.pip(
        'install', '-e',
        '%s#egg=pip-test-package' %
        local_checkout(
            'git+https://github.com/pypa/pip-test-package.git',
            tmpdir.join("cache"),
        ),
        expect_error=True,
    )
74
    result.assert_installed('pip-test-package', with_files=['.git'])
J
Jannis Leidel 已提交
75

H
Hugo Lopes Tavares 已提交
76

77
@pytest.mark.network
78 79 80 81 82 83
def test_install_noneditable_git(script, tmpdir):
    """
    Test installing from a non-editable git URL with a given tag.
    """
    result = script.pip(
        'install',
M
Marcus Smith 已提交
84 85 86
        'git+https://github.com/pypa/pip-test-package.git'
        '@0.1.1#egg=pip-test-package'
    )
87
    egg_info_folder = (
M
Marcus Smith 已提交
88 89 90 91 92 93
        script.site_packages /
        'pip_test_package-0.1.1-py%s.egg-info' % pyversion
    )
    result.assert_installed('piptestpackage',
                            without_egg_link=True,
                            editable=False)
94 95 96
    assert egg_info_folder in result.files_created, str(result)


97
@pytest.mark.network
98
def test_git_with_sha1_revisions(script):
H
Hugo Lopes Tavares 已提交
99 100 101
    """
    Git backend should be able to install from SHA1 revisions
    """
D
Donald Stufft 已提交
102 103
    version_pkg_path = _create_test_package(script)
    _change_test_package_version(script, version_pkg_path)
104 105 106 107 108 109 110
    sha1 = script.run(
        'git', 'rev-parse', 'HEAD~1',
        cwd=version_pkg_path,
    ).stdout.strip()
    script.pip(
        'install', '-e',
        '%s@%s#egg=version_pkg' %
111 112
        ('git+file://' + version_pkg_path.abspath.replace('\\', '/'), sha1),
        expect_stderr=True
113
    )
D
Donald Stufft 已提交
114
    version = script.run('version_pkg')
H
Hugo Lopes Tavares 已提交
115 116
    assert '0.1' in version.stdout, version.stdout

117

118
@pytest.mark.network
119
def test_git_with_branch_name_as_revision(script):
120 121 122
    """
    Git backend should be able to install from branch names
    """
D
Donald Stufft 已提交
123
    version_pkg_path = _create_test_package(script)
124 125 126 127 128
    script.run(
        'git', 'checkout', '-b', 'test_branch',
        expect_stderr=True,
        cwd=version_pkg_path,
    )
D
Donald Stufft 已提交
129
    _change_test_package_version(script, version_pkg_path)
130 131 132 133
    script.pip(
        'install', '-e', '%s@test_branch#egg=version_pkg' %
        ('git+file://' + version_pkg_path.abspath.replace('\\', '/'))
    )
D
Donald Stufft 已提交
134
    version = script.run('version_pkg')
135 136
    assert 'some different version' in version.stdout

137

138
@pytest.mark.network
139
def test_git_with_tag_name_as_revision(script):
140 141 142
    """
    Git backend should be able to install from tag names
    """
D
Donald Stufft 已提交
143
    version_pkg_path = _create_test_package(script)
144 145 146 147 148
    script.run(
        'git', 'tag', 'test_tag',
        expect_stderr=True,
        cwd=version_pkg_path,
    )
D
Donald Stufft 已提交
149
    _change_test_package_version(script, version_pkg_path)
150 151 152 153
    script.pip(
        'install', '-e', '%s@test_tag#egg=version_pkg' %
        ('git+file://' + version_pkg_path.abspath.replace('\\', '/'))
    )
D
Donald Stufft 已提交
154
    version = script.run('version_pkg')
155 156
    assert '0.1' in version.stdout

157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
@pytest.mark.network
def test_git_with_ref_as_revision(script):
    """
    Git backend should be able to install from a ref
    """
    version_pkg_path = _create_test_package(script)
    script.run(
        'git', 'update-ref', 'refs/foo/bar', 'HEAD',
        expect_stderr=True,
        cwd=version_pkg_path,
    )
    _change_test_package_version(script, version_pkg_path)
    script.pip(
        'install', '-e', '%s@refs/foo/bar#egg=version_pkg' %
        ('git+file://' + version_pkg_path.abspath.replace('\\', '/')),
        expect_stderr=True
    )
    version = script.run('version_pkg')
    assert '0.1' in version.stdout


179
@pytest.mark.network
180
def test_git_with_tag_name_and_update(script, tmpdir):
181 182 183
    """
    Test cloning a git repository and updating to a different version.
    """
184 185 186
    result = script.pip(
        'install', '-e', '%s#egg=pip-test-package' %
        local_checkout(
187
            'git+https://github.com/pypa/pip-test-package.git',
188 189 190 191
            tmpdir.join("cache"),
        ),
        expect_error=True,
    )
192
    result.assert_installed('pip-test-package', with_files=['.git'])
193 194 195 196
    result = script.pip(
        'install', '--global-option=--version', '-e',
        '%s@0.1.2#egg=pip-test-package' %
        local_checkout(
197
            'git+https://github.com/pypa/pip-test-package.git',
198 199 200 201
            tmpdir.join("cache"),
        ),
        expect_error=True,
    )
202
    assert '0.1.2' in result.stdout
203

204

205
@pytest.mark.network
206
def test_git_branch_should_not_be_changed(script, tmpdir):
207 208 209 210
    """
    Editable installations should not change branch
    related to issue #32 and #161
    """
211 212 213
    script.pip(
        'install', '-e', '%s#egg=pip-test-package' %
        local_checkout(
214
            'git+https://github.com/pypa/pip-test-package.git',
215 216 217 218
            tmpdir.join("cache"),
        ),
        expect_error=True,
    )
D
Donald Stufft 已提交
219
    source_dir = script.venv_path / 'src' / 'pip-test-package'
D
Donald Stufft 已提交
220
    result = script.run('git', 'branch', cwd=source_dir)
C
Carl Meyer 已提交
221
    assert '* master' in result.stdout, result.stdout
222

223

224
@pytest.mark.network
225
def test_git_with_non_editable_unpacking(script, tmpdir):
226 227 228
    """
    Test cloning a git repository from a non-editable URL with a given tag.
    """
229 230 231
    result = script.pip(
        'install', '--global-option=--version',
        local_checkout(
232
            'git+https://github.com/pypa/pip-test-package.git@0.1.2'
233 234 235 236 237
            '#egg=pip-test-package',
            tmpdir.join("cache")
        ),
        expect_error=True,
    )
238
    assert '0.1.2' in result.stdout
239

240

241
@pytest.mark.network
242
def test_git_with_editable_where_egg_contains_dev_string(script, tmpdir):
243
    """
244 245
    Test cloning a git repository from an editable url which contains "dev"
    string
246
    """
247 248 249 250 251
    result = script.pip(
        'install', '-e',
        '%s#egg=django-devserver' %
        local_checkout(
            'git+git://github.com/dcramer/django-devserver.git',
D
Donald Stufft 已提交
252
            tmpdir.join("cache")
253
        )
D
Donald Stufft 已提交
254
    )
255 256
    result.assert_installed('django-devserver', with_files=['.git'])

257

258
@pytest.mark.network
259
def test_git_with_non_editable_where_egg_contains_dev_string(script, tmpdir):
260
    """
261 262
    Test cloning a git repository from a non-editable url which contains "dev"
    string
263
    """
264 265 266 267 268
    result = script.pip(
        'install',
        '%s#egg=django-devserver' %
        local_checkout(
            'git+git://github.com/dcramer/django-devserver.git',
D
Donald Stufft 已提交
269 270 271 272
            tmpdir.join("cache")
        ),
    )
    devserver_folder = script.site_packages / 'devserver'
273
    assert devserver_folder in result.files_created, str(result)
274 275


276
@pytest.mark.network
277
def test_git_with_ambiguous_revs(script):
278 279 280
    """
    Test git with two "names" (tag/branch) pointing to the same commit
    """
D
Donald Stufft 已提交
281
    version_pkg_path = _create_test_package(script)
282 283 284 285
    package_url = (
        'git+file://%s@0.1#egg=version_pkg' %
        (version_pkg_path.abspath.replace('\\', '/'))
    )
D
Donald Stufft 已提交
286 287
    script.run('git', 'tag', '0.1', cwd=version_pkg_path)
    result = script.pip('install', '-e', package_url)
288 289 290 291
    assert 'Could not find a tag or branch' not in result.stdout
    # it is 'version-pkg' instead of 'version_pkg' because
    # egg-link name is version-pkg.egg-link because it is a single .py module
    result.assert_installed('version-pkg', with_files=['.git'])
R
test  
Rory McCann 已提交
292

293

294
@pytest.mark.network
295
def test_git_works_with_editable_non_origin_repo(script):
296 297
    # set up, create a git repo and install it as editable from a local
    # directory path
D
Donald Stufft 已提交
298 299
    version_pkg_path = _create_test_package(script)
    script.pip('install', '-e', version_pkg_path.abspath)
R
test  
Rory McCann 已提交
300

301 302
    # 'freeze'ing this should not fall over, but should result in stderr output
    # warning
D
Donald Stufft 已提交
303
    result = script.pip('freeze', expect_stderr=True)
304 305 306
    assert "Error when trying to get requirement" in result.stderr
    assert "Could not determine repository location" in result.stdout
    assert "version-pkg==0.1" in result.stdout
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335


@pytest.mark.network
def test_reinstalling_works_with_editible_non_master_branch(script):
    """
    Reinstalling an editable installation should not assume that the "master"
    branch exists. See https://github.com/pypa/pip/issues/4448.
    """
    version_pkg_path = _create_test_package(script)

    # Switch the default branch to something other than 'master'
    script.run('git', 'branch', '-m', 'foobar', cwd=version_pkg_path)

    script.pip(
        'install', '-e',
        '%s#egg=version_pkg' %
        ('git+file://' + version_pkg_path.abspath.replace('\\', '/')),
    )
    version = script.run('version_pkg')
    assert '0.1' in version.stdout

    _change_test_package_version(script, version_pkg_path)
    script.pip(
        'install', '-e',
        '%s#egg=version_pkg' %
        ('git+file://' + version_pkg_path.abspath.replace('\\', '/')),
    )
    version = script.run('version_pkg')
    assert 'some different version' in version.stdout