xonsh/tests/api/test_subprocess.xsh

62 lines
1.7 KiB
Text
Raw Normal View History

2018-08-27 18:00:31 -05:00
"""Tests for subprocess lib"""
2018-07-03 11:22:58 -04:00
import tempfile
2022-01-16 17:38:21 +05:30
from subprocess import CalledProcessError
2018-07-03 11:22:58 -04:00
from xonsh.api.os import indir
from xonsh.api.subprocess import run, check_call, check_output
2018-08-27 18:00:31 -05:00
import pytest
2022-03-24 00:09:28 +05:30
from xonsh.pytest.tools import ON_WINDOWS
2018-06-27 21:55:40 -04:00
2018-07-03 11:22:58 -04:00
def test_run():
if ON_WINDOWS:
pytest.skip("On Windows")
2018-07-03 11:22:58 -04:00
with tempfile.TemporaryDirectory() as tmpdir:
with indir(tmpdir):
run(['touch', 'hello.txt'])
assert 'hello.txt' in g`*.txt`
rm hello.txt
mkdir tst_dir
run(['touch', 'hello.txt'], cwd='tst_dir')
assert 'tst_dir/hello.txt' in g`tst_dir/*.txt`
2018-06-27 21:55:40 -04:00
2018-07-03 11:22:58 -04:00
def test_check_call():
if ON_WINDOWS:
pytest.skip("On Windows")
2018-07-03 11:22:58 -04:00
with tempfile.TemporaryDirectory() as tmpdir:
with indir(tmpdir):
check_call(['touch', 'hello.txt'])
assert 'hello.txt' in g`*.txt`
rm hello.txt
mkdir tst_dir
check_call(['touch', 'hello.txt'], cwd='tst_dir')
assert 'tst_dir/hello.txt' in g`tst_dir/*.txt`
2018-08-27 18:00:31 -05:00
def test_check_call_raises():
if ON_WINDOWS:
pytest.skip("On Windows")
2018-08-27 18:00:31 -05:00
try:
2022-01-16 17:38:21 +05:30
check_call(['false'])
2018-08-27 18:00:31 -05:00
got_raise = False
except CalledProcessError:
got_raise = True
assert got_raise
def test_check_output():
if ON_WINDOWS:
pytest.skip("On Windows")
with tempfile.TemporaryDirectory() as tmpdir:
with indir(tmpdir):
check_call(['touch', 'hello.txt'])
assert 'hello.txt' in g`*.txt`
rm hello.txt
mkdir tst_dir
p = check_output(['touch', 'hello.txt'], cwd='tst_dir')
assert p.decode('utf-8') == ''
2018-07-03 11:22:58 -04:00
assert 'tst_dir/hello.txt' in g`tst_dir/*.txt`