Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add unittests for AtomicFile

  • Loading branch information...
commit 1cd263089dd9ad6e3b0af84706ec7cb73621b85e 1 parent 37381de
@spoof spoof authored
Showing with 61 additions and 0 deletions.
  1. +61 −0 test.py
View
61 test.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+# encoding: utf-8
+from __future__ import with_statement
+import os
+import unittest
+
+from atomicfile import AtomicFile
+
+
+def create_test_file(filename):
+ f = open(filename, 'w')
+ f.write('test\n')
+ f.close()
+
+
+class AtomicFileTest(unittest.TestCase):
+
+ def setUp(self):
+ self.filename = 'test-atomicfile.txt'
+
+ def test_write(self):
+ create_test_file(self.filename)
+ af = AtomicFile(self.filename)
+ expected = "this is written by AtomicFile.\n"
+ af.write(expected)
+ af.close()
+ result = open(self.filename, 'r').read()
+ try:
+ self.assertEqual(result, expected)
+ finally:
+ os.remove(self.filename)
+
+ def test_close(self):
+ af = AtomicFile(self.filename)
+ af.write('test\n')
+ af.close()
+ try:
+ af.write('test again\n')
+ self.fail('ValueError not raised')
+ except ValueError:
+ pass
+ finally:
+ os.remove(self.filename)
+
+ def test_with(self):
+ data = "this is written by AtomicFile.\n"
+
+ with AtomicFile(self.filename) as f:
+ f.write(data)
+
+ try:
+ f.write(data)
+ self.fail("'ValueError: I/O operation on closed file' not raised")
+ except ValueError:
+ pass
+ finally:
+ os.remove(self.filename)
+
+
+if __name__ == '__main__':
+ unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.