Skip to content

Commit cd8fbdb

Browse files
committed
Make tests NamespaceCollector seperate from code_quality
It's a general tool, and centralizing this outside of 'code quality' makes sense for some things that pkgcore and pkgcheck do w/ registries. Signed-off-by: Brian Harring <ferringb@gmail.com>
1 parent bc1955a commit cd8fbdb

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

src/snakeoil/test/code_quality.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,8 @@
1212
get_slots_of,
1313
get_subclasses_of,
1414
)
15-
from snakeoil.python_namespaces import get_submodules_of
16-
from snakeoil.test import AbstractTest
1715

18-
T = typing.TypeVar("T")
19-
20-
21-
class NamespaceCollector(AbstractTest):
22-
namespaces: tuple[str] = abstractclassvar(tuple[str])
23-
namespace_ignores: tuple[str, ...] = ()
24-
25-
strict_configurable_tests: typing.ClassVar[tuple[str, ...]] = ()
26-
strict: typing.ClassVar[typing.Literal[True] | tuple[str, ...]] = ()
27-
28-
def __init_subclass__(cls, **kwargs) -> None:
29-
if not inspect.isabstract(cls):
30-
targets = cls.strict_configurable_tests
31-
strict = targets if cls.strict is True else cls.strict
32-
for test_name in cls.strict_configurable_tests:
33-
if test_name not in strict:
34-
setattr(cls, test_name, pytest.mark.xfail(getattr(cls, test_name)))
35-
36-
return super().__init_subclass__(**kwargs)
37-
38-
@classmethod
39-
def collect_modules(cls) -> typing.Iterable[ModuleType]:
40-
for namespace in cls.namespaces:
41-
yield from get_submodules_of(
42-
namespace,
43-
dont_import=cls.namespace_ignores,
44-
include_root=True,
45-
)
16+
from .util import NamespaceCollector
4617

4718

4819
class Slots(NamespaceCollector, still_abstract=True):

src/snakeoil/test/util.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
__all__ = ("NamespaceCollector",)
2+
import inspect
3+
import types
4+
import typing
5+
6+
import pytest
7+
8+
from snakeoil import klass
9+
from snakeoil.python_namespaces import get_submodules_of
10+
from snakeoil.test import AbstractTest
11+
12+
13+
class NamespaceCollector(AbstractTest):
14+
namespaces: tuple[str] = klass.abstractclassvar(tuple[str])
15+
namespace_ignores: tuple[str, ...] = ()
16+
17+
strict_configurable_tests: typing.ClassVar[tuple[str, ...]] = ()
18+
strict: typing.ClassVar[typing.Literal[True] | tuple[str, ...]] = ()
19+
20+
def __init_subclass__(cls, **kwargs) -> None:
21+
if not inspect.isabstract(cls):
22+
targets = cls.strict_configurable_tests
23+
strict = targets if cls.strict is True else cls.strict
24+
for test_name in cls.strict_configurable_tests:
25+
if test_name not in strict:
26+
setattr(cls, test_name, pytest.mark.xfail(getattr(cls, test_name)))
27+
28+
return super().__init_subclass__(**kwargs)
29+
30+
@classmethod
31+
def collect_modules(cls) -> typing.Iterable[types.ModuleType]:
32+
for namespace in cls.namespaces:
33+
yield from get_submodules_of(
34+
namespace,
35+
dont_import=cls.namespace_ignores,
36+
include_root=True,
37+
)

0 commit comments

Comments
 (0)