|
| 1 | +#! python3 # noqa E265 |
| 2 | + |
| 3 | +"""Usage from the repo root folder: |
| 4 | +
|
| 5 | +.. code-block:: python |
| 6 | +
|
| 7 | + # for whole test |
| 8 | + python -m unittest tests.test_job_plugins_synchronizer |
| 9 | + # for specific |
| 10 | + python -m unittest tests.test_job_plugins_synchronizer.TestJobPluginsSynchronizer.test_install_plugin_upgrade_mode_delete |
| 11 | +""" |
| 12 | + |
| 13 | +# ############################################################################# |
| 14 | +# ########## Libraries ############# |
| 15 | +# ################################## |
| 16 | + |
| 17 | +# Standard library |
| 18 | +import tempfile |
| 19 | +import unittest |
| 20 | +import zipfile |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +# package |
| 24 | +from qgis_deployment_toolbelt.jobs.job_plugins_synchronizer import ( |
| 25 | + JobPluginsSynchronizer, |
| 26 | +) |
| 27 | +from qgis_deployment_toolbelt.plugins.plugin import QgisPlugin |
| 28 | +from qgis_deployment_toolbelt.profiles.qdt_profile import QdtProfile |
| 29 | + |
| 30 | + |
| 31 | +# ############################################################################# |
| 32 | +# ########## Classes ############### |
| 33 | +# ################################## |
| 34 | + |
| 35 | + |
| 36 | +class TestJobPluginsSynchronizer(unittest.TestCase): |
| 37 | + """Test plugins synchronizer job.""" |
| 38 | + |
| 39 | + # -- Standard methods -------------------------------------------------------- |
| 40 | + @classmethod |
| 41 | + def setUpClass(cls): |
| 42 | + """Executed when module is loaded before any test.""" |
| 43 | + pass |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def tearDownClass(cls): |
| 47 | + """Executed when module is unloaded after all tests.""" |
| 48 | + pass |
| 49 | + |
| 50 | + # -- Helpers ----------------------------------------------------------------- |
| 51 | + @staticmethod |
| 52 | + def _create_fake_plugin_zip(zip_path: Path, folder_name: str) -> None: |
| 53 | + """Create a minimal plugin zip archive with a metadata.txt.""" |
| 54 | + with zipfile.ZipFile(zip_path, "w") as zf: |
| 55 | + zf.writestr( |
| 56 | + f"{folder_name}/metadata.txt", |
| 57 | + "[general]\nname=Test Plugin\nversion=2.0.0\n", |
| 58 | + ) |
| 59 | + zf.writestr(f"{folder_name}/__init__.py", "") |
| 60 | + |
| 61 | + @staticmethod |
| 62 | + def _make_profile_in_tmpdir(tmp_dir: Path, name: str) -> QdtProfile: |
| 63 | + """Create a QdtProfile whose path_in_qgis points into the temp dir.""" |
| 64 | + profile = QdtProfile(name=name) |
| 65 | + profile.os_config.qgis_profiles_path = tmp_dir / "qgis_profiles" |
| 66 | + return profile |
| 67 | + |
| 68 | + # -- Tests ------------------------------------------------------------------- |
| 69 | + def test_install_plugin_upgrade_mode_delete(self): |
| 70 | + """Test that upgrade_mode=delete removes the plugin folder before unzip.""" |
| 71 | + with tempfile.TemporaryDirectory( |
| 72 | + prefix="QDT_test_plugins_sync_upgrade_delete_" |
| 73 | + ) as tmp_dir: |
| 74 | + options = {"action": "create_or_restore"} |
| 75 | + job = JobPluginsSynchronizer(options=options) |
| 76 | + |
| 77 | + profile = self._make_profile_in_tmpdir(Path(tmp_dir), "test_delete") |
| 78 | + plugins_folder = profile.path_in_qgis / "python/plugins" |
| 79 | + plugin_folder = plugins_folder / "test_plugin" |
| 80 | + plugin_folder.mkdir(parents=True, exist_ok=True) |
| 81 | + |
| 82 | + # simulate an old installed plugin with a leftover file |
| 83 | + leftover_file = plugin_folder / "old_leftover.py" |
| 84 | + leftover_file.write_text("# this file should be removed") |
| 85 | + |
| 86 | + # create a fake plugin zip |
| 87 | + zip_path = Path(tmp_dir) / "test_plugin_delete.zip" |
| 88 | + self._create_fake_plugin_zip(zip_path, "test_plugin") |
| 89 | + |
| 90 | + # plugin object with upgrade_mode=delete |
| 91 | + plugin = QgisPlugin.from_dict( |
| 92 | + { |
| 93 | + "name": "Test Plugin", |
| 94 | + "folder_name": "test_plugin", |
| 95 | + "version": "2.0.0", |
| 96 | + "upgrade_mode": "delete", |
| 97 | + } |
| 98 | + ) |
| 99 | + |
| 100 | + # run install |
| 101 | + job.install_plugin_into_profile([(profile, plugin, zip_path)]) |
| 102 | + |
| 103 | + # the leftover file should be gone |
| 104 | + self.assertFalse(leftover_file.exists()) |
| 105 | + # the plugin folder should exist with new files |
| 106 | + self.assertTrue(plugin_folder.is_dir()) |
| 107 | + self.assertTrue((plugin_folder / "metadata.txt").exists()) |
| 108 | + self.assertTrue((plugin_folder / "__init__.py").exists()) |
| 109 | + |
| 110 | + def test_install_plugin_upgrade_mode_keep(self): |
| 111 | + """Test that upgrade_mode=keep preserves existing files in the plugin folder.""" |
| 112 | + with tempfile.TemporaryDirectory( |
| 113 | + prefix="QDT_test_plugins_sync_upgrade_keep_" |
| 114 | + ) as tmp_dir: |
| 115 | + options = {"action": "create_or_restore"} |
| 116 | + job = JobPluginsSynchronizer(options=options) |
| 117 | + |
| 118 | + profile = self._make_profile_in_tmpdir(Path(tmp_dir), "test_keep") |
| 119 | + plugins_folder = profile.path_in_qgis / "python/plugins" |
| 120 | + plugin_folder = plugins_folder / "test_plugin" |
| 121 | + plugin_folder.mkdir(parents=True, exist_ok=True) |
| 122 | + |
| 123 | + # simulate an old installed plugin with a leftover file |
| 124 | + leftover_file = plugin_folder / "old_leftover.py" |
| 125 | + leftover_file.write_text("# this file should remain") |
| 126 | + |
| 127 | + # create a fake plugin zip |
| 128 | + zip_path = Path(tmp_dir) / "test_plugin_keep.zip" |
| 129 | + self._create_fake_plugin_zip(zip_path, "test_plugin") |
| 130 | + |
| 131 | + # plugin object with upgrade_mode=keep (default) |
| 132 | + plugin = QgisPlugin.from_dict( |
| 133 | + { |
| 134 | + "name": "Test Plugin", |
| 135 | + "folder_name": "test_plugin", |
| 136 | + "version": "2.0.0", |
| 137 | + "upgrade_mode": "keep", |
| 138 | + } |
| 139 | + ) |
| 140 | + |
| 141 | + # run install |
| 142 | + job.install_plugin_into_profile([(profile, plugin, zip_path)]) |
| 143 | + |
| 144 | + # the leftover file should still be there |
| 145 | + self.assertTrue(leftover_file.exists()) |
| 146 | + # and the new files should also be present |
| 147 | + self.assertTrue((plugin_folder / "metadata.txt").exists()) |
| 148 | + self.assertTrue((plugin_folder / "__init__.py").exists()) |
| 149 | + |
| 150 | + |
| 151 | +# ############################################################################# |
| 152 | +# ####### Stand-alone run ######## |
| 153 | +# ################################ |
| 154 | +if __name__ == "__main__": |
| 155 | + unittest.main() |
0 commit comments