diff --git a/src/poetry/installation/installer.py b/src/poetry/installation/installer.py
index 12d54f3c567..21230da6988 100644
--- a/src/poetry/installation/installer.py
+++ b/src/poetry/installation/installer.py
@@ -245,12 +245,12 @@ def _do_install(self) -> int:
locked_repository = self._locker.locked_repository()
if not self._locker.is_fresh():
- self._io.write_error_line(
- ""
- "Warning: poetry.lock is not consistent with pyproject.toml. "
+ raise ValueError(
+ ""
+ "Error: poetry.lock is not consistent with pyproject.toml. "
"You may be getting improper dependencies. "
"Run `poetry lock [--no-update]` to fix it."
- ""
+ ""
)
locker_extras = {
diff --git a/tests/installation/test_installer.py b/tests/installation/test_installer.py
index be9aa175c59..8f131a330cf 100644
--- a/tests/installation/test_installer.py
+++ b/tests/installation/test_installer.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import json
+import re
from pathlib import Path
from typing import TYPE_CHECKING
@@ -97,6 +98,7 @@ def __init__(self, lock_path: Path) -> None:
self._lock = lock_path / "poetry.lock"
self._written_data = None
self._locked = False
+ self._fresh = True
self._lock_data = None
self._content_hash = self._get_content_hash()
@@ -121,8 +123,13 @@ def mock_lock_data(self, data: dict[str, Any]) -> None:
def is_locked(self) -> bool:
return self._locked
+ def fresh(self, is_fresh: bool = True) -> Locker:
+ self._fresh = is_fresh
+
+ return self
+
def is_fresh(self) -> bool:
- return True
+ return self._fresh
def _get_content_hash(self) -> str:
return "123456789"
@@ -208,6 +215,19 @@ def test_run_no_dependencies(installer: Installer, locker: Locker) -> None:
assert locker.written_data == expected
+def test_not_fresh_lock(installer: Installer, locker: Locker) -> None:
+ locker.locked().fresh(False)
+ with pytest.raises(
+ ValueError,
+ match=re.escape(
+ "Error: poetry.lock is not consistent with pyproject.toml. You may"
+ " be getting improper dependencies. Run `poetry lock [--no-update]` to fix"
+ " it."
+ ),
+ ):
+ installer.run()
+
+
def test_run_with_dependencies(
installer: Installer, locker: Locker, repo: Repository, package: ProjectPackage
) -> None: