🪀 tests for insert_localized_ios_app_metadata

Split some functions from insert_localized_ios_app_metadata into
sub-functions and implemented separate tests for each.
This commit is contained in:
Michael Pöhn 2024-03-19 23:30:45 +01:00 committed by Hans-Christoph Steiner
parent bbf17ee59c
commit 6152abee08
2 changed files with 195 additions and 38 deletions

View File

@ -1256,6 +1256,56 @@ def parse_ios_screenshot_name(path):
return ("phoneScreenshots", 'unknown', 'unknown')
def discover_ios_screenshots(fastlane_dir):
"""Traverse git checkouts in build dir, search for fastlane-screenshots and put findings into a dict."""
fastlane_screenshot_dir = fastlane_dir / 'screenshots'
screenshots = {}
if fastlane_screenshot_dir.is_dir():
for lang_sdir in fastlane_screenshot_dir.iterdir():
locale = lang_sdir.name
m = LANG_CODE.match(locale)
if m:
screenshots[locale] = {}
fifo_idevice = {}
fifo_ios = {}
for screenshot in lang_sdir.iterdir():
if screenshot.suffix[1:] in ALLOWED_EXTENSIONS:
screenshot_type, idevice_name, ios_name = parse_ios_screenshot_name(screenshot)
# since there is no easy mapping here, we're just
# resorting to fifo here, so ieg. if there's 2
# screenshots categorized for more than one
# iPhone/iOS combinations we just remember the
# first combination, use them as screenshots in
# F-Droid and ignore all other screenshots, for
# this screenshot type
if not fifo_idevice.get(screenshot_type):
fifo_idevice[screenshot_type] = idevice_name
fifo_ios[screenshot_type] = ios_name
if fifo_idevice[screenshot_type] == idevice_name and fifo_ios[screenshot_type] == ios_name:
if screenshot_type not in screenshots[locale]:
screenshots[locale][screenshot_type] = []
screenshots[locale][screenshot_type].append(screenshot)
# sort all found screenshots alphanumerically
for locale, translated_screenshots in screenshots.items():
for device in translated_screenshots.keys():
translated_screenshots[device].sort()
return screenshots
def copy_ios_screenshots_to_repo(screenshots, package_name):
for locale, translated_screenshots in screenshots.items():
for device, translated_device_screenshots in translated_screenshots.items():
dest_dir = pathlib.Path('repo') / package_name / locale / device
dest_dir.mkdir(mode=0o755, parents=True, exist_ok=True)
for path in translated_device_screenshots:
dest = dest_dir / (path.name.replace(" ", "_").replace("\t", "_"))
fdroidserver.update._strip_and_copy_image(str(path), str(dest))
def insert_localized_ios_app_metadata(apps_with_packages):
if not any(pathlib.Path('repo').glob('*.ipa')):
@ -1269,46 +1319,20 @@ def insert_localized_ios_app_metadata(apps_with_packages):
continue
fastlane_dir = pathlib.Path('build', package_name, 'fastlane')
fastlane_meta_dir = (fastlane_dir / "metadata")
for lang_dir in (fastlane_dir / 'metadata').iterdir():
locale = lang_dir.name
m = LANG_CODE.match(locale)
if m:
for metadata_file in (lang_dir).iterdir():
key = FASTLANE_IOS_MAP.get(metadata_file.name)
if key:
_set_localized_text_entry(app, locale, key, metadata_file)
if fastlane_meta_dir.is_dir():
for lang_dir in fastlane_meta_dir.iterdir():
locale = lang_dir.name
m = LANG_CODE.match(locale)
if m:
for metadata_file in (lang_dir).iterdir():
key = FASTLANE_IOS_MAP.get(metadata_file.name)
if key:
fdroidserver.update._set_localized_text_entry(app, locale, key, metadata_file)
# discover available screenshots and put findings in a dict
screenshots = {}
for lang_sdir in (fastlane_dir / 'screenshots').iterdir():
locale = lang_sdir.name
m = LANG_CODE.match(locale)
if m:
screenshots[locale] = {}
fcfs_idevice = None
fcfs_ios = None
for screenshot in (lang_sdir).iterdir():
if screenshot.suffix[1:] in ALLOWED_EXTENSIONS:
screenshot_type, idevice_name, ios_name = parse_ios_screenshot_name(screenshot)
if not fcfs_idevice:
fcfs_idevice = idevice_name
fcfs_ios = ios_name
if fcfs_idevice == idevice_name and fcfs_ios == ios_name:
if not screenshots[locale].get(screenshot_type):
screenshots[locale][screenshot_type] = {}
screenshots[locale][screenshot_type][screenshot] = screenshot
# copy screenshots to repo dir
for locale, translated_screenshots in screenshots.items():
for device, translated_device_screenshots in translated_screenshots.items():
dest_dir = pathlib.Path('repo') / package_name / locale / device
dest_dir.mkdir(mode=0o755, parents=True, exist_ok=True)
for name, path in translated_device_screenshots.items():
dest = dest_dir / (name.replace(" ", "_").replace("\t", "_") + path.suffix)
_strip_and_copy_image(str(path), str(dest))
screenshots = fdroidserver.update.discover_ios_screenshots(fastlane_dir)
fdroidserver.update.copy_ios_screenshots_to_repo(screenshots, package_name)
def scan_repo_files(apkcache, repodir, knownapks, use_date_from_file=False):

