Skip to content

Commit

Permalink
Merge pull request #33 from Xpirix/package_name_validator
Browse files Browse the repository at this point in the history
Check if new plugin's package_name is PEP 8 compliant
  • Loading branch information
Xpirix authored Nov 15, 2024
2 parents 7540a27 + 59ec11d commit 19049e2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion qgis-app/plugins/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def clean_package(self):
"""
package = self.cleaned_data.get("package")
try:
self.cleaned_data.update(validator(package))
self.cleaned_data.update(validator(package, is_new=True))
except ValidationError as e:
msg = _(
"There were errors reading plugin package (please check also your plugin's metadata)."
Expand Down
27 changes: 27 additions & 0 deletions qgis-app/plugins/tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,33 @@ def test_zipfile_with_gitignore(self, mock_namelist):
)


class TestValidatorInvalidPackageName(TestCase):
"""Test if plugin's directory is not PEP8 compliant """

def setUp(self) -> None:
invalid_package_name = os.path.join(TESTFILE_DIR, "invalid_package_name.zip_")
self.plugin_package = open(invalid_package_name, "rb")

def tearDown(self):
self.plugin_package.close()

# Package name does not match PEP8
def test_new_plugin_invalid_package_name(self):
self.assertRaises(
ValidationError,
validator,
InMemoryUploadedFile(
self.plugin_package,
field_name="tempfile",
name="testfile.zip",
content_type="application/zip",
size=39889,
charset="utf8",
),
is_new=True
)


class TestLicenseValidator(TestCase):
"""Test if zipfile contains LICENSE file """

Expand Down
Binary file not shown.
12 changes: 11 additions & 1 deletion qgis-app/plugins/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def error_check_if_exist(url: str)->bool:
)


def validator(package):
def validator(package, is_new: bool = False):
"""
Analyzes a zipped file, returns metadata if success, False otherwise.
If the new icon metadata is found, an inmemory file object is also returned
Expand All @@ -162,6 +162,7 @@ def validator(package):
* mandatory metadata: ('name', 'description', 'version', 'qgisMinimumVersion', 'author', 'email')
* package_name regexp: [A-Za-z][A-Za-z0-9-_]+
* author regexp: [^/]+
* New plugins package_name is PEP8 compliant
"""
try:
Expand Down Expand Up @@ -243,6 +244,15 @@ def validator(package):
"Cannot find a folder inside the compressed package: this does not seems a valid plugin"
)
)
# Check if package_name is PEP 8 compliant
if is_new and not package_name.isidentifier():
raise ValidationError(
_(
"The name of the top level directory inside the zip package must be PEP 8 compliant: "
"a valid Python identifier, which means it must start with a letter or underscore, "
"and can only contain letters, digits, and underscores."
)
)

# Cuts the trailing slash
if package_name.endswith("/"):
Expand Down

0 comments on commit 19049e2

Please sign in to comment.