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
|
import os
import pathlib
import tarfile
import pytest
from twine import exceptions
from twine import sdist
from .helpers import TESTS_DIR
from .helpers import build_archive
@pytest.fixture(
params=[
"fixtures/twine-1.5.0.tar.gz",
"fixtures/twine-1.6.5.tar.gz",
]
)
def example_sdist(request):
file_name = os.path.join(TESTS_DIR, request.param)
return sdist.SDist(file_name)
@pytest.fixture(params=["tar.gz", "zip"])
def archive_format(request):
return request.param
def test_read_example(example_sdist):
"""Parse metadata from a valid sdist file."""
metadata = example_sdist.read()
assert b"Metadata-Version: 1.1" in metadata
assert b"Name: twine" in metadata
assert b"Version: 1." in metadata
def test_read_non_existent():
"""Raise an exception when sdist file doesn't exist."""
file_name = str(pathlib.Path("/foo/bar/baz.tar.gz").resolve())
with pytest.raises(exceptions.InvalidDistribution, match="No such file"):
sdist.SDist(file_name).read()
def test_formar_not_supported():
"""Raise an exception when sdist is not a .tar.gz or a .zip."""
file_name = str(pathlib.Path("/foo/bar/baz.foo").resolve())
with pytest.raises(exceptions.InvalidDistribution, match="Unsupported sdist"):
sdist.SDist(file_name).read()
def test_read(archive_format, tmp_path):
"""Read PKG-INFO from a valid sdist."""
filepath = build_archive(
tmp_path,
"test-1.2.3",
archive_format,
{
"test-1.2.3/README": "README",
"test-1.2.3/PKG-INFO": """
Metadata-Version: 1.1
Name: test
Version: 1.2.3
""",
},
)
metadata = sdist.SDist(str(filepath)).read()
assert b"Metadata-Version: 1.1" in metadata
assert b"Name: test" in metadata
assert b"Version: 1.2.3" in metadata
def test_missing_pkg_info(archive_format, tmp_path):
"""Raise an exception when sdist does not contain PKG-INFO."""
filepath = build_archive(
tmp_path,
"test-1.2.3",
archive_format,
{
"test-1.2.3/README": "README",
},
)
with pytest.raises(exceptions.InvalidDistribution, match="No PKG-INFO in archive"):
sdist.SDist(str(filepath)).read()
def test_invalid_pkg_info(archive_format, tmp_path):
"""Raise an exception when PKG-INFO does not contain ``Metadata-Version``."""
filepath = build_archive(
tmp_path,
"test-1.2.3",
archive_format,
{
"test-1.2.3/README": "README",
"test-1.2.3/PKG-INFO": """
Name: test
Version: 1.2.3.
""",
},
)
with pytest.raises(exceptions.InvalidDistribution, match="No PKG-INFO in archive"):
sdist.SDist(str(filepath)).read()
def test_pkg_info_directory(archive_format, tmp_path):
"""Raise an exception when PKG-INFO is a directory."""
filepath = build_archive(
tmp_path,
"test-1.2.3",
archive_format,
{
"test-1.2.3/README": "README",
"test-1.2.3/PKG-INFO/content": """
Metadata-Version: 1.1
Name: test
Version: 1.2.3.
""",
},
)
with pytest.raises(exceptions.InvalidDistribution, match="No PKG-INFO in archive"):
sdist.SDist(str(filepath)).read()
def test_pkg_info_not_regular_file(tmp_path):
"""Raise an exception when PKG-INFO is a directory."""
link = tarfile.TarInfo()
link.type = tarfile.LNKTYPE
link.linkname = "README"
filepath = build_archive(
tmp_path,
"test-1.2.3",
"tar.gz",
{
"test-1.2.3/README": "README",
"test-1.2.3/PKG-INFO": link,
},
)
with pytest.raises(
exceptions.InvalidDistribution,
match=r"^PKG-INFO is not a reg.*test-1.2.3.tar.gz$",
):
sdist.SDist(str(filepath)).read()
def test_multiple_top_level(archive_format, tmp_path):
"""Raise an exception when there are too many top-level members."""
filepath = build_archive(
tmp_path,
"test-1.2.3",
archive_format,
{
"test-1.2.3/README": "README",
"test-1.2.3/PKG-INFO": """
Metadata-Version: 1.1
Name: test
Version: 1.2.3.
""",
"test-2.0.0/README": "README",
},
)
with pytest.raises(
exceptions.InvalidDistribution,
match=r"^Too many top-level.*test-1.2.3.(tar.gz|zip)$",
):
sdist.SDist(str(filepath)).read()
def test_py_version(example_sdist):
assert example_sdist.py_version == "source"
|