Skip to content

Commit

Permalink
replace Factory.create_config() w/ Config.create()
Browse files Browse the repository at this point in the history
Prior to this change when `Config` was initialised for non-command use,
user `config.toml` and `auth.toml` files were not loaded. This caused
unintended side effects when configuration look up were performed from
the `Authenticator` and other parts of the code.
  • Loading branch information
abn committed May 9, 2022
1 parent 529281e commit 6cddbe4
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 50 deletions.
30 changes: 30 additions & 0 deletions src/poetry/config/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import os
import re

Expand All @@ -9,8 +10,12 @@
from typing import Any
from typing import Callable

from poetry.core.toml import TOMLFile

from poetry.config.dict_config_source import DictConfigSource
from poetry.config.file_config_source import FileConfigSource
from poetry.locations import CACHE_DIR
from poetry.locations import CONFIG_DIR


if TYPE_CHECKING:
Expand All @@ -29,6 +34,9 @@ def int_normalizer(val: str) -> int:
return int(val)


logger = logging.getLogger(__name__)


class Config:

default_config: dict[str, Any] = {
Expand Down Expand Up @@ -186,3 +194,25 @@ def _get_normalizer(name: str) -> Callable[[str], Any]:
return int_normalizer

return lambda val: val

@classmethod
def create(cls) -> Config:
config = cls()

# Load global config
config_file = TOMLFile(CONFIG_DIR / "config.toml")
if config_file.exists():
logger.debug("Loading configuration file %s", config_file.path)
config.merge(config_file.read())

config.set_config_source(FileConfigSource(config_file))

# Load global auth config
auth_config_file = TOMLFile(CONFIG_DIR / "auth.toml")
if auth_config_file.exists():
logger.debug("Loading configuration file %s", auth_config_file.path)
config.merge(auth_config_file.read())

config.set_auth_config_source(FileConfigSource(auth_config_file))

return config
4 changes: 2 additions & 2 deletions src/poetry/console/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ def handle(self) -> int | None:
from poetry.core.pyproject.exceptions import PyProjectException
from poetry.core.toml.file import TOMLFile

from poetry.config.config import Config
from poetry.config.file_config_source import FileConfigSource
from poetry.factory import Factory
from poetry.locations import CONFIG_DIR

config = Factory.create_config(self.io)
config = Config.create()
config_file = TOMLFile(CONFIG_DIR / "config.toml")

try:
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/console/commands/self/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _update(self, version: Version) -> None:
root,
NullLocker(self.data_dir.joinpath("poetry.lock"), {}),
self.pool,
Config(),
config=Config.create(),
installed=installed,
)
installer.update(True)
Expand Down
39 changes: 3 additions & 36 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
from tomlkit.toml_document import TOMLDocument

from poetry.config.config import Config
from poetry.config.file_config_source import FileConfigSource
from poetry.locations import CONFIG_DIR
from poetry.packages.locker import Locker
from poetry.packages.project_package import ProjectPackage
from poetry.plugins.plugin import Plugin
from poetry.plugins.plugin_manager import PluginManager
from poetry.poetry import Poetry
from poetry.utils.dependency_specification import dependency_to_specification


try:
Expand Down Expand Up @@ -65,7 +62,7 @@ def create_poetry(
)

# Loading global configuration
config = self.create_config(io)
config = Config.create()

