Skip to content

Commit 304c54a

Browse files
committed
non-package-mode: fix poetry add (#9046)
(cherry picked from commit f66f8ab)
1 parent 78f7dd6 commit 304c54a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/poetry/console/commands/add.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ def handle(self) -> int:
122122
# dictionary.
123123
content: dict[str, Any] = self.poetry.file.read()
124124
poetry_content = content["tool"]["poetry"]
125-
project_name = canonicalize_name(poetry_content["name"])
125+
project_name = (
126+
canonicalize_name(name) if (name := poetry_content.get("name")) else None
127+
)
126128

127129
if group == MAIN_GROUP:
128130
if "dependencies" not in poetry_content:

tests/console/commands/test_add.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@ def test_add_no_constraint(
101101
assert content["dependencies"]["cachy"] == "^0.2.0"
102102

103103

104+
def test_add_non_package_mode_no_name(
105+
repo: TestRepository,
106+
project_factory: ProjectFactory,
107+
command_tester_factory: CommandTesterFactory,
108+
) -> None:
109+
repo.add_package(get_package("cachy", "0.2.0"))
110+
111+
poetry = project_factory(
112+
name="foobar", pyproject_content="[tool.poetry]\npackage-mode = false\n"
113+
)
114+
tester = command_tester_factory("add", poetry=poetry)
115+
tester.execute("cachy")
116+
117+
assert isinstance(tester.command, InstallerCommand)
118+
assert tester.command.installer.executor.installations_count == 1
119+
120+
pyproject: dict[str, Any] = poetry.file.read()
121+
content = pyproject["tool"]["poetry"]
122+
123+
assert "cachy" in content["dependencies"]
124+
assert content["dependencies"]["cachy"] == "^0.2.0"
125+
126+
104127
def test_add_replace_by_constraint(
105128
app: PoetryTestApplication, repo: TestRepository, tester: CommandTester
106129
) -> None:

0 commit comments

Comments
 (0)