summaryrefslogtreecommitdiff
path: root/tests/functional/test_install_vcs_git.py
blob: d7e8c26024f37d998da6237c1f5e3cc170906d83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
from pathlib import Path
from typing import Optional

import pytest

from tests.lib import pyversion  # noqa: F401
from tests.lib import (
    PipTestEnvironment,
    _change_test_package_version,
    _create_test_package,
)
from tests.lib.git_submodule_helpers import (
    _change_test_package_submodule,
    _create_test_package_with_submodule,
    _pull_in_submodule_changes_to_module,
)
from tests.lib.local_repos import local_checkout


def _get_editable_repo_dir(script: PipTestEnvironment, package_name: str) -> Path:
    """
    Return the repository directory for an editable install.
    """
    return script.venv_path / "src" / package_name


def _get_editable_branch(script: PipTestEnvironment, package_name: str) -> str:
    """
    Return the current branch of an editable install.
    """
    repo_dir = _get_editable_repo_dir(script, package_name)
    result = script.run("git", "rev-parse", "--abbrev-ref", "HEAD", cwd=repo_dir)
    return result.stdout.strip()


def _get_branch_remote(
    script: PipTestEnvironment, package_name: str, branch: str
) -> str:
    """ """
    repo_dir = _get_editable_repo_dir(script, package_name)
    result = script.run("git", "config", f"branch.{branch}.remote", cwd=repo_dir)
    return result.stdout.strip()


def _github_checkout(
    url_path: str,
    tmpdir: Path,
    rev: Optional[str] = None,
    egg: Optional[str] = None,
    scheme: Optional[str] = None,
) -> str:
    """
    Call local_checkout() with a GitHub URL, and return the resulting URL.

    Args:
      url_path: the string used to create the package URL by filling in the
        format string "git+{scheme}://github.com/{url_path}".
      temp_dir: the pytest tmpdir value.
      egg: an optional project name to append to the URL as the egg fragment,
        prior to returning.
      scheme: the scheme without the "git+" prefix. Defaults to "https".
    """
    if scheme is None:
        scheme = "https"
    url = f"git+{scheme}://github.com/{url_path}"
    local_url = local_checkout(url, tmpdir)
    if rev is not None:
        local_url += f"@{rev}"
    if egg is not None:
        local_url += f"#egg={egg}"

    return local_url


def _make_version_pkg_url(
    path: Path, rev: Optional[str] = None, name: str = "version_pkg"
) -> str:
    """
    Return a "git+file://" URL to the version_pkg test package.

    Args:
      path: a pathlib.Path object pointing to a Git repository
        containing the version_pkg package.
      rev: an optional revision to install like a branch name, tag, or SHA.
    """
    file_url = path.as_uri()
    url_rev = "" if rev is None else f"@{rev}"
    url = f"git+{file_url}{url_rev}#egg={name}"

    return url


def _install_version_pkg_only(
    script: PipTestEnvironment,
    path: Path,
    rev: Optional[str] = None,
    allow_stderr_warning: bool = False,
) -> None:
    """
    Install the version_pkg package in editable mode (without returning
    the version).

    Args:
      path: a pathlib.Path object pointing to a Git repository
        containing the package.
      rev: an optional revision to install like a branch name or tag.
    """
    version_pkg_url = _make_version_pkg_url(path, rev=rev)
    script.pip(
        "install", "-e", version_pkg_url, allow_stderr_warning=allow_stderr_warning
    )


def _install_version_pkg(
    script: PipTestEnvironment,
    path: Path,
    rev: Optional[str] = None,
    allow_stderr_warning: bool = False,
) -> str:
    """
    Install the version_pkg package in editable mode, and return the version
    installed.

    Args:
      path: a pathlib.Path object pointing to a Git repository
        containing the package.
      rev: an optional revision to install like a branch name or tag.
    """
    _install_version_pkg_only(
        script,
        path,
        rev=rev,
        allow_stderr_warning=allow_stderr_warning,
    )
    result = script.run("version_pkg")
    version = result.stdout.strip()

    return version


def test_git_install_again_after_changes(script: PipTestEnvironment) -> None:
    """
    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.scratch_path)
    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"


def test_git_install_branch_again_after_branch_changes(
    script: PipTestEnvironment,
) -> None:
    """
    Test installing a branch again after the branch is updated in the remote
    repository.
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    version = _install_version_pkg(script, version_pkg_path, rev="master")
    assert version == "0.1"

    _change_test_package_version(script, version_pkg_path)
    version = _install_version_pkg(script, version_pkg_path, rev="master")
    assert version == "some different version"