# Loading local configuration
local_config_file = TOMLFile(base_poetry.file.parent / "poetry.toml")
Expand Down Expand Up @@ -114,38 +111,6 @@ def create_poetry(
def get_package(cls, name: str, version: str) -> ProjectPackage:
return ProjectPackage(name, version, version)

@classmethod
def create_config(cls, io: IO | None = None) -> Config:
if io is None:
io = NullIO()

config = Config()
# Load global config
config_file = TOMLFile(CONFIG_DIR / "config.toml")
if config_file.exists():
if io.is_debug():
io.write_line(
f"<debug>Loading configuration file {config_file.path}</debug>"
)

config.merge(config_file.read())

config.set_config_source(FileConfigSource(config_file))

# Load global auth config
auth_config_file = TOMLFile(CONFIG_DIR / "auth.toml")
if auth_config_file.exists():
if io.is_debug():
io.write_line(
f"<debug>Loading configuration file {auth_config_file.path}</debug>"
)

config.merge(auth_config_file.read())

config.set_auth_config_source(FileConfigSource(auth_config_file))

return config

@classmethod
def configure_sources(
cls,
Expand Down Expand Up @@ -223,6 +188,8 @@ def create_pyproject_from_package(
) -> TOMLDocument:
import tomlkit

from poetry.utils.dependency_specification import dependency_to_specification

pyproject: dict[str, Any] = tomlkit.document()

tool_table = tomlkit.table()
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/utils/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(
cache_id: str | None = None,
disable_cache: bool = False,
) -> None:
self._config = config or Config(use_environment=True)
self._config = config or Config.create()
self._io = io
self._sessions_for_netloc: dict[str, requests.Session] = {}
self._credentials: dict[str, HTTPAuthCredential] = {}
Expand Down
10 changes: 4 additions & 6 deletions src/poetry/vcs/git/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,20 +349,18 @@ def _clone_submodules(cls, repo: Repo) -> None:

@staticmethod
def is_using_legacy_client() -> bool:
from poetry.factory import Factory
from poetry.config.config import Config

legacy_client: bool = (
Factory.create_config()
.get("experimental", {})
.get("system-git-client", False)
Config.create().get("experimental", {}).get("system-git-client", False)
)
return legacy_client

@staticmethod
def get_default_source_root() -> Path:
from poetry.factory import Factory
from poetry.config.config import Config

return Path(Factory.create_config().get("cache-dir")) / "src"
return Path(Config.create().get("cache-dir")) / "src"

@classmethod
def clone(
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def config(
c.set_config_source(config_source)
c.set_auth_config_source(auth_config_source)

mocker.patch("poetry.factory.Factory.create_config", return_value=c)
mocker.patch("poetry.config.config.Config.create", return_value=c)
mocker.patch("poetry.config.config.Config.set_config_source")

return c
Expand All @@ -219,7 +219,7 @@ def config_dir(tmp_dir: str) -> Path:
@pytest.fixture(autouse=True)
def mock_user_config_dir(mocker: MockerFixture, config_dir: Path) -> None:
mocker.patch("poetry.locations.CONFIG_DIR", new=config_dir)
mocker.patch("poetry.factory.CONFIG_DIR", new=config_dir)
mocker.patch("poetry.config.config.CONFIG_DIR", new=config_dir)


@pytest.fixture(autouse=True)
Expand Down
3 changes: 2 additions & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from poetry.core.toml.file import TOMLFile
from poetry.core.vcs.git import ParsedUrl

from poetry.config.config import Config
from poetry.console.application import Application
from poetry.factory import Factory
from poetry.installation.executor import Executor
Expand Down Expand Up @@ -107,7 +108,7 @@ def mock_clone(
folder = Path(__file__).parent / "fixtures" / "git" / parsed.resource / path

if not source_root:
source_root = Path(Factory.create_config().get("cache-dir")) / "src"
source_root = Path(Config.create().get("cache-dir")) / "src"

dest = source_root / path
dest.parent.mkdir(parents=True, exist_ok=True)
Expand Down
2 changes: 1 addition & 1 deletion tests/publishing/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_publish_can_publish_to_given_repository(
}
)

mocker.patch("poetry.factory.Factory.create_config", return_value=config)
mocker.patch("poetry.config.config.Config.create", return_value=config)
poetry = Factory().create_poetry(fixture_dir(fixture_name))

io = BufferedIO()
Expand Down

0 comments on commit 6cddbe4

Please sign in to comment.