3
3
# The original PyScaffold license can be found in 'NOTICE.txt'
4
4
5
5
import sys
6
+ from types import ModuleType
7
+ from typing import Any , List
6
8
7
9
import pytest
8
10
9
11
from validate_pyproject import plugins
10
- from validate_pyproject .plugins import ENTRYPOINT_GROUP , ErrorLoadingPlugin
12
+ from validate_pyproject .plugins import ErrorLoadingPlugin
11
13
12
14
EXISTING = (
13
15
"setuptools" ,
17
19
18
20
if sys .version_info [:2 ] >= (3 , 8 ):
19
21
# TODO: Import directly (no need for conditional) when `python_requires = >= 3.8`
20
- from importlib . metadata import EntryPoint # pragma: no cover
22
+ from importlib import metadata # pragma: no cover
21
23
else :
22
- from importlib_metadata import EntryPoint # pragma: no cover
24
+ import importlib_metadata as metadata # pragma: no cover
23
25
24
26
25
27
def test_load_from_entry_point__error ():
26
28
# This module does not exist, so Python will have some trouble loading it
27
- # EntryPoint(name, value, group)
29
+ # metadata. EntryPoint(name, value, group)
28
30
entry = "mypkg.SOOOOO___fake___:activate"
29
- fake = EntryPoint ("fake" , entry , ENTRYPOINT_GROUP )
31
+ fake = metadata . EntryPoint ("fake" , entry , "validate_pyproject.tool_schema" )
30
32
with pytest .raises (ErrorLoadingPlugin ):
31
33
plugins .load_from_entry_point (fake )
32
34
@@ -36,7 +38,7 @@ def is_entry_point(ep):
36
38
37
39
38
40
def test_iterate_entry_points ():
39
- plugin_iter = plugins .iterate_entry_points ()
41
+ plugin_iter = plugins .iterate_entry_points ("validate_pyproject.tool_schema" )
40
42
assert hasattr (plugin_iter , "__iter__" )
41
43
pluging_list = list (plugin_iter )
42
44
assert all (is_entry_point (e ) for e in pluging_list )
@@ -47,14 +49,14 @@ def test_iterate_entry_points():
47
49
48
50
def test_list_from_entry_points ():
49
51
# Should return a list with all the plugins registered in the entrypoints
50
- pluging_list = plugins .list_from_entry_points ()
52
+ pluging_list = plugins .list_plugins_from_entry_points ()
51
53
orig_len = len (pluging_list )
52
54
plugin_names = " " .join (e .tool for e in pluging_list )
53
55
for example in EXISTING :
54
56
assert example in plugin_names
55
57
56
58
# a filtering function can be passed to avoid loading plugins that are not needed
57
- pluging_list = plugins .list_from_entry_points (
59
+ pluging_list = plugins .list_plugins_from_entry_points (
58
60
filtering = lambda e : e .name != "setuptools"
59
61
)
60
62
plugin_names = " " .join (e .tool for e in pluging_list )
@@ -76,3 +78,32 @@ def _fn2(_):
76
78
77
79
pw = plugins .PluginWrapper ("name" , _fn2 )
78
80
assert pw .help_text == "Help for `name`"
81
+
82
+
83
+ def loader (name : str ) -> Any :
84
+ return {"example" : "thing" }
85
+
86
+
87
+ def dynamic_ep ():
88
+ return {"some#fragment" : loader }
89
+
90
+
91
+ class Select (list ):
92
+ def select (self , group : str ) -> List [str ]:
93
+ return list (self ) if group == "validate_pyproject.multi_schema" else []
94
+
95
+
96
+ def test_process_checks (monkeypatch : pytest .MonkeyPatch ) -> None :
97
+ ep = metadata .EntryPoint (
98
+ name = "_" ,
99
+ group = "validate_pyproject.multi_schema" ,
100
+ value = "test_module:dynamic_ep" ,
101
+ )
102
+ sys .modules ["test_module" ] = ModuleType ("test_module" )
103
+ sys .modules ["test_module" ].dynamic_ep = dynamic_ep # type: ignore[attr-defined]
104
+ sys .modules ["test_module" ].loader = loader # type: ignore[attr-defined]
105
+ monkeypatch .setattr (plugins , "entry_points" , lambda : Select ([ep ]))
106
+ eps = plugins .list_plugins_from_entry_points ()
107
+ (ep ,) = eps
108
+ assert ep .tool == "some"
109
+ assert ep .fragment == "fragment"
0 commit comments