Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package name validator #486

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Xpirix marked this conversation as resolved.
Show resolved Hide resolved
"""
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