File: helper.py

package info (click to toggle)
python-phx-class-registry 4.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: python: 886; makefile: 19
file content (41 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import sys
from importlib.metadata import DistributionFinder, PathDistribution
from os import path
from pathlib import Path


class DummyDistributionFinder(DistributionFinder):
    """
    Injects a dummy distribution into the meta path finder, so that we can
    pretend like it's been pip installed during unit tests (i.e., so that we
    can test ``EntryPointsClassRegistry``), without polluting the persistent
    virtualenv.
    """

    DUMMY_PACKAGE_DIR = 'dummy_package.egg-info'

    @classmethod
    def install(cls):
        for finder in sys.meta_path:
            if isinstance(finder, cls):
                # If we've already installed an instance of the class, then
                # something is probably wrong with our tests.
                raise ValueError(f'{cls.__name__} is already installed')

        sys.meta_path.append(cls())

    @classmethod
    def uninstall(cls):
        for i, finder in enumerate(sys.meta_path):
            if isinstance(finder, cls):
                sys.meta_path.pop(i)
                return
        else:
            # If we didn't find an installed instance of the class, then
            # something is probably wrong with our tests.
            raise ValueError(f'{cls.__name__} was not installed')

    def find_distributions(self, context=...) -> list[PathDistribution]:
        return [PathDistribution(
            Path(path.join(path.dirname(__file__), self.DUMMY_PACKAGE_DIR))
        )]