mirror of
https://gitlab.com/fdroid/fdroidserver.git
synced 2024-11-04 14:30:11 +01:00
lint: get Categories from config
This commit is contained in:
parent
6124caf341
commit
1c3a87e002
@ -222,6 +222,7 @@ versioncode_check_pattern = re.compile(r"(\\d|\[(0-9|\\d)_?(a-fA-F)?])[+]")
|
|||||||
|
|
||||||
ANTIFEATURES_KEYS = None
|
ANTIFEATURES_KEYS = None
|
||||||
ANTIFEATURES_PATTERN = None
|
ANTIFEATURES_PATTERN = None
|
||||||
|
CATEGORIES_KEYS = list()
|
||||||
|
|
||||||
|
|
||||||
def load_antiFeatures_config():
|
def load_antiFeatures_config():
|
||||||
@ -234,6 +235,18 @@ def load_antiFeatures_config():
|
|||||||
ANTIFEATURES_PATTERN = ','.join(ANTIFEATURES_KEYS)
|
ANTIFEATURES_PATTERN = ','.join(ANTIFEATURES_KEYS)
|
||||||
|
|
||||||
|
|
||||||
|
def load_categories_config():
|
||||||
|
"""Lazy loading, since it might read a lot of files."""
|
||||||
|
global CATEGORIES_KEYS
|
||||||
|
k = 'categories'
|
||||||
|
if not CATEGORIES_KEYS:
|
||||||
|
if config and k in config:
|
||||||
|
CATEGORIES_KEYS = config[k]
|
||||||
|
else:
|
||||||
|
config[k] = common.load_localized_config(k, 'repo')
|
||||||
|
CATEGORIES_KEYS = list(config[k].keys())
|
||||||
|
|
||||||
|
|
||||||
def check_regexes(app):
|
def check_regexes(app):
|
||||||
for f, checks in regex_checks.items():
|
for f, checks in regex_checks.items():
|
||||||
for m, r in checks:
|
for m, r in checks:
|
||||||
@ -371,32 +384,10 @@ def check_empty_fields(app):
|
|||||||
yield _("Categories are not set")
|
yield _("Categories are not set")
|
||||||
|
|
||||||
|
|
||||||
all_categories = set(
|
|
||||||
[
|
|
||||||
"Connectivity",
|
|
||||||
"Development",
|
|
||||||
"Games",
|
|
||||||
"Graphics",
|
|
||||||
"Internet",
|
|
||||||
"Money",
|
|
||||||
"Multimedia",
|
|
||||||
"Navigation",
|
|
||||||
"Phone & SMS",
|
|
||||||
"Reading",
|
|
||||||
"Science & Education",
|
|
||||||
"Security",
|
|
||||||
"Sports & Health",
|
|
||||||
"System",
|
|
||||||
"Theming",
|
|
||||||
"Time",
|
|
||||||
"Writing",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def check_categories(app):
|
def check_categories(app):
|
||||||
|
"""App uses 'Categories' key and parsed config uses 'categories' key."""
|
||||||
for categ in app.Categories:
|
for categ in app.Categories:
|
||||||
if categ not in all_categories:
|
if categ not in CATEGORIES_KEYS:
|
||||||
yield _("Categories '%s' is not valid" % categ)
|
yield _("Categories '%s' is not valid" % categ)
|
||||||
|
|
||||||
|
|
||||||
@ -798,6 +789,7 @@ def main():
|
|||||||
|
|
||||||
config = common.read_config(options)
|
config = common.read_config(options)
|
||||||
load_antiFeatures_config()
|
load_antiFeatures_config()
|
||||||
|
load_categories_config()
|
||||||
|
|
||||||
# Get all apps...
|
# Get all apps...
|
||||||
allapps = metadata.read_metadata(options.appid)
|
allapps = metadata.read_metadata(options.appid)
|
||||||
|
@ -10,6 +10,7 @@ import sys
|
|||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from testcommon import mkdtemp
|
||||||
|
|
||||||
localmodule = Path(__file__).resolve().parent.parent
|
localmodule = Path(__file__).resolve().parent.parent
|
||||||
print('localmodule: ' + str(localmodule))
|
print('localmodule: ' + str(localmodule))
|
||||||
@ -30,6 +31,14 @@ class LintTest(unittest.TestCase):
|
|||||||
self.tmpdir = localmodule / '.testfiles'
|
self.tmpdir = localmodule / '.testfiles'
|
||||||
self.tmpdir.mkdir(exist_ok=True)
|
self.tmpdir.mkdir(exist_ok=True)
|
||||||
os.chdir(self.basedir)
|
os.chdir(self.basedir)
|
||||||
|
fdroidserver.common.config = None
|
||||||
|
fdroidserver.lint.config = None
|
||||||
|
fdroidserver.lint.CATEGORIES_KEYS = None
|
||||||
|
self._td = mkdtemp()
|
||||||
|
self.testdir = self._td.name
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self._td.cleanup()
|
||||||
|
|
||||||
def test_check_for_unsupported_metadata_files(self):
|
def test_check_for_unsupported_metadata_files(self):
|
||||||
self.assertTrue(fdroidserver.lint.check_for_unsupported_metadata_files())
|
self.assertTrue(fdroidserver.lint.check_for_unsupported_metadata_files())
|
||||||
@ -313,12 +322,58 @@ class LintTest(unittest.TestCase):
|
|||||||
logging.debug(warn)
|
logging.debug(warn)
|
||||||
self.assertFalse(anywarns)
|
self.assertFalse(anywarns)
|
||||||
|
|
||||||
|
def test_check_categories_in_config(self):
|
||||||
|
fdroidserver.lint.config = {'categories': ['InConfig']}
|
||||||
|
fdroidserver.lint.load_categories_config()
|
||||||
|
app = fdroidserver.metadata.App({'Categories': ['InConfig']})
|
||||||
|
self.assertEqual(0, len(list(fdroidserver.lint.check_categories(app))))
|
||||||
|
|
||||||
|
def test_check_categories_not_in_config(self):
|
||||||
|
fdroidserver.lint.config = dict()
|
||||||
|
fdroidserver.lint.load_categories_config()
|
||||||
|
app = fdroidserver.metadata.App({'Categories': ['NotInConfig']})
|
||||||
|
self.assertEqual(1, len(list(fdroidserver.lint.check_categories(app))))
|
||||||
|
|
||||||
|
def test_check_categories_empty_is_error(self):
|
||||||
|
fdroidserver.lint.config = {'categories': []}
|
||||||
|
fdroidserver.lint.load_categories_config()
|
||||||
|
app = fdroidserver.metadata.App({'Categories': ['something']})
|
||||||
|
self.assertEqual(1, len(list(fdroidserver.lint.check_categories(app))))
|
||||||
|
|
||||||
|
def test_check_categories_old_hardcoded_not_defined(self):
|
||||||
|
fdroidserver.lint.config = {'categories': ['foo', 'bar']}
|
||||||
|
fdroidserver.lint.load_categories_config()
|
||||||
|
app = fdroidserver.metadata.App({'Categories': ['Writing']})
|
||||||
|
self.assertEqual(1, len(list(fdroidserver.lint.check_categories(app))))
|
||||||
|
|
||||||
|
def test_check_categories_from_config_yml(self):
|
||||||
|
"""In config.yml, categories is a list."""
|
||||||
|
os.chdir(self.testdir)
|
||||||
|
Path('config.yml').write_text('categories: [foo, bar]')
|
||||||
|
fdroidserver.lint.config = fdroidserver.common.read_config()
|
||||||
|
fdroidserver.lint.load_categories_config()
|
||||||
|
self.assertEqual(fdroidserver.lint.CATEGORIES_KEYS, ['foo', 'bar'])
|
||||||
|
app = fdroidserver.metadata.App({'Categories': ['bar']})
|
||||||
|
self.assertEqual(0, len(list(fdroidserver.lint.check_categories(app))))
|
||||||
|
|
||||||
|
def test_check_categories_from_config_categories_yml(self):
|
||||||
|
"""In config/categories.yml, categories is a localized STRINGMAP dict."""
|
||||||
|
os.chdir(self.testdir)
|
||||||
|
os.mkdir('config')
|
||||||
|
Path('config/categories.yml').write_text('{foo: {name: foo}, bar: {name: bar}}')
|
||||||
|
fdroidserver.lint.config = fdroidserver.common.read_config()
|
||||||
|
fdroidserver.lint.load_categories_config()
|
||||||
|
self.assertEqual(fdroidserver.lint.CATEGORIES_KEYS, ['foo', 'bar'])
|
||||||
|
app = fdroidserver.metadata.App({'Categories': ['bar']})
|
||||||
|
self.assertEqual(0, len(list(fdroidserver.lint.check_categories(app))))
|
||||||
|
|
||||||
|
|
||||||
class LintAntiFeaturesTest(unittest.TestCase):
|
class LintAntiFeaturesTest(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.basedir = localmodule / 'tests'
|
self.basedir = localmodule / 'tests'
|
||||||
os.chdir(self.basedir)
|
os.chdir(self.basedir)
|
||||||
fdroidserver.common.config = dict()
|
fdroidserver.common.config = dict()
|
||||||
|
fdroidserver.lint.ANTIFEATURES_KEYS = None
|
||||||
fdroidserver.lint.load_antiFeatures_config()
|
fdroidserver.lint.load_antiFeatures_config()
|
||||||
|
|
||||||
def test_check_antiFeatures_empty(self):
|
def test_check_antiFeatures_empty(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user