Skip to content

Commit

Permalink
feat(cli): add --migration option to config command
Browse files Browse the repository at this point in the history
  • Loading branch information
finswimmer committed Nov 7, 2024
1 parent 3c10711 commit 20c0be1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/poetry/console/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from poetry.config.config import boolean_normalizer
from poetry.config.config import boolean_validator
from poetry.config.config import int_normalizer
from poetry.config.config_source import UNSET
from poetry.config.config_source import ConfigSourceMigration
from poetry.console.commands.command import Command


Expand All @@ -25,6 +27,17 @@

from poetry.config.config_source import ConfigSource

CONFIG_MIGRATIONS = [
ConfigSourceMigration(
old_key="experimental.system-git-client", new_key="system-git-client"
),
ConfigSourceMigration(
old_key="virtualenvs.prefer-active-python",
new_key="virtualenvs.use-poetry-python",
value_migration={True: UNSET, False: True},
),
]


class ConfigCommand(Command):
name = "config"
Expand All @@ -39,6 +52,7 @@ class ConfigCommand(Command):
option("list", None, "List configuration settings."),
option("unset", None, "Unset configuration setting."),
option("local", None, "Set/Get from the project's local configuration."),
option("migrate", None, "Migrate outdated configuration settings."),
]

help = """\
Expand Down Expand Up @@ -97,6 +111,9 @@ def handle(self) -> int:
from poetry.locations import CONFIG_DIR
from poetry.toml.file import TOMLFile

if self.option("migrate"):
self._migrate()

config = Config.create()
config_file = TOMLFile(CONFIG_DIR / "config.toml")

Expand Down Expand Up @@ -324,3 +341,14 @@ def _list_configuration(
message = f"<c1>{k + key}</c1> = <c2>{json.dumps(value)}</c2>"

self.line(message)

def _migrate(self) -> None:
from poetry.config.file_config_source import FileConfigSource
from poetry.locations import CONFIG_DIR
from poetry.toml.file import TOMLFile

config_file = TOMLFile(CONFIG_DIR / "config.toml")
config_source = FileConfigSource(config_file)

for migration in CONFIG_MIGRATIONS:
migration.apply(config_source)
34 changes: 32 additions & 2 deletions tests/console/commands/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import json
import os
import textwrap

from pathlib import Path
from typing import TYPE_CHECKING

import pytest
Expand All @@ -18,8 +20,6 @@


if TYPE_CHECKING:
from pathlib import Path

from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture

Expand Down Expand Up @@ -560,3 +560,33 @@ def test_config_solver_lazy_wheel(

repo = LegacyRepository("foo", "https://foo.com")
assert not repo._lazy_wheel


def test_config_migrate(
tester: CommandTester, mocker: MockerFixture, tmp_path: Path
) -> None:
config_dir = tmp_path / "config"
mocker.patch("poetry.locations.CONFIG_DIR", config_dir)

config_file = Path(config_dir / "config.toml")
config_data = textwrap.dedent("""\
[experimental]
system-git-client = true
[virtualenvs]
prefer-active-python = false
""")
with config_file.open("w") as fh:
fh.write(config_data)

tester.execute("--migrate")

expected_config = textwrap.dedent("""\
system-git-client = true
[virtualenvs]
use-poetry-python = true
""")

with config_file.open("r") as fh:
assert fh.read() == expected_config

0 comments on commit 20c0be1

Please sign in to comment.