-
Notifications
You must be signed in to change notification settings - Fork 215
/
test_cli.py
210 lines (184 loc) Β· 7.01 KB
/
test_cli.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from time import time
from unittest import TestCase, main
from mock import patch, MagicMock, Mock
from ntfy.cli import run_cmd, auto_done
from ntfy.cli import main as ntfy_main
def process_mock(returncode=0, stdout=None, stderr=None):
process_mock = Mock()
attrs = {'communicate.return_value': (stdout, stderr), 'returncode': returncode}
process_mock.configure_mock(**attrs)
return process_mock
class TestRunCmd(TestCase):
@patch('ntfy.cli.Popen')
def test_default(self, mock_Popen):
mock_Popen.return_value = process_mock()
args = MagicMock()
args.longer_than = -1
args.command = ['true']
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual(('"true" succeeded in 0:00 minutes', 0), run_cmd(args))
@patch('ntfy.cli.Popen')
def test_emoji(self, mock_Popen):
mock_Popen.return_value = process_mock()
args = MagicMock()
args.longer_than = -1
args.command = ['true']
args.pid = None
args.no_emoji = False
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual((':white_check_mark: "true" succeeded in 0:00 minutes', 0),
run_cmd(args))
def tests_usage(self):
args = MagicMock()
args.pid = False
args.formatter = False
args.command = []
self.assertRaises(SystemExit, run_cmd, args)
@patch('ntfy.cli.Popen')
def test_longerthan(self, mock_Popen):
mock_Popen.return_value = process_mock()
args = MagicMock()
args.longer_than = 1
args.command = ['true']
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual((None, None), run_cmd(args))
@patch('ntfy.cli.Popen')
def test_failure(self, mock_Popen):
mock_Popen.return_value = process_mock(42)
args = MagicMock()
args.longer_than = -1
args.command = ['false']
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual(('"false" failed (code 42) in 0:00 minutes', 42), run_cmd(args))
@patch('ntfy.cli.Popen')
def test_stdout(self, mock_Popen):
mock_Popen.return_value = process_mock(stdout='output')
args = MagicMock()
args.longer_than = -1
args.command = ['true']
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
# not actually used
args.stdout = True
args.stderr = False
self.assertEqual(('"true" succeeded in 0:00 minutes:\noutput', 0), run_cmd(args))
@patch('ntfy.cli.Popen')
def test_stderr(self, mock_Popen):
mock_Popen.return_value = process_mock(stderr='error')
args = MagicMock()
args.longer_than = -1
args.command = ['true']
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
# not actually used
args.stdout = False
args.stderr = True
self.assertEqual(('"true" succeeded in 0:00 minutes:\nerror', 0), run_cmd(args))
@patch('ntfy.cli.Popen')
def test_stdout_and_stderr(self, mock_Popen):
mock_Popen.return_value = process_mock(stdout='output', stderr='error')
args = MagicMock()
args.longer_than = -1
args.command = ['true']
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
# not actually used
args.stdout = True
args.stderr = True
self.assertEqual(('"true" succeeded in 0:00 minutes:\noutputerror', 0), run_cmd(args))
@patch('ntfy.cli.Popen')
def test_failure_stdout_and_stderr(self, mock_Popen):
mock_Popen.return_value = process_mock(1, stdout='output', stderr='error')
args = MagicMock()
args.longer_than = -1
args.command = ['true']
args.pid = None
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
# not actually used
args.stdout = True
args.stderr = True
self.assertEqual(('"true" failed (code 1) in 0:00 minutes:\noutputerror', 1), run_cmd(args))
@patch('ntfy.cli.Popen')
def test_hide_command(self, mock_Popen):
mock_Popen.return_value = process_mock()
args = MagicMock()
args.longer_than = -1
args.command = ['true']
args.pid = None
args.unfocused_only = False
args.hide_command = True
self.assertEqual(('Your command succeeded in 0:00 minutes', 0), run_cmd(args))
def test_formatter(self):
args = MagicMock()
args.pid = None
args.command = None
args.formatter = ("true", 0, 65)
args.longer_than = -1
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual(('"true" succeeded in 1:05 minutes', 0), run_cmd(args))
def test_formatter_failure(self):
args = MagicMock()
args.pid = None
args.command = None
args.formatter = ("false", 1, 10)
args.longer_than = -1
args.unfocused_only = False
args.hide_command = False
args.locked_only = False
self.assertEqual(('"false" failed (code 1) in 0:10 minutes', 1), run_cmd(args))
class TestMain(TestCase):
@patch('ntfy.backends.default.notify')
def test_args(self, mock_notify):
mock_notify.return_value = None
self.assertEquals(0, ntfy_main(['-o', 'foo', 'bar',
'-b', 'default',
'-t', 'TITLE',
'send', 'test']))
mock_notify.assert_called_once_with(message='test',
title='TITLE',
foo='bar',
retcode=0)
class ShellIntegrationTestCase(TestCase):
def test_shellintegration_printout(self):
# not mocking print to check calls because test runner uses print...
args = MagicMock()
auto_done(args)
class TestWatchPID(TestCase):
@patch('psutil.Process')
def test_watch_pid(self, mock_process):
mock_process.return_value.pid = 1
mock_process.return_value.create_time.return_value = time()
mock_process.return_value.cmdline.return_value = ['cmd']
args = MagicMock()
args.pid = 1
args.unfocused_only = False
args.locked_only = False
self.assertEqual('PID[1]: "cmd" finished in 0:00 minutes',
run_cmd(args)[0])
def test_watch_bad_pid(self):
args = MagicMock()
args.pid = 100000
self.assertRaises(SystemExit, run_cmd, args)
if __name__ == '__main__':
main()