@pytest.mark.network
def test_install_editable_from_git_with_https(
    script: PipTestEnvironment, tmpdir: Path
) -> None:
    """
    Test cloning from Git with https.
    """
    url_path = "pypa/pip-test-package.git"
    local_url = _github_checkout(url_path, tmpdir, egg="pip-test-package")
    result = script.pip("install", "-e", local_url)
    result.assert_installed("pip-test-package", with_files=[".git"])


@pytest.mark.network
def test_install_noneditable_git(script: PipTestEnvironment) -> None:
    """
    Test installing from a non-editable git URL with a given tag.
    """
    result = script.pip(
        "install",
        "git+https://github.com/pypa/pip-test-package.git"
        "@0.1.1#egg=pip-test-package",
    )
    dist_info_folder = script.site_packages / "pip_test_package-0.1.1.dist-info"
    result.assert_installed("piptestpackage", without_egg_link=True, editable=False)
    result.did_create(dist_info_folder)


def test_git_with_sha1_revisions(script: PipTestEnvironment) -> None:
    """
    Git backend should be able to install from SHA1 revisions
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    _change_test_package_version(script, version_pkg_path)
    sha1 = script.run(
        "git",
        "rev-parse",
        "HEAD~1",
        cwd=version_pkg_path,
    ).stdout.strip()
    version = _install_version_pkg(script, version_pkg_path, rev=sha1)
    assert "0.1" == version


def test_git_with_short_sha1_revisions(script: PipTestEnvironment) -> None:
    """
    Git backend should be able to install from SHA1 revisions
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    _change_test_package_version(script, version_pkg_path)
    sha1 = script.run(
        "git",
        "rev-parse",
        "HEAD~1",
        cwd=version_pkg_path,
    ).stdout.strip()[:7]
    version = _install_version_pkg(
        script,
        version_pkg_path,
        rev=sha1,
        # WARNING: Did not find branch or tag ..., assuming revision or ref.
        allow_stderr_warning=True,
    )
    assert "0.1" == version


def test_git_with_branch_name_as_revision(script: PipTestEnvironment) -> None:
    """
    Git backend should be able to install from branch names
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    branch = "test_branch"
    script.run("git", "checkout", "-b", branch, cwd=version_pkg_path)
    _change_test_package_version(script, version_pkg_path)
    version = _install_version_pkg(script, version_pkg_path, rev=branch)
    assert "some different version" == version


def test_git_with_tag_name_as_revision(script: PipTestEnvironment) -> None:
    """
    Git backend should be able to install from tag names
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    script.run("git", "tag", "test_tag", cwd=version_pkg_path)
    _change_test_package_version(script, version_pkg_path)
    version = _install_version_pkg(script, version_pkg_path, rev="test_tag")
    assert "0.1" == version


def _add_ref(script: PipTestEnvironment, path: Path, ref: str) -> None:
    """
    Add a new ref to a repository at the given path.
    """
    script.run("git", "update-ref", ref, "HEAD", cwd=path)


def test_git_install_ref(script: PipTestEnvironment) -> None:
    """
    The Git backend should be able to install a ref with the first install.
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    _add_ref(script, version_pkg_path, "refs/foo/bar")
    _change_test_package_version(script, version_pkg_path)

    version = _install_version_pkg(
        script,
        version_pkg_path,
        rev="refs/foo/bar",
        # WARNING: Did not find branch or tag ..., assuming revision or ref.
        allow_stderr_warning=True,
    )
    assert "0.1" == version


def test_git_install_then_install_ref(script: PipTestEnvironment) -> None:
    """
    The Git backend should be able to install a ref after a package has
    already been installed.
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    _add_ref(script, version_pkg_path, "refs/foo/bar")
    _change_test_package_version(script, version_pkg_path)

    version = _install_version_pkg(script, version_pkg_path)
    assert "some different version" == version

    # Now install the ref.
    version = _install_version_pkg(
        script,
        version_pkg_path,
        rev="refs/foo/bar",
        # WARNING: Did not find branch or tag ..., assuming revision or ref.
        allow_stderr_warning=True,
    )
    assert "0.1" == version