View File

@ -2071,6 +2071,136 @@ class TestParseIosScreenShotName(unittest.TestCase):
)
class TestInsertLocalizedIosAppMetadata(unittest.TestCase):
def test_insert_localized_ios_app_metadata(self):
self.maxDiff = None
self.apps_with_packages = {
"org.fake": {}
}
def _mock_discover(fastlane_dir):
self.assertEqual(
fastlane_dir,
Path('build/org.fake/fastlane'),
)
return {"fake screenshots": "fake"}
def _mock_copy(screenshots, package_name):
self.assertEqual(screenshots, {"fake screenshots": "fake"})
self.assertEqual(package_name, "org.fake")
with mock.patch('fdroidserver.update.discover_ios_screenshots', _mock_discover):
self.set_localized_mock = mock.Mock()
with mock.patch('fdroidserver.update.copy_ios_screenshots_to_repo', _mock_copy):
with mock.patch("fdroidserver.update._set_localized_text_entry", self.set_localized_mock):
return fdroidserver.update.insert_localized_ios_app_metadata(
self.apps_with_packages
)
self.assertListEqual(
self.set_localized_mock.call_args_list,
[
mock.call({}, 'en-US', 'name', Path('build/org.fake/fastlane/metadata/en-US/name.txt')),
mock.call({}, 'en-US', 'summary', Path('build/org.fake/fastlane/metadata/en-US/subtitle.txt')),
mock.call({}, 'en-US', 'description', Path('build/org.fake/fastlane/metadata/en-US/description.txt')),
mock.call({}, 'de-DE', 'name', Path('build/org.fake/fastlane/metadata/de-DE/name.txt')),
mock.call({}, 'de-DE', 'summary', Path('build/org.fake/fastlane/metadata/de-DE/subtitle.txt')),
mock.call({}, 'de-DE', 'description', Path('build/org.fake/fastlane/metadata/de-DE/description.txt')),
],
)
class TestDiscoverIosScreenshots(unittest.TestCase):
def test_discover_ios_screenshots(self):
self.maxDiff = None
with tempfile.TemporaryDirectory() as fastlane_dir:
fastlane_dir = Path(fastlane_dir)
(fastlane_dir / "screenshots/en-US").mkdir(parents=True)
with open(fastlane_dir / "screenshots/en-US/iPhone 8+ @ iOS 16-1.png", 'w') as f:
f.write("1")
with open(fastlane_dir / "screenshots/en-US/iPad Pro 12.9\" 2gen @ iOS 16-1.png", "w") as f:
f.write("2")
with open(fastlane_dir / "screenshots/en-US/iPad Pro 12.9\" 2gen @ iOS 16-2.png", "w") as f:
f.write("3")
(fastlane_dir / "screenshots/de-DE").mkdir(parents=True)
with open(fastlane_dir / "screenshots/de-DE/1_ipadPro129_1.1.png", "w") as f:
f.write("4")
screenshots = fdroidserver.update.discover_ios_screenshots(fastlane_dir)
self.assertDictEqual(
screenshots,
{
"en-US": {
"phoneScreenshots": [
fastlane_dir / "screenshots/en-US/iPhone 8+ @ iOS 16-1.png",
],
"tenInchScreenshots": [
fastlane_dir / "screenshots/en-US/iPad Pro 12.9\" 2gen @ iOS 16-1.png",
fastlane_dir / "screenshots/en-US/iPad Pro 12.9\" 2gen @ iOS 16-2.png",
],
},
"de-DE": {
"tenInchScreenshots": [
fastlane_dir / "screenshots/de-DE/1_ipadPro129_1.1.png",
],
},
},
)
class TestCopyIosScreenshotsToRepo(unittest.TestCase):
def test_copy_ios_screenshots_to_repo(self):
self.maxDiff = None
screenshot_dir_en = Path("build/org.fake/fastlane/screenshots/en-US")
s1 = screenshot_dir_en / "iPhone 8+ @ iOS 16-1.png"
s2 = screenshot_dir_en / "iPad Pro 12.9\" 2gen @ iOS 16-1.png"
s3 = screenshot_dir_en / "iPad Pro 12.9\" 2gen @ iOS 16-2.png"
screenshot_dir_de = Path("build/org.fake/fastlane/screenshots/de-DE")
s4 = screenshot_dir_de / "1_ipadPro129_1.1.png"
cmock = mock.Mock()
with mock.patch("fdroidserver.update._strip_and_copy_image", cmock):
fdroidserver.update.copy_ios_screenshots_to_repo(
{
"en-US": {
"phoneScreenshots": [s1],
"tenInchScreenshots": [s2, s3],
},
"de-DE": {
"tenInchScreenshots": [s4],
},
},
"org.fake",
)
self.assertListEqual(
cmock.call_args_list,
[
mock.call(
'build/org.fake/fastlane/screenshots/en-US/iPhone 8+ @ iOS 16-1.png',
'repo/org.fake/en-US/phoneScreenshots/iPhone_8+_@_iOS_16-1.png',
),
mock.call(
'build/org.fake/fastlane/screenshots/en-US/iPad Pro 12.9" 2gen @ iOS 16-1.png',
'repo/org.fake/en-US/tenInchScreenshots/iPad_Pro_12.9"_2gen_@_iOS_16-1.png',
),
mock.call(
'build/org.fake/fastlane/screenshots/en-US/iPad Pro 12.9" 2gen @ iOS 16-2.png',
'repo/org.fake/en-US/tenInchScreenshots/iPad_Pro_12.9"_2gen_@_iOS_16-2.png',
),
mock.call(
'build/org.fake/fastlane/screenshots/de-DE/1_ipadPro129_1.1.png',
'repo/org.fake/de-DE/tenInchScreenshots/1_ipadPro129_1.1.png',
),
],
)
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__))
@ -2088,4 +2218,7 @@ if __name__ == "__main__":
newSuite.addTest(unittest.makeSuite(UpdateTest))
newSuite.addTest(unittest.makeSuite(TestUpdateVersionStringToInt))
newSuite.addTest(unittest.makeSuite(TestScanRepoForIpas))
newSuite.addTest(unittest.makeSuite(TestParseIosScreenShotName))
newSuite.addTest(unittest.makeSuite(TestInsertLocalizedIosAppMetadata))
newSuite.addTest(unittest.makeSuite(TestDiscoverIosScreenshots))
unittest.main(failfast=False)