|
1 | 1 | #! /usr/bin/env python |
2 | 2 |
|
3 | 3 | # standard |
| 4 | +import os |
| 5 | +import tempfile |
4 | 6 | import unittest |
5 | 7 | from datetime import datetime |
6 | 8 | from pathlib import Path |
|
11 | 13 |
|
12 | 14 |
|
13 | 15 | class TestParameters(unittest.TestCase): |
| 16 | + # -- helpers ---------------------------------------------------------- |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def _minimal_metadata(plugin_dir: Path) -> None: |
| 20 | + """Write a valid metadata.txt with all mandatory fields.""" |
| 21 | + (plugin_dir / "metadata.txt").write_text( |
| 22 | + data="[general]\n" |
| 23 | + "name=Quicui\n" |
| 24 | + "about=Lorem ipsum GIS ergo sum\n" |
| 25 | + "description=Test plugin\n" |
| 26 | + "qgisMinimumVersion=3.44.10\n" |
| 27 | + "author=Former Esri repentant\n" |
| 28 | + "homepage=https://opengisch.github.io/qgis-plugin-ci/\n" |
| 29 | + "tracker=https://github.com/opengisch/qgis-plugin-ci/issues\n" |
| 30 | + "repository=https://github.com/opengisch/qgis-plugin-ci\n", |
| 31 | + encoding="UTF-8", |
| 32 | + ) |
| 33 | + |
14 | 34 | def test_changelog_parameters(self): |
15 | 35 | """Test parameters for changelog command.""" |
16 | 36 | # For the changelog command, the configuration file is optional. |
@@ -46,3 +66,88 @@ def test_plugin_repo_url_from_config(self): |
46 | 66 | self.assertEqual( |
47 | 67 | "https://opengisch.github.io/qgis-plugin-ci/", parameters.plugin_repo_url |
48 | 68 | ) |
| 69 | + |
| 70 | + def test_metadata_txt_ci_section_merged_when_plugin_path_only(self): |
| 71 | + """[tool:qgis-plugin-ci] from metadata.txt is merged when config only defines plugin_path.""" |
| 72 | + original_dir = Path.cwd() |
| 73 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 74 | + try: |
| 75 | + os.chdir(tmp_dir) |
| 76 | + plugin_dir = Path(tmp_dir) / "my_plugin" |
| 77 | + plugin_dir.mkdir() |
| 78 | + self._minimal_metadata(plugin_dir) |
| 79 | + # Append [tool:qgis-plugin-ci] section |
| 80 | + with (plugin_dir / "metadata.txt").open("a") as fh: |
| 81 | + fh.write( |
| 82 | + "\n[tool:qgis-plugin-ci]\n" |
| 83 | + "github_organization_slug=my_org\n" |
| 84 | + "project_slug=my_project\n" |
| 85 | + "timezone=Europe/Paris\n" |
| 86 | + ) |
| 87 | + config_file = Path(tmp_dir) / ".qgis-plugin-ci" |
| 88 | + config_file.write_text("plugin_path: my_plugin\n") |
| 89 | + |
| 90 | + parameters = Parameters.make_from(path_to_config_file=config_file) |
| 91 | + |
| 92 | + self.assertEqual("my_org", parameters.github_organization_slug) |
| 93 | + self.assertEqual("my_project", parameters.project_slug) |
| 94 | + self.assertEqual("Europe/Paris", parameters.timezone) |
| 95 | + finally: |
| 96 | + os.chdir(original_dir) |
| 97 | + |
| 98 | + def test_missing_ci_section_in_metadata_txt_logs_warning(self): |
| 99 | + """A warning is logged when metadata.txt exists but has no [tool:qgis-plugin-ci] section.""" |
| 100 | + original_dir = Path.cwd() |
| 101 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 102 | + try: |
| 103 | + os.chdir(tmp_dir) |
| 104 | + plugin_dir = Path(tmp_dir) / "my_plugin" |
| 105 | + plugin_dir.mkdir() |
| 106 | + self._minimal_metadata(plugin_dir) |
| 107 | + config_file = Path(tmp_dir) / ".qgis-plugin-ci" |
| 108 | + config_file.write_text("plugin_path: my_plugin\n") |
| 109 | + |
| 110 | + with self.assertLogs("qgispluginci.parameters", level="WARNING") as cm: |
| 111 | + Parameters.make_from(path_to_config_file=config_file) |
| 112 | + |
| 113 | + self.assertTrue( |
| 114 | + any("tool:qgis-plugin-ci" in msg for msg in cm.output), |
| 115 | + "Expected a warning mentioning [tool:qgis-plugin-ci]", |
| 116 | + ) |
| 117 | + finally: |
| 118 | + os.chdir(original_dir) |
| 119 | + |
| 120 | + def test_missing_metadata_txt_logs_warning(self): |
| 121 | + """A warning is logged when metadata.txt itself is absent.""" |
| 122 | + original_dir = Path.cwd() |
| 123 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 124 | + try: |
| 125 | + os.chdir(tmp_dir) |
| 126 | + config_file = Path(tmp_dir) / ".qgis-plugin-ci" |
| 127 | + config_file.write_text("plugin_path: ghost_plugin\n") |
| 128 | + |
| 129 | + with self.assertLogs("qgispluginci.parameters", level="WARNING") as cm: |
| 130 | + with self.assertRaises(FileNotFoundError): |
| 131 | + Parameters.make_from(path_to_config_file=config_file) |
| 132 | + |
| 133 | + self.assertTrue( |
| 134 | + any( |
| 135 | + "ghost_plugin" in msg and "metadata.txt" in msg |
| 136 | + for msg in cm.output |
| 137 | + ), |
| 138 | + "Expected a warning mentioning the missing metadata.txt path", |
| 139 | + ) |
| 140 | + finally: |
| 141 | + os.chdir(original_dir) |
| 142 | + |
| 143 | + def test_metadata_txt_not_checked_when_config_has_extra_keys(self): |
| 144 | + """metadata.txt is not consulted when the config file has more than plugin_path.""" |
| 145 | + # Fixture has github_organization_slug in addition to plugin_path → new block must NOT trigger. |
| 146 | + # If it were triggered and merged a metadata.txt section with conflicting values, |
| 147 | + # the assertion below would still pass (config file takes priority), but any warning |
| 148 | + # log would betray the wrong code path. |
| 149 | + with self.assertNoLogs("qgispluginci.parameters", level="WARNING"): |
| 150 | + parameters = Parameters.make_from( |
| 151 | + path_to_config_file=Path("test/fixtures/.qgis-plugin-ci") |
| 152 | + ) |
| 153 | + self.assertEqual("opengisch", parameters.github_organization_slug) |
0 commit comments