Skip to content

Commit

Permalink
Use meaningful names for fake build deps
Browse files Browse the repository at this point in the history
  • Loading branch information
apljungquist committed Aug 2, 2023
1 parent 51af3bb commit 4e854f1
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 51 deletions.
1 change: 1 addition & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def _build_requirements(
result = collections.defaultdict(set)
for req in builder.build_system_requires:
result[req].add(f"{package_name} ({src_file}::build-system.requires)")
# TODO: Follow PEP and install deps
for dist in distributions:
for req in builder.get_requires_for_build(dist):
result[req].add(
Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,23 @@ def fake_dists(tmpdir, make_package, make_wheel):
return dists_path


@pytest.fixture
def fake_dists_with_build_deps(fake_dists, make_package, make_wheel):
"""
Generate distribution packages with names that make sense for testing build deps
"""
pkgs = [
make_package("fake_static_build_dep", version="0.1"),
make_package("fake_dynamic_build_dep_for_all", version="0.2"),
make_package("fake_dynamic_build_dep_for_sdist", version="0.3"),
make_package("fake_dynamic_build_dep_for_wheel", version="0.4"),
make_package("fake_dynamic_build_dep_for_editable", version="0.5"),
]
for pkg in pkgs:
make_wheel(pkg, fake_dists)
return fake_dists


@pytest.fixture
def venv(tmp_path):
"""Create a temporary venv and get the path of its directory of executables."""
Expand Down
102 changes: 59 additions & 43 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2813,29 +2813,32 @@ def copytree_dirs_exist_ok(
[],
{
"setuptools": Dependency("*"),
"small-fake-c": Dependency("0.3"),
"small-fake-d": Dependency("0.4"),
"small-fake-f": Dependency("0.6"),
"fake-static-build-dep": Dependency("0.1"),
"fake-dynamic-build-dep-for-all": Dependency("0.2"),
"fake-dynamic-build-dep-for-editable": Dependency("0.5"),
"small-fake-a": Dependency("0.1"),
},
),
(
["sdist"],
[],
{
"setuptools": Dependency("*"),
"fake-static-build-dep": Dependency("0.1"),
"fake-dynamic-build-dep-for-all": Dependency("0.2"),
"fake-dynamic-build-dep-for-sdist": Dependency("0.3"),
"small-fake-a": Dependency("0.1"),
"small-fake-d": Dependency("0.4"),
"small-fake-f": Dependency("0.6"),
},
),
(
["wheel"],
[],
{
"setuptools": Dependency("*"),
"small-fake-b": Dependency("0.2"),
"small-fake-d": Dependency("0.4"),
"small-fake-f": Dependency("0.6"),
"fake-static-build-dep": Dependency("0.1"),
"fake-dynamic-build-dep-for-all": Dependency("0.2"),
"fake-dynamic-build-dep-for-wheel": Dependency("0.4"),
"small-fake-a": Dependency("0.1"),
"wheel": Dependency("*"),
},
),
Expand All @@ -2844,11 +2847,12 @@ def copytree_dirs_exist_ok(
[],
{
"setuptools": Dependency("*"),
"fake-static-build-dep": Dependency("0.1"),
"fake-dynamic-build-dep-for-all": Dependency("0.2"),
"fake-dynamic-build-dep-for-sdist": Dependency("0.3"),
"fake-dynamic-build-dep-for-wheel": Dependency("0.4"),
"fake-dynamic-build-dep-for-editable": Dependency("0.5"),
"small-fake-a": Dependency("0.1"),
"small-fake-b": Dependency("0.2"),
"small-fake-c": Dependency("0.3"),
"small-fake-d": Dependency("0.4"),
"small-fake-f": Dependency("0.6"),
"wheel": Dependency("*"),
},
),
Expand All @@ -2866,48 +2870,54 @@ def copytree_dirs_exist_ok(
"small-fake-with-build-deps (pyproject.toml::build-system.requires)",
],
),
"small-fake-a": Dependency(
"fake-static-build-dep": Dependency(
"0.1",
[
"small-fake-with-build-deps (pyproject.toml::build-system.backend::sdist)",
"small-fake-with-build-deps (pyproject.toml::build-system.requires)",
],
),
"small-fake-b": Dependency(
"fake-dynamic-build-dep-for-all": Dependency(
"0.2",
[
"small-fake-with-build-deps (pyproject.toml::build-system.backend::wheel)",
],
),
"small-fake-c": Dependency(
"0.3",
[
(
"small-fake-with-build-deps"
" (pyproject.toml::build-system.backend::editable)"
),
"small-fake-with-build-deps (pyproject.toml::build-system.backend::sdist)",
"small-fake-with-build-deps (pyproject.toml::build-system.backend::wheel)",
],
),
"small-fake-d": Dependency(
"0.4",
"fake-dynamic-build-dep-for-sdist": Dependency(
"0.3",
[
"small-fake-with-build-deps (setup.py)",
"small-fake-with-build-deps (pyproject.toml::build-system.backend::sdist)",
],
),
"small-fake-e": Dependency(
"0.5",
"fake-dynamic-build-dep-for-wheel": Dependency(
"0.4",
[
"small-fake-with-build-deps (setup.py)",
"small-fake-with-build-deps (pyproject.toml::build-system.backend::wheel)",
],
),
"small-fake-f": Dependency(
"0.6",
"fake-dynamic-build-dep-for-editable": Dependency(
"0.5",
[
(
"small-fake-with-build-deps"
" (pyproject.toml::build-system.backend::editable)"
),
"small-fake-with-build-deps (pyproject.toml::build-system.backend::sdist)",
"small-fake-with-build-deps (pyproject.toml::build-system.backend::wheel)",
],
),
"small-fake-a": Dependency(
"0.1",
[
"small-fake-with-build-deps (setup.py)",
],
),
"small-fake-b": Dependency(
"0.2",
[
"small-fake-with-build-deps (setup.py)",
],
),
"wheel": Dependency(
Expand All @@ -2921,7 +2931,7 @@ def copytree_dirs_exist_ok(
),
)
def test_build_distribution(
fake_dists,
fake_dists_with_build_deps,
runner,
tmp_path,
monkeypatch,
Expand All @@ -2936,7 +2946,7 @@ def test_build_distribution(
included.
"""
# When used as argument to the runner it is not passed to pip
monkeypatch.setenv("PIP_FIND_LINKS", fake_dists)
monkeypatch.setenv("PIP_FIND_LINKS", fake_dists_with_build_deps)
src_pkg_path = os.path.join(PACKAGES_PATH, "small_fake_with_build_deps")
base_cmd = ["-n", "--allow-unsafe"]

Expand All @@ -2952,14 +2962,14 @@ def test_build_distribution(

@pytest.mark.network
def test_all_build_distributions(
fake_dists, runner, tmp_path, monkeypatch, make_package, make_wheel
fake_dists_with_build_deps, runner, tmp_path, monkeypatch, make_package, make_wheel
):
"""
Test that ``--all-build-deps`` is equivalent to specifying every
``--build-deps-for``.
"""
# When used as argument to the runner it is not passed to pip
monkeypatch.setenv("PIP_FIND_LINKS", fake_dists)
monkeypatch.setenv("PIP_FIND_LINKS", fake_dists_with_build_deps)
src_pkg_path = os.path.join(PACKAGES_PATH, "small_fake_with_build_deps")
base_cmd = ["-n", "--allow-unsafe"]

Expand Down Expand Up @@ -2989,23 +2999,29 @@ def test_all_build_distributions(


@pytest.mark.network
def test_only_build_distributions(fake_dists, runner, tmp_path, monkeypatch):
def test_only_build_distributions(
fake_dists_with_build_deps, runner, tmp_path, monkeypatch
):
"""
Test that ``--build-deps-only`` excludes dependencies other than build dependencies.
"""
expected_deps = {
"setuptools": Dependency("*"),
"small-fake-a": Dependency("0.1"),
"small-fake-b": Dependency("0.2"),
"small-fake-c": Dependency("0.3"),
"fake-static-build-dep": Dependency("0.1"),
"fake-dynamic-build-dep-for-all": Dependency("0.2"),
"fake-dynamic-build-dep-for-sdist": Dependency("0.3"),
"fake-dynamic-build-dep-for-wheel": Dependency("0.4"),
"fake-dynamic-build-dep-for-editable": Dependency("0.5"),
"small-fake-a": None,
"small-fake-b": None,
"small-fake-c": None,
"small-fake-d": None,
"small-fake-e": None,
"small-fake-f": Dependency("0.6"),
"wheel": Dependency("*"),
}

# When used as argument to the runner it is not passed to pip
monkeypatch.setenv("PIP_FIND_LINKS", fake_dists)
monkeypatch.setenv("PIP_FIND_LINKS", fake_dists_with_build_deps)
src_pkg_path = os.path.join(PACKAGES_PATH, "small_fake_with_build_deps")
base_cmd = ["-n", "--allow-unsafe"]

Expand All @@ -3024,7 +3040,7 @@ def test_only_build_distributions(fake_dists, runner, tmp_path, monkeypatch):
# This should not depend on the metadata format so testing all cases is wasteful
@pytest.mark.parametrize(("fname", "content"), METADATA_TEST_CASES[:1])
def test_all_build_distributions_fail_with_build_distribution(
fake_dists, runner, make_module, fname, content
fake_dists_with_build_deps, runner, make_module, fname, content
):
"""
Test that passing ``--all-build-deps`` and ``--build-deps-for`` fails.
Expand All @@ -3038,7 +3054,7 @@ def test_all_build_distributions_fail_with_build_distribution(
"--build-deps-for",
"sdist",
"--find-links",
fake_dists,
fake_dists_with_build_deps,
"--no-annotate",
"--no-emit-options",
"--no-header",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@
# Re-export all names in case more hooks are added in the future
from setuptools.build_meta import * # noqa: F401, F403

# FIXME: install static build deps somehow
# Here the backend imports another dep but the same problem applies if the backend
# itself is not installed in the environment where pip-compile runs.
# import fake_static_build_dep

build_wheel = setuptools.build_meta.build_wheel
build_sdist = setuptools.build_meta.build_sdist


def get_requires_for_build_sdist(config_settings=None):
result = setuptools.build_meta.get_requires_for_build_sdist(config_settings)
assert result == []
result.append("small-fake-a")
result.append("small-fake-f")
result.append("fake_dynamic_build_dep_for_all")
result.append("fake_dynamic_build_dep_for_sdist")
return result


def get_requires_for_build_wheel(config_settings=None):
result = setuptools.build_meta.get_requires_for_build_wheel(config_settings)
assert result == ["wheel"]
result.append("small-fake-b")
result.append("small-fake-f")
result.append("fake_dynamic_build_dep_for_all")
result.append("fake_dynamic_build_dep_for_wheel")
return result


def get_requires_for_build_editable(config_settings=None):
return ["small-fake-c", "small-fake-f"]
return ["fake_dynamic_build_dep_for_all", "fake_dynamic_build_dep_for_editable"]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[build-system]
requires = ["setuptools"]
requires = ["setuptools", "fake-static-build-dep"]
build-backend = "backend"
backend-path = ["backend"]
4 changes: 2 additions & 2 deletions tests/test_data/packages/small_fake_with_build_deps/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
name="small_fake_with_build_deps",
version=0.1,
install_requires=[
"small_fake_d",
"small_fake_a",
],
extras_require={
"e": ["small_fake_e"],
"b": ["small_fake_b"],
},
)

0 comments on commit 4e854f1

Please sign in to comment.