"""
test rezplugins manager behaviors
"""
from rez.tests.util import TestBase, TempdirMixin, restore_sys_path
from rez.plugin_managers import (
    uncache_sys_module_paths,
    RezPluginManager,
    ShellPluginType,
    ReleaseVCSPluginType,
    ReleaseHookPluginType,
    BuildSystemPluginType,
    PackageRepositoryPluginType,
    BuildProcessPluginType,
    CommandPluginType
)
import os
import sys
import unittest


class TestPluginManagers(TestBase, TempdirMixin):
    def __init__(self, *nargs, **kwargs):
        TestBase.__init__(self, *nargs, **kwargs)
        self._original_modules = set()
        self.plugin_manager = None

    def setUp(self):
        TestBase.setUp(self)
        uncache_sys_module_paths()

        plugin_manager = RezPluginManager()
        plugin_manager.register_plugin_type(ShellPluginType)
        plugin_manager.register_plugin_type(ReleaseVCSPluginType)
        plugin_manager.register_plugin_type(ReleaseHookPluginType)
        plugin_manager.register_plugin_type(BuildSystemPluginType)
        plugin_manager.register_plugin_type(PackageRepositoryPluginType)
        plugin_manager.register_plugin_type(BuildProcessPluginType)
        plugin_manager.register_plugin_type(CommandPluginType)

        self._original_modules.update(sys.modules.keys())
        self.plugin_manager = plugin_manager

    def tearDown(self):
        TestBase.tearDown(self)
        self.plugin_manager = None

        for key in set(sys.modules.keys()):
            if key not in self._original_modules:
                del sys.modules[key]
        self._original_modules.clear()

    def test_old_loading_style(self):
        """Test loading rez plugin from plugin_path"""
        path = os.path.realpath(os.path.dirname(__file__))
        self.update_settings(dict(
            plugin_path=[os.path.join(path, "data", "extensions", "foo")]
        ))

        cloud_cls = self.plugin_manager.get_plugin_class(
            "package_repository", "cloud")
        self.assertEqual(cloud_cls.name(), "cloud")

    def test_new_loading_style(self):
        """Test loading rez plugin from python modules"""
        path = os.path.realpath(os.path.dirname(__file__))
        with restore_sys_path():
            sys.path.append(os.path.join(path, "data", "extensions"))

            cloud_cls = self.plugin_manager.get_plugin_class(
                "package_repository", "cloud")
            self.assertEqual(cloud_cls.name(), "cloud")

    def test_plugin_override_1(self):
        """Test plugin from plugin_path can override the default"""
        path = os.path.realpath(os.path.dirname(__file__))
        self.update_settings(dict(
            plugin_path=[os.path.join(path, "data", "extensions", "non-mod")]
        ))

        mem_cls = self.plugin_manager.get_plugin_class(
            "package_repository", "memory")
        self.assertEqual("non-mod", mem_cls.on_test)

    def test_plugin_override_2(self):
        """Test plugin from python modules can override the default"""
        path = os.path.realpath(os.path.dirname(__file__))
        with restore_sys_path():
            sys.path.append(os.path.join(path, "data", "extensions"))

            mem_cls = self.plugin_manager.get_plugin_class(
                "package_repository", "memory")
            self.assertEqual("bar", mem_cls.on_test)

    def test_plugin_override_3(self):
        """Test plugin from python modules can override plugin_path"""
        path = os.path.realpath(os.path.dirname(__file__))
        with restore_sys_path():
            # setup new
            sys.path.append(os.path.join(path, "data", "extensions"))
            # setup old
            self.update_settings(dict(
                plugin_path=[os.path.join(path, "data", "extensions", "non-mod")]
            ))

            mem_cls = self.plugin_manager.get_plugin_class(
                "package_repository", "memory")
            self.assertEqual("bar", mem_cls.on_test)


if __name__ == '__main__':
    unittest.main()


@@ -139,247 +139,48 @@
 ort 
-(%0A    uncache_sys_module_paths,%0A    RezP
+p
 lugin
-M
+_m
 anager,
-%0A    ShellPluginType,%0A    ReleaseVCSPluginType,%0A    ReleaseHookPluginType,%0A    BuildSystemPluginType,%0A    PackageRepositoryPluginType,%0A    BuildProcessPluginType,%0A    CommandPluginType%0A)
+ uncache_sys_module_paths
 %0Aimp
@@ -375,118 +375,84 @@
 lf._
-original_modules = set()%0A        self.plugin_manager = None%0A%0A    def setUp(self):%0A        TestBase.setUp(self)
+reset_plugin_manager()%0A%0A    @classmethod%0A    def _reset_plugin_manager(cls):
 %0A   
@@ -503,44 +503,43 @@
 gin_
-manager = RezPluginManager()%0A       
+types = %5B%5D%0A        for singleton in
  plu
@@ -546,32 +546,24 @@
 gin_manager.
-register
 _plugin_type
@@ -566,90 +566,76 @@
 type
-(ShellPluginType)%0A        plugin_manager.register_plugin_type(ReleaseVCSPluginType
+s.values():%0A            plugin_types.append(singleton.instance_class
 )%0A  
@@ -651,32 +651,24 @@
 gin_manager.
-register
 _plugin_type
@@ -671,99 +671,64 @@
 type
-(ReleaseHookPluginType)%0A        plugin_manager.register_plugin_type(BuildSystemP
+s.clear()%0A%0A        for plugin_type in p
 lugin
-Type)%0A
+_types:%0A    
     
@@ -771,45 +771,30 @@
 ype(
-PackageRepositoryP
+p
 lugin
-T
+_t
 ype)%0A
+%0A
         
 plug
@@ -793,235 +793,235 @@
     
-plugin_manager.register_plugin_type(BuildProcessPluginType)%0A        plugin_manager.register_plugin_type(CommandPluginType)%0A%0A        self._original_modules.update(sys.modules.keys())%0A        self.plugin_manager = plugin_manager%0A
+for key in list(sys.modules.keys()):%0A            if key.startswith(%22rezplugins.%22):%0A                del sys.modules%5Bkey%5D%0A%0A    @classmethod%0A    def setUpClass(cls):%0A        cls.settings = %7B%22debug_plugins%22: True%7D%0A%0A    @classmethod
 %0A   
@@ -1033,21 +1033,25 @@
 tearDown
-(self
+Class(cls
 ):%0A     
@@ -1057,109 +1057,55 @@
     
-TestBase.tearDown(self)%0A        self.plugin_manager = None%0A%0A        for key in set(sys.modules.keys()
+cls._reset_plugin_manager()%0A%0A    def setUp(self
 ):%0A 
@@ -1115,122 +1115,62 @@
     
-    if key not in self._original_modules:%0A                del sys.modules%5Bkey%5D%0A        self._original_modules.clea
+TestBase.setUp(self)%0A        self._reset_plugin_manage
 r()%0A
@@ -1455,37 +1455,32 @@
     cloud_cls = 
-self.
 plugin_manager.g
@@ -1871,29 +1871,24 @@
 cloud_cls = 
-self.
 plugin_manag
@@ -2312,37 +2312,32 @@
       mem_cls = 
-self.
 plugin_manager.g
@@ -2737,37 +2737,32 @@
       mem_cls = 
-self.
 plugin_manager.g
@@ -3358,21 +3358,16 @@
 m_cls = 
-self.
 plugin_m

fix test