Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle poetry init non-interactive dependencies #2899

Merged
merged 5 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ def handle(self):

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 @@ -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"):
Expand All @@ -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("")
Comment on lines +185 to +186
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we really need this self.line(""). I guess at least it can be placed directly after the last output above and we can avoid this second if-statement.

Same thing applies to line 183-184.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It serves to separate the interactive output of _determine_requirements, so that would change the output. Still want the change?


dev_requirements = {}
if self.option("dev-dependency"):
Expand All @@ -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,
Expand Down Expand Up @@ -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

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 @@ -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"))
Expand Down