From 9acae25c0f60018dd929ba64b743e3d23c0e9e1b Mon Sep 17 00:00:00 2001 From: Chih-Hsuan Yen Date: Tue, 31 Aug 2021 09:48:07 +0800 Subject: [PATCH] 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] https://github.com/lilydjwg/nvchecker/commit/789731729472824e79e519c033a432a35cf402b2#diff-fd40f1295f9ddece86df8d2c385ddb02bd4e2a10cd20bd9a95306d3d35a9b601 --- nvchecker_source/regex.py | 10 ++++------ tests/test_regex.py | 8 ++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/nvchecker_source/regex.py b/nvchecker_source/regex.py index 0828d4d..1660b4f 100644 --- a/nvchecker_source/regex.py +++ b/nvchecker_source/regex.py @@ -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): - raise GetVersionError('version string not found.') - return version + versions = regex.findall(body) + if not versions and not conf.get('missing_ok', False): + raise GetVersionError('version string not found.') + return versions diff --git a/tests/test_regex.py b/tests/test_regex.py index 97a9af7..e54bca1 100644 --- a/tests/test_regex.py +++ b/tests/test_regex.py @@ -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",