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 px import px
from px import px_process
from unittest.mock import patch
from typing import List
# FIXME: Distribute these tests to the other *_test.py files and remove this
# one.
#
# These tests all require Python 3. Back when we also supported Python 2 the
# tests in here used to live in their own Python 3 specific directory.
#
# But since we're now entirely on Python 3, I just moved the whole file in here.
# These tests should be moved into the correct files, and this file should be
# removed / renamed.
@patch("px.px.install")
def test_cmdline_install(mock):
args = ["px", "--install"]
px._main(args)
mock.assert_called_once_with(args)
@patch("px.px_top.top")
def test_cmdline_top(mock):
px._main(["px", "--top"])
mock.assert_called_once()
@patch("px.px_top.top")
def test_cmdline_ptop(mock):
px._main(["ptop"])
mock.assert_called_once()
@patch("px.px_top.top")
def test_cmdline_top_with_search(mock):
px._main(["px", "--top", "kalas"])
mock.assert_called_once()
args, kwargs = mock.call_args
print(args)
print(kwargs)
assert kwargs.get("search") == "kalas"
def test_px_sort_cpupercent():
px._main(["px", "--sort=cpupercent"])
@patch("px.px_top.top")
def test_cmdline_ptop_with_search(mock):
px._main(["ptop", "kalas"])
mock.assert_called_once()
args, kwargs = mock.call_args
print(args)
print(kwargs)
assert kwargs.get("search") == "kalas"
@patch("builtins.print")
def test_cmdline_help(mock):
px._main(["px", "--help"])
mock.assert_called_once_with(px.__doc__)
@patch("builtins.print")
def test_cmdline_version(mock):
px._main(["px", "--version"])
mock.assert_called_once()
first_print_arg: str = mock.call_args.args[0]
assert first_print_arg
@patch("px.px_processinfo.print_pid_info")
def test_cmdline_pid(mock):
px._main(["px", "1235", "--no-pager"])
mock.assert_called_once()
print(mock)
print(mock.call_args)
args, kwargs = mock.call_args
print(args)
print(kwargs)
# First arg is a file descriptor that changes on every invocation, check the
# PID arg only.
assert args[1] == 1235
@patch("px.px_terminal.to_screen_lines")
def test_cmdline_filter(mock):
px._main(["px", "root", "--no-pager"])
mock.assert_called_once()
print(mock)
print(mock.call_args)
args, kwargs = mock.call_args
print(args)
print(kwargs)
processes: List[px_process.PxProcess] = args[0]
# This assumes something is running as root. We can assume that, right?
assert len(processes) > 0
# All listed processes must match
for process in processes:
assert process.match("root")
@patch("px.px_terminal.to_screen_lines")
def test_cmdline_list_all_processes(mock):
px._main(["px"])
mock.assert_called_once()
args, kwargs = mock.call_args
print(args)
print(kwargs)
processes: List[px_process.PxProcess] = args[0]
# We are running, and something started us, so this list must not be empty
assert len(processes) > 0
assert processes[0].command
|