From 8c0395204f248f6b88ba61734ccdeb89090dba2b Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Thu, 10 Sep 2020 14:28:33 -0500 Subject: [PATCH 1/3] 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 | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/poetry/console/commands/init.py b/poetry/console/commands/init.py index c843a15212f..f577f82b6b2 100644 --- a/poetry/console/commands/init.py +++ b/poetry/console/commands/init.py @@ -73,11 +73,12 @@ def handle(self): 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: @@ -158,12 +159,14 @@ def handle(self): ) 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 = self._format_requirements( self._determine_requirements(self.option("dependency")) ) - self.line("") + if self.io.is_interactive(): + self.line("") dev_requirements = {} @@ -171,13 +174,14 @@ def handle(self): "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 = self._format_requirements( self._determine_requirements(self.option("dev-dependency")) - ) - self.line("") + ) + if self.io.is_interactive(): + self.line("") layout_ = layout("standard")( name, @@ -293,7 +297,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 From 50f0adabb209282c3c0f522d24013fd6cc1074ab Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Thu, 10 Sep 2020 15:13:07 -0500 Subject: [PATCH 2/3] black --- poetry/console/commands/init.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poetry/console/commands/init.py b/poetry/console/commands/init.py index f577f82b6b2..dc042c95bc7 100644 --- a/poetry/console/commands/init.py +++ b/poetry/console/commands/init.py @@ -179,7 +179,7 @@ def handle(self): dev_requirements = self._format_requirements( self._determine_requirements(self.option("dev-dependency")) - ) + ) if self.io.is_interactive(): self.line("") From 9c2c4abb8650b039064e6d7820c846628a81a360 Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Thu, 17 Sep 2020 17:21:11 -0500 Subject: [PATCH 3/3] Add test for noninteractive init --- poetry/console/commands/init.py | 3 ++- tests/console/commands/test_init.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/poetry/console/commands/init.py b/poetry/console/commands/init.py index dc042c95bc7..57f45c1e1cc 100644 --- a/poetry/console/commands/init.py +++ b/poetry/console/commands/init.py @@ -142,7 +142,8 @@ def handle(self): ) python = self.ask(question) - self.line("") + if self.io.is_interactive(): + self.line("") requirements = {} diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index 4ca7d44c44c..cd77286d660 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -45,6 +45,28 @@ def test_basic_interactive(app, mocker, poetry): assert expected 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(app, repo, mocker, poetry): repo.add_package(get_package("django-pendulum", "0.1.6-pre4")) repo.add_package(get_package("pendulum", "2.0.0"))