Commit 7fac6769 by Dan Rose Committed by GitHub

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?"
parent b34cd718
...@@ -86,11 +86,12 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the ...@@ -86,11 +86,12 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the
vcs_config = GitConfig() vcs_config = GitConfig()
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(
) "This command will guide you through creating your <info>pyproject.toml</> config."
self.line("") )
self.line("")
name = self.option("name") name = self.option("name")
if not name: if not name:
...@@ -154,7 +155,8 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the ...@@ -154,7 +155,8 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the
) )
python = self.ask(question) python = self.ask(question)
self.line("") if self.io.is_interactive():
self.line("")
requirements = {} requirements = {}
if self.option("dependency"): if self.option("dependency"):
...@@ -175,12 +177,14 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the ...@@ -175,12 +177,14 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the
) )
help_displayed = False help_displayed = False
if self.confirm(question, True): if self.confirm(question, True):
self.line(help_message) if self.io.is_interactive():
help_displayed = True self.line(help_message)
help_displayed = True
requirements.update( requirements.update(
self._format_requirements(self._determine_requirements([])) self._format_requirements(self._determine_requirements([]))
) )
self.line("") if self.io.is_interactive():
self.line("")
dev_requirements = {} dev_requirements = {}
if self.option("dev-dependency"): if self.option("dev-dependency"):
...@@ -192,13 +196,14 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the ...@@ -192,13 +196,14 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the
"Would you like to define your development dependencies interactively?" "Would you like to define your development dependencies interactively?"
) )
if self.confirm(question, True): if self.confirm(question, True):
if not help_displayed: if self.io.is_interactive() and not help_displayed:
self.line(help_message) self.line(help_message)
dev_requirements.update( dev_requirements.update(
self._format_requirements(self._determine_requirements([])) self._format_requirements(self._determine_requirements([]))
) )
self.line("") if self.io.is_interactive():
self.line("")
layout_ = layout("standard")( layout_ = layout("standard")(
name, name,
...@@ -317,7 +322,8 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the ...@@ -317,7 +322,8 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the
if package is not False: if package is not False:
requires.append(constraint) requires.append(constraint)
package = self.ask("\nAdd a package:") if self.io.is_interactive():
package = self.ask("\nAdd a package:")
return requires return requires
......
...@@ -81,6 +81,28 @@ def test_basic_interactive(tester, init_basic_inputs, init_basic_toml): ...@@ -81,6 +81,28 @@ def test_basic_interactive(tester, init_basic_inputs, init_basic_toml):
assert init_basic_toml in tester.io.fetch_output() 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): def test_interactive_with_dependencies(tester, repo):
repo.add_package(get_package("django-pendulum", "0.1.6-pre4")) repo.add_package(get_package("django-pendulum", "0.1.6-pre4"))
repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pendulum", "2.0.0"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment