Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions movement/cli_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import argparse
import platform
import subprocess
import sys

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -41,12 +43,19 @@ def main() -> None:
"""Entrypoint for the CLI."""
parser = argparse.ArgumentParser(prog="movement")
subparsers = parser.add_subparsers(dest="command", title="commands")

# Add 'info' command
info_parser = subparsers.add_parser(
"info", help="output diagnostic information about the environment"
)
info_parser.set_defaults(func=info)

# Add 'launch' command
launch_parser = subparsers.add_parser(
"launch", help="launch the movement plugin in napari"
)
launch_parser.set_defaults(func=launch)

args = parser.parse_args()
if args.command is None:
help_message = parser.format_help()
Expand All @@ -68,5 +77,20 @@ def info() -> None:
)


def launch() -> None:
"""Launch the movement plugin in napari."""
try:
# Use sys.executable to ensure the correct Python interpreter is used
subprocess.run(
[sys.executable, "-m", "napari", "-w", "movement"], check=True
)
except subprocess.CalledProcessError as e:
# if subprocess.run() fails with non-zero exit code
print(
"\nAn error occurred while launching the movement plugin "
f"for napari:\n {e}"
)


if __name__ == "__main__": # pragma: no cover
main()
30 changes: 30 additions & 0 deletions tests/test_unit/test_cli_entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import subprocess
import sys
from contextlib import nullcontext as does_not_raise
from unittest.mock import patch

Expand All @@ -21,6 +23,7 @@
],
)
def test_entrypoint_command(command, expected_exception):
"""Test the entrypoint with different commands: 'info', 'invalid', ''."""
with (
patch("sys.argv", command),
patch("builtins.print") as mock_print,
Expand All @@ -29,3 +32,30 @@ def test_entrypoint_command(command, expected_exception):
main()
printed_message = " ".join(map(str, mock_print.call_args.args))
assert e in printed_message


@pytest.mark.parametrize(
"run_side_effect, expected_message",
[
(None, ""), # No error
(subprocess.CalledProcessError(1, "napari"), "error occurred while"),
],
)
def test_launch_command(run_side_effect, expected_message, capsys):
"""Test the 'launch' command.

We mock the subprocess.run function to avoid actually launching napari.
"""
with (
patch("sys.argv", ["movement", "launch"]),
patch("subprocess.run", side_effect=run_side_effect) as mock_run,
):
main()
# Assert that subprocess.run was called with the correct arguments
mock_run.assert_called_once()
args = mock_run.call_args[0][0]
assert args[0] == sys.executable
assert args[1:] == ["-m", "napari", "-w", "movement"]
# Assert that the expected message was printed
captured = capsys.readouterr()
assert expected_message in captured.out
Loading