Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify generation of test packages used in test_check #1208

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,41 @@
# limitations under the License.
"""Test functions useful across twine's tests."""

import io
import os
import pathlib
import tarfile
import textwrap
import zipfile

TESTS_DIR = pathlib.Path(__file__).parent
FIXTURES_DIR = os.path.join(TESTS_DIR, "fixtures")
SDIST_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.5.0.tar.gz")
WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.5.0-py2.py3-none-any.whl")
NEW_SDIST_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5.tar.gz")
NEW_WHEEL_FIXTURE = os.path.join(FIXTURES_DIR, "twine-1.6.5-py2.py3-none-any.whl")


def build_archive(path, name, archive_format, files):
filepath = path / f"{name}.{archive_format}"

if archive_format == "tar.gz":
with tarfile.open(filepath, "x:gz") as archive:
for mname, content in files.items():
if isinstance(content, tarfile.TarInfo):
content.name = mname
archive.addfile(content)
else:
data = textwrap.dedent(content).encode("utf8")
member = tarfile.TarInfo(mname)
member.size = len(data)
archive.addfile(member, io.BytesIO(data))
return filepath

if archive_format == "zip":
with zipfile.ZipFile(filepath, mode="w") as archive:
for mname, content in files.items():
archive.writestr(mname, textwrap.dedent(content))
return filepath

raise ValueError(format)
200 changes: 62 additions & 138 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import textwrap

import build
import pretend
import pytest

Expand Down Expand Up @@ -50,45 +48,30 @@ def test_fails_no_distributions(caplog):
]


def build_package(src_path, project_files, distribution="sdist"):
"""
Build a source distribution similar to `python3 -m build --sdist`.

Returns the absolute path of the built distribution.
"""
project_files = {
"pyproject.toml": (
"""
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
"""
),
**project_files,
}

for filename, content in project_files.items():
(src_path / filename).write_text(textwrap.dedent(content))

builder = build.ProjectBuilder(src_path)
return builder.build(distribution, str(src_path / "dist"))
def build_sdist_with_metadata(path, metadata):
name = "test"
version = "1.2.3"
sdist = helpers.build_archive(
path,
f"{name}-{version}",
"tar.gz",
{
f"{name}-{version}/README": "README",
f"{name}-{version}/PKG-INFO": metadata,
},
)
return str(sdist)


@pytest.mark.parametrize("distribution", ["sdist", "wheel"])
@pytest.mark.parametrize("strict", [False, True])
def test_warns_missing_description(distribution, strict, tmp_path, capsys, caplog):
sdist = build_package(
def test_warns_missing_description(strict, tmp_path, capsys, caplog):
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
"""
),
},
distribution=distribution,
"""\
Metadata-Version: 2.1
Name: test
Version: 1.2.3
""",
)

assert check.check([sdist], strict=strict) is strict
Expand All @@ -111,54 +94,19 @@ def test_warns_missing_description(distribution, strict, tmp_path, capsys, caplo
]


def test_warns_missing_file(tmp_path, capsys, caplog):
sdist = build_package(
def test_fails_rst_syntax_error(tmp_path, capsys, caplog):
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.rst
long_description_content_type = text/x-rst
"""
),
},
)
"""\
Metadata-Version: 2.1
Name: test-package
Version: 1.2.3
Description-Content-Type: text/x-rst

assert not check.check([sdist])

assert capsys.readouterr().out == f"Checking {sdist}: PASSED with warnings\n"
============

assert caplog.record_tuples == [
(
"twine.commands.check",
logging.WARNING,
"`long_description` missing.",
),
]


def test_fails_rst_syntax_error(tmp_path, capsys, caplog):
sdist = build_package(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.rst
long_description_content_type = text/x-rst
"""
),
"README.rst": (
"""
============
"""
),
},
""",
)

assert check.check([sdist])
Expand All @@ -177,25 +125,17 @@ def test_fails_rst_syntax_error(tmp_path, capsys, caplog):


def test_fails_rst_no_content(tmp_path, capsys, caplog):
sdist = build_package(
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.rst
long_description_content_type = text/x-rst
"""
),
"README.rst": (
"""
test-package
============
"""
),
},
"""\
Metadata-Version: 2.1
Name: test-package
Version: 1.2.3
Description-Content-Type: text/x-rst

test-package
============
""",
)

assert check.check([sdist])
Expand All @@ -214,27 +154,19 @@ def test_fails_rst_no_content(tmp_path, capsys, caplog):


def test_passes_rst_description(tmp_path, capsys, caplog):
sdist = build_package(
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.rst
long_description_content_type = text/x-rst
"""
),
"README.rst": (
"""
test-package
============

A test package.
"""
),
},
"""\
Metadata-Version: 2.1
Name: test-package
Version: 1.2.3
Description-Content-Type: text/x-rst

test-package
============

A test package.
""",
)

assert not check.check([sdist])
Expand All @@ -246,26 +178,18 @@ def test_passes_rst_description(tmp_path, capsys, caplog):

@pytest.mark.parametrize("content_type", ["text/markdown", "text/plain"])
def test_passes_markdown_description(content_type, tmp_path, capsys, caplog):
sdist = build_package(
sdist = build_sdist_with_metadata(
tmp_path,
{
"setup.cfg": (
f"""
[metadata]
name = test-package
version = 0.0.1
long_description = file:README.md
long_description_content_type = {content_type}
"""
),
"README.md": (
"""
# test-package

A test package.
"""
),
},
f"""\
Metadata-Version: 2.1
Name: test-package
Version: 1.2.3
Description-Content-Type: {content_type}

# test-package

A test package.
""",
)

assert not check.check([sdist])
Expand Down
Loading
Loading