Skip to content

Commit

Permalink
init: handle pyproject exceptions for existing file
Browse files Browse the repository at this point in the history
Resolves: #3073
  • Loading branch information
abn committed Oct 4, 2020
1 parent 3539026 commit b980292
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from cleo import option
from tomlkit import inline_table

from poetry.core.pyproject import PyProjectException
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.utils._compat import OrderedDict
from poetry.utils._compat import Path
Expand Down Expand Up @@ -378,7 +379,7 @@ def _parse_requirements(

try:
cwd = self.poetry.file.parent
except RuntimeError:
except (PyProjectException, RuntimeError):
cwd = Path.cwd()

for requirement in requirements:
Expand Down
36 changes: 36 additions & 0 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,42 @@ def test_init_existing_pyproject_simple(
)


def test_init_non_interactive_existing_pyproject_add_dependency(
tester, source_dir, init_basic_inputs, repo
):
pyproject_file = source_dir / "pyproject.toml"
existing_section = """
[tool.black]
line-length = 88
"""
pyproject_file.write_text(decode(existing_section))

repo.add_package(get_package("foo", "1.19.2"))

tester.execute(
"--author 'Your Name <you@example.com>' "
"--name 'my-package' "
"--python '^3.6' "
"--dependency foo",
interactive=False,
)

expected = """\
[tool.poetry]
name = "my-package"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
[tool.poetry.dependencies]
python = "^3.6"
foo = "^1.19.2"
[tool.poetry.dev-dependencies]
"""
assert "{}\n{}".format(existing_section, expected) in pyproject_file.read_text()


def test_init_existing_pyproject_with_build_system_fails(
tester, source_dir, init_basic_inputs
):
Expand Down

0 comments on commit b980292

Please sign in to comment.