Skip to content

Commit

Permalink
Unify Loader.__repr__ (tox-dev#3343)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziima committed Sep 15, 2024
1 parent 8cadfa2 commit e87204b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/tox/config/loader/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def found_keys(self) -> set[str]:
raise NotImplementedError

def __repr__(self) -> str:
return f"{type(self).__name__}"
return f"{self.__class__.__name__}(section={self._section.key}, overrides={self.overrides!r})"

def __contains__(self, item: str) -> bool:
return item in self.found_keys()
Expand Down
3 changes: 0 additions & 3 deletions src/tox/config/loader/ini/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,3 @@ def get_section(self, name: str) -> SectionProxy | None:
if self._parser.has_section(name):
return self._parser[name]
return None

def __repr__(self) -> str:
return f"{self.__class__.__name__}(section={self._section.key}, overrides={self.overrides!r})"
8 changes: 1 addition & 7 deletions tests/config/loader/ini/test_ini_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from tox.config.loader.api import ConfigLoadArgs, Override
from tox.config.loader.api import ConfigLoadArgs
from tox.config.loader.ini import IniLoader
from tox.config.source.ini_section import IniSection

Expand All @@ -18,12 +18,6 @@ def test_ini_loader_keys(mk_ini_conf: Callable[[str], ConfigParser]) -> None:
assert loader.found_keys() == {"a", "c"}


def test_ini_loader_repr(mk_ini_conf: Callable[[str], ConfigParser]) -> None:
core = IniSection(None, "tox")
loader = IniLoader(core, mk_ini_conf("\n[tox]\n\na=b\nc=d\n\n"), [Override("tox.a=1")], core_section=core)
assert repr(loader) == "IniLoader(section=tox, overrides={'a': [Override('tox.a=1')]})"


def test_ini_loader_has_section(mk_ini_conf: Callable[[str], ConfigParser]) -> None:
core = IniSection(None, "tox")
loader = IniLoader(core, mk_ini_conf("[magic]\n[tox]\n\na=b\nc=d\n\n"), [], core_section=core)
Expand Down
21 changes: 20 additions & 1 deletion tests/config/loader/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import pytest

from tox.config.cli.parse import get_options
from tox.config.loader.api import Override
from tox.config.loader.api import Loader, Override
from tox.config.loader.section import Section
from tox.config.loader.str_convert import StrConvert

if TYPE_CHECKING:
from tox.config.main import Config
from tox.pytest import CaptureFixture


Expand Down Expand Up @@ -62,3 +65,19 @@ def test_override_not_equals_different_type() -> None:

def test_override_repr() -> None:
assert repr(Override("b.a=c")) == "Override('b.a=c')"


class SimpleLoader(StrConvert, Loader[str]):
"""Simple loader for tests."""

def load_raw(self, key: str, conf: Config | None, env_name: str | None) -> str: # type: ignore[empty-body]
pass

def found_keys(self) -> set[str]: # type: ignore[empty-body]
pass


def test_loader_repr() -> None:
core = Section(None, "tox")
loader = SimpleLoader(core, [Override("tox.a=1")])
assert repr(loader) == "SimpleLoader(section=tox, overrides={'a': [Override('tox.a=1')]})"
5 changes: 0 additions & 5 deletions tests/config/loader/test_memory_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
from tox.config.types import Command, EnvList


def test_memory_loader_repr() -> None:
loader = MemoryLoader(a=1)
assert repr(loader) == "MemoryLoader"


def test_memory_loader_override() -> None:
loader = MemoryLoader(a=1)
loader.overrides["a"] = [Override("a=2")]
Expand Down

0 comments on commit e87204b

Please sign in to comment.