Skip to content

Commit

Permalink
Changed default Process.label behavior to allow it to contain the ful…
Browse files Browse the repository at this point in the history
…l command if unspecified. Truncation now happens at the monitor level.
  • Loading branch information
Asaurus1 committed Apr 3, 2024
1 parent 4a0d6ff commit 30ca2ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
10 changes: 3 additions & 7 deletions src/streamlit_process_manager/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,14 +674,10 @@ def can_be_started(self) -> t.Literal[False]:
return False


def _default_label_if_unset(label: "str | None", proc: Process) -> str:
"""Return a concatenated and truncated string from the process's "cmd" if 'label' is None.
Otherwise return the label.
"""
def _default_label_if_unset(label: "str | None", args: "t.Sequence[str]") -> str:
"""Return the label specified, or the joined args as a string if None."""
if label is None:
cmd_str = " ".join(proc.cmd).replace("\n", " ")
return cmd_str[:30] + ("..." if len(cmd_str) > 30 else "")
return " ".join(args).replace("\n", " ")
return label


Expand Down
15 changes: 11 additions & 4 deletions tests/test_subprocess_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,12 @@ def test_process_open_output_with_no_file(fake_process: process.Process):


def test_process_start_common_args(fake_process: process.Process):
fake_process.env = {"MY": "VAR"}
fake_process.cwd = "/dev/null"
mock_sp = start_with_mock_sp(fake_process)
assert mock_sp.call_args[0][0] == fake_process.cmd
assert mock_sp.call_args[1]["env"] == fake_process.env
assert mock_sp.call_args[1]["cwd"] == fake_process.cwd


def test_process_start_with_no_file(fake_process: process.Process):
Expand Down Expand Up @@ -1119,6 +1122,12 @@ def test_process_monitor_core(mock_output, fake_process: process.Process):
assert pm._get_lines_from_process(10) == ["test\n", "lines\n"]


def test_process_monitor_long_label(fake_process: process.Process):
fake_process.label = "a" * 50
pm = monitor.ProcessMonitor(fake_process)
assert pm.config.label == "a"*30 + "..."


@mock.patch.object(process.Process, "peek_output")
def test_process_monitor_skip_empty_lines(mock_output, fake_process: process.Process):
pm = monitor.ProcessMonitor(fake_process)
Expand Down Expand Up @@ -1390,10 +1399,8 @@ def test_process_monitor_func_app(real_process_3s, real_process_short, real_proc

# Helper Function Tests -----------------------------------------------------
def test_default_label_if_unset(fake_process: process.Process):
assert process._default_label_if_unset("set_label", fake_process) == "set_label"
assert process._default_label_if_unset(None, fake_process) == "foo bar"
fake_process.cmd = ["a"] * 50
assert process._default_label_if_unset(None, fake_process) == " ".join(["a"] * 15) + " ..."
assert process._default_label_if_unset("set_label", fake_process.cmd) == "set_label"
assert process._default_label_if_unset(None, fake_process.cmd) == "foo bar"


def test_is_pid_child_of_current(real_process_infinite):
Expand Down

0 comments on commit 30ca2ac

Please sign in to comment.