From fac7ceffe387279a1e19965afcd282939dc3a464 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 24 May 2023 21:54:29 +0200 Subject: [PATCH] metadata: remove non-values from Builds: entries --- fdroidserver/metadata.py | 8 ++++++++ tests/metadata.TestCase | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/fdroidserver/metadata.py b/fdroidserver/metadata.py index 3692e7cb..d41b8fd9 100644 --- a/fdroidserver/metadata.py +++ b/fdroidserver/metadata.py @@ -1077,8 +1077,14 @@ def _format_multiline(value): return str(value) +def _format_list(value): + """TYPE_LIST should not contain null values.""" + return [v for v in value if v] + + def _format_script(value): """TYPE_SCRIPT with one value are converted to YAML string values.""" + value = [v for v in value if v] if len(value) == 1: return value[0] return value @@ -1175,6 +1181,8 @@ def _builds_to_yaml(app): _flagtype = flagtype(field) if _flagtype == TYPE_MULTILINE: v = _format_multiline(v) + elif _flagtype == TYPE_LIST: + v = _format_list(v) elif _flagtype == TYPE_SCRIPT: v = _format_script(v) elif _flagtype == TYPE_STRINGMAP: diff --git a/tests/metadata.TestCase b/tests/metadata.TestCase index f430fede..064c9ce8 100755 --- a/tests/metadata.TestCase +++ b/tests/metadata.TestCase @@ -1943,6 +1943,24 @@ class MetadataTest(unittest.TestCase): '\none\ntwo\nthree\n', ) + def test_format_list_empty(self): + self.assertEqual(metadata._format_list(['', None]), list()) + + def test_format_list_one_empty(self): + self.assertEqual(metadata._format_list(['foo', None]), ['foo']) + + def test_format_list_two(self): + self.assertEqual(metadata._format_list(['2', '1']), ['2', '1']) + + def test_format_list_newline(self): + self.assertEqual(metadata._format_list(['one\ntwo']), ['one\ntwo']) + + def test_format_list_newline_char(self): + self.assertEqual(metadata._format_list(['one\\ntwo']), ['one\\ntwo']) + + def test_format_script_empty(self): + self.assertEqual(metadata._format_script(['', None]), list()) + def test_format_script_newline(self): self.assertEqual(metadata._format_script(['one\ntwo']), 'one\ntwo') @@ -2086,6 +2104,18 @@ class MetadataTest(unittest.TestCase): metadata._builds_to_yaml(app), [{'versionCode': 0, 'gradle': ['false']}] ) + def test_builds_to_yaml_stripped(self): + self.assertEqual( + metadata._builds_to_yaml( + { + 'Builds': [ + metadata.Build({'versionCode': 0, 'rm': [None], 'init': ['']}) + ] + } + ), + [{'versionCode': 0}], + ) + def test_builds_to_yaml(self): """Include one of each flag type with a valid value.""" app = {