diff --git a/fdroidserver/metadata.py b/fdroidserver/metadata.py index 5531d369..b1be88f8 100644 --- a/fdroidserver/metadata.py +++ b/fdroidserver/metadata.py @@ -1159,10 +1159,19 @@ def post_parse_yaml_metadata(yamldata): for flag in build.keys(): _flagtype = flagtype(flag) - # concatenate script flags into a single string if they are stored as list if _flagtype is TYPE_SCRIPT: + # concatenate script flags into a single string if they are stored as list if isinstance(build[flag], list): build[flag] = ' && '.join(build[flag]) + elif _flagtype is TYPE_STRING: + # things like versionNames are strings, but without quotes can be numbers + if isinstance(build[flag], float) or isinstance(build[flag], int): + build[flag] = str(build[flag]) + elif _flagtype is TYPE_INT: + # versionCode must be int + if not isinstance(build[flag], int): + warn_or_exception(_('{build_flag} must be an integer, found: {value}') + .format(build_flag=flag, value=build[flag])) def write_yaml(mf, app): diff --git a/tests/metadata.TestCase b/tests/metadata.TestCase index c2c9aa57..ff0e11d7 100755 --- a/tests/metadata.TestCase +++ b/tests/metadata.TestCase @@ -15,6 +15,7 @@ import unittest import yaml import tempfile import textwrap +from collections import OrderedDict from unittest import mock localmodule = os.path.realpath( @@ -180,6 +181,50 @@ class MetadataTest(unittest.TestCase): self.maxDiff = None self.assertEqual(result.read(), orig.read()) + def test_post_parse_yaml_metadata(self): + fdroidserver.metadata.warnings_action = 'error' + yamldata = OrderedDict() + builds = [] + yamldata['Builds'] = builds + build = OrderedDict() + builds.append(build) + + build['versionCode'] = 1.1 + self.assertRaises(fdroidserver.exception.MetaDataException, + fdroidserver.metadata.post_parse_yaml_metadata, yamldata) + + build['versionCode'] = '1' + self.assertRaises(fdroidserver.exception.MetaDataException, + fdroidserver.metadata.post_parse_yaml_metadata, yamldata) + + build['versionCode'] = 1 + build['versionName'] = 1 + fdroidserver.metadata.post_parse_yaml_metadata(yamldata) + self.assertNotEqual(1, yamldata['Builds'][0]['versionName']) + self.assertEqual('1', yamldata['Builds'][0]['versionName']) + self.assertEqual(1, yamldata['Builds'][0]['versionCode']) + + build['versionName'] = 1.0 + fdroidserver.metadata.post_parse_yaml_metadata(yamldata) + self.assertNotEqual(1.0, yamldata['Builds'][0]['versionName']) + self.assertEqual('1.0', yamldata['Builds'][0]['versionName']) + + build['commit'] = 1.0 + fdroidserver.metadata.post_parse_yaml_metadata(yamldata) + self.assertNotEqual(1.0, yamldata['Builds'][0]['commit']) + self.assertEqual('1.0', yamldata['Builds'][0]['commit']) + + teststr = '98234fab134b' + build['commit'] = teststr + fdroidserver.metadata.post_parse_yaml_metadata(yamldata) + self.assertEqual(teststr, yamldata['Builds'][0]['commit']) + + testcommitid = 1234567890 + build['commit'] = testcommitid + fdroidserver.metadata.post_parse_yaml_metadata(yamldata) + self.assertNotEqual(testcommitid, yamldata['Builds'][0]['commit']) + self.assertEqual('1234567890', yamldata['Builds'][0]['commit']) + def test_read_metadata_sort_by_time(self): testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) metadatadir = os.path.join(testdir, 'metadata')