6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-12-12 14:17:46 +00:00

More robust plugin/env config checking

This commit is contained in:
Régis Behmo 2019-07-10 16:20:43 +08:00
parent 7fbba104a1
commit 1e995aaf1a
4 changed files with 25 additions and 2 deletions

View File

@ -123,3 +123,14 @@ class EnvTests(unittest.TestCase):
self.assertTrue(os.path.exists(dst_rendered)) self.assertTrue(os.path.exists(dst_rendered))
with open(dst_rendered) as f: with open(dst_rendered) as f:
self.assertEqual("Hello my ID is abcd", f.read()) self.assertEqual("Hello my ID is abcd", f.read())
def test_renderer_is_reset_on_config_change(self):
config = {"PLUGINS": []}
env1 = env.Renderer.environment(config)
config["PLUGINS"].append("minio")
env2 = env.Renderer.environment(config)
self.assertNotIn(
"minio/hooks/mino-client/pre-init", env1.loader.list_templates()
)
self.assertIn("minio/hooks/minio-client/pre-init", env2.loader.list_templates())

View File

@ -189,3 +189,11 @@ class PluginsTests(unittest.TestCase):
self.assertEqual( self.assertEqual(
[("plugin1", "/tmp/templates")], list(plugins.iter_templates({})) [("plugin1", "/tmp/templates")], list(plugins.iter_templates({}))
) )
def test_plugins_are_updated_on_config_change(self):
config = {"PLUGINS": []}
instance1 = plugins.Plugins(config)
self.assertEqual(0, len(list(instance1.iter_enabled())))
config["PLUGINS"].append("minio")
instance2 = plugins.Plugins(config)
self.assertEqual(1, len(list(instance2.iter_enabled())))

View File

@ -1,4 +1,5 @@
import codecs import codecs
from copy import deepcopy
import os import os
import shutil import shutil
@ -22,7 +23,6 @@ class Renderer:
@classmethod @classmethod
def environment(cls, config): def environment(cls, config):
if cls.ENVIRONMENT_CONFIG != config: if cls.ENVIRONMENT_CONFIG != config:
cls.ENVIRONMENT_CONFIG = config
template_roots = [TEMPLATES_ROOT] template_roots = [TEMPLATES_ROOT]
for _, plugin_templates in plugins.iter_templates(config): for _, plugin_templates in plugins.iter_templates(config):
template_roots.append(plugin_templates) template_roots.append(plugin_templates)
@ -35,6 +35,8 @@ class Renderer:
environment.filters["reverse_host"] = utils.reverse_host environment.filters["reverse_host"] = utils.reverse_host
environment.filters["walk_templates"] = walk_templates environment.filters["walk_templates"] = walk_templates
environment.globals["TUTOR_VERSION"] = __version__ environment.globals["TUTOR_VERSION"] = __version__
cls.ENVIRONMENT_CONFIG = deepcopy(config)
cls.ENVIRONMENT = environment cls.ENVIRONMENT = environment
return cls.ENVIRONMENT return cls.ENVIRONMENT
@ -42,6 +44,7 @@ class Renderer:
@classmethod @classmethod
def reset(cls): def reset(cls):
cls.ENVIRONMENT = None cls.ENVIRONMENT = None
cls.ENVIRONMENT_CONFIG = None
@classmethod @classmethod
def render_str(cls, config, text): def render_str(cls, config, text):

View File

@ -1,3 +1,4 @@
from copy import deepcopy
import pkg_resources import pkg_resources
from . import exceptions from . import exceptions
@ -42,7 +43,7 @@ class Plugins:
EXTRA_INSTALLED = {} EXTRA_INSTALLED = {}
def __init__(self, config): def __init__(self, config):
self.config = config self.config = deepcopy(config)
self.patches = {} self.patches = {}
self.hooks = {} self.hooks = {}
self.templates = {} self.templates = {}