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.
This commit is contained in:
Sebastian Parborg 2024-09-26 19:04:17 +02:00
parent 60d79d4cc9
commit 3ba69d1605
2 changed files with 10 additions and 9 deletions

View file

@ -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':

View file

@ -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