conftest.py 898 B

1234567891011121314151617181920212223242526272829303132
  1. import os
  2. import shutil
  3. import pytest
  4. @pytest.fixture(scope="function")
  5. def testing_workdir(tmpdir, request):
  6. """Create a workdir in a safe temporary folder; cd into dir above before test, cd out after
  7. :param tmpdir: py.test fixture, will be injected
  8. :param request: py.test fixture-related, will be injected (see pytest docs)
  9. """
  10. saved_path = os.getcwd()
  11. tmpdir.chdir()
  12. # temporary folder for profiling output, if any
  13. tmpdir.mkdir("prof")
  14. def return_to_saved_path():
  15. if os.path.isdir(os.path.join(saved_path, "prof")):
  16. profdir = tmpdir.join("prof")
  17. files = profdir.listdir("*.prof") if profdir.isdir() else []
  18. for f in files:
  19. shutil.copy(str(f), os.path.join(saved_path, "prof", f.basename))
  20. os.chdir(saved_path)
  21. request.addfinalizer(return_to_saved_path)
  22. return str(tmpdir)