1 """unit tests for logilab.common.fileutils"""
2
3 import sys, os, tempfile, shutil
4 from os.path import join
5
6 from logilab.common.testlib import TestCase, unittest_main
7
8 from logilab.common.fileutils import *
9
10
11 DATA_DIR = 'data'
12 NEWLINES_TXT = join(DATA_DIR,'newlines.txt')
13
15
17 """return the first level directory of a path"""
18 self.assertEqual(first_level_directory('truc/bidule/chouette'), 'truc', None)
19 self.assertEqual(first_level_directory('/truc/bidule/chouette'), '/', None)
20
23 self.assertEqual(is_binary('toto.txt'), 0)
24
25 self.assertEqual(is_binary('toto.bin'), 1)
26 self.assertEqual(is_binary('toto.sxi'), 1)
27 self.assertEqual(is_binary('toto.whatever'), 1)
28
35
40
41
50
55
57 export('data', self.tempdir, verbose=0)
58 self.assert_(exists(join(self.tempdir, '__init__.py')))
59 self.assert_(exists(join(self.tempdir, 'sub')))
60 self.assert_(not exists(join(self.tempdir, '__init__.pyc')))
61 self.assert_(not exists(join(self.tempdir, 'CVS')))
62
65
68 self.rpath = 'data/write_protected_file.txt'
69 self.rwpath = 'data/normal_file.txt'
70
71 os.chmod(self.rpath, 33060)
72
73 os.chmod(self.rwpath, 33188)
74
76 """tests that mode is changed when needed"""
77
78 self.assert_(not os.access(self.rpath, os.W_OK))
79 wp_file = ProtectedFile(self.rpath, 'w')
80 self.assert_(os.access(self.rpath, os.W_OK))
81
82 self.assert_(os.access(self.rwpath, os.W_OK))
83 wp_file = ProtectedFile(self.rwpath, 'w')
84 self.assert_(os.access(self.rwpath, os.W_OK))
85
87 """tests original mode is restored on close"""
88
89 self.assert_(not os.access(self.rpath, os.W_OK))
90 ProtectedFile(self.rpath, 'w').close()
91 self.assert_(not os.access(self.rpath, os.W_OK))
92
93 self.assert_(os.access(self.rwpath, os.W_OK))
94 ProtectedFile(self.rwpath, 'w').close()
95 self.assert_(os.access(self.rwpath, os.W_OK))
96
98 """tests that mode is changed when file is opened in 'a' mode"""
99 self.assert_(not os.access(self.rpath, os.W_OK))
100 wp_file = ProtectedFile(self.rpath, 'a')
101 self.assert_(os.access(self.rpath, os.W_OK))
102 wp_file.close()
103 self.assert_(not os.access(self.rpath, os.W_OK))
104
105
106 from logilab.common.testlib import DocTest
111
112
113 del DocTest
114
115 if __name__ == '__main__':
116 unittest_main()
117