test_tmpdirs.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """ Test tmpdirs module """
  2. from os import getcwd
  3. from os.path import realpath, abspath, dirname, isfile, join as pjoin, exists
  4. from scipy._lib._tmpdirs import tempdir, in_tempdir, in_dir
  5. from numpy.testing import assert_, assert_equal
  6. import pytest
  7. MY_PATH = abspath(__file__)
  8. MY_DIR = dirname(MY_PATH)
  9. @pytest.mark.thread_unsafe(reason="tempdir is not thread-safe")
  10. def test_tempdir():
  11. with tempdir() as tmpdir:
  12. fname = pjoin(tmpdir, 'example_file.txt')
  13. with open(fname, "w") as fobj:
  14. fobj.write('a string\\n')
  15. assert_(not exists(tmpdir))
  16. @pytest.mark.thread_unsafe(reason="in_tempdir is not thread-safe")
  17. def test_in_tempdir():
  18. my_cwd = getcwd()
  19. with in_tempdir() as tmpdir:
  20. with open('test.txt', "w") as f:
  21. f.write('some text')
  22. assert_(isfile('test.txt'))
  23. assert_(isfile(pjoin(tmpdir, 'test.txt')))
  24. assert_(not exists(tmpdir))
  25. assert_equal(getcwd(), my_cwd)
  26. @pytest.mark.thread_unsafe(reason="in_dir is not thread-safe")
  27. def test_given_directory():
  28. # Test InGivenDirectory
  29. cwd = getcwd()
  30. with in_dir() as tmpdir:
  31. assert_equal(tmpdir, abspath(cwd))
  32. assert_equal(tmpdir, abspath(getcwd()))
  33. with in_dir(MY_DIR) as tmpdir:
  34. assert_equal(tmpdir, MY_DIR)
  35. assert_equal(realpath(MY_DIR), realpath(abspath(getcwd())))
  36. # We were deleting the given directory! Check not so now.
  37. assert_(isfile(MY_PATH))