thp / urlwatch

Watch (parts of) webpages and get notified when something changes via e-mail, on your phone or via other means. Highly configurable.
https://thp.io/2008/urlwatch/
Other
2.81k stars 352 forks source link

test_handler.py cannot be run with non-default pytest parameters: SystemExit: 2 #677

Closed sbraz closed 2 years ago

sbraz commented 2 years ago

Hi, Running these 4 tests with non-default pytest parameters causes argparse to be indirectly called with bad options, e.g.:

$ pytest -s
[…]
usage: pytest [-h] [--version] [-v] [--urls FILE] [--config FILE] [--hooks FILE] [--cache FILE] [--smtp-login] [--telegram-chats] [--test-reporter REPORTER] [--xmpp-login] [--list] [--add JOB] [--delete JOB]   
              [--test-filter JOB] [--test-diff-filter JOB] [--edit] [--edit-config] [--edit-hooks] [--features] [--gc-cache]                                                                                       
              [JOB ...]                                                                                                                                                                                            
pytest: error: unrecognized arguments: -s
[…]
FAILED lib/urlwatch/tests/test_handler.py::test_run_watcher - SystemExit: 2
FAILED lib/urlwatch/tests/test_handler.py::test_number_of_tries_in_cache_is_increased - SystemExit: 2
FAILED lib/urlwatch/tests/test_handler.py::test_report_error_when_out_of_tries - SystemExit: 2
FAILED lib/urlwatch/tests/test_handler.py::test_reset_tries_to_zero_when_successful - SystemExit: 2

The only reason the CI passes is because -v is also a valid argument for urlwatch: https://github.com/thp/urlwatch/blob/602da407f2e9a5907dd2cd066666db8828b2f220/.github/workflows/unit-tests.yml#L37

I don't know how to properly fix this but this hack works:

diff --git a/lib/urlwatch/tests/test_handler.py b/lib/urlwatch/tests/test_handler.py
index 736536a..fac9449 100644
--- a/lib/urlwatch/tests/test_handler.py
+++ b/lib/urlwatch/tests/test_handler.py
@@ -89,6 +89,7 @@ def test_pep8_conformance():

 class ConfigForTest(CommandConfig):
     def __init__(self, config, urls, cache, hooks, verbose):
+        sys.argv = sys.argv[:1]
         (prefix, bindir) = os.path.split(os.path.dirname(os.path.abspath(sys.argv[0])))
         super().__init__('urlwatch', os.path.dirname(__file__), bindir, prefix, config, urls, hooks, cache, verbose)
tomaszn commented 2 years ago

After this is fixed we could enable coverage reports needed for #94.

thp commented 2 years ago

Thanks, fixed this by explicitly passing in command-line parameters.