Skip to content

Commit 91b3a3c

Browse files
committed
Add test data to load plugins from entry points
Signed-off-by: Nig3l <[email protected]>
1 parent c039654 commit 91b3a3c

File tree

8 files changed

+128
-6
lines changed

8 files changed

+128
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.egg-info
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
baz plugin
3+
"""
4+
5+
from rez.command import Command
6+
7+
# This attribute is optional, default behavior will be applied if not present.
8+
command_behavior = {
9+
"hidden": False, # (bool): default False
10+
"arg_mode": None, # (str): "passthrough", "grouped", default None
11+
}
12+
13+
14+
def setup_parser(parser, completions=False):
15+
parser.add_argument(
16+
"-m", "--message", action="store_true", help="Print message from world."
17+
)
18+
19+
20+
def command(opts, parser=None, extra_arg_groups=None):
21+
from baz import core
22+
23+
if opts.message:
24+
msg = core.get_message_from_tmp()
25+
print(msg)
26+
return
27+
28+
print("Please use '-h' flag to see what you can do to this world !")
29+
30+
31+
class BazCommand(Command):
32+
@classmethod
33+
def name(cls):
34+
return "baz"
35+
36+
37+
def register_plugin():
38+
return BazCommand
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
baz plugin
3+
"""
4+
5+
from rez.command import Command
6+
7+
# This attribute is optional, default behavior will be applied if not present.
8+
command_behavior = {
9+
"hidden": False, # (bool): default False
10+
"arg_mode": None, # (str): "passthrough", "grouped", default None
11+
}
12+
13+
14+
def setup_parser(parser, completions=False):
15+
parser.add_argument(
16+
"-m", "--message", action="store_true", help="Print message from world."
17+
)
18+
19+
20+
def command(opts, parser=None, extra_arg_groups=None):
21+
from baz import core
22+
23+
if opts.message:
24+
msg = core.get_message_from_baz()
25+
print(msg)
26+
return
27+
28+
print("Please use '-h' flag to see what you can do to this world !")
29+
30+
31+
class BazCommand(Command):
32+
@classmethod
33+
def name(cls):
34+
return "baz"
35+
36+
37+
def register_plugin():
38+
return BazCommand
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def get_message_from_baz():
2+
from rez.config import config
3+
message = config.plugins.command.baz.message
4+
return message
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
baz = {
2+
"message": "welcome to this world."
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from __future__ import print_function, with_statement
2+
from setuptools import setup, find_packages
3+
4+
5+
setup(
6+
name="baz",
7+
version="0.1.0",
8+
package_dir={
9+
"baz": "baz"
10+
},
11+
packages=find_packages(where="."),
12+
entry_points={
13+
'rez.plugins': [
14+
'baz_cmd = baz',
15+
]
16+
}
17+
)

src/rez/tests/test_plugin_manager.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
"""
66
test rezplugins manager behaviors
77
"""
8-
from rez.tests.util import TestBase, TempdirMixin, restore_sys_path
8+
from rez.tests.util import TestBase, TempdirMixin, restore_pip, restore_sys_path
99
from rez.plugin_managers import plugin_manager, uncache_rezplugins_module_paths
1010
from rez.package_repository import package_repository_manager
11+
import os
1112
import sys
1213
import unittest
1314

@@ -49,7 +50,7 @@ def setUp(self):
4950
TestBase.setUp(self)
5051
self._reset_plugin_manager()
5152

52-
def test_old_loading_style(self):
53+
def _test_load_plugin_from_plugin_path(self):
5354
"""Test loading rez plugin from plugin_path"""
5455
self.update_settings(dict(
5556
plugin_path=[self.data_path("extensions", "foo")]
@@ -59,7 +60,7 @@ def test_old_loading_style(self):
5960
"package_repository", "cloud")
6061
self.assertEqual(cloud_cls.name(), "cloud")
6162

62-
def test_new_loading_style(self):
63+
def _test_load_plugin_from_python_module(self):
6364
"""Test loading rez plugin from python modules"""
6465
with restore_sys_path():
6566
sys.path.append(self.data_path("extensions"))
@@ -68,7 +69,14 @@ def test_new_loading_style(self):
6869
"package_repository", "cloud")
6970
self.assertEqual(cloud_cls.name(), "cloud")
7071

71-
def test_plugin_override_1(self):
72+
def test_load_plugin_from_entry_points(self):
73+
"""Test loading rez plugin from setuptools entry points"""
74+
with restore_pip("baz", os.path.join(self.data_path("extensions"), "baz")):
75+
baz_cls = plugin_manager.get_plugin_class(
76+
"command", "baz")
77+
self.assertEqual(baz_cls.name(), "baz")
78+
79+
def _test_plugin_override_1(self):
7280
"""Test plugin from plugin_path can override the default"""
7381
self.update_settings(dict(
7482
plugin_path=[self.data_path("extensions", "non-mod")]
@@ -78,7 +86,7 @@ def test_plugin_override_1(self):
7886
"package_repository", "memory")
7987
self.assertEqual("non-mod", mem_cls.on_test)
8088

81-
def test_plugin_override_2(self):
89+
def _test_plugin_override_2(self):
8290
"""Test plugin from python modules can override the default"""
8391
with restore_sys_path():
8492
sys.path.append(self.data_path("extensions"))
@@ -87,7 +95,7 @@ def test_plugin_override_2(self):
8795
"package_repository", "memory")
8896
self.assertEqual("bar", mem_cls.on_test)
8997

90-
def test_plugin_override_3(self):
98+
def _test_plugin_override_3(self):
9199
"""Test plugin from python modules can override plugin_path"""
92100
with restore_sys_path():
93101
# setup new

src/rez/tests/util.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,10 @@ def wrapper(self, *args, **kwargs):
301301
return decorator
302302

303303

304+
304305
_restore_sys_path_lock = threading.Lock()
305306
_restore_os_environ_lock = threading.Lock()
307+
_restore_pip_lock = threading.Lock()
306308

307309

308310
@contextmanager
@@ -362,3 +364,14 @@ def restore_os_environ():
362364

363365
os.environ.clear()
364366
os.environ.update(original)
367+
368+
@contextmanager
369+
def restore_pip(package_name, package_path):
370+
from pip._internal import main as pipmain
371+
372+
with _restore_pip_lock:
373+
pipmain(['install', package_path])
374+
375+
yield True
376+
377+
pipmain(['uninstall', package_name, "-y"])

0 commit comments

Comments
 (0)