diff --git a/src/poetry/console/commands/source/add.py b/src/poetry/console/commands/source/add.py index 3dd76834900..b522d8331f9 100644 --- a/src/poetry/console/commands/source/add.py +++ b/src/poetry/console/commands/source/add.py @@ -1,22 +1,14 @@ from __future__ import annotations -from typing import TYPE_CHECKING - from cleo.helpers import argument from cleo.helpers import option from cleo.io.null_io import NullIO -from tomlkit import nl -from tomlkit import table from tomlkit.items import AoT from poetry.config.source import Source from poetry.console.commands.command import Command -if TYPE_CHECKING: - from tomlkit.items import Table - - class SourceAddCommand(Command): name = "source add" @@ -41,17 +33,10 @@ class SourceAddCommand(Command): option("secondary", "s", "Set this source as secondary."), ] - @staticmethod - def source_to_table(source: Source) -> Table: - source_table: Table = table() - for key, value in source.to_dict().items(): - source_table.add(key, value) - source_table.add(nl()) - return source_table - def handle(self) -> int: from poetry.factory import Factory from poetry.repositories import Pool + from poetry.utils.source import source_to_table name = self.argument("name") url = self.argument("url") @@ -92,11 +77,11 @@ def handle(self) -> int: source = new_source new_source = None - sources.append(self.source_to_table(source)) + sources.append(source_to_table(source)) if new_source is not None: self.line(f"Adding source with name {name}.") - sources.append(self.source_to_table(new_source)) + sources.append(source_to_table(new_source)) # ensure new source is valid. eg: invalid name etc. self.poetry._pool = Pool() diff --git a/src/poetry/console/commands/source/remove.py b/src/poetry/console/commands/source/remove.py index 2dc4b380182..f4e32bf5034 100644 --- a/src/poetry/console/commands/source/remove.py +++ b/src/poetry/console/commands/source/remove.py @@ -1,21 +1,11 @@ from __future__ import annotations -from typing import TYPE_CHECKING - from cleo.helpers import argument -from tomlkit import nl -from tomlkit import table from tomlkit.items import AoT from poetry.console.commands.command import Command -if TYPE_CHECKING: - from tomlkit.items import Table - - from poetry.config.source import Source - - class SourceRemoveCommand(Command): name = "source remove" @@ -28,15 +18,9 @@ class SourceRemoveCommand(Command): ), ] - @staticmethod - def source_to_table(source: Source) -> Table: - source_table: Table = table() - for key, value in source.to_dict().items(): - source_table.add(key, value) - source_table.add(nl()) - return source_table - def handle(self) -> int: + from poetry.utils.source import source_to_table + name = self.argument("name") sources = AoT([]) @@ -47,7 +31,7 @@ def handle(self) -> int: self.line(f"Removing source with name {source.name}.") removed = True continue - sources.append(self.source_to_table(source)) + sources.append(source_to_table(source)) if not removed: self.line_error( diff --git a/src/poetry/utils/source.py b/src/poetry/utils/source.py new file mode 100644 index 00000000000..dc8e1c8c92f --- /dev/null +++ b/src/poetry/utils/source.py @@ -0,0 +1,20 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + + +if TYPE_CHECKING: + from tomlkit.items import Table + + from poetry.config.source import Source + + +def source_to_table(source: Source) -> Table: + from tomlkit import nl + from tomlkit import table + + source_table: Table = table() + for key, value in source.to_dict().items(): + source_table.add(key, value) + source_table.add(nl()) + return source_table diff --git a/tests/utils/test_source.py b/tests/utils/test_source.py new file mode 100644 index 00000000000..a970b7262ca --- /dev/null +++ b/tests/utils/test_source.py @@ -0,0 +1,40 @@ +from __future__ import annotations + +import pytest + +from tomlkit.container import Container +from tomlkit.items import Table +from tomlkit.items import Trivia + +from poetry.config.source import Source +from poetry.utils.source import source_to_table + + +@pytest.mark.parametrize( + "source,table_body", + [ + ( + Source("foo", "https://example.com"), + { + "default": False, + "name": "foo", + "secondary": False, + "url": "https://example.com", + }, + ), + ( + Source("bar", "https://example.com/bar", True, True), + { + "default": True, + "name": "bar", + "secondary": True, + "url": "https://example.com/bar", + }, + ), + ], +) +def test_source_to_table(source: Source, table_body: dict[str, str | bool]): + table = Table(Container(), Trivia(), False) + table._value = table_body + + assert source_to_table(source) == table