@pytest.mark.network
@pytest.mark.parametrize(
    "rev, expected_sha",
    [
        # Clone the default branch
        ("", "5547fa909e83df8bd743d3978d6667497983a4b7"),
        # Clone a specific tag
        ("@0.1.1", "7d654e66c8fa7149c165ddeffa5b56bc06619458"),
        # Clone a specific commit
        (
            "@65cf0a5bdd906ecf48a0ac241c17d656d2071d56",
            "65cf0a5bdd906ecf48a0ac241c17d656d2071d56",
        ),
    ],
)
def test_install_git_logs_commit_sha(
    script: PipTestEnvironment, rev: str, expected_sha: str, tmpdir: Path
) -> None:
    """
    Test installing from a git repository logs a commit SHA.
    """
    url_path = "pypa/pip-test-package.git"
    base_local_url = _github_checkout(url_path, tmpdir)
    local_url = f"{base_local_url}{rev}#egg=pip-test-package"
    result = script.pip("install", local_url)
    # `[4:]` removes a 'git+' prefix
    assert f"Resolved {base_local_url[4:]} to commit {expected_sha}" in result.stdout


@pytest.mark.network
def test_git_with_tag_name_and_update(script: PipTestEnvironment, tmpdir: Path) -> None:
    """
    Test cloning a git repository and updating to a different version.
    """
    url_path = "pypa/pip-test-package.git"
    base_local_url = _github_checkout(url_path, tmpdir)

    local_url = f"{base_local_url}#egg=pip-test-package"
    result = script.pip("install", "-e", local_url)
    result.assert_installed("pip-test-package", with_files=[".git"])

    new_local_url = f"{base_local_url}@0.1.2#egg=pip-test-package"
    result = script.pip(
        "install",
        "--global-option=--version",
        "-e",
        new_local_url,
        allow_stderr_warning=True,
    )
    assert "0.1.2" in result.stdout


@pytest.mark.network
def test_git_branch_should_not_be_changed(
    script: PipTestEnvironment, tmpdir: Path
) -> None:
    """
    Editable installations should not change branch
    related to issue #32 and #161
    """
    url_path = "pypa/pip-test-package.git"
    local_url = _github_checkout(url_path, tmpdir, egg="pip-test-package")
    script.pip("install", "-e", local_url)
    branch = _get_editable_branch(script, "pip-test-package")
    assert "master" == branch


@pytest.mark.network
def test_git_with_non_editable_unpacking(
    script: PipTestEnvironment, tmpdir: Path
) -> None:
    """
    Test cloning a git repository from a non-editable URL with a given tag.
    """
    url_path = "pypa/pip-test-package.git"
    local_url = _github_checkout(
        url_path,
        tmpdir,
        rev="0.1.2",
        egg="pip-test-package",
    )
    result = script.pip(
        "install",
        "--global-option=--version",
        local_url,
        allow_stderr_warning=True,
    )
    assert "0.1.2" in result.stdout


@pytest.mark.network
def test_git_with_editable_where_egg_contains_dev_string(
    script: PipTestEnvironment, tmpdir: Path
) -> None:
    """
    Test cloning a git repository from an editable url which contains "dev"
    string
    """
    url_path = "dcramer/django-devserver.git"
    local_url = _github_checkout(
        url_path,
        tmpdir,
        egg="django-devserver",
        scheme="https",
    )
    result = script.pip("install", "-e", local_url)
    result.assert_installed("django-devserver", with_files=[".git"])


@pytest.mark.network
def test_git_with_non_editable_where_egg_contains_dev_string(
    script: PipTestEnvironment, tmpdir: Path
) -> None:
    """
    Test cloning a git repository from a non-editable url which contains "dev"
    string
    """
    url_path = "dcramer/django-devserver.git"
    local_url = _github_checkout(
        url_path,
        tmpdir,
        egg="django-devserver",
        scheme="https",
    )
    result = script.pip("install", local_url)
    devserver_folder = script.site_packages / "devserver"
    result.did_create(devserver_folder)


def test_git_with_ambiguous_revs(script: PipTestEnvironment) -> None:
    """
    Test git with two "names" (tag/branch) pointing to the same commit
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    version_pkg_url = _make_version_pkg_url(version_pkg_path, rev="0.1")
    script.run("git", "tag", "0.1", cwd=version_pkg_path)
    result = script.pip("install", "-e", version_pkg_url)
    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"])


def test_editable__no_revision(script: PipTestEnvironment) -> None:
    """
    Test a basic install in editable mode specifying no revision.
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    _install_version_pkg_only(script, version_pkg_path)

    branch = _get_editable_branch(script, "version-pkg")
    assert branch == "master"

    remote = _get_branch_remote(script, "version-pkg", "master")
    assert remote == "origin"


