1 | # Copyright Bruno da Silva de Oliveira 2003. Use, modification and |
---|
2 | # distribution is subject to the Boost Software License, Version 1.0. |
---|
3 | # (See accompanying file LICENSE_1_0.txt or copy at |
---|
4 | # http:#www.boost.org/LICENSE_1_0.txt) |
---|
5 | import sys |
---|
6 | sys.path.append('../src') |
---|
7 | from SmartFile import * |
---|
8 | import unittest |
---|
9 | import tempfile |
---|
10 | import os |
---|
11 | import time |
---|
12 | |
---|
13 | |
---|
14 | class SmartFileTest(unittest.TestCase): |
---|
15 | |
---|
16 | FILENAME = tempfile.mktemp() |
---|
17 | |
---|
18 | def setUp(self): |
---|
19 | self._Clean() |
---|
20 | |
---|
21 | def tearDown(self): |
---|
22 | self._Clean() |
---|
23 | |
---|
24 | def _Clean(self): |
---|
25 | try: |
---|
26 | os.remove(self.FILENAME) |
---|
27 | except OSError: pass |
---|
28 | |
---|
29 | |
---|
30 | def testNonExistant(self): |
---|
31 | "Must override the file, as there's no file in the disk yet" |
---|
32 | self.assert_(not os.path.isfile(self.FILENAME)) |
---|
33 | f = SmartFile(self.FILENAME, 'w') |
---|
34 | f.write('Testing 123\nTesting again.') |
---|
35 | f.close() |
---|
36 | self.assert_(os.path.isfile(self.FILENAME)) |
---|
37 | |
---|
38 | |
---|
39 | def testOverride(self): |
---|
40 | "Must override the file, because the contents are different" |
---|
41 | contents = 'Contents!\nContents!' |
---|
42 | # create the file normally first |
---|
43 | f = file(self.FILENAME, 'w') |
---|
44 | f.write(contents) |
---|
45 | f.close() |
---|
46 | file_time = os.path.getmtime(self.FILENAME) |
---|
47 | self.assert_(os.path.isfile(self.FILENAME)) |
---|
48 | time.sleep(2) |
---|
49 | f = SmartFile(self.FILENAME, 'w') |
---|
50 | f.write(contents + '_') |
---|
51 | f.close() |
---|
52 | new_file_time = os.path.getmtime(self.FILENAME) |
---|
53 | self.assert_(new_file_time != file_time) |
---|
54 | |
---|
55 | |
---|
56 | def testNoOverride(self): |
---|
57 | "Must not override the file, because the contents are the same" |
---|
58 | contents = 'Contents!\nContents!' |
---|
59 | # create the file normally first |
---|
60 | f = file(self.FILENAME, 'w') |
---|
61 | f.write(contents) |
---|
62 | f.close() |
---|
63 | file_time = os.path.getmtime(self.FILENAME) |
---|
64 | self.assert_(os.path.isfile(self.FILENAME)) |
---|
65 | time.sleep(2) |
---|
66 | f = SmartFile(self.FILENAME, 'w') |
---|
67 | f.write(contents) |
---|
68 | f.close() |
---|
69 | new_file_time = os.path.getmtime(self.FILENAME) |
---|
70 | self.assert_(new_file_time == file_time) |
---|
71 | |
---|
72 | |
---|
73 | def testAutoClose(self): |
---|
74 | "Must be closed when garbage-collected" |
---|
75 | def foo(): |
---|
76 | f = SmartFile(self.FILENAME) |
---|
77 | f.write('testing') |
---|
78 | self.assert_(not os.path.isfile(self.FILENAME)) |
---|
79 | foo() |
---|
80 | self.assert_(os.path.isfile(self.FILENAME)) |
---|
81 | |
---|
82 | |
---|
83 | if __name__ == '__main__': |
---|
84 | unittest.main() |
---|