Commit 404ab141 by Arun Babu Neelicattu Committed by Bjorn Neergaard

replace Factory.create_config() w/ Config.create()

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.
parent 70e8e8ed
from __future__ import annotations
import logging
import os
import re
......@@ -9,8 +10,12 @@ from typing import TYPE_CHECKING
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:
......@@ -29,6 +34,12 @@ def int_normalizer(val: str) -> int:
return int(val)
logger = logging.getLogger(__name__)
_default_config: Config | None = None
class Config:
default_config: dict[str, Any] = {
......@@ -186,3 +197,28 @@ class Config:
return int_normalizer
return lambda val: val
@classmethod
def create(cls, reload: bool = False) -> Config:
global _default_config
if _default_config is None or reload:
_default_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)
_default_config.merge(config_file.read())
_default_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)
_default_config.merge(auth_config_file.read())
_default_config.set_auth_config_source(FileConfigSource(auth_config_file))
return _default_config
......@@ -117,11 +117,11 @@ To remove a repository (repo is a short alias for repositories):
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:
......
......@@ -191,7 +191,7 @@ class SelfUpdateCommand(Command):
root,
NullLocker(self.data_dir.joinpath("poetry.lock"), {}),
self.pool,
Config(),
config=Config.create(),
installed=installed,
)
installer.update(True)
......
......@@ -3,6 +3,7 @@ from __future__ import annotations
import contextlib
import logging
import re
import warnings
from typing import TYPE_CHECKING
from typing import Any
......@@ -14,14 +15,11 @@ from poetry.core.toml.file import TOMLFile
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:
......@@ -65,7 +63,12 @@ class Factory(BaseFactory): # type: ignore[misc]
)
# Loading global configuration
config = self.create_config(io)
with warnings.catch_warnings():
# this is preserved to ensure export plugin tests pass in ci,
# once poetry-plugin-export version is updated to use one that do not
# use Factory.create_config(), this can be safely removed.
warnings.filterwarnings("ignore", category=DeprecationWarning)
config = self.create_config()
# Loading local configuration
local_config_file = TOMLFile(base_poetry.file.parent / "poetry.toml")
......@@ -116,35 +119,13 @@ class Factory(BaseFactory): # type: ignore[misc]
@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
if io is not None:
logger.debug("Ignoring provided io when creating config.")
warnings.warn(
"Use of Factory.create_config() is deprecated, use Config.create() instead",
DeprecationWarning,
)
return Config.create()
@classmethod
def configure_sources(
......@@ -223,6 +204,8 @@ class Factory(BaseFactory): # type: ignore[misc]
) -> TOMLDocument:
import tomlkit
from poetry.utils.dependency_specification import dependency_to_specification
pyproject: dict[str, Any] = tomlkit.document()
tool_table = tomlkit.table()
......
......@@ -88,7 +88,7 @@ class Authenticator:
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] = {}
......
......@@ -349,20 +349,18 @@ class Git:
@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(
......
......@@ -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
......@@ -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)
......
......@@ -17,6 +17,7 @@ from poetry.core.packages.utils.link import Link
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
......@@ -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)
......
......@@ -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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment