From 60d79d4cc97fdd2ca50640e8044ea4d1a838f77a Mon Sep 17 00:00:00 2001 From: Sebastian Parborg Date: Sun, 9 Jun 2024 17:37:09 +0200 Subject: [PATCH 1/2] Fix pytest commandline flags being parsed by Beeref For example, running "pytest -l" will error out as Beeref would try to parse "-l" as a log level flag. Now we no longer try to blindly parse flags when including the Beeref commandline parser. --- CHANGELOG.rst | 2 ++ beeref/config/settings.py | 4 +++- tests/config/test_settings.py | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 9a8a277..803b3ae 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -35,6 +35,8 @@ Fixed crashes (by DarkDefender) * Fixed a crash when pressing the crop shortcut while dragging an image (by DarkDefender) +* Fix commandline arguments meant for pytest being parsed by BeeRef + (by DarkDefender) Changed diff --git a/beeref/config/settings.py b/beeref/config/settings.py index 5f7357f..92d50be 100644 --- a/beeref/config/settings.py +++ b/beeref/config/settings.py @@ -89,7 +89,9 @@ def __init__(self, with_check=False): if with_check: self._args = parser.parse_args() else: - self._args = parser.parse_known_args()[0] + # Do not parse any flags from sys.argv as we are + # being used as a module. + self._args = parser.parse_args([]) def __getattribute__(self, name): if name == '_args': diff --git a/tests/config/test_settings.py b/tests/config/test_settings.py index eede452..54046b7 100644 --- a/tests/config/test_settings.py +++ b/tests/config/test_settings.py @@ -20,8 +20,10 @@ def test_command_line_args_singleton(): def test_command_line_args_with_check_forces_new_parsing(parse_mock): args1 = CommandlineArgs() args2 = CommandlineArgs(with_check=True) - parse_mock.assert_called_once() + args3 = CommandlineArgs() + assert parse_mock.call_count == 2 assert args1 is not args2 + assert args2 is args3 CommandlineArgs._instance = None From 3ba69d16051daf98356cdcebb3ccff2020431a12 Mon Sep 17 00:00:00 2001 From: Sebastian Parborg Date: Thu, 26 Sep 2024 19:04:17 +0200 Subject: [PATCH 2/2] Fix command line args object not being the correct one In beeref/selection.py and beeref/view.py the commandline arg parse object would be created on import. Previously this would lead to the arg parse object being out of date when checking it. Now we don't recreate the arg parse helper object and instead make sure that it is static. --- beeref/config/settings.py | 15 +++++++-------- tests/config/test_settings.py | 4 +++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/beeref/config/settings.py b/beeref/config/settings.py index 92d50be..9f9e73f 100644 --- a/beeref/config/settings.py +++ b/beeref/config/settings.py @@ -80,18 +80,17 @@ class CommandlineArgs: _instance = None def __new__(cls, *args, **kwargs): - if not cls._instance or kwargs.get('with_check'): + if not cls._instance: cls._instance = super().__new__(cls) return cls._instance def __init__(self, with_check=False): - if not hasattr(self, '_args'): - if with_check: - self._args = parser.parse_args() - else: - # Do not parse any flags from sys.argv as we are - # being used as a module. - self._args = parser.parse_args([]) + if with_check: + self._args = parser.parse_args() + elif not hasattr(self, '_args'): + # Do not parse any flags from sys.argv unless speficially + # told to do so. + self._args = parser.parse_args([]) def __getattribute__(self, name): if name == '_args': diff --git a/tests/config/test_settings.py b/tests/config/test_settings.py index 54046b7..29cf691 100644 --- a/tests/config/test_settings.py +++ b/tests/config/test_settings.py @@ -19,10 +19,12 @@ def test_command_line_args_singleton(): @patch('beeref.config.settings.parser.parse_args') def test_command_line_args_with_check_forces_new_parsing(parse_mock): args1 = CommandlineArgs() + parse_mock.assert_called_with([]) args2 = CommandlineArgs(with_check=True) + parse_mock.assert_called_with() args3 = CommandlineArgs() assert parse_mock.call_count == 2 - assert args1 is not args2 + assert args1 is args2 assert args2 is args3 CommandlineArgs._instance = None