Files

283 lines
7.8 KiB
Python
Raw Permalink Normal View History

2026-05-28 00:20:08 +02:00
from __future__ import annotations
import tomllib
2021-01-31 17:14:15 -05:00
from textwrap import dedent
2024-11-04 15:25:01 -05:00
import pytest
from cibuildwheel.projectfiles import (
get_requires_python_str,
resolve_dependency_groups,
setup_py_python_requires,
)
2021-01-31 17:14:15 -05:00
2026-05-28 00:20:08 +02:00
TYPE_CHECKING = False
if TYPE_CHECKING:
from pathlib import Path
2021-01-31 17:14:15 -05:00
2026-04-01 10:23:02 -04:00
def test_read_setup_py_simple(tmp_path: Path) -> None:
2021-01-31 17:14:15 -05:00
with open(tmp_path / "setup.py", "w") as f:
2021-04-30 17:56:34 -04:00
f.write(
dedent(
"""
2021-05-02 16:37:38 +02:00
from setuptools import setup
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
setup(
name = "hello",
other = 23,
example = ["item", "other"],
python_requires = "1.23",
)
"""
2021-04-30 17:56:34 -04:00
)
)
2021-01-31 17:14:15 -05:00
assert setup_py_python_requires(tmp_path.joinpath("setup.py").read_text()) == "1.23"
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, {}) == "1.23"
2021-01-31 17:14:15 -05:00
2026-04-01 10:23:02 -04:00
def test_read_setup_py_if_main(tmp_path: Path) -> None:
with open(tmp_path / "setup.py", "w") as f:
f.write(
dedent(
"""
from setuptools import setup
if __name__ == "__main__":
setup(
name = "hello",
other = 23,
example = ["item", "other"],
python_requires = "1.23",
)
"""
)
)
assert setup_py_python_requires(tmp_path.joinpath("setup.py").read_text()) == "1.23"
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, {}) == "1.23"
2026-04-01 10:23:02 -04:00
def test_read_setup_py_if_main_reversed(tmp_path: Path) -> None:
with open(tmp_path / "setup.py", "w") as f:
f.write(
dedent(
"""
from setuptools import setup
if "__main__" == __name__:
setup(
name = "hello",
other = 23,
example = ["item", "other"],
python_requires = "1.23",
)
"""
)
)
assert setup_py_python_requires(tmp_path.joinpath("setup.py").read_text()) == "1.23"
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, {}) == "1.23"
2026-04-01 10:23:02 -04:00
def test_read_setup_py_if_invalid(tmp_path: Path) -> None:
with open(tmp_path / "setup.py", "w") as f:
f.write(
dedent(
"""
from setuptools import setup
if True:
setup(
name = "hello",
other = 23,
example = ["item", "other"],
python_requires = "1.23",
)
"""
)
)
assert not setup_py_python_requires(tmp_path.joinpath("setup.py").read_text())
2024-11-04 15:25:01 -05:00
assert not get_requires_python_str(tmp_path, {})
2026-04-01 10:23:02 -04:00
def test_read_setup_py_full(tmp_path: Path) -> None:
with open(tmp_path / "setup.py", "w", encoding="utf8") as f:
2021-04-30 17:56:34 -04:00
f.write(
dedent(
"""
2021-05-02 16:37:38 +02:00
import setuptools
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
setuptools.randomfunc()
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
setuptools.setup(
name = "hello",
description = "≥“”ü",
2021-05-02 16:37:38 +02:00
other = 23,
example = ["item", "other"],
python_requires = "1.24",
)
"""
2021-04-30 17:56:34 -04:00
)
)
2021-01-31 17:14:15 -05:00
assert (
setup_py_python_requires(tmp_path.joinpath("setup.py").read_text(encoding="utf8")) == "1.24"
)
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, {}) == "1.24"
2021-01-31 17:14:15 -05:00
2026-04-01 10:23:02 -04:00
def test_read_setup_py_assign(tmp_path: Path) -> None:
2021-01-31 17:14:15 -05:00
with open(tmp_path / "setup.py", "w") as f:
2021-04-30 17:56:34 -04:00
f.write(
dedent(
"""
2021-05-02 16:37:38 +02:00
from setuptools import setup
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
REQUIRES = "3.21"
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
setuptools.setup(
name = "hello",
other = 23,
example = ["item", "other"],
python_requires = REQUIRES,
)
"""
2021-04-30 17:56:34 -04:00
)
)
2021-01-31 17:14:15 -05:00
assert setup_py_python_requires(tmp_path.joinpath("setup.py").read_text()) is None
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, {}) is None
2021-01-31 17:14:15 -05:00
2026-04-01 10:23:02 -04:00
def test_read_setup_py_None(tmp_path: Path) -> None:
2021-01-31 17:14:15 -05:00
with open(tmp_path / "setup.py", "w") as f:
2021-04-30 17:56:34 -04:00
f.write(
dedent(
"""
2021-05-02 16:37:38 +02:00
from setuptools import setup
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
REQUIRES = None
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
setuptools.setup(
name = "hello",
other = 23,
example = ["item", "other"],
python_requires = None,
)
"""
2021-04-30 17:56:34 -04:00
)
)
2021-01-31 17:14:15 -05:00
assert setup_py_python_requires(tmp_path.joinpath("setup.py").read_text()) is None
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, {}) is None
2021-01-31 17:14:15 -05:00
2026-04-01 10:23:02 -04:00
def test_read_setup_py_empty(tmp_path: Path) -> None:
2021-01-31 17:14:15 -05:00
with open(tmp_path / "setup.py", "w") as f:
2021-04-30 17:56:34 -04:00
f.write(
dedent(
"""
2021-05-02 16:37:38 +02:00
from setuptools import setup
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
REQUIRES = "3.21"
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
setuptools.setup(
name = "hello",
other = 23,
example = ["item", "other"],
)
"""
2021-04-30 17:56:34 -04:00
)
)
2021-01-31 17:14:15 -05:00
assert setup_py_python_requires(tmp_path.joinpath("setup.py").read_text()) is None
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, {}) is None
2021-01-31 17:14:15 -05:00
2026-04-01 10:23:02 -04:00
def test_read_setup_cfg(tmp_path: Path) -> None:
2021-01-31 17:14:15 -05:00
with open(tmp_path / "setup.cfg", "w") as f:
2021-04-30 17:56:34 -04:00
f.write(
dedent(
"""
2021-05-02 16:37:38 +02:00
[options]
python_requires = 1.234
[metadata]
something = other
"""
2021-04-30 17:56:34 -04:00
)
)
2021-01-31 17:14:15 -05:00
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, {}) == "1.234"
2021-01-31 17:14:15 -05:00
2026-04-01 10:23:02 -04:00
def test_read_setup_cfg_empty(tmp_path: Path) -> None:
2021-01-31 17:14:15 -05:00
with open(tmp_path / "setup.cfg", "w") as f:
2021-04-30 17:56:34 -04:00
f.write(
dedent(
"""
2021-05-02 16:37:38 +02:00
[options]
other = 1.234
[metadata]
something = other
"""
2021-04-30 17:56:34 -04:00
)
)
2021-01-31 17:14:15 -05:00
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, {}) is None
2021-01-31 17:14:15 -05:00
2026-04-01 10:23:02 -04:00
def test_read_pyproject_toml(tmp_path: Path) -> None:
2021-01-31 17:14:15 -05:00
with open(tmp_path / "pyproject.toml", "w") as f:
2021-04-30 17:56:34 -04:00
f.write(
dedent(
"""
2021-05-02 16:37:38 +02:00
[project]
requires-python = "1.654"
2021-01-31 17:14:15 -05:00
2021-05-02 16:37:38 +02:00
[tool.cibuildwheel]
something = "other"
"""
2021-04-30 17:56:34 -04:00
)
)
2024-11-04 15:25:01 -05:00
with open(tmp_path / "pyproject.toml", "rb") as f:
pyproject_toml = tomllib.load(f)
2021-01-31 17:14:15 -05:00
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, pyproject_toml) == "1.654"
2021-01-31 17:14:15 -05:00
2026-04-01 10:23:02 -04:00
def test_read_pyproject_toml_empty(tmp_path: Path) -> None:
2021-01-31 17:14:15 -05:00
with open(tmp_path / "pyproject.toml", "w") as f:
2021-04-30 17:56:34 -04:00
f.write(
dedent(
"""
2021-05-02 16:37:38 +02:00
[project]
other = 1.234
"""
2021-04-30 17:56:34 -04:00
)
)
2024-11-04 15:25:01 -05:00
with open(tmp_path / "pyproject.toml", "rb") as f:
pyproject_toml = tomllib.load(f)
2021-01-31 17:14:15 -05:00
2024-11-04 15:25:01 -05:00
assert get_requires_python_str(tmp_path, pyproject_toml) is None
2026-04-01 10:23:02 -04:00
def test_read_dep_groups() -> None:
2024-11-04 15:25:01 -05:00
pyproject_toml = {"dependency-groups": {"group1": ["pkg1", "pkg2"], "group2": ["pkg3"]}}
assert resolve_dependency_groups(pyproject_toml) == ()
assert resolve_dependency_groups(pyproject_toml, "group1") == ("pkg1", "pkg2")
assert resolve_dependency_groups(pyproject_toml, "group2") == ("pkg3",)
assert resolve_dependency_groups(pyproject_toml, "group1", "group2") == ("pkg1", "pkg2", "pkg3")
2026-04-01 10:23:02 -04:00
def test_dep_group_no_file_error() -> None:
with pytest.raises(FileNotFoundError, match=r"pyproject\.toml"):
2024-11-04 15:25:01 -05:00
resolve_dependency_groups(None, "test")
2026-04-01 10:23:02 -04:00
def test_dep_group_no_section_error() -> None:
with pytest.raises(KeyError, match=r"pyproject\.toml"):
2024-11-04 15:25:01 -05:00
resolve_dependency_groups({}, "test")