2019-07-25 02:26:43 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
import tempfile
|
|
|
|
import textwrap
|
2021-06-08 15:31:55 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-05-04 14:51:04 +02:00
|
|
|
from testcommon import TmpCwd, mkdtemp
|
2019-07-25 02:26:43 +02:00
|
|
|
|
2021-06-08 15:31:55 +02:00
|
|
|
localmodule = Path(__file__).resolve().parent.parent
|
|
|
|
print('localmodule: ' + str(localmodule))
|
2019-07-25 02:26:43 +02:00
|
|
|
if localmodule not in sys.path:
|
2021-06-08 15:31:55 +02:00
|
|
|
sys.path.insert(0, str(localmodule))
|
2019-07-25 02:26:43 +02:00
|
|
|
|
2023-05-04 14:51:04 +02:00
|
|
|
from fdroidserver import common, metadata, rewritemeta
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RewriteMetaTest(unittest.TestCase):
|
|
|
|
'''fdroidserver/publish.py'''
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2021-06-08 15:31:55 +02:00
|
|
|
self.basedir = localmodule / 'tests'
|
2023-02-15 20:25:48 +01:00
|
|
|
os.chdir(self.basedir)
|
2023-05-04 14:51:04 +02:00
|
|
|
metadata.warnings_action = 'error'
|
|
|
|
self._td = mkdtemp()
|
|
|
|
self.testdir = self._td.name
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self._td.cleanup()
|
|
|
|
|
|
|
|
def test_remove_blank_flags_from_builds_com_politedroid_3(self):
|
|
|
|
"""Unset fields in Builds: entries should be removed."""
|
|
|
|
appid = 'com.politedroid'
|
|
|
|
app = metadata.read_metadata({appid: -1})[appid]
|
|
|
|
builds = rewritemeta.remove_blank_flags_from_builds(app.get('Builds'))
|
|
|
|
self.assertEqual(
|
|
|
|
builds[0],
|
|
|
|
{
|
|
|
|
'versionName': '1.2',
|
|
|
|
'versionCode': 3,
|
|
|
|
'commit': '6a548e4b19',
|
|
|
|
'target': 'android-10',
|
2023-04-21 10:00:40 +02:00
|
|
|
'antifeatures': {
|
|
|
|
'KnownVuln': {},
|
|
|
|
'UpstreamNonFree': {},
|
|
|
|
'NonFreeAssets': {},
|
|
|
|
},
|
2023-05-04 14:51:04 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_remove_blank_flags_from_builds_com_politedroid_4(self):
|
|
|
|
"""Unset fields in Builds: entries should be removed."""
|
|
|
|
appid = 'com.politedroid'
|
|
|
|
app = metadata.read_metadata({appid: -1})[appid]
|
|
|
|
builds = rewritemeta.remove_blank_flags_from_builds(app.get('Builds'))
|
|
|
|
self.assertEqual(
|
|
|
|
builds[1],
|
|
|
|
{
|
|
|
|
'versionName': '1.3',
|
|
|
|
'versionCode': 4,
|
|
|
|
'commit': 'ad865b57bf3ac59580f38485608a9b1dda4fa7dc',
|
|
|
|
'target': 'android-15',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-04-21 10:00:40 +02:00
|
|
|
def test_remove_blank_flags_from_builds_org_adaway_52(self):
|
|
|
|
"""Unset fields in Builds: entries should be removed."""
|
|
|
|
appid = 'org.adaway'
|
|
|
|
app = metadata.read_metadata({appid: -1})[appid]
|
|
|
|
builds = rewritemeta.remove_blank_flags_from_builds(app.get('Builds'))
|
|
|
|
self.assertEqual(
|
|
|
|
builds[-1],
|
|
|
|
{
|
|
|
|
'buildjni': ['yes'],
|
|
|
|
'commit': 'v3.0',
|
|
|
|
'gradle': ['yes'],
|
|
|
|
'preassemble': ['renameExecutables'],
|
|
|
|
'subdir': 'AdAway',
|
|
|
|
'versionCode': 52,
|
|
|
|
'versionName': '3.0',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-05-04 14:51:04 +02:00
|
|
|
def test_remove_blank_flags_from_builds_no_builds(self):
|
|
|
|
"""Unset fields in Builds: entries should be removed."""
|
|
|
|
self.assertEqual(
|
|
|
|
rewritemeta.remove_blank_flags_from_builds(None),
|
|
|
|
list(),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
rewritemeta.remove_blank_flags_from_builds(dict()),
|
|
|
|
list(),
|
|
|
|
)
|
2023-04-21 10:00:40 +02:00
|
|
|
self.assertEqual(
|
|
|
|
rewritemeta.remove_blank_flags_from_builds(list()),
|
|
|
|
list(),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
rewritemeta.remove_blank_flags_from_builds(set()),
|
|
|
|
list(),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
rewritemeta.remove_blank_flags_from_builds(tuple()),
|
|
|
|
list(),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_remove_blank_flags_from_builds_0_is_a_value(self):
|
|
|
|
self.assertEqual(
|
|
|
|
rewritemeta.remove_blank_flags_from_builds([{'versionCode': 0}]),
|
|
|
|
[{'versionCode': 0}],
|
|
|
|
)
|
2023-05-04 14:51:04 +02:00
|
|
|
|
2023-05-23 12:07:56 +02:00
|
|
|
def test_remove_blank_flags_from_builds_values_to_purge(self):
|
|
|
|
self.assertEqual(
|
|
|
|
rewritemeta.remove_blank_flags_from_builds(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'antifeatures': dict(),
|
|
|
|
'forceversion': False,
|
|
|
|
'init': None,
|
|
|
|
'rm': '',
|
|
|
|
'scandelete': list(),
|
|
|
|
'versionCode': 0,
|
|
|
|
},
|
|
|
|
{'antifeatures': list(), 'versionCode': 1},
|
|
|
|
{'antifeatures': '', 'versionCode': 2},
|
|
|
|
]
|
|
|
|
),
|
|
|
|
[{'versionCode': 0}, {'versionCode': 1}, {'versionCode': 2}],
|
|
|
|
)
|
|
|
|
|
2023-05-04 14:51:04 +02:00
|
|
|
def test_rewrite_no_builds(self):
|
|
|
|
os.chdir(self.testdir)
|
|
|
|
Path('metadata').mkdir()
|
|
|
|
with Path('metadata/a.yml').open('w') as f:
|
|
|
|
f.write('AutoName: a')
|
|
|
|
rewritemeta.main()
|
|
|
|
self.assertEqual(
|
|
|
|
Path('metadata/a.yml').read_text(encoding='utf-8'),
|
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
|
|
|
License: Unknown
|
|
|
|
|
|
|
|
AutoName: a
|
|
|
|
|
|
|
|
AutoUpdateMode: None
|
|
|
|
UpdateCheckMode: None
|
|
|
|
'''
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_rewrite_empty_build_field(self):
|
|
|
|
os.chdir(self.testdir)
|
|
|
|
Path('metadata').mkdir()
|
|
|
|
with Path('metadata/a.yml').open('w') as fp:
|
|
|
|
fp.write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
|
|
|
License: Apache-2.0
|
|
|
|
Builds:
|
|
|
|
- versionCode: 4
|
|
|
|
versionName: a
|
|
|
|
rm:
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
rewritemeta.main()
|
|
|
|
self.assertEqual(
|
|
|
|
Path('metadata/a.yml').read_text(encoding='utf-8'),
|
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
|
|
|
License: Apache-2.0
|
|
|
|
|
|
|
|
Builds:
|
|
|
|
- versionName: a
|
|
|
|
versionCode: 4
|
|
|
|
|
|
|
|
AutoUpdateMode: None
|
|
|
|
UpdateCheckMode: None
|
|
|
|
'''
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_remove_blank_flags_from_builds_app_with_special_build_params(self):
|
|
|
|
appid = 'app.with.special.build.params'
|
|
|
|
app = metadata.read_metadata({appid: -1})[appid]
|
|
|
|
builds = rewritemeta.remove_blank_flags_from_builds(app.get('Builds'))
|
|
|
|
self.assertEqual(
|
|
|
|
builds[-1],
|
|
|
|
{
|
|
|
|
'versionName': '2.1.2',
|
|
|
|
'versionCode': 51,
|
|
|
|
'disable': 'Labelled as pre-release, so skipped',
|
|
|
|
},
|
|
|
|
)
|
2019-07-25 02:26:43 +02:00
|
|
|
|
2023-05-08 22:03:25 +02:00
|
|
|
def test_remove_blank_flags_from_builds_app_with_special_build_params_af(self):
|
|
|
|
"""Unset fields in Builds: entries should be removed."""
|
|
|
|
appid = 'app.with.special.build.params'
|
|
|
|
app = metadata.read_metadata({appid: -1})[appid]
|
|
|
|
builds = rewritemeta.remove_blank_flags_from_builds(app.get('Builds'))
|
|
|
|
self.assertEqual(
|
|
|
|
builds[-2],
|
|
|
|
{
|
|
|
|
'antifeatures': {
|
|
|
|
'Ads': {'en-US': 'includes ad lib\n', 'zh-CN': '包括广告图书馆\n'},
|
|
|
|
'Tracking': {'en-US': 'standard suspects\n'},
|
|
|
|
},
|
|
|
|
'commit': '2.1.1',
|
|
|
|
'maven': '2',
|
|
|
|
'patch': [
|
|
|
|
'manifest-ads.patch',
|
|
|
|
'mobilecore.patch',
|
|
|
|
],
|
|
|
|
'srclibs': ['FacebookSDK@sdk-version-3.0.2'],
|
|
|
|
'versionCode': 50,
|
|
|
|
'versionName': '2.1.1-c',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-07-25 02:26:43 +02:00
|
|
|
def test_rewrite_scenario_trivial(self):
|
2019-07-26 14:07:22 +02:00
|
|
|
sys.argv = ['rewritemeta', 'a', 'b']
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
|
2021-06-08 15:31:55 +02:00
|
|
|
Path('metadata').mkdir()
|
|
|
|
with Path('metadata/a.yml').open('w') as f:
|
2020-07-02 00:48:03 +02:00
|
|
|
f.write('AutoName: a')
|
2021-06-08 15:31:55 +02:00
|
|
|
with Path('metadata/b.yml').open('w') as f:
|
2019-07-25 02:26:43 +02:00
|
|
|
f.write('AutoName: b')
|
|
|
|
|
|
|
|
rewritemeta.main()
|
|
|
|
|
2021-06-08 15:31:55 +02:00
|
|
|
self.assertEqual(
|
metadata: always open metadata files as UTF-8
Windows seems to require this, otherwise this happens:
Traceback (most recent call last):
File "tests/update.TestCase", line 737, in test_translate_per_build_anti_features
apps = fdroidserver.metadata.read_metadata(xref=True)
File "C:\Users\travis\build\fdroidtravis\fdroidserver\fdroidserver\metadata.py", line 813, in read_metadata
app = parse_metadata(metadatapath, appid in check_vcs, refresh)
File "C:\Users\travis\build\fdroidtravis\fdroidserver\fdroidserver\metadata.py", line 1023, in parse_metadata
parse_yaml_metadata(mf, app)
File "C:\Users\travis\build\fdroidtravis\fdroidserver\fdroidserver\metadata.py", line 1073, in parse_yaml_metadata
yamldata = yaml.safe_load(mf)
File "C:\python37\lib\site-packages\yaml\__init__.py", line 162, in safe_load
return load(stream, SafeLoader)
File "C:\python37\lib\site-packages\yaml\__init__.py", line 112, in load
loader = Loader(stream)
File "C:\python37\lib\site-packages\yaml\loader.py", line 34, in __init__
Reader.__init__(self, stream)
File "C:\python37\lib\site-packages\yaml\reader.py", line 85, in __init__
self.determine_encoding()
File "C:\python37\lib\site-packages\yaml\reader.py", line 124, in determine_encoding
self.update_raw()
File "C:\python37\lib\site-packages\yaml\reader.py", line 178, in update_raw
data = self.stream.read(size)
File "C:\python37\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 37: character maps to <undefined>
2019-09-25 13:34:11 +02:00
|
|
|
Path('metadata/a.yml').read_text(encoding='utf-8'),
|
2021-06-08 15:31:55 +02:00
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
2019-07-25 02:26:43 +02:00
|
|
|
License: Unknown
|
|
|
|
|
2020-07-02 00:48:03 +02:00
|
|
|
AutoName: a
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
AutoUpdateMode: None
|
|
|
|
UpdateCheckMode: None
|
2021-06-07 11:49:21 +02:00
|
|
|
'''
|
2021-06-08 15:31:55 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
metadata: always open metadata files as UTF-8
Windows seems to require this, otherwise this happens:
Traceback (most recent call last):
File "tests/update.TestCase", line 737, in test_translate_per_build_anti_features
apps = fdroidserver.metadata.read_metadata(xref=True)
File "C:\Users\travis\build\fdroidtravis\fdroidserver\fdroidserver\metadata.py", line 813, in read_metadata
app = parse_metadata(metadatapath, appid in check_vcs, refresh)
File "C:\Users\travis\build\fdroidtravis\fdroidserver\fdroidserver\metadata.py", line 1023, in parse_metadata
parse_yaml_metadata(mf, app)
File "C:\Users\travis\build\fdroidtravis\fdroidserver\fdroidserver\metadata.py", line 1073, in parse_yaml_metadata
yamldata = yaml.safe_load(mf)
File "C:\python37\lib\site-packages\yaml\__init__.py", line 162, in safe_load
return load(stream, SafeLoader)
File "C:\python37\lib\site-packages\yaml\__init__.py", line 112, in load
loader = Loader(stream)
File "C:\python37\lib\site-packages\yaml\loader.py", line 34, in __init__
Reader.__init__(self, stream)
File "C:\python37\lib\site-packages\yaml\reader.py", line 85, in __init__
self.determine_encoding()
File "C:\python37\lib\site-packages\yaml\reader.py", line 124, in determine_encoding
self.update_raw()
File "C:\python37\lib\site-packages\yaml\reader.py", line 178, in update_raw
data = self.stream.read(size)
File "C:\python37\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 37: character maps to <undefined>
2019-09-25 13:34:11 +02:00
|
|
|
Path('metadata/b.yml').read_text(encoding='utf-8'),
|
2021-06-08 15:31:55 +02:00
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
2019-07-25 02:26:43 +02:00
|
|
|
License: Unknown
|
|
|
|
|
2020-07-02 00:48:03 +02:00
|
|
|
AutoName: b
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
AutoUpdateMode: None
|
|
|
|
UpdateCheckMode: None
|
2021-06-07 11:49:21 +02:00
|
|
|
'''
|
2021-06-08 15:31:55 +02:00
|
|
|
),
|
|
|
|
)
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = optparse.OptionParser()
|
2021-06-07 11:49:21 +02:00
|
|
|
parser.add_option(
|
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Spew out even more information than normal",
|
|
|
|
)
|
2019-07-25 02:26:43 +02:00
|
|
|
(common.options, args) = parser.parse_args(['--verbose'])
|
|
|
|
|
|
|
|
newSuite = unittest.TestSuite()
|
|
|
|
newSuite.addTest(unittest.makeSuite(RewriteMetaTest))
|
|
|
|
unittest.main(failfast=False)
|