test_tmpdirs.py 1.3 KB

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