Skip to content

Commit

Permalink
Add test data to load plugins from entry points
Browse files Browse the repository at this point in the history
Signed-off-by: Nig3l <[email protected]>
  • Loading branch information
Ni-g-3l committed Nov 11, 2024
1 parent c039654 commit 91b3a3c
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/rez/data/tests/extensions/baz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.egg-info
38 changes: 38 additions & 0 deletions src/rez/data/tests/extensions/baz/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
baz plugin
"""

from rez.command import Command

# This attribute is optional, default behavior will be applied if not present.
command_behavior = {
"hidden": False, # (bool): default False
"arg_mode": None, # (str): "passthrough", "grouped", default None
}


def setup_parser(parser, completions=False):
parser.add_argument(
"-m", "--message", action="store_true", help="Print message from world."
)


def command(opts, parser=None, extra_arg_groups=None):
from baz import core

if opts.message:
msg = core.get_message_from_tmp()
print(msg)
return

print("Please use '-h' flag to see what you can do to this world !")


class BazCommand(Command):
@classmethod
def name(cls):
return "baz"


def register_plugin():
return BazCommand
38 changes: 38 additions & 0 deletions src/rez/data/tests/extensions/baz/baz/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
baz plugin
"""

from rez.command import Command

# This attribute is optional, default behavior will be applied if not present.
command_behavior = {
"hidden": False, # (bool): default False
"arg_mode": None, # (str): "passthrough", "grouped", default None
}


def setup_parser(parser, completions=False):
parser.add_argument(
"-m", "--message", action="store_true", help="Print message from world."
)


def command(opts, parser=None, extra_arg_groups=None):
from baz import core

if opts.message:
msg = core.get_message_from_baz()
print(msg)
return

print("Please use '-h' flag to see what you can do to this world !")


class BazCommand(Command):
@classmethod
def name(cls):
return "baz"


def register_plugin():
return BazCommand
4 changes: 4 additions & 0 deletions src/rez/data/tests/extensions/baz/baz/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def get_message_from_baz():
from rez.config import config
message = config.plugins.command.baz.message
return message
3 changes: 3 additions & 0 deletions src/rez/data/tests/extensions/baz/baz/rezconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
baz = {
"message": "welcome to this world."
}
17 changes: 17 additions & 0 deletions src/rez/data/tests/extensions/baz/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import print_function, with_statement
from setuptools import setup, find_packages


setup(
name="baz",
version="0.1.0",
package_dir={
"baz": "baz"
},
packages=find_packages(where="."),
entry_points={
'rez.plugins': [
'baz_cmd = baz',
]
}
)
20 changes: 14 additions & 6 deletions src/rez/tests/test_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"""
test rezplugins manager behaviors
"""
from rez.tests.util import TestBase, TempdirMixin, restore_sys_path
from rez.tests.util import TestBase, TempdirMixin, restore_pip, restore_sys_path
from rez.plugin_managers import plugin_manager, uncache_rezplugins_module_paths
from rez.package_repository import package_repository_manager
import os
import sys
import unittest

Expand Down Expand Up @@ -49,7 +50,7 @@ def setUp(self):
TestBase.setUp(self)
self._reset_plugin_manager()

def test_old_loading_style(self):
def _test_load_plugin_from_plugin_path(self):
"""Test loading rez plugin from plugin_path"""
self.update_settings(dict(
plugin_path=[self.data_path("extensions", "foo")]
Expand All @@ -59,7 +60,7 @@ def test_old_loading_style(self):
"package_repository", "cloud")
self.assertEqual(cloud_cls.name(), "cloud")

def test_new_loading_style(self):
def _test_load_plugin_from_python_module(self):
"""Test loading rez plugin from python modules"""
with restore_sys_path():
sys.path.append(self.data_path("extensions"))
Expand All @@ -68,7 +69,14 @@ def test_new_loading_style(self):
"package_repository", "cloud")
self.assertEqual(cloud_cls.name(), "cloud")

def test_plugin_override_1(self):
def test_load_plugin_from_entry_points(self):
"""Test loading rez plugin from setuptools entry points"""
with restore_pip("baz", os.path.join(self.data_path("extensions"), "baz")):
baz_cls = plugin_manager.get_plugin_class(
"command", "baz")
self.assertEqual(baz_cls.name(), "baz")

def _test_plugin_override_1(self):
"""Test plugin from plugin_path can override the default"""
self.update_settings(dict(
plugin_path=[self.data_path("extensions", "non-mod")]
Expand All @@ -78,7 +86,7 @@ def test_plugin_override_1(self):
"package_repository", "memory")
self.assertEqual("non-mod", mem_cls.on_test)

def test_plugin_override_2(self):
def _test_plugin_override_2(self):
"""Test plugin from python modules can override the default"""
with restore_sys_path():
sys.path.append(self.data_path("extensions"))
Expand All @@ -87,7 +95,7 @@ def test_plugin_override_2(self):
"package_repository", "memory")
self.assertEqual("bar", mem_cls.on_test)

def test_plugin_override_3(self):
def _test_plugin_override_3(self):
"""Test plugin from python modules can override plugin_path"""
with restore_sys_path():
# setup new
Expand Down
13 changes: 13 additions & 0 deletions src/rez/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,10 @@ def wrapper(self, *args, **kwargs):
return decorator



_restore_sys_path_lock = threading.Lock()
_restore_os_environ_lock = threading.Lock()
_restore_pip_lock = threading.Lock()


@contextmanager
Expand Down Expand Up @@ -362,3 +364,14 @@ def restore_os_environ():

os.environ.clear()
os.environ.update(original)

@contextmanager
def restore_pip(package_name, package_path):
from pip._internal import main as pipmain

with _restore_pip_lock:
pipmain(['install', package_path])

yield True

pipmain(['uninstall', package_name, "-y"])

0 comments on commit 91b3a3c

Please sign in to comment.