Skip to content

Commit cf72387

Browse files
committed
Add a functional test
1 parent 0f9250c commit cf72387

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

tests/functional/test_install.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import hashlib
2+
import io
23
import os
34
import re
45
import ssl
56
import sys
67
import sysconfig
8+
import tarfile
79
import textwrap
810
from os.path import curdir, join, pardir
911
from pathlib import Path
@@ -2590,3 +2592,123 @@ def test_install_pip_prints_req_chain_pypi(script: PipTestEnvironment) -> None:
25902592
f"Collecting python-openid "
25912593
f"(from Paste[openid]==1.7.5.1->-r {req_path} (line 1))" in result.stdout
25922594
)
2595+
2596+
2597+
@pytest.mark.parametrize("common_prefix", ("", "linktest-1.0/"))
2598+
def test_install_sdist_links(script: PipTestEnvironment, common_prefix: str) -> None:
2599+
"""
2600+
Test installing an sdist with hard and symbolic links.
2601+
"""
2602+
2603+
# Build an unpack an sdist that contains data files:
2604+
# - root.dat
2605+
# - sub/inner.dat
2606+
# and links (symbolic and hard) to both of those, both in the top-level
2607+
# and 'sub/' directories. That's 8 links total.
2608+
2609+
# We build the sdist from in-memory data, since the filesystem
2610+
# might not support both kinds of links.
2611+
2612+
sdist_path = script.scratch_path.joinpath("linktest-1.0.tar.gz")
2613+
2614+
def add_file(tar: tarfile.TarFile, name: str, content: str) -> None:
2615+
info = tarfile.TarInfo(common_prefix + name)
2616+
content_bytes = content.encode("utf-8")
2617+
info.size = len(content_bytes)
2618+
tar.addfile(info, io.BytesIO(content_bytes))
2619+
2620+
def add_link(tar: tarfile.TarFile, name: str, linktype: str, target: str) -> None:
2621+
info = tarfile.TarInfo(common_prefix + name)
2622+
info.type = {"sym": tarfile.SYMTYPE, "hard": tarfile.LNKTYPE}[linktype]
2623+
info.linkname = target
2624+
tar.addfile(info)
2625+
2626+
with tarfile.open(sdist_path, "w:gz") as sdist_tar:
2627+
add_file(
2628+
sdist_tar,
2629+
"PKG-INFO",
2630+
textwrap.dedent(
2631+
"""
2632+
Metadata-Version: 2.1
2633+
Name: linktest
2634+
Version: 1.0
2635+
"""
2636+
),
2637+
)
2638+
2639+
add_file(sdist_tar, "src/linktest/__init__.py", "")
2640+
add_file(sdist_tar, "src/linktest/root.dat", "Data")
2641+
add_file(sdist_tar, "src/linktest/sub/__init__.py", "")
2642+
add_file(sdist_tar, "src/linktest/sub/inner.dat", "Data")
2643+
linknames = []
2644+
pkg_root = f"{common_prefix}src/linktest"
2645+
for prefix, target_tag, linktype, target in [
2646+
("", "root", "sym", "root.dat"),
2647+
("", "root", "hard", f"{pkg_root}/root.dat"),
2648+
("", "inner", "sym", "sub/inner.dat"),
2649+
("", "inner", "hard", f"{pkg_root}/sub/inner.dat"),
2650+
("sub/", "root", "sym", "../root.dat"),
2651+
("sub/", "root", "hard", f"{pkg_root}/root.dat"),
2652+
("sub/", "inner", "sym", "inner.dat"),
2653+
("sub/", "inner", "hard", f"{pkg_root}/sub/inner.dat"),
2654+
]:
2655+
name = f"{prefix}link.{target_tag}.{linktype}.dat"
2656+
add_link(sdist_tar, "src/linktest/" + name, linktype, target)
2657+
linknames.append(name)
2658+
2659+
add_file(
2660+
sdist_tar,
2661+
"pyproject.toml",
2662+
textwrap.dedent(
2663+
"""
2664+
[build-system]
2665+
requires = ["setuptools"]
2666+
build-backend = "setuptools.build_meta"
2667+
[project]
2668+
name = "linktest"
2669+
version = "1.0"
2670+
[tool.setuptools]
2671+
include-package-data = true
2672+
[tool.setuptools.packages.find]
2673+
where = ["src"]
2674+
[tool.setuptools.package-data]
2675+
"*" = ["*.dat"]
2676+
"""
2677+
),
2678+
)
2679+
2680+
add_file(
2681+
sdist_tar,
2682+
"src/linktest/__main__.py",
2683+
textwrap.dedent(
2684+
f"""
2685+
from pathlib import Path
2686+
linknames = {linknames!r}
2687+
2688+
# we could use importlib.resources here once
2689+
# it has stable convenient API across supported versions
2690+
res_path = Path(__file__).parent
2691+
2692+
for name in linknames:
2693+
data_text = res_path.joinpath(name).read_text()
2694+
assert data_text == "Data"
2695+
print(str(len(linknames)) + ' files checked')
2696+
"""
2697+
),
2698+
)
2699+
2700+
# Show sdist content, for debugging the test
2701+
result = script.run("python", "-m", "tarfile", "-vl", str(sdist_path))
2702+
print(result)
2703+
2704+
# Install the package
2705+
result = script.pip("install", str(sdist_path))
2706+
print(result)
2707+
2708+
# Show installed content, for debugging the test
2709+
result = script.pip("show", "-f", "linktest")
2710+
print(result)
2711+
2712+
# Run the internal test
2713+
result = script.run("python", "-m", "linktest")
2714+
assert result.stdout.strip() == "8 files checked"

0 commit comments

Comments
 (0)