Skip to content

Commit

Permalink
Merge pull request #18 from Xpirix/package_name_validator
Browse files Browse the repository at this point in the history
Package name validator
  • Loading branch information
Xpirix authored Nov 14, 2024
2 parents ea5dd31 + 64b7556 commit 27cd80a
Show file tree
Hide file tree
Showing 5 changed files with 35 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
25 changes: 25 additions & 0 deletions qgis-app/plugins/tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,31 @@ def test_zipfile_with_gitignore(self, mock_namelist):
"However, there is one present at the root of the archive."
)

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()

# License file is required
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.
10 changes: 9 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=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 Down Expand Up @@ -243,6 +243,14 @@ 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 re.match(r"^[a-z_][a-z0-9_]*$", package_name):
raise ValidationError(
_(
"The name of the top level directory inside the zip package must be PEP 8 compliant: "
"lowercase with words separated by underscores, and must start with a letter or underscore."
)
)

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

0 comments on commit 27cd80a

Please sign in to comment.