From 0d9449f6282fff210c0b241d7cefd826e740c23c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20H=C3=B6ning?= Date: Mon, 4 Apr 2022 17:34:20 +0200 Subject: [PATCH] Backport PR #411: In is_running(), list CLI commands generically (#411) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * In is_running(), list CLI commands generically, also allowing commands from loaded plugins to do things outside of one account only Signed-off-by: Nicolas Höning * add a test for new util function cli.is_running Signed-off-by: Nicolas Höning --- flexmeasures/cli/__init__.py | 3 +-- flexmeasures/cli/tests/test_utils.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 flexmeasures/cli/tests/test_utils.py diff --git a/flexmeasures/cli/__init__.py b/flexmeasures/cli/__init__.py index 20f98d921..63716ee75 100644 --- a/flexmeasures/cli/__init__.py +++ b/flexmeasures/cli/__init__.py @@ -29,9 +29,8 @@ def is_running() -> bool: See also: the run_as_cli test fixture, which uses the (non-public) PRETEND_RUNNING_AS_CLI env setting. - TODO: How can plugins add their CLI set here, should they need that? """ - cli_sets = ("add", "delete", "show", "monitor", "jobs", "db-ops") + cli_sets = current_app.cli.list_commands(ctx=None) command_line = " ".join(sys.argv) for cli_set in cli_sets: if f"flexmeasures {cli_set}" in command_line: diff --git a/flexmeasures/cli/tests/test_utils.py b/flexmeasures/cli/tests/test_utils.py new file mode 100644 index 000000000..522a45e62 --- /dev/null +++ b/flexmeasures/cli/tests/test_utils.py @@ -0,0 +1,11 @@ +import sys + +from flexmeasures.cli import is_running as cli_is_running + + +def test_cli_is_running(app, monkeypatch): + assert cli_is_running() is False + monkeypatch.setattr( + sys, "argv", ["/bin/flexmeasures", "add", "account", "--name", "XCorp."] + ) + assert cli_is_running() is True