Add `missing_ok`, which suppresses no-result warnings

This commit is contained in:
Chih-Hsuan Yen 2018-09-21 12:17:21 +08:00
parent 42a02efec8
commit 1d16726c90
No known key found for this signature in database
GPG Key ID: 0453A6CA23C56315
5 changed files with 25 additions and 2 deletions

View File

@ -177,6 +177,10 @@ from_pattern, to_pattern
Both are Python-compatible regular expressions. If ``from_pattern`` is found Both are Python-compatible regular expressions. If ``from_pattern`` is found
in the version string, it will be replaced with ``to_pattern``. in the version string, it will be replaced with ``to_pattern``.
missing_ok
Suppress warnings and errors if a version checking module finds nothing.
Currently only ``regex`` supports it.
If both ``prefix`` and ``from_pattern``/``to_pattern`` are used, If both ``prefix`` and ``from_pattern``/``to_pattern`` are used,
``from_pattern``/``to_pattern`` are ignored. If you want to strip the prefix ``from_pattern``/``to_pattern`` are ignored. If you want to strip the prefix
and then do something special, just use ``from_pattern```/``to_pattern``. For and then do something special, just use ``from_pattern```/``to_pattern``. For

View File

@ -176,7 +176,8 @@ class Source:
elif result is not None: elif result is not None:
self.print_version_update(name, result) self.print_version_update(name, result)
else: else:
logger.warn('no-result', name=name) if not conf.getboolean('missing_ok', False):
logger.warn('no-result', name=name)
self.on_no_result(name) self.on_no_result(name)
if self.newver: if self.newver:

View File

@ -34,5 +34,6 @@ async def get_version(name, conf, **kwargs):
version = max(regex.findall(body), key=sort_version_key) version = max(regex.findall(body), key=sort_version_key)
except ValueError: except ValueError:
version = None version = None
logger.error('version string not found.', name=name) if not conf.getboolean('missing_ok', False):
logger.error('version string not found.', name=name)
return version return version

View File

@ -2,6 +2,7 @@ import configparser
import pytest import pytest
import asyncio import asyncio
import io import io
import structlog
from nvchecker.get_version import get_version as _get_version from nvchecker.get_version import get_version as _get_version
from nvchecker.core import Source from nvchecker.core import Source
@ -53,3 +54,12 @@ def event_loop(request):
""" """
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
yield loop yield loop
@pytest.fixture(scope="module")
def raise_on_logger_msg():
def proc(logger, method_name, event_dict):
if method_name in ('warn', 'error'):
raise RuntimeError(event_dict['event'])
return event_dict['event']
structlog.configure([proc])

View File

@ -18,3 +18,10 @@ async def test_regex(get_version):
"url": "https://example.net/", "url": "https://example.net/",
"regex": 'for (\w+) examples', "regex": 'for (\w+) examples',
}) == "illustrative" }) == "illustrative"
async def test_missing_ok(get_version, raise_on_logger_msg):
assert await get_version("example", {
"url": "https://example.net/",
"regex": "foobar",
"missing_ok": True,
}) is None