Clean up ChunkedUpdateDrawingAreaProxy
[WebKit-https.git] / WebKitTools / Scripts / webkitpy / common / system / filesystem_unittest.py
1 # vim: set fileencoding=utf-8 :
2 # Copyright (C) 2010 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #    * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #    * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #    * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 # NOTE: The fileencoding comment on the first line of the file is
31 # important; without it, Python will choke while trying to parse the file,
32 # since it includes non-ASCII characters.
33
34 from __future__ import with_statement
35
36 import os
37 import stat
38 import sys
39 import tempfile
40 import unittest
41
42 from filesystem import FileSystem
43
44
45 class FileSystemTest(unittest.TestCase):
46     def setUp(self):
47         self._this_dir = os.path.dirname(os.path.abspath(__file__))
48         self._missing_file = os.path.join(self._this_dir, 'missing_file.py')
49         self._this_file = os.path.join(self._this_dir, 'filesystem_unittest.py')
50
51     def test_exists__true(self):
52         fs = FileSystem()
53         self.assertTrue(fs.exists(self._this_file))
54
55     def test_exists__false(self):
56         fs = FileSystem()
57         self.assertFalse(fs.exists(self._missing_file))
58
59     def test_isdir__true(self):
60         fs = FileSystem()
61         self.assertTrue(fs.isdir(self._this_dir))
62
63     def test_isdir__false(self):
64         fs = FileSystem()
65         self.assertFalse(fs.isdir(self._this_file))
66
67     def test_join(self):
68         fs = FileSystem()
69         self.assertEqual(fs.join('foo', 'bar'),
70                          os.path.join('foo', 'bar'))
71
72     def test_listdir(self):
73         fs = FileSystem()
74         with fs.mkdtemp(prefix='filesystem_unittest_') as d:
75             self.assertEqual(fs.listdir(d), [])
76             new_file = os.path.join(d, 'foo')
77             fs.write_text_file(new_file, u'foo')
78             self.assertEqual(fs.listdir(d), ['foo'])
79             os.remove(new_file)
80
81     def test_maybe_make_directory__success(self):
82         fs = FileSystem()
83
84         with fs.mkdtemp(prefix='filesystem_unittest_') as base_path:
85             sub_path = os.path.join(base_path, "newdir")
86             self.assertFalse(os.path.exists(sub_path))
87             self.assertFalse(fs.isdir(sub_path))
88
89             fs.maybe_make_directory(sub_path)
90             self.assertTrue(os.path.exists(sub_path))
91             self.assertTrue(fs.isdir(sub_path))
92
93             # Make sure we can re-create it.
94             fs.maybe_make_directory(sub_path)
95             self.assertTrue(os.path.exists(sub_path))
96             self.assertTrue(fs.isdir(sub_path))
97
98             # Clean up.
99             os.rmdir(sub_path)
100
101         self.assertFalse(os.path.exists(base_path))
102         self.assertFalse(fs.isdir(base_path))
103
104     def test_maybe_make_directory__failure(self):
105         # FIXME: os.chmod() doesn't work on Windows to set directories
106         # as readonly, so we skip this test for now.
107         if sys.platform in ('win32', 'cygwin'):
108             return
109
110         fs = FileSystem()
111         with fs.mkdtemp(prefix='filesystem_unittest_') as d:
112             # Remove write permissions on the parent directory.
113             os.chmod(d, stat.S_IRUSR)
114
115             # Now try to create a sub directory - should fail.
116             sub_dir = fs.join(d, 'subdir')
117             self.assertRaises(OSError, fs.maybe_make_directory, sub_dir)
118
119             # Clean up in case the test failed and we did create the
120             # directory.
121             if os.path.exists(sub_dir):
122                 os.rmdir(sub_dir)
123
124     def test_read_and_write_file(self):
125         fs = FileSystem()
126         text_path = None
127         binary_path = None
128
129         unicode_text_string = u'Ūnĭcōde̽'
130         hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD'
131         try:
132             text_path = tempfile.mktemp(prefix='tree_unittest_')
133             binary_path = tempfile.mktemp(prefix='tree_unittest_')
134             fs.write_text_file(text_path, unicode_text_string)
135             contents = fs.read_binary_file(text_path)
136             self.assertEqual(contents, hex_equivalent)
137
138             fs.write_text_file(binary_path, hex_equivalent)
139             text_contents = fs.read_text_file(binary_path)
140             self.assertEqual(text_contents, unicode_text_string)
141         except:
142             if text_path:
143                 os.remove(text_path)
144             if binary_path:
145                 os.remove(binary_path)
146
147     def test_read_binary_file__missing(self):
148         fs = FileSystem()
149         self.assertRaises(IOError, fs.read_binary_file, self._missing_file)
150
151     def test_read_text_file__missing(self):
152         fs = FileSystem()
153         self.assertRaises(IOError, fs.read_text_file, self._missing_file)
154
155     def test_remove_file_with_retry(self):
156         FileSystemTest._remove_failures = 2
157
158         def remove_with_exception(filename):
159             FileSystemTest._remove_failures -= 1
160             if FileSystemTest._remove_failures >= 0:
161                 try:
162                     raise WindowsError
163                 except NameError:
164                     raise FileSystem._WindowsError
165
166         fs = FileSystem()
167         self.assertTrue(fs.remove('filename', remove_with_exception))
168         self.assertEquals(-1, FileSystemTest._remove_failures)
169
170
171 if __name__ == '__main__':
172     unittest.main()