diff --git a/tests/masonry/test_api.py b/tests/masonry/test_api.py index 56b7845c2..9f8cfc95c 100644 --- a/tests/masonry/test_api.py +++ b/tests/masonry/test_api.py @@ -4,8 +4,6 @@ import os import platform import sys -import tarfile -import zipfile from contextlib import contextmanager @@ -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 @@ -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( @@ -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(): diff --git a/tests/testutils.py b/tests/testutils.py index b18ae6a5f..161aed735 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -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 @@ -62,3 +65,24 @@ 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() + 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