From 7fac6769f8486b079f1235ff0d121b74f31598d2 Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Wed, 23 Jun 2021 13:06:32 -0500 Subject: [PATCH] handle poetry init non-interactive dependencies (#2899) * 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?" --- poetry/console/commands/init.py | 30 +++++++++++++++++------------ tests/console/commands/test_init.py | 22 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/poetry/console/commands/init.py b/poetry/console/commands/init.py index b2e4fd0a315..0d525dbe3e0 100644 --- a/poetry/console/commands/init.py +++ b/poetry/console/commands/init.py @@ -86,11 +86,12 @@ def handle(self) -> int: vcs_config = GitConfig() - self.line("") - self.line( - "This command will guide you through creating your pyproject.toml config." - ) - self.line("") + if self.io.is_interactive(): + self.line("") + self.line( + "This command will guide you through creating your pyproject.toml config." + ) + self.line("") name = self.option("name") if not name: @@ -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"): @@ -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"): @@ -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, @@ -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 diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index 4ae13465399..2d6301e05a0 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -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"))