File: test_Subprocess__cleanup.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 (31 lines) | stat: -rw-r--r-- 1,033 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
import subprocess
from unittest import mock


def test_clean_termination(mock_sub, capsys):
    """A popen process can be cleanly terminated."""
    process = mock.MagicMock(spec_set=subprocess.Popen)

    mock_sub.cleanup("testing process", process)

    process.terminate.assert_called_once()
    process.wait.assert_called_once_with(timeout=3)
    process.kill.assert_not_called()

    # No log messages for a clean exit
    assert capsys.readouterr().out == ""


def test_dirty_termination(mock_sub, capsys):
    """If terminate doesn't stop the process, it will be forcibly killed."""
    process = mock.MagicMock(spec_set=subprocess.Popen)
    process.wait.side_effect = subprocess.TimeoutExpired(cmd="ls", timeout=3)

    mock_sub.cleanup("testing process", process)

    process.terminate.assert_called_once()
    process.wait.assert_called_once_with(timeout=3)
    process.kill.assert_called_once_with()

    # Log contains a contextual message.
    assert capsys.readouterr().out == "Forcibly killing testing process...\n"