1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-11-19 21:30:10 +01:00

Merge branch 'validate-string-int-types' into 'master'

metadata: validate STRING and INT build field types

See merge request fdroid/fdroidserver!752
This commit is contained in:
Marcus 2020-06-03 22:03:37 +00:00
commit 0d609b4b1e
2 changed files with 55 additions and 1 deletions

View File

@ -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):

View File

@ -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')