| 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 | |
|---|
| 14 | class FirstleveldirectoryTC(TestCase): |
|---|
| 15 | |
|---|
| 16 | def test_known_values_first_level_directory(self): |
|---|
| 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 | |
|---|
| 21 | class IsBinaryTC(TestCase): |
|---|
| 22 | def test(self): |
|---|
| 23 | self.assertEqual(is_binary('toto.txt'), 0) |
|---|
| 24 | #self.assertEqual(is_binary('toto.xml'), 0) |
|---|
| 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 | |
|---|
| 29 | class GetModeTC(TestCase): |
|---|
| 30 | def test(self): |
|---|
| 31 | self.assertEqual(write_open_mode('toto.txt'), 'w') |
|---|
| 32 | #self.assertEqual(write_open_mode('toto.xml'), 'w') |
|---|
| 33 | self.assertEqual(write_open_mode('toto.bin'), 'wb') |
|---|
| 34 | self.assertEqual(write_open_mode('toto.sxi'), 'wb') |
|---|
| 35 | |
|---|
| 36 | class NormReadTC(TestCase): |
|---|
| 37 | def test_known_values_norm_read(self): |
|---|
| 38 | data = norm_read(NEWLINES_TXT) |
|---|
| 39 | self.assertEqual(data.strip(), '\n'.join(['# mixed new lines', '1', '2', '3'])) |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | class LinesTC(TestCase): |
|---|
| 43 | def test_known_values_lines(self): |
|---|
| 44 | self.assertEqual(lines(NEWLINES_TXT), |
|---|
| 45 | ['# mixed new lines', '1', '2', '3']) |
|---|
| 46 | |
|---|
| 47 | def test_known_values_lines_comment(self): |
|---|
| 48 | self.assertEqual(lines(NEWLINES_TXT, comments='#'), |
|---|
| 49 | ['1', '2', '3']) |
|---|
| 50 | |
|---|
| 51 | class ExportTC(TestCase): |
|---|
| 52 | def setUp(self): |
|---|
| 53 | self.tempdir = tempfile.mktemp() |
|---|
| 54 | os.mkdir(self.tempdir) |
|---|
| 55 | |
|---|
| 56 | def test(self): |
|---|
| 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 | |
|---|
| 63 | def tearDown(self): |
|---|
| 64 | shutil.rmtree(self.tempdir) |
|---|
| 65 | |
|---|
| 66 | class ProtectedFileTC(TestCase): |
|---|
| 67 | def setUp(self): |
|---|
| 68 | self.rpath = 'data/write_protected_file.txt' |
|---|
| 69 | self.rwpath = 'data/normal_file.txt' |
|---|
| 70 | # Make sure rpath is not writable ! |
|---|
| 71 | os.chmod(self.rpath, 33060) |
|---|
| 72 | # Make sure rwpath is writable ! |
|---|
| 73 | os.chmod(self.rwpath, 33188) |
|---|
| 74 | |
|---|
| 75 | def test_mode_change(self): |
|---|
| 76 | """tests that mode is changed when needed""" |
|---|
| 77 | # test on non-writable file |
|---|
| 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 | # test on writable-file |
|---|
| 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 | |
|---|
| 86 | def test_restore_on_close(self): |
|---|
| 87 | """tests original mode is restored on close""" |
|---|
| 88 | # test on non-writable file |
|---|
| 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 | # test on writable-file |
|---|
| 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 | |
|---|
| 97 | def test_mode_change_on_append(self): |
|---|
| 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 |
|---|
| 107 | class ModuleDocTest(DocTest): |
|---|
| 108 | """relative_path embed tests in docstring""" |
|---|
| 109 | from logilab.common import fileutils as module |
|---|
| 110 | skipped = ('abspath_listdir',) |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | del DocTest # necessary if we don't want it to be executed (we don't...) |
|---|
| 114 | |
|---|
| 115 | if __name__ == '__main__': |
|---|
| 116 | unittest_main() |
|---|