diff --git a/poetry/console/commands/init.py b/poetry/console/commands/init.py index af72318c31a..09aa1e3f28e 100644 --- a/poetry/console/commands/init.py +++ b/poetry/console/commands/init.py @@ -85,11 +85,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: @@ -153,7 +154,8 @@ def handle(self): ) python = self.ask(question) - self.line("") + if self.io.is_interactive(): + self.line("") requirements = {} if self.option("dependency"): @@ -174,12 +176,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.update( self._format_requirements(self._determine_requirements([])) ) - self.line("") + if self.io.is_interactive(): + self.line("") dev_requirements = {} if self.option("dev-dependency"): @@ -191,13 +195,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.update( self._format_requirements(self._determine_requirements([])) ) - self.line("") + if self.io.is_interactive(): + self.line("") layout_ = layout("standard")( name, @@ -313,7 +318,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 7a9212ab262..adf63b63641 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -78,6 +78,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"))