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
|
|
|
|
2024-06-29 11:58:11 +02: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
|
|
|
|
2018-12-06 13:52:46 -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():
|
2018-12-06 13:52:46 -05:00
|
|
|
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():
|
2018-12-06 13:52:46 -05:00
|
|
|
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')
|
2018-07-06 23:36:44 -04:00
|
|
|
assert 'tst_dir/hello.txt' in g`tst_dir/*.txt`
|
|
|
|
|
|
|
|
|
2018-08-27 18:00:31 -05:00
|
|
|
def test_check_call_raises():
|
2018-12-06 13:52:46 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-07-06 23:36:44 -04:00
|
|
|
def test_check_output():
|
2018-12-06 13:52:46 -05:00
|
|
|
if ON_WINDOWS:
|
|
|
|
pytest.skip("On Windows")
|
2018-07-06 23:36:44 -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
|
|
|
|
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`
|