2017-11-26 16:37:00 +00:00
|
|
|
import sys
|
2017-12-22 10:54:30 +00:00
|
|
|
import pytest
|
2017-12-24 00:21:09 +00:00
|
|
|
import time
|
2017-11-26 16:37:00 +00:00
|
|
|
from pydu.compat import string_types
|
2017-11-07 00:29:17 +00:00
|
|
|
from pydu.string import safeunicode
|
2017-12-24 00:21:09 +00:00
|
|
|
from pydu.cmd import TimeoutExpired, run, run_with_en_env, terminate, cmdline_argv
|
2017-11-07 00:29:17 +00:00
|
|
|
|
|
|
|
|
2017-12-21 12:33:07 +00:00
|
|
|
def test_run():
|
2017-12-23 02:27:08 +00:00
|
|
|
retcode, output = run('echo hello', shell=True)
|
2017-11-07 00:29:17 +00:00
|
|
|
assert retcode == 0
|
|
|
|
assert safeunicode(output).rstrip('\r\n') == 'hello'
|
|
|
|
|
2017-12-23 02:27:08 +00:00
|
|
|
p = run('echo hello', wait=False, shell=True)
|
2017-11-07 00:29:17 +00:00
|
|
|
assert p.wait() == 0
|
2017-11-06 15:41:51 +00:00
|
|
|
|
2017-12-23 03:05:22 +00:00
|
|
|
with pytest.raises(TimeoutExpired) as e:
|
|
|
|
cmd = '{} -c "import time; time.sleep(1)"'.format(sys.executable)
|
|
|
|
timeout = 0.2
|
|
|
|
run(cmd, shell=True, timeout=timeout, timeinterval=0.05)
|
|
|
|
assert e.cmd == cmd
|
|
|
|
assert e.timeout == timeout
|
|
|
|
assert hasattr(e, 'output')
|
|
|
|
assert hasattr(e, 'stderr')
|
2017-11-26 16:37:00 +00:00
|
|
|
|
2017-11-06 15:41:51 +00:00
|
|
|
|
2017-12-23 02:10:13 +00:00
|
|
|
def test_run_with_en_env():
|
2017-12-23 02:27:08 +00:00
|
|
|
_, output = run_with_en_env('nocmd', shell=True)
|
2017-12-23 03:44:46 +00:00
|
|
|
assert output.decode('ascii')
|
2017-12-23 02:10:13 +00:00
|
|
|
|
2017-12-23 02:27:08 +00:00
|
|
|
_, output = run_with_en_env(['nocmd'], shell=True)
|
2017-12-23 03:44:46 +00:00
|
|
|
assert output.decode('ascii')
|
2017-12-23 02:10:13 +00:00
|
|
|
|
|
|
|
|
2017-12-24 00:21:09 +00:00
|
|
|
def test_terminate():
|
|
|
|
p = run('{} -c "import time; time.sleep(1)"'.format(sys.executable),
|
|
|
|
wait=False, shell=True)
|
|
|
|
terminate(p.pid)
|
|
|
|
time.sleep(0.1)
|
|
|
|
assert p.poll() is not None
|
|
|
|
|
|
|
|
|
2017-11-07 00:15:05 +00:00
|
|
|
def test_cmdline_argv():
|
|
|
|
argv = cmdline_argv()
|
2017-11-06 15:41:51 +00:00
|
|
|
for s in argv[1:]:
|
2017-11-26 16:37:00 +00:00
|
|
|
assert isinstance(s, string_types)
|