Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit 320e56f

Browse files
authored
Merge pull request #486 from Xpirix/package_name_validator
Package name validator
2 parents 86c7758 + 80cf619 commit 320e56f

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

qgis-app/plugins/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def clean_package(self):
195195
"""
196196
package = self.cleaned_data.get("package")
197197
try:
198-
self.cleaned_data.update(validator(package))
198+
self.cleaned_data.update(validator(package, is_new=True))
199199
except ValidationError as e:
200200
msg = _(
201201
"There were errors reading plugin package (please check also your plugin's metadata)."

qgis-app/plugins/tests/test_validator.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,31 @@ def test_zipfile_with_gitignore(self, mock_namelist):
210210
"However, there is one present at the root of the archive."
211211
)
212212

213+
class TestValidatorInvalidPackageName(TestCase):
214+
"""Test if plugin's directory is not PEP8 compliant """
215+
216+
def setUp(self) -> None:
217+
invalid_package_name = os.path.join(TESTFILE_DIR, "invalid_package_name.zip_")
218+
self.plugin_package = open(invalid_package_name, "rb")
219+
220+
def tearDown(self):
221+
self.plugin_package.close()
222+
223+
# License file is required
224+
def test_new_plugin_invalid_package_name(self):
225+
self.assertRaises(
226+
ValidationError,
227+
validator,
228+
InMemoryUploadedFile(
229+
self.plugin_package,
230+
field_name="tempfile",
231+
name="testfile.zip",
232+
content_type="application/zip",
233+
size=39889,
234+
charset="utf8",
235+
),
236+
is_new=True
237+
)
213238

214239
class TestLicenseValidator(TestCase):
215240
"""Test if zipfile contains LICENSE file """
44.5 KB
Binary file not shown.

qgis-app/plugins/validator.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def error_check_if_exist(url: str)->bool:
149149
)
150150

151151

152-
def validator(package):
152+
def validator(package, is_new: bool = False):
153153
"""
154154
Analyzes a zipped file, returns metadata if success, False otherwise.
155155
If the new icon metadata is found, an inmemory file object is also returned
@@ -243,6 +243,14 @@ def validator(package):
243243
"Cannot find a folder inside the compressed package: this does not seems a valid plugin"
244244
)
245245
)
246+
# Check if package_name is PEP 8 compliant
247+
if is_new and not re.match(r"^[a-z_][a-z0-9_]*$", package_name):
248+
raise ValidationError(
249+
_(
250+
"The name of the top level directory inside the zip package must be PEP 8 compliant: "
251+
"lowercase with words separated by underscores, and must start with a letter or underscore."
252+
)
253+
)
246254

247255
# Cuts the trailing slash
248256
if package_name.endswith("/"):
File renamed without changes.

0 commit comments

Comments
 (0)