def test_editable__branch_with_sha_same_as_default(script: PipTestEnvironment) -> None:
    """
    Test installing in editable mode a branch whose sha matches the sha
    of the default branch, but is different from the default branch.
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    # Create a second branch with the same SHA.
    script.run("git", "branch", "develop", cwd=version_pkg_path)
    _install_version_pkg_only(script, version_pkg_path, rev="develop")

    branch = _get_editable_branch(script, "version-pkg")
    assert branch == "develop"

    remote = _get_branch_remote(script, "version-pkg", "develop")
    assert remote == "origin"


def test_editable__branch_with_sha_different_from_default(
    script: PipTestEnvironment,
) -> None:
    """
    Test installing in editable mode a branch whose sha is different from
    the sha of the default branch.
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    # Create a second branch.
    script.run("git", "branch", "develop", cwd=version_pkg_path)
    # Add another commit to the master branch to give it a different sha.
    _change_test_package_version(script, version_pkg_path)

    version = _install_version_pkg(script, version_pkg_path, rev="develop")
    assert version == "0.1"

    branch = _get_editable_branch(script, "version-pkg")
    assert branch == "develop"

    remote = _get_branch_remote(script, "version-pkg", "develop")
    assert remote == "origin"


def test_editable__non_master_default_branch(script: PipTestEnvironment) -> None:
    """
    Test the branch you get after an editable install from a remote repo
    with a non-master default branch.
    """
    version_pkg_path = _create_test_package(script.scratch_path)
    # Change the default branch of the remote repo to a name that is
    # alphabetically after "master".
    script.run("git", "checkout", "-b", "release", cwd=version_pkg_path)
    _install_version_pkg_only(script, version_pkg_path)

    branch = _get_editable_branch(script, "version-pkg")
    assert branch == "release"


def test_reinstalling_works_with_editable_non_master_branch(
    script: PipTestEnvironment,
) -> None:
    """
    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.scratch_path)

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

    version = _install_version_pkg(script, version_pkg_path)
    assert "0.1" == version

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


# TODO(pnasrat) fix all helpers to do right things with paths on windows.
@pytest.mark.skipif("sys.platform == 'win32'")
@pytest.mark.xfail(
    condition=True,
    reason="Git submodule against file: is not working; waiting for a good solution",
    run=True,
)
def test_check_submodule_addition(script: PipTestEnvironment) -> None:
    """
    Submodules are pulled in on install and updated on upgrade.
    """
    module_path, submodule_path = _create_test_package_with_submodule(
        script, rel_path="testpkg/static"
    )

    install_result = script.pip(
        "install", "-e", f"git+{module_path.as_uri()}#egg=version_pkg"
    )
    install_result.did_create(script.venv / "src/version-pkg/testpkg/static/testfile")

    _change_test_package_submodule(script, submodule_path)
    _pull_in_submodule_changes_to_module(
        script,
        module_path,
        rel_path="testpkg/static",
    )

    # expect error because git may write to stderr
    update_result = script.pip(
        "install",
        "-e",
        f"git+{module_path.as_uri()}#egg=version_pkg",
        "--upgrade",
    )

    update_result.did_create(script.venv / "src/version-pkg/testpkg/static/testfile2")


def test_install_git_branch_not_cached(script: PipTestEnvironment) -> None:
    """
    Installing git urls with a branch revision does not cause wheel caching.
    """
    PKG = "gitbranchnotcached"
    repo_dir = _create_test_package(script.scratch_path, name=PKG)
    url = _make_version_pkg_url(repo_dir, rev="master", name=PKG)
    result = script.pip("install", url, "--only-binary=:all:")
    assert f"Successfully built {PKG}" in result.stdout, result.stdout
    script.pip("uninstall", "-y", PKG)
    # build occurs on the second install too because it is not cached
    result = script.pip("install", url)
    assert f"Successfully built {PKG}" in result.stdout, result.stdout


def test_install_git_sha_cached(script: PipTestEnvironment) -> None:
    """
    Installing git urls with a sha revision does cause wheel caching.
    """
    PKG = "gitshacached"
    repo_dir = _create_test_package(script.scratch_path, name=PKG)
    commit = script.run("git", "rev-parse", "HEAD", cwd=repo_dir).stdout.strip()
    url = _make_version_pkg_url(repo_dir, rev=commit, name=PKG)
    result = script.pip("install", url)
    assert f"Successfully built {PKG}" in result.stdout, result.stdout
    script.pip("uninstall", "-y", PKG)
    # build does not occur on the second install because it is cached
    result = script.pip("install", url)
    assert f"Successfully built {PKG}" not in result.stdout, result.stdout