Пример #1
0
    def test_plugin_templates(self) -> None:
        with tempfile.TemporaryDirectory() as plugin_templates:
            # Create plugin
            plugin1 = env.plugins.DictPlugin({
                "name": "plugin1",
                "version": "0",
                "templates": plugin_templates
            })

            # Create two templates
            os.makedirs(os.path.join(plugin_templates, "plugin1", "apps"))
            with open(
                    os.path.join(plugin_templates, "plugin1",
                                 "unrendered.txt"), "w") as f:
                f.write("This file should not be rendered")
            with open(
                    os.path.join(plugin_templates, "plugin1", "apps",
                                 "rendered.txt"), "w") as f:
                f.write("Hello my ID is {{ ID }}")

            # Create configuration
            config: Config = {"ID": "abcd"}

            # Render templates
            with patch.object(
                    env.plugins,
                    "iter_enabled",
                    return_value=[plugin1],
            ):
                with tempfile.TemporaryDirectory() as root:
                    # Render plugin templates
                    env.save_plugin_templates(plugin1, root, config)

                    # Check that plugin template was rendered
                    dst_unrendered = os.path.join(root, "env", "plugins",
                                                  "plugin1", "unrendered.txt")
                    dst_rendered = os.path.join(root, "env", "plugins",
                                                "plugin1", "apps",
                                                "rendered.txt")
                    self.assertFalse(os.path.exists(dst_unrendered))
                    self.assertTrue(os.path.exists(dst_rendered))
                    with open(dst_rendered) as f:
                        self.assertEqual("Hello my ID is abcd", f.read())
Пример #2
0
    def test_plugin_templates(self):
        with tempfile.TemporaryDirectory() as plugin_templates:
            # Create two templates
            os.makedirs(os.path.join(plugin_templates, "plugin1", "apps"))
            with open(
                os.path.join(plugin_templates, "plugin1", "unrendered.txt"), "w"
            ) as f:
                f.write("This file should not be rendered")
            with open(
                os.path.join(plugin_templates, "plugin1", "apps", "rendered.txt"), "w"
            ) as f:
                f.write("Hello my ID is {{ ID }}")

            # Create configuration
            config = {"ID": "abcd"}

            # Create a single plugin
            with unittest.mock.patch.object(
                env.plugins,
                "iter_templates",
                return_value=[("plugin1", plugin_templates)],
            ):
                with tempfile.TemporaryDirectory() as root:
                    # Render plugin templates
                    env.save_plugin_templates("plugin1", plugin_templates, root, config)

                    # Check that plugin template was rendered
                    dst_unrendered = os.path.join(
                        root, "env", "plugins", "plugin1", "unrendered.txt"
                    )
                    dst_rendered = os.path.join(
                        root, "env", "plugins", "plugin1", "apps", "rendered.txt"
                    )
                    self.assertFalse(os.path.exists(dst_unrendered))
                    self.assertTrue(os.path.exists(dst_rendered))
                    with open(dst_rendered) as f:
                        self.assertEqual("Hello my ID is abcd", f.read())