forked from sashka/atomicfile-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
61 lines (47 loc) · 1.38 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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()