Skip to content

Commit

Permalink
Add new --check flag to lock command
Browse files Browse the repository at this point in the history
  • Loading branch information
cliebBS authored and finswimmer committed Mar 13, 2021
1 parent adc3cde commit c31864c
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ This command locks (without installing) the dependencies specified in `pyproject
poetry lock
```

### Options

* `--check`: Verify that `poetry.lock` is consistent with `pyproject.toml`

## version

This command shows the current version of the project or bumps the version of
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/pyproject.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ poetry install --extras "mysql pgsql"
poetry install -E mysql -E pgsql
```

When installing or specifying Poetry-built packages, the extras defined in this section can be activated
When installing or specifying Poetry-built packages, the extras defined in this section can be activated
as described in [PEP 508](https://www.python.org/dev/peps/pep-0508/#extras).

For example, when installing the package using `pip`, the dependencies required by
For example, when installing the package using `pip`, the dependencies required by
the `databases` extra can be installed as shown below.

```bash
Expand Down
13 changes: 13 additions & 0 deletions poetry/console/commands/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class LockCommand(InstallerCommand):
option(
"no-update", None, "Do not update locked versions, only refresh lock file."
),
option(
"check",
None,
"Check that the <comment>poetry.lock</> file corresponds to the current version "
"of <comment>pyproject.toml</>.",
),
]

help = """
Expand All @@ -29,6 +35,13 @@ def handle(self) -> int:
self.poetry.config.get("experimental.new-installer", False)
)

if self.option("check"):
return (
0
if self.poetry.locker.is_locked() and self.poetry.locker.is_fresh()
else 1
)

self._installer.lock(update=not self.option("no-update"))

return self._installer.run()
56 changes: 53 additions & 3 deletions tests/console/commands/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ def tester(command_tester_factory):
return command_tester_factory("lock")


@pytest.fixture
def poetry_with_old_lockfile(project_factory, fixture_dir, source_dir):
source = fixture_dir("old_lock")
def _project_factory(fixture_name, project_factory, fixture_dir):
source = fixture_dir(fixture_name)
pyproject_content = (source / "pyproject.toml").read_text(encoding="utf-8")
poetry_lock_content = (source / "poetry.lock").read_text(encoding="utf-8")
return project_factory(
Expand All @@ -28,6 +27,57 @@ def poetry_with_old_lockfile(project_factory, fixture_dir, source_dir):
)


@pytest.fixture
def poetry_with_outdated_lockfile(project_factory, fixture_dir):
return _project_factory("outdated_lock", project_factory, fixture_dir)


@pytest.fixture
def poetry_with_up_to_date_lockfile(project_factory, fixture_dir):
return _project_factory("up_to_date_lock", project_factory, fixture_dir)


@pytest.fixture
def poetry_with_old_lockfile(project_factory, fixture_dir):
return _project_factory("old_lock", project_factory, fixture_dir)


def test_lock_check_outdated(
command_tester_factory, poetry_with_outdated_lockfile, http
):
http.disable()

locker = Locker(
lock=poetry_with_outdated_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config=poetry_with_outdated_lockfile.locker._local_config,
)
poetry_with_outdated_lockfile.set_locker(locker)

tester = command_tester_factory("lock", poetry=poetry_with_outdated_lockfile)
status_code = tester.execute("--check")

# exit with an error
assert status_code == 1


def test_lock_check_up_to_date(
command_tester_factory, poetry_with_up_to_date_lockfile, http
):
http.disable()

locker = Locker(
lock=poetry_with_up_to_date_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config=poetry_with_up_to_date_lockfile.locker._local_config,
)
poetry_with_up_to_date_lockfile.set_locker(locker)

tester = command_tester_factory("lock", poetry=poetry_with_up_to_date_lockfile)
status_code = tester.execute("--check")

# exit with an error
assert status_code == 0


def test_lock_no_update(command_tester_factory, poetry_with_old_lockfile, repo):
repo.add_package(get_package("sampleproject", "1.3.1"))
repo.add_package(get_package("sampleproject", "2.0.0"))
Expand Down
152 changes: 152 additions & 0 deletions tests/fixtures/outdated_lock/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/fixtures/outdated_lock/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tool.poetry]
name = "foobar"
version = "0.1.0"
description = ""
authors = ["Poetry Developer <developer@python-poetry.org>"]

[tool.poetry.dependencies]
python = "^3.8"
docker = "4.3.1"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

0 comments on commit c31864c

Please sign in to comment.