Skip to content

Commit 8e17747

Browse files
committed
Added some unit tests for ProcReader and ContextFlag utility classes
1 parent 76741c3 commit 8e17747

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

cmd2/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#
21
# coding=utf-8
32
"""Shared utility functions"""
43

tests/test_utils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Copyright 2018 Todd Leonhardt <todd.leonhardt@gmail.com>
77
Released under MIT license, see LICENSE file
88
"""
9+
import signal
910
import sys
1011

1112
import pytest
@@ -216,3 +217,53 @@ def test_stdsim_pause_storage(stdout_sim):
216217
stdout_sim.pause_storage = True
217218
stdout_sim.buffer.write(b_str)
218219
assert stdout_sim.getbytes() == b''
220+
221+
222+
@pytest.fixture
223+
def pr_none():
224+
import subprocess
225+
command = 'ls'
226+
if sys.platform.startswith('win'):
227+
command = 'dir'
228+
proc = subprocess.Popen([command], shell=True)
229+
pr = cu.ProcReader(proc, None, None)
230+
return pr
231+
232+
def test_proc_reader_send_sigint(pr_none):
233+
assert pr_none._proc.poll() is None
234+
pr_none.send_sigint()
235+
pr_none.wait()
236+
ret_code = pr_none._proc.poll()
237+
if sys.platform.startswith('win'):
238+
assert ret_code is not None
239+
else:
240+
assert ret_code == -signal.SIGINT
241+
242+
def test_proc_reader_terminate(pr_none):
243+
assert pr_none._proc.poll() is None
244+
pr_none.terminate()
245+
pr_none.wait()
246+
ret_code = pr_none._proc.poll()
247+
if sys.platform.startswith('win'):
248+
assert ret_code is not None
249+
else:
250+
assert ret_code == -signal.SIGTERM
251+
252+
def test_proc_reader_wait(pr_none):
253+
assert pr_none._proc.poll() is None
254+
pr_none.wait()
255+
assert pr_none._proc.poll() == 0
256+
257+
258+
@pytest.fixture
259+
def context_flag():
260+
return cu.ContextFlag()
261+
262+
def test_context_flag_bool(context_flag):
263+
assert not context_flag
264+
with context_flag:
265+
assert context_flag
266+
267+
def test_context_flag_exit_err(context_flag):
268+
with pytest.raises(ValueError):
269+
context_flag.__exit__()

0 commit comments

Comments
 (0)