mirror of
https://github.com/f-droid/fdroidserver.git
synced 2025-09-13 22:42:29 +03:00
throw useful error if a config YAML file is not a dict
This commit is contained in:
parent
00aa595f37
commit
64b8ee772c
2 changed files with 41 additions and 0 deletions
|
@ -399,6 +399,11 @@ def read_config(opts=None):
|
||||||
config = yaml.safe_load(fp)
|
config = yaml.safe_load(fp)
|
||||||
if not config:
|
if not config:
|
||||||
config = {}
|
config = {}
|
||||||
|
if not isinstance(config, dict):
|
||||||
|
msg = _('{path} is not "key: value" dict, but a {datatype}!')
|
||||||
|
raise TypeError(
|
||||||
|
msg.format(path=config_file, datatype=type(config).__name__)
|
||||||
|
)
|
||||||
elif os.path.exists(old_config_file):
|
elif os.path.exists(old_config_file):
|
||||||
logging.warning(_("""{oldfile} is deprecated, use {newfile}""")
|
logging.warning(_("""{oldfile} is deprecated, use {newfile}""")
|
||||||
.format(oldfile=old_config_file, newfile=config_file))
|
.format(oldfile=old_config_file, newfile=config_file))
|
||||||
|
@ -528,6 +533,9 @@ def load_localized_config(name, repodir):
|
||||||
locale = DEFAULT_LOCALE
|
locale = DEFAULT_LOCALE
|
||||||
with open(f, encoding="utf-8") as fp:
|
with open(f, encoding="utf-8") as fp:
|
||||||
elem = yaml.safe_load(fp)
|
elem = yaml.safe_load(fp)
|
||||||
|
if not isinstance(elem, dict):
|
||||||
|
msg = _('{path} is not "key: value" dict, but a {datatype}!')
|
||||||
|
raise TypeError(msg.format(path=f, datatype=type(elem).__name__))
|
||||||
for afname, field_dict in elem.items():
|
for afname, field_dict in elem.items():
|
||||||
if afname not in ret:
|
if afname not in ret:
|
||||||
ret[afname] = dict()
|
ret[afname] = dict()
|
||||||
|
|
|
@ -1919,6 +1919,18 @@ class CommonTest(unittest.TestCase):
|
||||||
config = fdroidserver.common.read_config(fdroidserver.common.options)
|
config = fdroidserver.common.read_config(fdroidserver.common.options)
|
||||||
self.assertEqual(os.getenv('SECRET', 'fail'), config.get('keypass'))
|
self.assertEqual(os.getenv('SECRET', 'fail'), config.get('keypass'))
|
||||||
|
|
||||||
|
def test_with_config_yml_is_dict(self):
|
||||||
|
os.chdir(self.tmpdir)
|
||||||
|
Path('config.yml').write_text('apksigner = /placeholder/path')
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
fdroidserver.common.read_config(fdroidserver.common.options)
|
||||||
|
|
||||||
|
def test_with_config_yml_is_not_mixed_type(self):
|
||||||
|
os.chdir(self.tmpdir)
|
||||||
|
Path('config.yml').write_text('k: v\napksigner = /placeholder/path')
|
||||||
|
with self.assertRaises(yaml.scanner.ScannerError):
|
||||||
|
fdroidserver.common.read_config(fdroidserver.common.options)
|
||||||
|
|
||||||
def test_with_config_py(self):
|
def test_with_config_py(self):
|
||||||
"""Make sure it is still possible to use config.py alone."""
|
"""Make sure it is still possible to use config.py alone."""
|
||||||
os.chdir(self.tmpdir)
|
os.chdir(self.tmpdir)
|
||||||
|
@ -2716,6 +2728,27 @@ class CommonTest(unittest.TestCase):
|
||||||
)
|
)
|
||||||
self.assertEqual(['en-US'], list(categories['GuardianProject']['name'].keys()))
|
self.assertEqual(['en-US'], list(categories['GuardianProject']['name'].keys()))
|
||||||
|
|
||||||
|
def test_load_localized_config_0_file(self):
|
||||||
|
os.chdir(self.testdir)
|
||||||
|
os.mkdir('config')
|
||||||
|
Path('config/categories.yml').write_text('')
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
fdroidserver.common.load_localized_config(CATEGORIES_CONFIG_NAME, 'repo')
|
||||||
|
|
||||||
|
def test_load_localized_config_string(self):
|
||||||
|
os.chdir(self.testdir)
|
||||||
|
os.mkdir('config')
|
||||||
|
Path('config/categories.yml').write_text('this is a string')
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
fdroidserver.common.load_localized_config(CATEGORIES_CONFIG_NAME, 'repo')
|
||||||
|
|
||||||
|
def test_load_localized_config_list(self):
|
||||||
|
os.chdir(self.testdir)
|
||||||
|
os.mkdir('config')
|
||||||
|
Path('config/categories.yml').write_text('- System')
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
fdroidserver.common.load_localized_config(CATEGORIES_CONFIG_NAME, 'repo')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
os.chdir(os.path.dirname(__file__))
|
os.chdir(os.path.dirname(__file__))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue