metadata: get_single_build() for new subcommands

The new subcommands operate on a single APPID:VERSIONCODE pair, so this new
function fetches the metadata needed for that operation, and includes any
required checks.

The algorithm came from:
uniqx/fdroidserver@2b779e6599

This reworking of it includes:

* removed `check_disabled`, seems like the scheduler should handle that? If
  it is required, it can be added later.
* standard variable names
* fixed spelling errors and hopefully improved docstrings
* black format
* added tests
* standardized the strings, made them localizable
This commit is contained in:
Hans-Christoph Steiner 2025-10-09 17:00:58 +02:00
parent e6df386b13
commit b00b9c9390
3 changed files with 107 additions and 5 deletions

View file

@ -18,7 +18,7 @@ import fdroidserver
from fdroidserver import metadata
from fdroidserver._yaml import yaml
from fdroidserver.common import DEFAULT_LOCALE
from fdroidserver.exception import MetaDataException
from fdroidserver.exception import FDroidException, MetaDataException
from .shared_test_code import TmpCwd, mkdtemp
@ -233,6 +233,55 @@ class MetadataTest(unittest.TestCase):
logging_error.assert_called()
self.assertEqual(3, len(logging_error.call_args_list))
def test_get_single_build(self):
"""Test if this can successfully return an app and a build"""
os.chdir(self.testdir)
appid = 'one.build'
versionCode = 1234567890
metadatapath = Path(fdroidserver.common.get_metadatapath(appid))
metadatapath.parent.mkdir()
metadatapath.write_text(f'Name: One\nBuilds:\n - versionCode: {versionCode}\n')
app, build = metadata.get_single_build(appid, versionCode)
self.assertEqual(app.id, appid)
self.assertEqual(build.versionCode, versionCode)
def test_get_single_build_no_metadatapath(self):
"""Test if the right error is thrown if no matching metadatapath found."""
os.chdir(self.testdir)
appid = 'does.not.exist'
versionCode = 1234567890
with self.assertLogs(level='DEBUG') as logs:
with self.assertRaises(FDroidException):
metadata.get_single_build(appid, versionCode)
self.assertIn(appid, logs.output[0])
self.assertNotIn(str(versionCode), logs.output[0])
def test_get_single_build_no_builds(self):
"""Test if the right error is thrown if Builds: field found."""
os.chdir(self.testdir)
appid = 'no.builds'
versionCode = 1234567890
metadatapath = Path(fdroidserver.common.get_metadatapath(appid))
metadatapath.parent.mkdir()
metadatapath.write_text('Name: No Builds\n')
with self.assertRaises(MetaDataException) as e:
metadata.get_single_build(appid, versionCode)
self.assertIn(appid, e.exception.value)
self.assertNotIn(str(versionCode), e.exception.value)
def test_get_single_build_no_match(self):
"""Test if the right error is thrown if Builds: field found."""
os.chdir(self.testdir)
appid = 'no.builds'
versionCode = 1234567890
metadatapath = Path(fdroidserver.common.get_metadatapath(appid))
metadatapath.parent.mkdir()
metadatapath.write_text('Name: No Builds\nBuilds: [{versionCode: 0}]\n')
with self.assertRaises(MetaDataException) as e:
metadata.get_single_build(appid, versionCode)
self.assertIn(appid, e.exception.value)
self.assertIn(str(versionCode), e.exception.value)
@mock.patch('git.Repo', mock.Mock())
def test_metadata_overrides_dot_fdroid_yml(self):
"""Fields in metadata files should override anything in .fdroid.yml."""