Created
May 24, 2018 21:25
-
-
Save micviklui/eff8f852b7a20bafd49e0ac51330120d to your computer and use it in GitHub Desktop.
run executables in subprocesses in temporary directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires ls/a.file in the root directory | |
import os | |
import subprocess | |
import shutil | |
import tempfile | |
import pytest | |
def mk_cd_tempdir(path, suffix=''): | |
cur_dir = os.path.abspath(os.path.curdir) | |
test_dir = tempfile.mkdtemp(suffix=suffix) | |
shutil.copytree(path, os.path.join(test_dir, path)) | |
os.chdir(test_dir) | |
yield test_dir | |
os.chdir(cur_dir) | |
#shutil.rmtree(test_dir) | |
@pytest.fixture() | |
def ls_dirs(): | |
# with python >= 3.5 | |
# yield from mk_cd_tempdir | |
for td in mk_cd_tempdir('ls', '_ls_test'): | |
yield td | |
def test_ls(ls_dirs): | |
test_dir = ls_dirs | |
output = subprocess.check_output(['ls', 'ls']) | |
assert b'a.file' in output | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment