OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding=utf-8 | 2 # coding=utf-8 |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 import StringIO | 7 import StringIO |
8 import logging | 8 import logging |
9 import os | 9 import os |
10 import tempfile | 10 import sys |
11 import unittest | 11 import unittest |
12 import shutil | |
13 import sys | |
14 | 12 |
15 BASE_DIR = unicode(os.path.dirname(os.path.abspath(__file__))) | 13 BASE_DIR = unicode(os.path.dirname(os.path.abspath(__file__))) |
16 ROOT_DIR = os.path.dirname(BASE_DIR) | 14 ROOT_DIR = os.path.dirname(BASE_DIR) |
17 sys.path.insert(0, ROOT_DIR) | 15 sys.path.insert(0, ROOT_DIR) |
18 | 16 |
19 FILE_PATH = unicode(os.path.abspath(__file__)) | 17 FILE_PATH = unicode(os.path.abspath(__file__)) |
20 | 18 |
21 import trace_inputs | 19 import trace_inputs |
22 | 20 |
23 # Access to a protected member _FOO of a client class | 21 # Access to a protected member _FOO of a client class |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 self.assertEqual(True, actual.tainted) | 71 self.assertEqual(True, actual.tainted) |
74 | 72 |
75 def test_variable_rel(self): | 73 def test_variable_rel(self): |
76 value = trace_inputs.Results.File( | 74 value = trace_inputs.Results.File( |
77 u'/usr', u'foo/bar', None, None, trace_inputs.Results.File.READ) | 75 u'/usr', u'foo/bar', None, None, trace_inputs.Results.File.READ) |
78 actual = value.replace_variables({'$FOO': 'foo'}) | 76 actual = value.replace_variables({'$FOO': 'foo'}) |
79 self.assertEqual('$FOO/bar', actual.path) | 77 self.assertEqual('$FOO/bar', actual.path) |
80 self.assertEqual(os.path.join('/usr', '$FOO/bar'), actual.full_path) | 78 self.assertEqual(os.path.join('/usr', '$FOO/bar'), actual.full_path) |
81 self.assertEqual(True, actual.tainted) | 79 self.assertEqual(True, actual.tainted) |
82 | 80 |
83 def test_native_case_end_with_os_path_sep(self): | |
84 # Make sure the trailing os.path.sep is kept. | |
85 path = trace_inputs.get_native_path_case(ROOT_DIR) + os.path.sep | |
86 self.assertEqual(trace_inputs.get_native_path_case(path), path) | |
87 | |
88 def test_native_case_end_with_dot_os_path_sep(self): | |
89 path = trace_inputs.get_native_path_case(ROOT_DIR + os.path.sep) | |
90 self.assertEqual( | |
91 trace_inputs.get_native_path_case(path + '.' + os.path.sep), | |
92 path) | |
93 | |
94 def test_native_case_non_existing(self): | |
95 # Make sure it doesn't throw on non-existing files. | |
96 non_existing = 'trace_input_test_this_file_should_not_exist' | |
97 path = os.path.expanduser('~/' + non_existing) | |
98 self.assertFalse(os.path.exists(path)) | |
99 path = trace_inputs.get_native_path_case(ROOT_DIR) + os.path.sep | |
100 self.assertEqual(trace_inputs.get_native_path_case(path), path) | |
101 | |
102 def test_strace_filename(self): | 81 def test_strace_filename(self): |
103 filename = u'foo, bar, ~p#o,,ué^t%t.txt' | 82 filename = u'foo, bar, ~p#o,,ué^t%t.txt' |
104 data = 'foo, bar, ~p#o,,u\\303\\251^t%t.txt' | 83 data = 'foo, bar, ~p#o,,u\\303\\251^t%t.txt' |
105 self.assertEqual(filename, trace_inputs.Strace.load_filename(data)) | 84 self.assertEqual(filename, trace_inputs.Strace.load_filename(data)) |
106 | 85 |
107 def test_CsvReader(self): | 86 def test_CsvReader(self): |
108 test_cases = { | 87 test_cases = { |
109 u' Next is empty, , {00000000-0000}': | 88 u' Next is empty, , {00000000-0000}': |
110 [u'Next is empty', u'', u'{00000000-0000}'], | 89 [u'Next is empty', u'', u'{00000000-0000}'], |
111 | 90 |
(...skipping 18 matching lines...) Expand all Loading... |
130 # To whoever wrote this code at Microsoft: You did it wrong. | 109 # To whoever wrote this code at Microsoft: You did it wrong. |
131 u'"cmd.exe", ""C:\\\\Winz\\\\cmd.exe" /k ""C:\\\\MSVS\\\\vc.bat"" x86"': | 110 u'"cmd.exe", ""C:\\\\Winz\\\\cmd.exe" /k ""C:\\\\MSVS\\\\vc.bat"" x86"': |
132 [u'cmd.exe', u'"C:\\\\Winz\\\\cmd.exe" /k "C:\\\\MSVS\\\\vc.bat" x86'], | 111 [u'cmd.exe', u'"C:\\\\Winz\\\\cmd.exe" /k "C:\\\\MSVS\\\\vc.bat" x86'], |
133 } | 112 } |
134 for data, expected in test_cases.iteritems(): | 113 for data, expected in test_cases.iteritems(): |
135 csv = trace_inputs.LogmanTrace.Tracer.CsvReader(StringIO.StringIO(data)) | 114 csv = trace_inputs.LogmanTrace.Tracer.CsvReader(StringIO.StringIO(data)) |
136 actual = [i for i in csv] | 115 actual = [i for i in csv] |
137 self.assertEqual(1, len(actual)) | 116 self.assertEqual(1, len(actual)) |
138 self.assertEqual(expected, actual[0]) | 117 self.assertEqual(expected, actual[0]) |
139 | 118 |
140 if sys.platform in ('darwin', 'win32'): | |
141 def test_native_case_not_sensitive(self): | |
142 # The home directory is almost guaranteed to have mixed upper/lower case | |
143 # letters on both Windows and OSX. | |
144 # This test also ensures that the output is independent on the input | |
145 # string case. | |
146 path = os.path.expanduser(u'~') | |
147 self.assertTrue(os.path.isdir(path)) | |
148 path = path.replace('/', os.path.sep) | |
149 if sys.platform == 'win32': | |
150 # Make sure the drive letter is upper case for consistency. | |
151 path = path[0].upper() + path[1:] | |
152 # This test assumes the variable is in the native path case on disk, this | |
153 # should be the case. Verify this assumption: | |
154 self.assertEqual(path, trace_inputs.get_native_path_case(path)) | |
155 self.assertEqual( | |
156 trace_inputs.get_native_path_case(path.lower()), | |
157 trace_inputs.get_native_path_case(path.upper())) | |
158 | |
159 def test_native_case_not_sensitive_non_existent(self): | |
160 # This test also ensures that the output is independent on the input | |
161 # string case. | |
162 non_existing = os.path.join( | |
163 'trace_input_test_this_dir_should_not_exist', 'really not', '') | |
164 path = os.path.expanduser(os.path.join(u'~', non_existing)) | |
165 path = path.replace('/', os.path.sep) | |
166 self.assertFalse(os.path.exists(path)) | |
167 lower = trace_inputs.get_native_path_case(path.lower()) | |
168 upper = trace_inputs.get_native_path_case(path.upper()) | |
169 # Make sure non-existing element is not modified: | |
170 self.assertTrue(lower.endswith(non_existing.lower())) | |
171 self.assertTrue(upper.endswith(non_existing.upper())) | |
172 self.assertEqual(lower[:-len(non_existing)], upper[:-len(non_existing)]) | |
173 | |
174 if sys.platform != 'win32': | |
175 def test_symlink(self): | |
176 # This test will fail if the checkout is in a symlink. | |
177 actual = trace_inputs.split_at_symlink(None, ROOT_DIR) | |
178 expected = (ROOT_DIR, None, None) | |
179 self.assertEqual(expected, actual) | |
180 | |
181 actual = trace_inputs.split_at_symlink( | |
182 None, os.path.join(BASE_DIR, 'trace_inputs')) | |
183 expected = ( | |
184 os.path.join(BASE_DIR, 'trace_inputs'), None, None) | |
185 self.assertEqual(expected, actual) | |
186 | |
187 actual = trace_inputs.split_at_symlink( | |
188 None, os.path.join(BASE_DIR, 'trace_inputs', 'files2')) | |
189 expected = ( | |
190 os.path.join(BASE_DIR, 'trace_inputs'), 'files2', '') | |
191 self.assertEqual(expected, actual) | |
192 | |
193 actual = trace_inputs.split_at_symlink( | |
194 ROOT_DIR, os.path.join('tests', 'trace_inputs', 'files2')) | |
195 expected = ( | |
196 os.path.join('tests', 'trace_inputs'), 'files2', '') | |
197 self.assertEqual(expected, actual) | |
198 actual = trace_inputs.split_at_symlink( | |
199 ROOT_DIR, os.path.join('tests', 'trace_inputs', 'files2', 'bar')) | |
200 expected = ( | |
201 os.path.join('tests', 'trace_inputs'), 'files2', '/bar') | |
202 self.assertEqual(expected, actual) | |
203 | |
204 def test_native_case_symlink_right_case(self): | |
205 actual = trace_inputs.get_native_path_case( | |
206 os.path.join(BASE_DIR, 'trace_inputs')) | |
207 self.assertEqual('trace_inputs', os.path.basename(actual)) | |
208 | |
209 # Make sure the symlink is not resolved. | |
210 actual = trace_inputs.get_native_path_case( | |
211 os.path.join(BASE_DIR, 'trace_inputs', 'files2')) | |
212 self.assertEqual('files2', os.path.basename(actual)) | |
213 | |
214 if sys.platform == 'darwin': | |
215 def test_native_case_symlink_wrong_case(self): | |
216 base_dir = trace_inputs.get_native_path_case(BASE_DIR) | |
217 trace_inputs_dir = os.path.join(base_dir, 'trace_inputs') | |
218 actual = trace_inputs.get_native_path_case(trace_inputs_dir) | |
219 self.assertEqual(trace_inputs_dir, actual) | |
220 | |
221 # Make sure the symlink is not resolved. | |
222 data = os.path.join(trace_inputs_dir, 'Files2') | |
223 actual = trace_inputs.get_native_path_case(data) | |
224 self.assertEqual( | |
225 os.path.join(trace_inputs_dir, 'files2'), actual) | |
226 | |
227 data = os.path.join(trace_inputs_dir, 'Files2', '') | |
228 actual = trace_inputs.get_native_path_case(data) | |
229 self.assertEqual( | |
230 os.path.join(trace_inputs_dir, 'files2', ''), actual) | |
231 | |
232 data = os.path.join(trace_inputs_dir, 'Files2', 'Child1.py') | |
233 actual = trace_inputs.get_native_path_case(data) | |
234 # TODO(maruel): Should be child1.py. | |
235 self.assertEqual( | |
236 os.path.join(trace_inputs_dir, 'files2', 'Child1.py'), actual) | |
237 | |
238 if sys.platform == 'win32': | |
239 def test_native_case_alternate_datastream(self): | |
240 # Create the file manually, since tempfile doesn't support ADS. | |
241 tempdir = unicode(tempfile.mkdtemp(prefix='trace_inputs')) | |
242 try: | |
243 tempdir = trace_inputs.get_native_path_case(tempdir) | |
244 basename = 'foo.txt' | |
245 filename = basename + ':Zone.Identifier' | |
246 filepath = os.path.join(tempdir, filename) | |
247 open(filepath, 'w').close() | |
248 self.assertEqual(filepath, trace_inputs.get_native_path_case(filepath)) | |
249 data_suffix = ':$DATA' | |
250 self.assertEqual( | |
251 filepath + data_suffix, | |
252 trace_inputs.get_native_path_case(filepath + data_suffix)) | |
253 | |
254 open(filepath + '$DATA', 'w').close() | |
255 self.assertEqual( | |
256 filepath + data_suffix, | |
257 trace_inputs.get_native_path_case(filepath + data_suffix)) | |
258 # Ensure the ADS weren't created as separate file. You love NTFS, don't | |
259 # you? | |
260 self.assertEqual([basename], os.listdir(tempdir)) | |
261 finally: | |
262 shutil.rmtree(tempdir) | |
263 | |
264 | 119 |
265 if sys.platform != 'win32': | 120 if sys.platform != 'win32': |
266 class StraceInputs(unittest.TestCase): | 121 class StraceInputs(unittest.TestCase): |
267 # Represents the root process pid (an arbitrary number). | 122 # Represents the root process pid (an arbitrary number). |
268 _ROOT_PID = 27 | 123 _ROOT_PID = 27 |
269 _CHILD_PID = 14 | 124 _CHILD_PID = 14 |
270 _GRAND_CHILD_PID = 70 | 125 _GRAND_CHILD_PID = 70 |
271 | 126 |
272 @staticmethod | 127 @staticmethod |
273 def _load_context(lines, initial_cwd): | 128 def _load_context(lines, initial_cwd): |
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
794 } | 649 } |
795 self.assertContext(lines, ROOT_DIR, expected, False) | 650 self.assertContext(lines, ROOT_DIR, expected, False) |
796 | 651 |
797 | 652 |
798 if __name__ == '__main__': | 653 if __name__ == '__main__': |
799 logging.basicConfig( | 654 logging.basicConfig( |
800 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) | 655 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
801 if '-v' in sys.argv: | 656 if '-v' in sys.argv: |
802 unittest.TestCase.maxDiff = None | 657 unittest.TestCase.maxDiff = None |
803 unittest.main() | 658 unittest.main() |
OLD | NEW |