From 3ee2e14f40b1a02ea5d07a98341e75f2d79946b9 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Mon, 9 Oct 2023 10:45:32 -0600 Subject: [PATCH 1/5] fix: error when lockfile without metadata entry is encountered without this, you'll get a cryptic 'metadata' in red when running `poetry install` this can occur if a poetry.lock file is empty --- src/poetry/packages/locker.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/poetry/packages/locker.py b/src/poetry/packages/locker.py index 2031194d492..cf2d65f837f 100644 --- a/src/poetry/packages/locker.py +++ b/src/poetry/packages/locker.py @@ -323,6 +323,13 @@ def _get_lock_data(self) -> dict[str, Any]: except tomllib.TOMLDecodeError as e: raise RuntimeError(f"Unable to read the lock file ({e}).") + # if the lockfile doesn't contain a metadata section at all, it probably needs to be rebuilt completely + if not "metadata" in lock_data: + raise RuntimeError( + "The lock file does not have a metadata entry.\n" + "Regenerate the lock file with the `poetry lock` command." + ) + metadata = lock_data["metadata"] lock_version = Version.parse(metadata.get("lock-version", "1.0")) current_version = Version.parse(self._VERSION) From 2caee5f41a67ecefb068a91da98148eb301ca2c4 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Tue, 10 Oct 2023 18:27:58 -0600 Subject: [PATCH 2/5] test: for pyproject without metadata --- tests/packages/test_locker.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/packages/test_locker.py b/tests/packages/test_locker.py index 715ba5efb33..d94358c32ba 100644 --- a/tests/packages/test_locker.py +++ b/tests/packages/test_locker.py @@ -621,6 +621,33 @@ def test_reading_lock_file_should_raise_an_error_on_invalid_data( assert "Unable to read the lock file" in str(e.value) +def test_reading_lock_file_should_raise_an_error_on_missing_metadata( + locker: Locker, +) -> None: + content = f"""\ +# {GENERATED_COMMENT} + +[[package]] +name = "A" +version = "1.0.0" +description = "" +optional = false +python-versions = "*" +files = [] + +[package.source] +type = "legacy" +url = "https://foo.bar" +reference = "legacy" +""" + with locker.lock.open("w", encoding="utf-8") as f: + f.write(content) + + with pytest.raises(RuntimeError) as e: + _ = locker.lock_data + + assert 'The lock file does not have a metadata entry.\nRegenerate the lock file with the `poetry lock` command.' in str(e.value) + def test_locking_legacy_repository_package_should_include_source_section( root: ProjectPackage, locker: Locker From d3a72d9b3554d3fd5f1f6e40dd6501102afba602 Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Tue, 10 Oct 2023 18:31:24 -0600 Subject: [PATCH 3/5] style: lint fixes --- src/poetry/packages/locker.py | 5 +++-- tests/packages/test_locker.py | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/poetry/packages/locker.py b/src/poetry/packages/locker.py index cf2d65f837f..822e0a59be5 100644 --- a/src/poetry/packages/locker.py +++ b/src/poetry/packages/locker.py @@ -323,8 +323,9 @@ def _get_lock_data(self) -> dict[str, Any]: except tomllib.TOMLDecodeError as e: raise RuntimeError(f"Unable to read the lock file ({e}).") - # if the lockfile doesn't contain a metadata section at all, it probably needs to be rebuilt completely - if not "metadata" in lock_data: + # if the lockfile doesn't contain a metadata section at all, + # it probably needs to be rebuilt completely + if "metadata" not in lock_data: raise RuntimeError( "The lock file does not have a metadata entry.\n" "Regenerate the lock file with the `poetry lock` command." diff --git a/tests/packages/test_locker.py b/tests/packages/test_locker.py index d94358c32ba..e115c32c799 100644 --- a/tests/packages/test_locker.py +++ b/tests/packages/test_locker.py @@ -621,6 +621,7 @@ def test_reading_lock_file_should_raise_an_error_on_invalid_data( assert "Unable to read the lock file" in str(e.value) + def test_reading_lock_file_should_raise_an_error_on_missing_metadata( locker: Locker, ) -> None: @@ -646,7 +647,11 @@ def test_reading_lock_file_should_raise_an_error_on_missing_metadata( with pytest.raises(RuntimeError) as e: _ = locker.lock_data - assert 'The lock file does not have a metadata entry.\nRegenerate the lock file with the `poetry lock` command.' in str(e.value) + assert ( + "The lock file does not have a metadata entry.\nRegenerate the lock file with" + " the `poetry lock` command." + in str(e.value) + ) def test_locking_legacy_repository_package_should_include_source_section( From cbb46680a3fce25d42517b03dcc7e971b682ab0e Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Thu, 12 Oct 2023 06:02:39 -0600 Subject: [PATCH 4/5] fix: assert location --- tests/packages/test_locker.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/packages/test_locker.py b/tests/packages/test_locker.py index e115c32c799..d6e59b0d20a 100644 --- a/tests/packages/test_locker.py +++ b/tests/packages/test_locker.py @@ -647,11 +647,11 @@ def test_reading_lock_file_should_raise_an_error_on_missing_metadata( with pytest.raises(RuntimeError) as e: _ = locker.lock_data - assert ( - "The lock file does not have a metadata entry.\nRegenerate the lock file with" - " the `poetry lock` command." - in str(e.value) - ) + assert ( + "The lock file does not have a metadata entry.\nRegenerate the lock file with" + " the `poetry lock` command." + in str(e.value) + ) def test_locking_legacy_repository_package_should_include_source_section( From b807f420d19c5a0f539ef4d9768b8a0cc6499d0f Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Sat, 14 Oct 2023 08:58:17 -0600 Subject: [PATCH 5/5] Revert "fix: assert location" This reverts commit cbb46680a3fce25d42517b03dcc7e971b682ab0e. --- tests/packages/test_locker.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/packages/test_locker.py b/tests/packages/test_locker.py index d6e59b0d20a..e115c32c799 100644 --- a/tests/packages/test_locker.py +++ b/tests/packages/test_locker.py @@ -647,11 +647,11 @@ def test_reading_lock_file_should_raise_an_error_on_missing_metadata( with pytest.raises(RuntimeError) as e: _ = locker.lock_data - assert ( - "The lock file does not have a metadata entry.\nRegenerate the lock file with" - " the `poetry lock` command." - in str(e.value) - ) + assert ( + "The lock file does not have a metadata entry.\nRegenerate the lock file with" + " the `poetry lock` command." + in str(e.value) + ) def test_locking_legacy_repository_package_should_include_source_section(