test_file_io.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import unittest
  2. import os
  3. import shutil
  4. import makesite
  5. from test import path
  6. class FileIOTest(unittest.TestCase):
  7. """Tests for file I/O functions."""
  8. def test_fread(self):
  9. text = 'foo\nbar\n'
  10. filepath = path.temppath('foo.txt')
  11. with open(filepath, 'w') as f:
  12. f.write(text)
  13. text_read = makesite.fread(filepath)
  14. os.remove(filepath)
  15. self.assertEqual(text_read, text)
  16. def test_fwrite(self):
  17. text = 'baz\nqux\n'
  18. filepath = path.temppath('foo.txt')
  19. makesite.fwrite(filepath, text)
  20. with open(filepath) as f:
  21. text_read = f.read()
  22. os.remove(filepath)
  23. self.assertEqual(text_read, text)
  24. def test_fwrite_makedir(self):
  25. text = 'baz\nqux\n'
  26. dirpath = path.temppath('foo', 'bar')
  27. filepath = os.path.join(dirpath, 'foo.txt')
  28. makesite.fwrite(filepath, text)
  29. with open(filepath) as f:
  30. text_read = f.read()
  31. self.assertTrue(os.path.isdir(dirpath))
  32. shutil.rmtree(path.temppath('foo'))
  33. self.assertEqual(text_read, text)