lint: check syntax of countryCode: fields for mirrors

This commit is contained in:
Hans-Christoph Steiner 2023-12-07 17:38:34 +01:00
parent 4511da68b9
commit 96fc49d7fc
4 changed files with 145 additions and 1 deletions

View file

@ -0,0 +1,47 @@
#!/usr/bin/env python3
#
# This generates a list of ISO_3166-1 alpha 2 country codes for use in lint.
import collections
import os
import re
import requests
import requests_cache
import sys
import tempfile
def main():
# we want all the data
url = 'https://api.worldbank.org/v2/country?format=json&per_page=500'
r = requests.get(url, timeout=30)
data = r.json()
if data[0]['pages'] != 1:
print(
'ERROR: %d pages in data, this script only reads one page!'
% data[0]['pages']
)
sys.exit(1)
iso2Codes = set()
ISO3166_1_alpha_2_codes = set()
names = dict()
regions = collections.defaultdict(set)
for country in data[1]:
iso2Code = country['iso2Code']
iso2Codes.add(iso2Code)
if country['region']['value'] == 'Aggregates':
continue
if re.match(r'[A-Z][A-Z]', iso2Code):
ISO3166_1_alpha_2_codes.add(iso2Code)
names[iso2Code] = country['name']
regions[country['region']['value']].add(country['name'])
for code in sorted(ISO3166_1_alpha_2_codes):
print(f" '{code}', # " + names[code])
if __name__ == "__main__":
requests_cache.install_cache(
os.path.join(tempfile.gettempdir(), os.path.basename(__file__) + '.cache')
)
main()

View file

@ -5,6 +5,7 @@
import logging
import optparse
import os
import ruamel.yaml
import shutil
import sys
import tempfile
@ -368,6 +369,75 @@ class LintTest(unittest.TestCase):
app = fdroidserver.metadata.App({'Categories': ['bar']})
self.assertEqual(0, len(list(fdroidserver.lint.check_categories(app))))
def test_lint_config_basic_mirrors_yml(self):
os.chdir(self.testdir)
yaml = ruamel.yaml.YAML(typ='safe')
with Path('mirrors.yml').open('w') as fp:
yaml.dump([{'url': 'https://example.com/fdroid/repo'}], fp)
self.assertTrue(fdroidserver.lint.lint_config('mirrors.yml'))
def test_lint_config_mirrors_yml_kenya_countryCode(self):
os.chdir(self.testdir)
yaml = ruamel.yaml.YAML(typ='safe')
with Path('mirrors.yml').open('w') as fp:
yaml.dump([{'url': 'https://foo.com/fdroid/repo', 'countryCode': 'KE'}], fp)
self.assertTrue(fdroidserver.lint.lint_config('mirrors.yml'))
def test_lint_config_mirrors_yml_invalid_countryCode(self):
"""WV is "indeterminately reserved" so it should never be used."""
os.chdir(self.testdir)
yaml = ruamel.yaml.YAML(typ='safe')
with Path('mirrors.yml').open('w') as fp:
yaml.dump([{'url': 'https://foo.com/fdroid/repo', 'countryCode': 'WV'}], fp)
self.assertFalse(fdroidserver.lint.lint_config('mirrors.yml'))
def test_lint_config_mirrors_yml_alpha3_countryCode(self):
"""Only ISO 3166-1 alpha 2 are supported"""
os.chdir(self.testdir)
yaml = ruamel.yaml.YAML(typ='safe')
with Path('mirrors.yml').open('w') as fp:
yaml.dump([{'url': 'https://de.com/fdroid/repo', 'countryCode': 'DEU'}], fp)
self.assertFalse(fdroidserver.lint.lint_config('mirrors.yml'))
def test_lint_config_mirrors_yml_one_invalid_countryCode(self):
"""WV is "indeterminately reserved" so it should never be used."""
os.chdir(self.testdir)
yaml = ruamel.yaml.YAML(typ='safe')
with Path('mirrors.yml').open('w') as fp:
yaml.dump(
[
{'url': 'https://bar.com/fdroid/repo', 'countryCode': 'BA'},
{'url': 'https://foo.com/fdroid/repo', 'countryCode': 'FO'},
{'url': 'https://wv.com/fdroid/repo', 'countryCode': 'WV'},
],
fp,
)
self.assertFalse(fdroidserver.lint.lint_config('mirrors.yml'))
def test_lint_config_bad_mirrors_yml_dict(self):
os.chdir(self.testdir)
Path('mirrors.yml').write_text('baz: [foo, bar]\n')
with self.assertRaises(TypeError):
fdroidserver.lint.lint_config('mirrors.yml')
def test_lint_config_bad_mirrors_yml_float(self):
os.chdir(self.testdir)
Path('mirrors.yml').write_text('1.0\n')
with self.assertRaises(TypeError):
fdroidserver.lint.lint_config('mirrors.yml')
def test_lint_config_bad_mirrors_yml_int(self):
os.chdir(self.testdir)
Path('mirrors.yml').write_text('1\n')
with self.assertRaises(TypeError):
fdroidserver.lint.lint_config('mirrors.yml')
def test_lint_config_bad_mirrors_yml_str(self):
os.chdir(self.testdir)
Path('mirrors.yml').write_text('foo\n')
with self.assertRaises(TypeError):
fdroidserver.lint.lint_config('mirrors.yml')
class LintAntiFeaturesTest(unittest.TestCase):
def setUp(self):