File: test_get_process_id_by_command.py

package info (click to toggle)
python-briefcase 0.3.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,300 kB
  • sloc: python: 59,405; makefile: 57
file content (131 lines) | stat: -rw-r--r-- 4,165 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from collections import namedtuple

import pytest

from briefcase.console import Console
from briefcase.integrations.subprocess import get_process_id_by_command

Process = namedtuple("Process", "info")
process_list_one_proc = [
    Process(
        info=dict(cmdline=["/bin/cmd.sh", "--input", "data"], create_time=20, pid=100)
    )
]

process_list_two_procs_diff_cmd = [
    Process(
        info=dict(
            cmdline=["/bin/first_cmd.sh", "--input", "data"], create_time=20, pid=100
        )
    ),
    Process(
        info=dict(
            cmdline=["/bin/second_cmd.sh", "--input", "data"], create_time=10, pid=200
        )
    ),
]

process_list_two_procs_same_cmd = [
    Process(
        info=dict(cmdline=["/bin/cmd.sh", "--input", "data"], create_time=20, pid=100)
    ),
    Process(
        info=dict(cmdline=["/bin/cmd.sh", "--input", "data"], create_time=10, pid=200)
    ),
]


@pytest.mark.parametrize(
    ("process_list", "command_list", "expected_pid", "expected_stdout"),
    [
        ([], ["/bin/cmd.sh", "--input", "data"], None, ""),
        (process_list_one_proc, ["/bin/cmd.sh", "--input", "data"], 100, ""),
        (process_list_one_proc, ["/bin/random_cmd.sh", "--input", "data"], None, ""),
        (
            process_list_two_procs_diff_cmd,
            ["/bin/first_cmd.sh", "--input", "data"],
            100,
            "",
        ),
        (
            process_list_two_procs_diff_cmd,
            ["/bin/random_cmd.sh", "--input", "data"],
            None,
            "",
        ),
        (
            process_list_two_procs_same_cmd,
            ["/bin/cmd.sh", "--input", "data"],
            100,
            "Multiple running instances of app found. "
            "Using most recently created app process 100.\n",
        ),
        (
            process_list_two_procs_same_cmd,
            ["/bin/random_cmd.sh", "--input", "data"],
            None,
            "",
        ),
    ],
)
def test_get_process_id_by_command_w_command_line(
    process_list,
    command_list,
    expected_pid,
    expected_stdout,
    monkeypatch,
    capsys,
):
    """Finds correct process for command line or returns None."""
    monkeypatch.setattr("psutil.process_iter", lambda attrs: process_list)
    found_pid = get_process_id_by_command(command_list=command_list, console=Console())
    assert found_pid == expected_pid
    assert capsys.readouterr().out == expected_stdout


@pytest.mark.parametrize(
    ("process_list", "command", "expected_pid", "expected_stdout"),
    [
        ([], "/bin/cmd.sh", None, ""),
        (process_list_one_proc, "/bin/cmd", 100, ""),
        (process_list_one_proc, "/bin/cmd.sh --input data", None, ""),
        (process_list_one_proc, "/bin/cmd.sh", 100, ""),
        (process_list_one_proc, "/bin/random_cmd.sh", None, ""),
        (process_list_two_procs_diff_cmd, "/bin/first_cmd.sh", 100, ""),
        (process_list_two_procs_diff_cmd, "/bin/random_cmd.sh", None, ""),
        (
            process_list_two_procs_same_cmd,
            "/bin/cmd.sh",
            100,
            "Multiple running instances of app found. "
            "Using most recently created app process 100.\n",
        ),
        (process_list_two_procs_same_cmd, "/bin/random_cmd.sh", None, ""),
    ],
)
def test_get_process_id_by_command_w_command(
    process_list,
    command,
    expected_pid,
    expected_stdout,
    monkeypatch,
    capsys,
):
    """Finds correct process for command or returns None."""
    monkeypatch.setattr("psutil.process_iter", lambda attrs: process_list)
    found_pid = get_process_id_by_command(command=command, console=Console())
    assert found_pid == expected_pid
    assert capsys.readouterr().out == expected_stdout


def test_get_process_id_no_logging(monkeypatch, capsys):
    """If no logger is provided, warnings about ambiguous matches aren't printed."""
    monkeypatch.setattr(
        "psutil.process_iter",
        lambda attrs: process_list_two_procs_same_cmd,
    )
    found_pid = get_process_id_by_command(
        command_list=["/bin/cmd.sh", "--input", "data"]
    )
    assert found_pid == 100
    assert capsys.readouterr().out == ""