Skip to content

Commit 182f66d

Browse files
committed
Add test for scripts generated in build.py
1 parent 6146d56 commit 182f66d

File tree

6 files changed

+68
-1
lines changed

6 files changed

+68
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pathlib import Path
2+
from setuptools.command.build_py import build_py
3+
4+
5+
class BuildPyCommand(build_py):
6+
def run(self):
7+
with open('script_generated/generated/file.py', 'w') as out:
8+
print("#!/bin/env python3\nprint('Success!')\n", file=out)
9+
return super().run()
10+
11+
12+
def build(setup_kwargs):
13+
setup_kwargs.update(
14+
{
15+
"cmdclass": {
16+
"build_py": BuildPyCommand,
17+
},
18+
"scripts": ["script_generated/generated/file.py"]
19+
}
20+
21+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[tool.poetry]
2+
name = "script_generated"
3+
version = "0.1"
4+
description = ""
5+
authors = ["Evgenii Gorchakov <[email protected]>", "Rob Taylor <[email protected]>"]
6+
7+
[tool.poetry.dependencies]
8+
python = "^3.8"
9+
10+
[build-system]
11+
requires = ["poetry-core", "setuptools"]
12+
build-backend = "poetry.core.masonry.api"
13+
14+
[tool.poetry.build]
15+
generate-setup-file = true
16+
script = "build.py"

tests/masonry/builders/fixtures/script_generated/script_generated/__init__.py

Whitespace-only changes.

tests/masonry/builders/fixtures/script_generated/script_generated/generated/__init__.py

Whitespace-only changes.

tests/masonry/test_api.py

+22
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@ def test_build_wheel_extended() -> None:
9393
assert whl.exists()
9494
validate_wheel_contents(name="extended", version="0.1", path=whl.as_posix())
9595

96+
97+
@pytest.mark.skipif(
98+
sys.platform == "win32"
99+
and sys.version_info <= (3, 6)
100+
or platform.python_implementation().lower() == "pypy",
101+
reason="Disable test on Windows for Python <=3.6 and for PyPy",
102+
)
103+
def test_build_wheel_script_generated() -> None:
104+
with temporary_directory() as tmp_dir, cwd(
105+
os.path.join(fixtures, "script_generated")
106+
):
107+
filename = api.build_wheel(tmp_dir)
108+
whl = Path(tmp_dir) / filename
109+
assert whl.exists()
110+
validate_wheel_contents(
111+
name="script_generated",
112+
version="0.1",
113+
path=whl.as_posix(),
114+
data_files=["scripts/file.py"],
115+
)
116+
117+
96118
def test_build_sdist() -> None:
97119
with temporary_directory() as tmp_dir, cwd(os.path.join(fixtures, "complete")):
98120
filename = api.build_sdist(tmp_dir)

tests/testutils.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,24 @@ def subprocess_run(*args: str, **kwargs: Any) -> subprocess.CompletedProcess[str
6060

6161

6262
def validate_wheel_contents(
63-
name: str, version: str, path: str, files: list[str] | None = None
63+
name: str,
64+
version: str,
65+
path: str,
66+
files: list[str] | None = None,
67+
data_files: list[str] | None = None,
6468
) -> None:
6569
dist_info = f"{name}-{version}.dist-info"
70+
dist_data = f"{name}-{version}.data"
6671
files = files or []
72+
data_files = data_files or []
6773

6874
with zipfile.ZipFile(path) as z:
6975
namelist = z.namelist()
7076
# we use concatenation here for PY2 compat
7177
for filename in ["WHEEL", "METADATA", "RECORD"] + files:
7278
assert f"{dist_info}/{filename}" in namelist
79+
for filename in data_files:
80+
assert f"{dist_data}/{filename}" in namelist
7381

7482

7583
def validate_sdist_contents(

0 commit comments

Comments
 (0)