Skip to content

Commit

Permalink
fix(show): ensure lockfile exists
Browse files Browse the repository at this point in the history
  • Loading branch information
branchvincent committed Feb 27, 2022
1 parent ea6e189 commit 835ecc8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def handle(self) -> Optional[int]:
if self.option("default"):
only_groups.append("default")

if not self.poetry.locker.is_locked():
self.line_error("<comment>The lock file does not exist. Locking.</comment>")
self.call("lock")

locked_repo = self.poetry.locker.locked_repository(True)

if only_groups:
Expand Down
43 changes: 43 additions & 0 deletions tests/console/commands/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture

from poetry.poetry import Poetry
from poetry.repositories import Repository
Expand Down Expand Up @@ -1587,3 +1588,45 @@ def test_show_required_by_deps(
""".splitlines()
actual = [line.rstrip() for line in tester.io.fetch_output().splitlines()]
assert actual == expected


def test_show_locks_if_no_lock_file(
mocker: "MockerFixture",
tester: "CommandTester",
poetry: "Poetry",
installed: "Repository",
):
call = mocker.patch("cleo.commands.command.Command.call")

poetry.package.add_dependency(Factory.create_dependency("cachy", "0.1.0"))
installed.add_package(get_package("cachy", "0.1.0"))
lock_data = {
"package": [
{
"name": "cachy",
"version": "0.1.0",
"description": "Cachy package",
"category": "main",
"optional": False,
"platform": "*",
"python-versions": "*",
"checksum": [],
},
],
"metadata": {
"python-versions": "*",
"platform": "*",
"content-hash": "123456789",
"hashes": {"cachy": []},
},
}
call.side_effect = lambda *args, **kwargs: poetry.locker.mock_lock_data(lock_data)

assert not poetry.locker.lock.exists()

tester.execute()

assert "The lock file does not exist. Locking." in tester.io.fetch_error()
assert tester.io.fetch_output() == "cachy 0.1.0 Cachy package\n"
assert call.call_count == 1
assert call.call_args == [("lock",)]

0 comments on commit 835ecc8

Please sign in to comment.