From debac3fd616a8d715a9dc1b95fa4cb8f57e49484 Mon Sep 17 00:00:00 2001 From: linsui <2873532-linsui@users.noreply.gitlab.com> Date: Thu, 5 Sep 2024 02:09:55 +0800 Subject: [PATCH 1/2] lint.py: format --- fdroidserver/lint.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/fdroidserver/lint.py b/fdroidserver/lint.py index 1ae42852..77fe21f5 100644 --- a/fdroidserver/lint.py +++ b/fdroidserver/lint.py @@ -16,19 +16,17 @@ # You should have received a copy of the GNU Affero General Public Licen # along with this program. If not, see . -from argparse import ArgumentParser import difflib +import platform import re import sys -import platform -import ruamel.yaml import urllib.parse +from argparse import ArgumentParser from pathlib import Path -from . import _ -from . import common -from . import metadata -from . import rewritemeta +import ruamel.yaml + +from . import _, common, metadata, rewritemeta config = None @@ -588,7 +586,7 @@ def check_app_field_types(app): fieldtype=v.__class__.__name__, ) ) - elif t == metadata.TYPE_STRING and not type(v) in (str, bool, dict): + elif t == metadata.TYPE_STRING and type(v) not in (str, bool, dict): yield ( _( "{appid}: {field} must be a '{type}', but it is a '{fieldtype}'!" From 0ad2820b6f0e0c0a77c3ab9d09e10d82f99a2d98 Mon Sep 17 00:00:00 2001 From: linsui <2873532-linsui@users.noreply.gitlab.com> Date: Thu, 5 Sep 2024 02:10:08 +0800 Subject: [PATCH 2/2] lint: check config keys --- fdroidserver/lint.py | 36 +++++++++++++++++++++++++++++++++++- tests/lint.TestCase | 18 ++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/fdroidserver/lint.py b/fdroidserver/lint.py index 77fe21f5..c4568dcb 100644 --- a/fdroidserver/lint.py +++ b/fdroidserver/lint.py @@ -749,6 +749,12 @@ def check_certificate_pinned_binaries(app): def lint_config(arg): path = Path(arg) passed = True + + mirrors_name = f'{common.MIRRORS_CONFIG_NAME}.yml' + config_name = f'{common.CONFIG_CONFIG_NAME}.yml' + categories_name = f'{common.CATEGORIES_CONFIG_NAME}.yml' + antifeatures_name = f'{common.ANTIFEATURES_CONFIG_NAME}.yml' + yamllintresult = common.run_yamllint(path) if yamllintresult: print(yamllintresult) @@ -758,7 +764,7 @@ def lint_config(arg): data = ruamel.yaml.YAML(typ='safe').load(fp) common.config_type_check(arg, data) - if path.name == 'mirrors.yml': + if path.name == mirrors_name: import pycountry valid_country_codes = [c.alpha_2 for c in pycountry.countries] @@ -779,6 +785,34 @@ def lint_config(arg): msg += ' ' msg += _('Did you mean {code}?').format(code=', '.join(sorted(m))) print(msg) + elif path.name in (config_name, categories_name, antifeatures_name): + for key in data: + if path.name == config_name and key not in ('archive', 'repo'): + passed = False + print( + _('ERROR: {key} in {path} is not "archive" or "repo"!').format( + key=key, path=path + ) + ) + allowed_keys = ['name'] + if path.name in [config_name, antifeatures_name]: + allowed_keys.append('description') + # only for source strings currently + if path.parent.name == 'config': + allowed_keys.append('icon') + for subkey in data[key]: + if subkey not in allowed_keys: + passed = False + print( + _( + 'ERROR: {key}:{subkey} in {path} is not in allowed keys: {allowed_keys}!' + ).format( + key=key, + subkey=subkey, + path=path, + allowed_keys=', '.join(allowed_keys), + ) + ) return passed diff --git a/tests/lint.TestCase b/tests/lint.TestCase index 5937aadf..409c807c 100755 --- a/tests/lint.TestCase +++ b/tests/lint.TestCase @@ -4,23 +4,25 @@ import logging import os -import ruamel.yaml import shutil import sys import tempfile import unittest from pathlib import Path +import ruamel.yaml + localmodule = Path(__file__).resolve().parent.parent print('localmodule: ' + str(localmodule)) if localmodule not in sys.path: sys.path.insert(0, str(localmodule)) +from testcommon import mkdtemp, parse_args_for_test + import fdroidserver.common import fdroidserver.lint import fdroidserver.metadata from fdroidserver.common import CATEGORIES_CONFIG_NAME -from testcommon import mkdtemp, parse_args_for_test class LintTest(unittest.TestCase): @@ -437,6 +439,18 @@ class LintTest(unittest.TestCase): with self.assertRaises(TypeError): fdroidserver.lint.lint_config('mirrors.yml') + def test_lint_invalid_config_keys(self): + os.chdir(self.testdir) + Path('config').mkdir() + Path('config/config.yml').write_text('repo:\n invalid_key: test') + self.assertFalse(fdroidserver.lint.lint_config('config/config.yml')) + + def test_lint_invalid_localized_config_keys(self): + os.chdir(self.testdir) + Path('config/en').mkdir(parents=True) + Path('config/en/antiFeatures.yml').write_text('NonFreeNet:\n icon: test.png') + self.assertFalse(fdroidserver.lint.lint_config('config/en/antiFeatures.yml')) + def test_check_certificate_pinned_binaries_empty(self): fdroidserver.common.config = {} app = fdroidserver.metadata.App()