From 784bebfee9eeadb383d4130fb4eed6d1fae46429 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 4 May 2023 17:13:19 +0200 Subject: [PATCH] metadata: keep manually added NoSourceSince in AntiFeatures If the metadata file contains NoSourceSince:, it is added to the collection of Anti-Features. When rewriting the .yml file, NoSourceSince should only be written into the AntiFeatures: collection if there are manual changes, e.g. the user had provided translations. --- fdroidserver/metadata.py | 12 +++++++ tests/metadata.TestCase | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/fdroidserver/metadata.py b/fdroidserver/metadata.py index 0e79ade9..b86dd004 100644 --- a/fdroidserver/metadata.py +++ b/fdroidserver/metadata.py @@ -1033,6 +1033,17 @@ def post_parse_yaml_metadata(yamldata): } +def _del_duplicated_NoSourceSince(app): + # noqa: D403 NoSourceSince is the word. + """NoSourceSince gets auto-added to AntiFeatures, but can also be manually added.""" + key = 'NoSourceSince' + if key in app: + no_source_since = app.get(key) + af_no_source_since = app.get('AntiFeatures', dict()).get(key) + if af_no_source_since == {common.DEFAULT_LOCALE: no_source_since}: + del app['AntiFeatures'][key] + + def write_yaml(mf, app): """Write metadata in yaml format. @@ -1123,6 +1134,7 @@ def write_yaml(mf, app): return builds + _del_duplicated_NoSourceSince(app) yaml_app = _app_to_yaml(app) yaml = ruamel.yaml.YAML() yaml.indent(mapping=4, sequence=4, offset=2) diff --git a/tests/metadata.TestCase b/tests/metadata.TestCase index f04d62a8..a687714f 100755 --- a/tests/metadata.TestCase +++ b/tests/metadata.TestCase @@ -1613,6 +1613,77 @@ class MetadataTest(unittest.TestCase): with self.assertRaises(TypeError): build.ndk_path() + def test_del_duplicated_NoSourceSince(self): + app = { + 'AntiFeatures': {'Ads': {}, 'NoSourceSince': {DEFAULT_LOCALE: '1.0'}}, + 'NoSourceSince': '1.0', + } + metadata._del_duplicated_NoSourceSince(app) + self.assertEqual(app, {'AntiFeatures': {'Ads': {}}, 'NoSourceSince': '1.0'}) + + def test_check_manually_extended_NoSourceSince(self): + app = { + 'AntiFeatures': {'NoSourceSince': {DEFAULT_LOCALE: '1.0', 'de': '1,0'}}, + 'NoSourceSince': '1.0', + } + metadata._del_duplicated_NoSourceSince(app) + self.assertEqual( + app, + { + 'AntiFeatures': {'NoSourceSince': {DEFAULT_LOCALE: '1.0', 'de': '1,0'}}, + 'NoSourceSince': '1.0', + }, + ) + + def test_make_sure_nosourcesince_does_not_get_written(self): + appid = 'com.politedroid' + app = metadata.read_metadata({appid: -1})[appid] + builds = app['Builds'] + app['Builds'] = [copy.deepcopy(builds[0])] + mf = io.StringIO() + metadata.write_yaml(mf, app) + mf.seek(0) + self.maxDiff = None + self.assertEqual( + mf.read(), + textwrap.dedent( + """\ + AntiFeatures: + NonFreeNet: {} + Categories: + - Time + License: GPL-3.0-only + SourceCode: https://github.com/miguelvps/PoliteDroid + IssueTracker: https://github.com/miguelvps/PoliteDroid/issues + + AutoName: Polite Droid + Summary: Calendar tool + Description: Activates silent mode during calendar events. + + RepoType: git + Repo: https://github.com/miguelvps/PoliteDroid.git + + Builds: + - versionName: '1.2' + versionCode: 3 + commit: 6a548e4b19 + target: android-10 + antifeatures: + KnownVuln: {} + UpstreamNonFree: {} + NonFreeAssets: {} + + ArchivePolicy: 4 versions + AutoUpdateMode: Version v%v + UpdateCheckMode: Tags + CurrentVersion: '1.5' + CurrentVersionCode: 6 + + NoSourceSince: '1.5' + """ + ), + ) + class PostMetadataParseTest(unittest.TestCase): """Test the functions that post process the YAML input.