test_tmpdirs.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. MY_PATH = abspath(__file__)
  7. MY_DIR = dirname(MY_PATH)
  8. def test_tempdir():
  9. with tempdir() as tmpdir:
  10. fname = pjoin(tmpdir, 'example_file.txt')
  11. with open(fname, 'wt') as fobj:
  12. fobj.write('a string\\n')
  13. assert_(not exists(tmpdir))
  14. def test_in_tempdir():
  15. my_cwd = getcwd()
  16. with in_tempdir() as tmpdir:
  17. with open('test.txt', 'wt') as f:
  18. f.write('some text')
  19. assert_(isfile('test.txt'))
  20. assert_(isfile(pjoin(tmpdir, 'test.txt')))
  21. assert_(not exists(tmpdir))
  22. assert_equal(getcwd(), my_cwd)
  23. def test_given_directory():
  24. # Test InGivenDirectory
  25. cwd = getcwd()
  26. with in_dir() as tmpdir:
  27. assert_equal(tmpdir, abspath(cwd))
  28. assert_equal(tmpdir, abspath(getcwd()))
  29. with in_dir(MY_DIR) as tmpdir:
  30. assert_equal(tmpdir, MY_DIR)
  31. assert_equal(realpath(MY_DIR), realpath(abspath(getcwd())))
  32. # We were deleting the given directory! Check not so now.
  33. assert_(isfile(MY_PATH))