コード例 #1
0
    def test_pluginpath(self):
        # The COMET_PLUGINPATH environment variable should set the locations
        # set for plugins.
        dummy_plugin = dedent(f"""
        # Comet VOEvent Broker.
        # This is a no-op plugin, used to demonstrate plugin registration.

        from zope.interface import implementer
        from twisted.plugin import IPlugin
        from comet.icomet import IHandler

        # Event handlers must implement IPlugin and IHandler.
        @implementer(IPlugin, IHandler)
        class TestPlugin(object):
            name = "test-plugin-{os.getpid()}"

            # When the handler is called, it is passed an instance of
            # comet.utility.xml.xml_document.
            def __call__(self, event):
                pass

        # This instance of the handler is what actually constitutes our plugin.
        test_plugin = TestPlugin()""")

        # We'll restore the initial environment at the end of the test case.
        init_environ = os.environ.copy()
        init_pluginpath = comet.plugins.__path__.copy()

        initial_plugin_count = len(list(getPlugins(IHandler, comet.plugins)))

        try:
            with TemporaryDirectory() as tempdir:
                with open(os.path.join(tempdir, "plugin.py"), "w") as f:
                    f.write(dummy_plugin)

                os.environ["COMET_PLUGINPATH"] = tempdir

                # Trigger update of comet.plugins.__path__
                BaseOptions()

                # Should now be one more plugin available.
                self.assertEqual(
                    len(list(getPlugins(IHandler, comet.plugins))),
                    initial_plugin_count + 1)

        finally:
            os.environ = init_environ
            comet.plugins.__path__ = init_pluginpath

        # Should be back to the original plugin count.
        self.assertEqual(len(list(getPlugins(IHandler, comet.plugins))),
                         initial_plugin_count)