From 40c77892a26d7aec3c2877908965d8774c4dac1f Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 8 Apr 2021 12:44:45 +0200 Subject: [PATCH] do not crash when config.yml is 0 bytes or empty of data --- fdroidserver/common.py | 2 ++ tests/common.TestCase | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/fdroidserver/common.py b/fdroidserver/common.py index 41a76605..15635923 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -362,6 +362,8 @@ def read_config(opts=None): logging.debug(_("Reading '{config_file}'").format(config_file=config_file)) with open(config_file, encoding='utf-8') as fp: config = yaml.safe_load(fp) + if not config: + config = {} elif os.path.exists(old_config_file): logging.warning(_("""{oldfile} is deprecated, use {newfile}""") .format(oldfile=old_config_file, newfile=config_file)) diff --git a/tests/common.TestCase b/tests/common.TestCase index ebaad78c..83f9e3af 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -1675,6 +1675,17 @@ class CommonTest(unittest.TestCase): self.assertIsNone(config.get('stats_server')) self.assertIsNotNone(config.get('char_limits')) + def test_with_zero_size_config(self): + """It should set defaults if config file has nothing in it""" + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) + os.chdir(testdir) + open('config.yml', 'w').close() + self.assertTrue(os.path.exists('config.yml')) + self.assertFalse(os.path.exists('config.py')) + config = fdroidserver.common.read_config(fdroidserver.common.options) + self.assertIsNone(config.get('stats_server')) + self.assertIsNotNone(config.get('char_limits')) + def test_with_config_yml(self): """Make sure it is possible to use config.yml alone.""" testdir = tempfile.mkdtemp(