diff --git a/fdroidserver/update.py b/fdroidserver/update.py index afde773e..57e7e4b7 100644 --- a/fdroidserver/update.py +++ b/fdroidserver/update.py @@ -768,23 +768,41 @@ def _get_localized_dict(app, locale): def _set_localized_text_entry(app, locale, key, f): - limit = config['char_limits'][key] - localized = _get_localized_dict(app, locale) - with open(f, errors='replace') as fp: - text = fp.read()[:limit] - if len(text) > 0: - if key in ('name', 'summary', 'video'): # hardcoded as a single line - localized[key] = text.strip('\n') - else: - localized[key] = text + """Read a fastlane/triple-t metadata file and add an entry to the app + + This reads more than the limit, in case there is leading or + trailing whitespace to be stripped + + """ + try: + limit = config['char_limits'][key] + localized = _get_localized_dict(app, locale) + with open(f, errors='replace') as fp: + text = fp.read(limit * 2) + if len(text) > 0: + if key in ('name', 'summary', 'video'): # hardcoded as a single line + localized[key] = text.strip('\n')[:limit] + else: + localized[key] = text[:limit] + except Exception as e: + logging.error(_('{path}: {error}').format(path=f, error=str(e))) def _set_author_entry(app, key, f): - limit = config['char_limits']['author'] - with open(f, errors='replace') as fp: - text = fp.read()[:limit] - if len(text) > 0: - app[key] = text.strip() + """read a fastlane/triple-t author file and add the entry to the app + + This reads more than the limit, in case there is leading or + trailing whitespace to be stripped + + """ + try: + limit = config['char_limits']['author'] + with open(f, errors='replace') as fp: + text = fp.read(limit * 2) + if len(text) > 0: + app[key] = text.strip()[:limit] + except Exception as e: + logging.error(_('{path}: {error}').format(path=f, error=str(e))) def _strip_and_copy_image(in_file, outpath): diff --git a/tests/update.TestCase b/tests/update.TestCase index 19b7436e..1c299d74 100755 --- a/tests/update.TestCase +++ b/tests/update.TestCase @@ -11,6 +11,7 @@ import optparse import os import random import shutil +import string import subprocess import sys import tempfile @@ -1309,6 +1310,73 @@ class UpdateTest(unittest.TestCase): self.assertIsNotNone(fdroidserver.update.sanitize_funding_yml_entry(' WhyIncludeWhitespace ')) self.assertIsNotNone(fdroidserver.update.sanitize_funding_yml_entry(['first', 'second'])) + def test_set_localized_text_entry(self): + tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, + dir=self.tmpdir) + os.chdir(tmptestsdir) + config = dict() + fdroidserver.common.fill_config_defaults(config) + fdroidserver.update.config = config + fdroidserver.update.options = fdroidserver.common.options + + files = { + 'full-description.txt': 'description', + 'short-description.txt': 'summary', + 'title.txt': 'name', + 'video-url.txt': 'video', + } + + for f, key in files.items(): + limit = config['char_limits'][key] + with open(f, 'w') as fp: + fp.write(''.join(random.choice(string.ascii_letters) for i in range(limit + 100))) + locale = 'ru_US' + app = dict() + fdroidserver.update._set_localized_text_entry(app, locale, key, f) + self.assertEqual(limit, len(app['localized'][locale][key])) + + f = 'badlink-' + f + os.symlink('/path/to/nowhere', f) + app = dict() + fdroidserver.update._set_localized_text_entry(app, locale, key, f) + self.assertIsNone(app['localized'].get(locale, {}).get(key)) + + def test_set_author_entry(self): + tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, + dir=self.tmpdir) + os.chdir(tmptestsdir) + config = dict() + fdroidserver.common.fill_config_defaults(config) + fdroidserver.update.config = config + fdroidserver.update.options = fdroidserver.common.options + + f = 'contact-website.txt' + key = 'author' + url = 'https://f-droid.org/' + limit = config['char_limits']['author'] + with open(f, 'w') as fp: + fp.write(url) + fp.write('\n') + app = dict() + fdroidserver.update._set_author_entry(app, key, f) + self.assertEqual(url, app[key]) + + f = 'limits.txt' + key = 'author' + limit = config['char_limits']['author'] + for key in ('authorEmail', 'authorPhone', 'authorWebSite'): + with open(f, 'w') as fp: + fp.write(''.join(random.choice(string.ascii_letters) for i in range(limit + 100))) + app = dict() + fdroidserver.update._set_author_entry(app, key, f) + self.assertEqual(limit, len(app[key])) + + f = 'badlink.txt' + os.symlink('/path/to/nowhere', f) + app = dict() + fdroidserver.update._set_author_entry(app, key, f) + self.assertIsNone(app.get(key)) + if __name__ == "__main__": os.chdir(os.path.dirname(__file__))