Skip to content

Commit

Permalink
tests: refactor wheel/sdist content validation
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed Jul 20, 2020
1 parent 0a2f2fa commit 547befd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
61 changes: 27 additions & 34 deletions tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import os
import platform
import sys
import tarfile
import zipfile

from contextlib import contextmanager

Expand All @@ -16,6 +14,8 @@
from poetry.core.utils._compat import Path
from poetry.core.utils._compat import decode
from poetry.core.utils.helpers import temporary_directory
from tests.testutils import validate_sdist_contents
from tests.testutils import validate_wheel_contents


@contextmanager
Expand Down Expand Up @@ -46,25 +46,23 @@ def test_get_requires_for_build_sdist():
def test_build_wheel():
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
filename = api.build_wheel(tmp_dir)

with zipfile.ZipFile(str(os.path.join(tmp_dir, filename))) as zip:
namelist = zip.namelist()

assert "my_package-1.2.3.dist-info/entry_points.txt" in namelist
assert "my_package-1.2.3.dist-info/WHEEL" in namelist
assert "my_package-1.2.3.dist-info/METADATA" in namelist
validate_wheel_contents(
name="my_package",
version="1.2.3",
path=str(os.path.join(tmp_dir, filename)),
files=["entry_points.txt"],
)


def test_build_wheel_with_include():
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "with-include")):
filename = api.build_wheel(tmp_dir)

with zipfile.ZipFile(str(os.path.join(tmp_dir, filename))) as zip:
namelist = zip.namelist()

assert "with_include-1.2.3.dist-info/entry_points.txt" in namelist
assert "with_include-1.2.3.dist-info/WHEEL" in namelist
assert "with_include-1.2.3.dist-info/METADATA" in namelist
validate_wheel_contents(
name="with_include",
version="1.2.3",
path=str(os.path.join(tmp_dir, filename)),
files=["entry_points.txt"],
)


@pytest.mark.skipif(
Expand All @@ -76,36 +74,31 @@ def test_build_wheel_with_include():
def test_build_wheel_extended():
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "extended")):
filename = api.build_wheel(tmp_dir)

whl = Path(tmp_dir) / filename
assert whl.exists()

with zipfile.ZipFile(str(os.path.join(tmp_dir, filename))) as zip:
namelist = zip.namelist()

assert "extended-0.1.dist-info/RECORD" in namelist
assert "extended-0.1.dist-info/WHEEL" in namelist
assert "extended-0.1.dist-info/METADATA" in namelist
validate_wheel_contents(name="extended", version="0.1", path=whl.as_posix())


def test_build_sdist():
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
filename = api.build_sdist(tmp_dir)

with tarfile.open(str(os.path.join(tmp_dir, filename))) as tar:
namelist = tar.getnames()

assert "my-package-1.2.3/LICENSE" in namelist
validate_sdist_contents(
name="my-package",
version="1.2.3",
path=str(os.path.join(tmp_dir, filename)),
files=["LICENSE"],
)


def test_build_sdist_with_include():
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "with-include")):
filename = api.build_sdist(tmp_dir)

with tarfile.open(str(os.path.join(tmp_dir, filename))) as tar:
namelist = tar.getnames()

assert "with-include-1.2.3/LICENSE" in namelist
validate_sdist_contents(
name="with-include",
version="1.2.3",
path=str(os.path.join(tmp_dir, filename)),
files=["LICENSE"],
)


def test_prepare_metadata_for_build_wheel():
Expand Down
25 changes: 25 additions & 0 deletions tests/testutils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import shutil
import subprocess
import tarfile
import zipfile

from contextlib import contextmanager
from typing import Any
from typing import ContextManager
from typing import Dict
from typing import List
from typing import Optional

from poetry.core.utils._compat import Path
Expand Down Expand Up @@ -62,3 +65,25 @@ def subprocess_run(*args, **kwargs): # type: (str, Any) -> subprocess.Completed
)
assert result.returncode == 0
return result


def validate_wheel_contents(
name, version, path, files=None
): # type: (str, str, str, Optional[List[str]]) -> None
dist_info = "{}-{}.dist-info".format(name, version)
files = files or []

with zipfile.ZipFile(path) as z:
namelist = z.namelist()
# we use concatenation here for PY2 compat
for filename in ["WHEEL", "METADATA", "RECORD"] + files:
assert "{}/{}".format(dist_info, filename) in namelist


def validate_sdist_contents(
name, version, path, files
): # type: (str, str, str, List[str]) -> None
with tarfile.open(path) as tar:
namelist = tar.getnames()
for filename in files:
assert "{}-{}/{}".format(name, version, filename) in namelist

0 comments on commit 547befd

Please sign in to comment.