Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Jul 9, 2024
1 parent 017e247 commit 35c9f5a
Show file tree
Hide file tree
Showing 9 changed files with 1,046 additions and 6 deletions.
6 changes: 3 additions & 3 deletions doc/src/plugins/backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ hatchling
Select groups to lock
~~~~~~~~~~~~~~~~~~~~~

By default, the default group and all optional groups will be locked, but you can specify the groups to lock by setting `lock_groups` in the configuration.
By default, the default group and all optional groups will be locked, but you can specify the groups to lock by setting `lock-groups` in the configuration.

.. code-block:: toml
:caption: pyproject.toml
# for pdm-backend
[tool.pdm.build]
locked = true
lock_groups = ["default", "optional1"]
lock-groups = ["default", "optional1"]
# for hatchling
[tool.hatch.metadata.hooks.build-locked]
lock_groups = ["default", "optional1"]
lock-groups = ["default", "optional1"]
2 changes: 1 addition & 1 deletion src/pdm_build_locked/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def pdm_build_hook_enabled(self, context: Context) -> bool:
def pdm_build_initialize(self, context: Context) -> None:
static_fields = list(context.config.metadata)
update_metadata_with_locked(
context.config.metadata, context.root, context.config.build_config.get("lock_groups")
context.config.metadata, context.root, context.config.build_config.get("lock-groups")
)
new_fields = set(context.config.metadata) - set(static_fields)
for field in new_fields:
Expand Down
2 changes: 1 addition & 1 deletion src/pdm_build_locked/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
if dev_dependencies := project.pyproject.settings.get("dev-dependencies"):
pdm_dev_dependencies = dev_dependencies.keys()

groups = project.pyproject.settings.get("build", {}).get("lock_groups", None)
groups = project.pyproject.settings.get("build", {}).get("lock-groups", None)
if groups is None:
groups = {group for group in project.all_dependencies if group not in pdm_dev_dependencies}
else:
Expand Down
2 changes: 1 addition & 1 deletion src/pdm_build_locked/hatchling.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BuildLockedMetadataHook(MetadataHookInterface):
PLUGIN_NAME = "build-locked"

def update(self, metadata: dict) -> None:
update_metadata_with_locked(metadata, Path(self.root), self.config.get("lock_groups"))
update_metadata_with_locked(metadata, Path(self.root), self.config.get("lock-groups"))


@hookimpl
Expand Down
942 changes: 942 additions & 0 deletions tests/data/large-selected/pdm.lock

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions tests/data/large-selected/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
########################################################################################################################
# Project Settings #
########################################################################################################################
[project]
name = "large"
dynamic = ["version"]
description = "test"
keywords = [
"python",
"module",
]
classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
dependencies = [
"cement==3.0.8",
"jinja2==3.1.2",
"dacite==1.8.1",
"matplotlib==3.7.2",
"platformdirs==3.8.1",
"packaging==23.1",
"requests==2.31.0",
"gitpython==3.1.32",
"matplotlib==3.7.1"
]
requires-python = ">=3.8"

[project.optional-dependencies]
cow = [
"pycowsay"
]
extras = [
"typing-extensions"
]

[build-system]
requires = ["pdm-backend>=2.0.7"]
build-backend = "pdm.backend"


########################################################################################################################
# Tools Settings #
########################################################################################################################

####################
# PDM #
####################
[tool.pdm.version]
source = "scm"

[tool.pdm.build]
package-dir = "src"
locked = true
lock-groups = ["default"]

[tool.pdm.dev-dependencies]
dev = [
# format
"black>=23.7.0",
"isort>=5.12.0",
# lint
"mypy>=1.4.1",
]
Empty file.
12 changes: 12 additions & 0 deletions tests/unit/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest
from pkginfo import Wheel

from tests.utils import count_group_dependencies


def build_wheel(src_dir: Path, wheel_dir: Path) -> Wheel:
from build.__main__ import build_package
Expand All @@ -26,6 +28,16 @@ def test_pdm_backend(temp_dir: Path, data_base_path: Path, test_project: str) ->
}


@pytest.mark.usefixtures("assert_pyproject_unmodified")
@pytest.mark.parametrize("test_project", ["large-selected"])
def test_pdm_backend_with_selected_groups(temp_dir: Path, data_base_path: Path, test_project: str) -> None:
project = data_base_path / test_project
wheel = build_wheel(project, temp_dir)
assert count_group_dependencies(wheel, "locked") == 26
assert count_group_dependencies(wheel, "extras-locked") == 0
assert count_group_dependencies(wheel, "cow-locked") == 0


@pytest.mark.usefixtures("assert_pyproject_unmodified")
@pytest.mark.parametrize("test_project", ["lock-disabled"])
def test_pdm_backend_disabled(temp_dir: Path, data_base_path: Path, test_project: str) -> None:
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_build_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ def test_build_locked_pyproject(pdm: PDMCallable, data_base_path: Path, temp_dir
assert count_group_dependencies(wheel, "cow-locked") == 1


@pytest.mark.usefixtures("assert_pyproject_unmodified")
@pytest.mark.parametrize("test_project", ["large-selected"])
def test_build_locked_pyproject_with_selected_groups(
pdm: PDMCallable, data_base_path: Path, temp_dir: Path, test_project: str
) -> None:
"""this project has a lockfile and the pyproject.toml setting tool.pdm.build.locked"""
project_path = data_base_path.joinpath(test_project).as_posix()
cmd = ["build", "--project", project_path, "--dest", temp_dir.as_posix()]
result = pdm(cmd)
assert result.exit_code == 0

wheel = wheel_from_tempdir(temp_dir)
assert count_group_dependencies(wheel, "locked") == 26
assert count_group_dependencies(wheel, "extras-locked") == 0
assert count_group_dependencies(wheel, "cow-locked") == 0


@pytest.mark.usefixtures("assert_pyproject_unmodified")
@pytest.mark.parametrize("test_project", ["invalid"])
def test_build_locked_invalid(pdm: PDMCallable, data_base_path: Path, temp_dir: Path, test_project: str) -> None:
Expand Down

0 comments on commit 35c9f5a

Please sign in to comment.