summaryrefslogtreecommitdiff
path: root/test/git/test_utils.py
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-10-22 19:23:21 +0200
committerSebastian Thiel <byronimo@gmail.com>2009-10-22 19:23:21 +0200
commit7184340f9d826a52b9e72fce8a30b21a7c73d5be (patch)
treecabee1826da8d91f8c902a78e179a3170ab7f8ec /test/git/test_utils.py
parent36f838e7977e62284d2928f65cacf29771f1952f (diff)
downloadgitpython-7184340f9d826a52b9e72fce8a30b21a7c73d5be.tar.gz
Added test for ConcurrentWriteOperation
Diffstat (limited to 'test/git/test_utils.py')
-rw-r--r--test/git/test_utils.py51
1 files changed, 50 insertions, 1 deletions
diff --git a/test/git/test_utils.py b/test/git/test_utils.py
index 61527758..029d2054 100644
--- a/test/git/test_utils.py
+++ b/test/git/test_utils.py
@@ -53,5 +53,54 @@ class TestUtils(TestCase):
lock_file._obtain_lock_or_raise()
lock_file._release_lock()
+ def _cmp_contents(self, file_path, data):
+ # raise if data from file at file_path
+ # does not match data string
+ fp = open(file_path, "r")
+ try:
+ assert fp.read() == data
+ finally:
+ fp.close()
+
def test_safe_operation(self):
- self.fail("todo")
+ my_file = tempfile.mktemp()
+ orig_data = "hello"
+ new_data = "world"
+ my_file_fp = open(my_file, "w")
+ my_file_fp.write(orig_data)
+ my_file_fp.close()
+
+ try:
+ cwrite = ConcurrentWriteOperation(my_file)
+
+ # didn't start writing, doesnt matter
+ cwrite._end_writing(False)
+ cwrite._end_writing(True)
+ assert not cwrite._is_writing()
+
+ # write data and fail
+ stream = cwrite._begin_writing()
+ assert cwrite._is_writing()
+ stream.write(new_data)
+ cwrite._end_writing(successful=False)
+ self._cmp_contents(my_file, orig_data)
+ assert not os.path.exists(stream.name)
+
+ # write data - concurrently
+ ocwrite = ConcurrentWriteOperation(my_file)
+ stream = cwrite._begin_writing()
+ self.failUnlessRaises(IOError, ocwrite._begin_writing)
+
+ stream.write("world")
+ cwrite._end_writing(successful=True)
+ self._cmp_contents(my_file, new_data)
+ assert not os.path.exists(stream.name)
+
+ # could test automatic _end_writing on destruction
+ finally:
+ os.remove(my_file)
+ # END final cleanup
+
+
+
+