Skip to content

Commit

Permalink
feat: add --check flag to pdm lock (#1460)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgolsson authored and frostming committed Oct 25, 2022
1 parent 7877f73 commit 053cddc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/1459.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A new `pdm lock --check` flag to validate whether the lock is up to date.
26 changes: 26 additions & 0 deletions src/pdm/cli/commands/lock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import argparse
import sys

from pdm import termui
from pdm.cli import actions
from pdm.cli.commands.base import BaseCommand
from pdm.cli.hooks import HookManager
Expand All @@ -23,7 +25,31 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
help="Don't update pinned versions, only refresh the lock file",
)

parser.add_argument(
"--check",
action="store_true",
help="Check if the lock file is up to date",
)

def handle(self, project: Project, options: argparse.Namespace) -> None:
if options.check:
strategy = actions.check_lockfile(project, False)
if strategy:
project.core.ui.echo(
f"[error]{termui.Emoji.FAIL}[/] Lockfile is [error]out of date[/].",
err=True,
verbosity=termui.Verbosity.DETAIL,
)
sys.exit(1)
else:
project.core.ui.echo(
f"[success]{termui.Emoji.SUCC}[/] Lockfile is "
"[success]up to date[/].",
err=True,
verbosity=termui.Verbosity.DETAIL,
)
sys.exit(0)

actions.do_lock(
project,
refresh=options.refresh,
Expand Down
21 changes: 21 additions & 0 deletions tests/cli/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ def test_lock_refresh_keep_consistent(invoke, project, repository):
assert project.lockfile_file.read_text() == previous


def test_lock_check_no_change_success(invoke, project, repository):
project.add_dependencies({"requests": parse_requirement("requests")})
result = invoke(["lock"], obj=project)
assert result.exit_code == 0
assert project.is_lockfile_hash_match()

result = invoke(["lock", "--check"], obj=project)
assert result.exit_code == 0


def test_lock_check_change_fails(invoke, project, repository):
project.add_dependencies({"requests": parse_requirement("requests")})
result = invoke(["lock"], obj=project)
assert result.exit_code == 0
assert project.is_lockfile_hash_match()

project.add_dependencies({"pyyaml": parse_requirement("pyyaml")})
result = invoke(["lock", "--check"], obj=project)
assert result.exit_code == 1


@pytest.mark.usefixtures("repository")
def test_innovations_with_specified_lockfile(invoke, project, working_set):
project.add_dependencies({"requests": parse_requirement("requests")})
Expand Down

0 comments on commit 053cddc

Please sign in to comment.