test_path.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import unittest
  2. import os
  3. import shutil
  4. from test import path
  5. class PathTest(unittest.TestCase):
  6. def test_temppath(self):
  7. self.assertTrue(path.temppath())
  8. def test_move_existing_file(self):
  9. src = os.path.join(path.temppath(), 'foo.txt')
  10. dst = os.path.join(path.temppath(), 'bar.txt')
  11. with open(src, 'w') as f:
  12. f.write('foo')
  13. path.move(src, dst)
  14. self.assertFalse(os.path.isfile(src))
  15. self.assertTrue(os.path.isfile(dst))
  16. with open(dst) as f:
  17. text = f.read()
  18. os.remove(dst)
  19. self.assertEqual(text, 'foo')
  20. def test_move_missing_file(self):
  21. src = os.path.join(path.temppath(), 'foo.txt')
  22. dst = os.path.join(path.temppath(), 'bar.txt')
  23. path.move(src, dst)
  24. self.assertFalse(os.path.isfile(src))
  25. self.assertFalse(os.path.isfile(dst))
  26. def test_move_file_cleanup(self):
  27. src = os.path.join(path.temppath(), 'foo.txt')
  28. dst = os.path.join(path.temppath(), 'bar.txt')
  29. with open(dst, 'w') as f:
  30. f.write('foo')
  31. path.move(src, dst)
  32. self.assertFalse(os.path.isfile(src))
  33. self.assertFalse(os.path.isfile(dst))
  34. def test_move_existing_dir(self):
  35. src = os.path.join(path.temppath(), 'foo')
  36. srcf = os.path.join(src, 'foo.txt')
  37. dst = os.path.join(path.temppath(), 'bar')
  38. dstf = os.path.join(dst, 'foo.txt')
  39. os.makedirs(src)
  40. with open(srcf, 'w') as f:
  41. f.write('foo')
  42. path.move(src, dst)
  43. self.assertFalse(os.path.isdir(src))
  44. self.assertTrue(os.path.isdir(dst))
  45. with open(dstf) as f:
  46. text = f.read()
  47. shutil.rmtree(dst)
  48. self.assertEqual(text, 'foo')
  49. def test_move_missing_dir(self):
  50. src = os.path.join(path.temppath(), 'foo')
  51. dst = os.path.join(path.temppath(), 'bar')
  52. path.move(src, dst)
  53. self.assertFalse(os.path.isdir(src))
  54. self.assertFalse(os.path.isdir(dst))
  55. def test_move_dir_cleanup(self):
  56. src = os.path.join(path.temppath(), 'foo')
  57. dst = os.path.join(path.temppath(), 'bar')
  58. os.makedirs(dst)
  59. path.move(src, dst)
  60. self.assertFalse(os.path.isdir(src))
  61. self.assertFalse(os.path.isdir(dst))