regex: make sure regex fails if nothing is found

This is a regression since 2019 [1].
Before that commit, max() raises ValuError if re.findall returns an
empty list. After that commit, regex fails silently if nothing is found.

[1] 7897317294 (diff-fd40f1295f9ddece86df8d2c385ddb02bd4e2a10cd20bd9a95306d3d35a9b601)
This commit is contained in:
Chih-Hsuan Yen 2021-08-31 09:48:07 +08:00
parent f443fa8e96
commit 9acae25c0f
No known key found for this signature in database
GPG Key ID: F98EF2A7B0A098AE
2 changed files with 12 additions and 6 deletions

View File

@ -22,9 +22,7 @@ async def get_version_impl(info):
res = await session.get(conf['url'])
body = res.body.decode(encoding)
try:
version = regex.findall(body)
except ValueError:
if not conf.get('missing_ok', False):
versions = regex.findall(body)
if not versions and not conf.get('missing_ok', False):
raise GetVersionError('version string not found.')
return version
return versions

View File

@ -43,6 +43,14 @@ async def test_missing_ok(get_version, httpbin):
"missing_ok": True,
}) is None
async def test_missing(get_version, httpbin):
with pytest.raises(RuntimeError):
await get_version("example", {
"source": "regex",
"url": httpbin.url + "/base64/" + base64_encode("something not there"),
"regex": "foobar",
})
async def test_regex_with_tokenBasic(get_version, httpbin):
assert await get_version("example", {
"source": "regex",