forked from python-poetry/poetry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python-poetry#8083): Added support for poetry config being store…
…d in a project .poetry directory and added the 'self init' command.
- Loading branch information
Showing
7 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
from cleo.helpers import option | ||
|
||
from poetry.console.commands.self.self_command import SelfCommand | ||
|
||
|
||
class SelfInitCommand(SelfCommand): | ||
name = "self init" | ||
description = """\ | ||
Initializes a local .poetry directory to be used instead of the global poetry \ | ||
configuration.\ | ||
""" | ||
options = [ | ||
option( | ||
"project-dir", | ||
None, | ||
( | ||
"The directory containing a pyproject.toml file to which the .poetry " | ||
" directory will be added." | ||
), | ||
flag=False, | ||
default=None, | ||
), | ||
] | ||
help = """\ | ||
The <c1>self init</c1> command creates and initializes a .poetry directory that\ | ||
contains poetry configuration specific for the project directory instead of using the\ | ||
global poetry configuration. | ||
""" | ||
|
||
loggers = ["poetry.repositories.pypi_repository", "poetry.inspection.info"] | ||
|
||
def __init__(self) -> None: | ||
self._system_pyproject = super().system_pyproject | ||
super().__init__() | ||
|
||
def handle(self) -> int: | ||
project_dir = self.option("project-dir") | ||
if project_dir is None: | ||
project_dir = Path.cwd() | ||
self._system_pyproject = Path(project_dir) / ".poetry" / "pyproject.toml" | ||
if self.system_pyproject.exists(): | ||
self.line(f"Poetry settings already exist for project {project_dir}") | ||
self.line_error("\nNo changes were applied.") | ||
return 1 | ||
|
||
self.line(f"Initialising poetry settings for project {project_dir}") | ||
self.system_pyproject.parent.mkdir(parents=True, exist_ok=True) | ||
self.reset_poetry() | ||
return 0 | ||
|
||
@property | ||
def system_pyproject(self) -> Path: | ||
return self._system_pyproject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
|
||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
from poetry.core.utils.helpers import temporary_directory | ||
|
||
|
||
if TYPE_CHECKING: | ||
from cleo.testers.command_tester import CommandTester | ||
|
||
from tests.types import CommandTesterFactory | ||
|
||
|
||
@pytest.fixture() | ||
def tester(command_tester_factory: CommandTesterFactory) -> CommandTester: | ||
return command_tester_factory("self init") | ||
|
||
|
||
def test_init_no_args( | ||
tester: CommandTester, | ||
) -> None: | ||
with temporary_directory() as tmp_dir: | ||
current_dir = os.getcwd() | ||
try: | ||
os.chdir(tmp_dir) | ||
tester.execute() | ||
assert tester.io.fetch_output() == f"""\ | ||
Initialising poetry settings for project {tmp_dir} | ||
""" | ||
assert (Path(tmp_dir) / ".poetry" / "pyproject.toml").exists() | ||
assert tester.status_code == 0 | ||
finally: | ||
os.chdir(current_dir) | ||
|
||
|
||
def test_init_project_dir( | ||
tester: CommandTester, | ||
) -> None: | ||
with temporary_directory() as tmp_dir: | ||
tester.execute(args=f"--project-dir {tmp_dir}") | ||
assert tester.io.fetch_output() == f"""\ | ||
Initialising poetry settings for project {tmp_dir} | ||
""" | ||
assert (Path(tmp_dir) / ".poetry" / "pyproject.toml").exists() | ||
assert tester.status_code == 0 | ||
|
||
|
||
def test_init_project_dir_already_exists( | ||
tester: CommandTester, | ||
) -> None: | ||
with temporary_directory() as tmp_dir: | ||
pyproject_file = Path(tmp_dir) / ".poetry" / "pyproject.toml" | ||
pyproject_file.parent.mkdir() | ||
pyproject_file.write_text("hello world") | ||
tester.execute(args=f"--project-dir {tmp_dir}") | ||
assert tester.io.fetch_output() == f"""\ | ||
Poetry settings already exist for project {tmp_dir} | ||
""" | ||
assert tester.status_code == 1 |