Skip to content

Instantly share code, notes, and snippets.

@micviklui
Created May 24, 2018 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micviklui/eff8f852b7a20bafd49e0ac51330120d to your computer and use it in GitHub Desktop.
Save micviklui/eff8f852b7a20bafd49e0ac51330120d to your computer and use it in GitHub Desktop.
run executables in subprocesses in temporary directory
# 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