Skip to content

Commit

Permalink
handle poetry init non-interactive dependencies (python-poetry#2899)
Browse files Browse the repository at this point in the history
* handle poetry init non-interactive dependencies

1. in non-interactive mode, suppress "This command will guide you through..."
2. in interactive mode, respect command line --dependency if the user chooses NO for "Would you like to define your main dependencies interactively?"
  • Loading branch information
rotu authored Jun 23, 2021
1 parent b34cd71 commit 7fac676
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
30 changes: 18 additions & 12 deletions poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ def handle(self) -> int:

vcs_config = GitConfig()

self.line("")
self.line(
"This command will guide you through creating your <info>pyproject.toml</> config."
)
self.line("")
if self.io.is_interactive():
self.line("")
self.line(
"This command will guide you through creating your <info>pyproject.toml</> config."
)
self.line("")

name = self.option("name")
if not name:
Expand Down Expand Up @@ -154,7 +155,8 @@ def handle(self) -> int:
)
python = self.ask(question)

self.line("")
if self.io.is_interactive():
self.line("")

requirements = {}
if self.option("dependency"):
Expand All @@ -175,12 +177,14 @@ def handle(self) -> int:
)
help_displayed = False
if self.confirm(question, True):
self.line(help_message)
help_displayed = True
if self.io.is_interactive():
self.line(help_message)
help_displayed = True
requirements.update(
self._format_requirements(self._determine_requirements([]))
)
self.line("")
if self.io.is_interactive():
self.line("")

dev_requirements = {}
if self.option("dev-dependency"):
Expand All @@ -192,13 +196,14 @@ def handle(self) -> int:
"Would you like to define your development dependencies interactively?"
)
if self.confirm(question, True):
if not help_displayed:
if self.io.is_interactive() and not help_displayed:
self.line(help_message)

dev_requirements.update(
self._format_requirements(self._determine_requirements([]))
)
self.line("")
if self.io.is_interactive():
self.line("")

layout_ = layout("standard")(
name,
Expand Down Expand Up @@ -317,7 +322,8 @@ def _determine_requirements(
if package is not False:
requires.append(constraint)

package = self.ask("\nAdd a package:")
if self.io.is_interactive():
package = self.ask("\nAdd a package:")

return requires

Expand Down
22 changes: 22 additions & 0 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ def test_basic_interactive(tester, init_basic_inputs, init_basic_toml):
assert init_basic_toml in tester.io.fetch_output()


def test_noninteractive(app, mocker, poetry, repo, tmp_path):
command = app.find("init")
command._pool = poetry.pool

repo.add_package(get_package("pytest", "3.6.0"))

p = mocker.patch("poetry.utils._compat.Path.cwd")
p.return_value = tmp_path

tester = CommandTester(command)
args = "--name my-package --dependency pytest"
tester.execute(args=args, interactive=False)

expected = "Using version ^3.6.0 for pytest\n"
assert tester.io.fetch_output() == expected
assert "" == tester.io.fetch_error()

toml_content = (tmp_path / "pyproject.toml").read_text()
assert 'name = "my-package"' in toml_content
assert 'pytest = "^3.6.0"' in toml_content


def test_interactive_with_dependencies(tester, repo):
repo.add_package(get_package("django-pendulum", "0.1.6-pre4"))
repo.add_package(get_package("pendulum", "2.0.0"))
Expand Down

0 comments on commit 7fac676

Please sign in to comment.