From 7211e9f9b43433154231ce25ef7f724ed9933fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20P=C3=B6hn?= Date: Wed, 20 Dec 2023 04:11:05 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=B2=20add=20unit=20test=20for=20update?= =?UTF-8?q?.scan=5Frepo=5Ffor=5Fipas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fdroidserver/update.py | 10 ++++----- tests/update.TestCase | 49 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/fdroidserver/update.py b/fdroidserver/update.py index b5b956a2..26e248d5 100644 --- a/fdroidserver/update.py +++ b/fdroidserver/update.py @@ -49,10 +49,10 @@ from binascii import hexlify from . import _ from . import common -from . import index from . import metadata from .common import DEFAULT_LOCALE from .exception import BuildException, FDroidException, VerificationException +import fdroidserver.index from PIL import Image, PngImagePlugin @@ -599,7 +599,7 @@ def scan_repo_for_ipas(apkcache, repodir, knownapks): ipa = apkcache.get(ipa_name, {}) if ipa.get('hash') != sha256: - ipa = parse_ipa(ipa_path, file_size, sha256) + ipa = fdroidserver.update.parse_ipa(ipa_path, file_size, sha256) apkcache[ipa_name] = ipa cachechanged = True @@ -2433,7 +2433,7 @@ def main(): if len(repodirs) > 1: archive_old_apks(apps, apks, archapks, repodirs[0], repodirs[1], config['archive_older']) archived_apps = prepare_apps(apps, archapks, repodirs[1]) - index.make(archived_apps, archapks, repodirs[1], True) + fdroidserver.index.make(archived_apps, archapks, repodirs[1], True) repoapps = prepare_apps(apps, apks, repodirs[0]) @@ -2446,13 +2446,13 @@ def main(): app_dict = dict() app_dict[appid] = app if os.path.isdir(repodir): - index.make(app_dict, apks, repodir, False) + fdroidserver.index.make(app_dict, apks, repodir, False) else: logging.info(_('Skipping index generation for {appid}').format(appid=appid)) return # Make the index for the main repo... - index.make(repoapps, apks, repodirs[0], False) + fdroidserver.index.make(repoapps, apks, repodirs[0], False) git_remote = config.get('binary_transparency_remote') if git_remote or os.path.isdir(os.path.join('binary_transparency', '.git')): diff --git a/tests/update.TestCase b/tests/update.TestCase index 9d1dea23..d8f278b6 100755 --- a/tests/update.TestCase +++ b/tests/update.TestCase @@ -1958,6 +1958,54 @@ class TestUpdateVersionStringToInt(unittest.TestCase): fdroidserver.update.version_string_to_int("0.0.0x3") +class TestScanRepoForIpas(unittest.TestCase): + + def setUp(self): + self.maxDiff = None + + def test_scan_repo_for_ipas_no_cache(self): + self.maxDiff = None + with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir): + os.mkdir("repo") + with open('repo/abc.Def_123.ipa', 'w') as f: + f.write('abc') + with open('repo/xyz.XXX_123.ipa', 'w') as f: + f.write('xyz') + + apkcache = mock.MagicMock() + # apkcache['a'] = 1 + repodir = "repo" + knownapks = mock.MagicMock() + + def mocked_parse(p, s, c): + return { + 'packageName': 'abc' if 'abc' in p else 'xyz' + } + + with mock.patch('fdroidserver.update.parse_ipa', mocked_parse): + ipas, checkchanged = fdroidserver.update.scan_repo_for_ipas(apkcache, repodir, knownapks) + + self.assertEqual(checkchanged, True) + self.assertEqual(len(ipas), 2) + self.assertEqual(ipas[0]['packageName'], 'xyz') + self.assertEqual(ipas[1]['packageName'], 'abc') + + self.assertEqual(apkcache.__setitem__.mock_calls[0].args[1]['packageName'], 'xyz') + self.assertEqual(apkcache.__setitem__.mock_calls[1].args[1]['packageName'], 'abc') + self.assertEqual(apkcache.__setitem__.call_count, 2) + + knownapks.recordapk.call_count = 2 + self.assertEqual( + knownapks.recordapk.mock_calls[0], + unittest.mock.call('xyz.XXX_123.ipa', 'xyz'), + ) + # skipping one call here, because accessing `if added:` shows up in mock_calls + self.assertEqual( + knownapks.recordapk.mock_calls[2], + unittest.mock.call('abc.Def_123.ipa', 'abc'), + ) + + if __name__ == "__main__": os.chdir(os.path.dirname(__file__)) @@ -1974,4 +2022,5 @@ if __name__ == "__main__": newSuite = unittest.TestSuite() newSuite.addTest(unittest.makeSuite(UpdateTest)) newSuite.addTest(unittest.makeSuite(TestUpdateVersionStringToInt)) + newSuite.addTest(unittest.makeSuite(TestScanRepoForIpas)) unittest.main(failfast=False)