Extra validation of the uploaded ZIP #73

Merged
Anna Sirota merged 13 commits from validation-single-theme-xml into main 2024-04-11 12:32:50 +02:00
2 changed files with 10 additions and 12 deletions
Showing only changes of commit 712388bfb2 - Show all commits

View File

@ -1,6 +1,6 @@
from django.test import TestCase from django.test import TestCase
from files.utils import find_path_by_name, find_full_path, filter_paths_by_ext from files.utils import find_path_by_name, find_exact_path, filter_paths_by_ext
class UtilsTest(TestCase): class UtilsTest(TestCase):
@ -50,7 +50,7 @@ class UtilsTest(TestCase):
manifest_file = find_path_by_name(name_list, self.manifest) manifest_file = find_path_by_name(name_list, self.manifest)
self.assertEqual(manifest_file, None) self.assertEqual(manifest_file, None)
def test_find_full_path_found(self): def test_find_exact_path_found(self):
name_list = [ name_list = [
'foobar-1.0.3/theme.xml', 'foobar-1.0.3/theme.xml',
'foobar-1.0.3/theme1.xml', 'foobar-1.0.3/theme1.xml',
@ -60,10 +60,10 @@ class UtilsTest(TestCase):
'foobar-1.0.3/foobar-1.0.3/__init__.py', 'foobar-1.0.3/foobar-1.0.3/__init__.py',
'blender_manifest.toml', 'blender_manifest.toml',
] ]
path = find_full_path(name_list, 'foobar-1.0.3', '__init__.py') path = find_exact_path(name_list, 'foobar-1.0.3/__init__.py')
self.assertEqual(path, 'foobar-1.0.3/__init__.py') self.assertEqual(path, 'foobar-1.0.3/__init__.py')
def test_find_full_path_nothing_found(self): def test_find_exact_path_nothing_found(self):
name_list = [ name_list = [
'foobar-1.0.3/theme.xml', 'foobar-1.0.3/theme.xml',
'foobar-1.0.3/theme1.xml', 'foobar-1.0.3/theme1.xml',
@ -72,7 +72,7 @@ class UtilsTest(TestCase):
'foobar-1.0.3/foobar-1.0.3/__init__.py', 'foobar-1.0.3/foobar-1.0.3/__init__.py',
'blender_manifest.toml', 'blender_manifest.toml',
] ]
path = find_full_path(name_list, 'foobar-1.0.3', '__init__.py') path = find_exact_path(name_list, 'foobar-1.0.3/__init__.py')
self.assertIsNone(path) self.assertIsNone(path)
def test_filter_paths_by_ext_found(self): def test_filter_paths_by_ext_found(self):

View File

@ -61,11 +61,9 @@ def find_path_by_name(paths: typing.List[str], name: str) -> typing.Optional[str
return None return None
def find_full_path( def find_exact_path(paths: typing.List[str], exact_path: str) -> typing.Optional[str]:
paths: typing.List[str], *full_path_components: typing.List[str] """Return a first path equal to a given one if it exists in a given list of paths."""
) -> typing.Optional[str]: matching_paths = (path for path in paths if path == exact_path)
"""Return a path equal given path components if it exists in a given list of paths."""
matching_paths = (path for path in paths if path == os.path.join(*full_path_components))
return next(matching_paths, None) return next(matching_paths, None)
@ -128,8 +126,8 @@ def read_manifest_from_zip(archive_path):
error_codes.append('invalid_theme_multiple_xmls') error_codes.append('invalid_theme_multiple_xmls')
elif type_slug == 'add-on': elif type_slug == 'add-on':
# __init__.py is expected to be next to the manifest # __init__.py is expected to be next to the manifest
init_directory = os.path.dirname(manifest_filepath) expected_init_path = os.path.join(os.path.dirname(manifest_filepath), '__init__.py')
init_filepath = find_full_path(file_list, init_directory, '__init__.py') init_filepath = find_exact_path(file_list, expected_init_path)
if not init_filepath: if not init_filepath:
error_codes.append('invalid_missing_init') error_codes.append('invalid_missing_init')