def testing_workdir(tmpdir, request):
""" Create a workdir in a safe temporary folder; cd into dir above before test, cd out after
:param tmpdir: py.test fixture, will be injected
:param request: py.test fixture-related, will be injected (see pytest docs)
"""
saved_path = os.getcwd()
tmpdir.chdir()
# temporary folder for profiling output, if any
tmpdir.mkdir('prof')
def return_to_saved_path():
if os.path.isdir(os.path.join(saved_path, 'prof')):
profdir = tmpdir.join('prof')
files = profdir.listdir('*.prof') if profdir.isdir() else []
for f in files:
f.rename(os.path.join(saved_path, 'prof', f.basename))
os.chdir(saved_path)
request.addfinalizer(return_to_saved_path)
return str(tmpdir)
评论列表
文章目录