Skip to content

Commit

Permalink
Add a functional test
Browse files Browse the repository at this point in the history
  • Loading branch information
encukou committed Jun 28, 2024
1 parent d4b4a1a commit b6bcf9e
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import hashlib
import io
import os
import re
import ssl
import sys
import sysconfig
import tarfile
import textwrap
from os.path import curdir, join, pardir
from pathlib import Path
Expand Down Expand Up @@ -2590,3 +2592,123 @@ def test_install_pip_prints_req_chain_pypi(script: PipTestEnvironment) -> None:
f"Collecting python-openid "
f"(from Paste[openid]==1.7.5.1->-r {req_path} (line 1))" in result.stdout
)


@pytest.mark.parametrize("common_prefix", ("", "linktest-1.0/"))
def test_install_sdist_links(script: PipTestEnvironment, common_prefix: str) -> None:
"""
Test installing an sdist with hard and symbolic links.
"""

# Build an unpack an sdist that contains data files:
# - root.dat
# - sub/inner.dat
# and links (symbolic and hard) to both of those, both in the top-level
# and 'sub/' directories. That's 8 links total.

# We build the sdist from in-memory data, since the filesystem
# might not support both kinds of links.

sdist_path = script.scratch_path.joinpath("linktest-1.0.tar.gz")

def add_file(tar: tarfile.TarFile, name: str, content: str) -> None:
info = tarfile.TarInfo(common_prefix + name)
content_bytes = content.encode("utf-8")
info.size = len(content_bytes)
tar.addfile(info, io.BytesIO(content_bytes))

def add_link(tar: tarfile.TarFile, name: str, linktype: str, target: str) -> None:
info = tarfile.TarInfo(common_prefix + name)
info.type = {"sym": tarfile.SYMTYPE, "hard": tarfile.LNKTYPE}[linktype]
info.linkname = target
tar.addfile(info)

with tarfile.open(sdist_path, "w:gz") as sdist_tar:
add_file(
sdist_tar,
"PKG-INFO",
textwrap.dedent(
"""
Metadata-Version: 2.1
Name: linktest
Version: 1.0
"""
),
)

add_file(sdist_tar, "src/linktest/__init__.py", "")
add_file(sdist_tar, "src/linktest/root.dat", "Data")
add_file(sdist_tar, "src/linktest/sub/__init__.py", "")
add_file(sdist_tar, "src/linktest/sub/inner.dat", "Data")
linknames = []
pkg_root = f"{common_prefix}src/linktest"
for prefix, target_tag, linktype, target in [
("", "root", "sym", "root.dat"),
("", "root", "hard", f"{pkg_root}/root.dat"),
("", "inner", "sym", "sub/inner.dat"),
("", "inner", "hard", f"{pkg_root}/sub/inner.dat"),
("sub/", "root", "sym", "../root.dat"),
("sub/", "root", "hard", f"{pkg_root}/root.dat"),
("sub/", "inner", "sym", "inner.dat"),
("sub/", "inner", "hard", f"{pkg_root}/sub/inner.dat"),
]:
name = f"{prefix}link.{target_tag}.{linktype}.dat"
add_link(sdist_tar, "src/linktest/" + name, linktype, target)
linknames.append(name)

add_file(
sdist_tar,
"pyproject.toml",
textwrap.dedent(
"""
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "linktest"
version = "1.0"
[tool.setuptools]
include-package-data = true
[tool.setuptools.packages.find]
where = ["src"]
[tool.setuptools.package-data]
"*" = ["*.dat"]
"""
),
)

add_file(
sdist_tar,
"src/linktest/__main__.py",
textwrap.dedent(
f"""
from pathlib import Path
linknames = {linknames!r}
# we could use importlib.resources here once
# it has stable convenient API across supported versions
res_path = Path(__file__).parent
for name in linknames:
data_text = res_path.joinpath(name).read_text()
assert data_text == "Data"
print(str(len(linknames)) + ' files checked')
"""
),
)

# Show sdist content, for debugging the test
result = script.run("python", "-m", "tarfile", "-vl", str(sdist_path))
print(result)

# Install the package
result = script.pip("install", str(sdist_path))
print(result)

# Show installed content, for debugging the test
result = script.pip("show", "-f", "linktest")
print(result)

# Run the internal test
result = script.run("python", "-m", "linktest")
assert result.stdout.strip() == "8 files checked"

0 comments on commit b6bcf9e

Please sign in to comment.