Skip to content

Commit

Permalink
Add test for scripts generated in build.py
Browse files Browse the repository at this point in the history
  • Loading branch information
robtaylor committed Nov 28, 2022
1 parent 6146d56 commit 182f66d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
21 changes: 21 additions & 0 deletions tests/masonry/builders/fixtures/script_generated/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path
from setuptools.command.build_py import build_py


class BuildPyCommand(build_py):
def run(self):
with open('script_generated/generated/file.py', 'w') as out:
print("#!/bin/env python3\nprint('Success!')\n", file=out)
return super().run()


def build(setup_kwargs):
setup_kwargs.update(
{
"cmdclass": {
"build_py": BuildPyCommand,
},
"scripts": ["script_generated/generated/file.py"]
}

)
16 changes: 16 additions & 0 deletions tests/masonry/builders/fixtures/script_generated/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "script_generated"
version = "0.1"
description = ""
authors = ["Evgenii Gorchakov <[email protected]>", "Rob Taylor <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.8"

[build-system]
requires = ["poetry-core", "setuptools"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.build]
generate-setup-file = true
script = "build.py"
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ def test_build_wheel_extended() -> None:
assert whl.exists()
validate_wheel_contents(name="extended", version="0.1", path=whl.as_posix())


@pytest.mark.skipif(
sys.platform == "win32"
and sys.version_info <= (3, 6)
or platform.python_implementation().lower() == "pypy",
reason="Disable test on Windows for Python <=3.6 and for PyPy",
)
def test_build_wheel_script_generated() -> None:
with temporary_directory() as tmp_dir, cwd(
os.path.join(fixtures, "script_generated")
):
filename = api.build_wheel(tmp_dir)
whl = Path(tmp_dir) / filename
assert whl.exists()
validate_wheel_contents(
name="script_generated",
version="0.1",
path=whl.as_posix(),
data_files=["scripts/file.py"],
)


def test_build_sdist() -> None:
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
filename = api.build_sdist(tmp_dir)
Expand Down
10 changes: 9 additions & 1 deletion tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,24 @@ def subprocess_run(*args: str, **kwargs: Any) -> subprocess.CompletedProcess[str


def validate_wheel_contents(
name: str, version: str, path: str, files: list[str] | None = None
name: str,
version: str,
path: str,
files: list[str] | None = None,
data_files: list[str] | None = None,
) -> None:
dist_info = f"{name}-{version}.dist-info"
dist_data = f"{name}-{version}.data"
files = files or []
data_files = data_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 f"{dist_info}/{filename}" in namelist
for filename in data_files:
assert f"{dist_data}/{filename}" in namelist


def validate_sdist_contents(
Expand Down

0 comments on commit 182f66d

Please sign in to comment.