OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # coding=utf-8 |
| 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 |
| 5 # found in the LICENSE file. |
| 6 |
| 7 import logging |
| 8 import os |
| 9 import tempfile |
| 10 import unittest |
| 11 import shutil |
| 12 import sys |
| 13 |
| 14 BASE_DIR = unicode(os.path.dirname(os.path.abspath(__file__))) |
| 15 ROOT_DIR = os.path.dirname(BASE_DIR) |
| 16 sys.path.insert(0, ROOT_DIR) |
| 17 |
| 18 FILE_PATH = unicode(os.path.abspath(__file__)) |
| 19 |
| 20 from utils import file_path |
| 21 |
| 22 |
| 23 class FilePath(unittest.TestCase): |
| 24 def test_native_case_end_with_os_path_sep(self): |
| 25 # Make sure the trailing os.path.sep is kept. |
| 26 path = file_path.get_native_path_case(ROOT_DIR) + os.path.sep |
| 27 self.assertEqual(file_path.get_native_path_case(path), path) |
| 28 |
| 29 def test_native_case_end_with_dot_os_path_sep(self): |
| 30 path = file_path.get_native_path_case(ROOT_DIR + os.path.sep) |
| 31 self.assertEqual( |
| 32 file_path.get_native_path_case(path + '.' + os.path.sep), |
| 33 path) |
| 34 |
| 35 def test_native_case_non_existing(self): |
| 36 # Make sure it doesn't throw on non-existing files. |
| 37 non_existing = 'trace_input_test_this_file_should_not_exist' |
| 38 path = os.path.expanduser('~/' + non_existing) |
| 39 self.assertFalse(os.path.exists(path)) |
| 40 path = file_path.get_native_path_case(ROOT_DIR) + os.path.sep |
| 41 self.assertEqual(file_path.get_native_path_case(path), path) |
| 42 |
| 43 if sys.platform in ('darwin', 'win32'): |
| 44 def test_native_case_not_sensitive(self): |
| 45 # The home directory is almost guaranteed to have mixed upper/lower case |
| 46 # letters on both Windows and OSX. |
| 47 # This test also ensures that the output is independent on the input |
| 48 # string case. |
| 49 path = os.path.expanduser(u'~') |
| 50 self.assertTrue(os.path.isdir(path)) |
| 51 path = path.replace('/', os.path.sep) |
| 52 if sys.platform == 'win32': |
| 53 # Make sure the drive letter is upper case for consistency. |
| 54 path = path[0].upper() + path[1:] |
| 55 # This test assumes the variable is in the native path case on disk, this |
| 56 # should be the case. Verify this assumption: |
| 57 self.assertEqual(path, file_path.get_native_path_case(path)) |
| 58 self.assertEqual( |
| 59 file_path.get_native_path_case(path.lower()), |
| 60 file_path.get_native_path_case(path.upper())) |
| 61 |
| 62 def test_native_case_not_sensitive_non_existent(self): |
| 63 # This test also ensures that the output is independent on the input |
| 64 # string case. |
| 65 non_existing = os.path.join( |
| 66 'trace_input_test_this_dir_should_not_exist', 'really not', '') |
| 67 path = os.path.expanduser(os.path.join(u'~', non_existing)) |
| 68 path = path.replace('/', os.path.sep) |
| 69 self.assertFalse(os.path.exists(path)) |
| 70 lower = file_path.get_native_path_case(path.lower()) |
| 71 upper = file_path.get_native_path_case(path.upper()) |
| 72 # Make sure non-existing element is not modified: |
| 73 self.assertTrue(lower.endswith(non_existing.lower())) |
| 74 self.assertTrue(upper.endswith(non_existing.upper())) |
| 75 self.assertEqual(lower[:-len(non_existing)], upper[:-len(non_existing)]) |
| 76 |
| 77 if sys.platform != 'win32': |
| 78 def test_symlink(self): |
| 79 # This test will fail if the checkout is in a symlink. |
| 80 actual = file_path.split_at_symlink(None, ROOT_DIR) |
| 81 expected = (ROOT_DIR, None, None) |
| 82 self.assertEqual(expected, actual) |
| 83 |
| 84 actual = file_path.split_at_symlink( |
| 85 None, os.path.join(BASE_DIR, 'trace_inputs')) |
| 86 expected = ( |
| 87 os.path.join(BASE_DIR, 'trace_inputs'), None, None) |
| 88 self.assertEqual(expected, actual) |
| 89 |
| 90 actual = file_path.split_at_symlink( |
| 91 None, os.path.join(BASE_DIR, 'trace_inputs', 'files2')) |
| 92 expected = ( |
| 93 os.path.join(BASE_DIR, 'trace_inputs'), 'files2', '') |
| 94 self.assertEqual(expected, actual) |
| 95 |
| 96 actual = file_path.split_at_symlink( |
| 97 ROOT_DIR, os.path.join('tests', 'trace_inputs', 'files2')) |
| 98 expected = ( |
| 99 os.path.join('tests', 'trace_inputs'), 'files2', '') |
| 100 self.assertEqual(expected, actual) |
| 101 actual = file_path.split_at_symlink( |
| 102 ROOT_DIR, os.path.join('tests', 'trace_inputs', 'files2', 'bar')) |
| 103 expected = ( |
| 104 os.path.join('tests', 'trace_inputs'), 'files2', '/bar') |
| 105 self.assertEqual(expected, actual) |
| 106 |
| 107 def test_native_case_symlink_right_case(self): |
| 108 actual = file_path.get_native_path_case( |
| 109 os.path.join(BASE_DIR, 'trace_inputs')) |
| 110 self.assertEqual('trace_inputs', os.path.basename(actual)) |
| 111 |
| 112 # Make sure the symlink is not resolved. |
| 113 actual = file_path.get_native_path_case( |
| 114 os.path.join(BASE_DIR, 'trace_inputs', 'files2')) |
| 115 self.assertEqual('files2', os.path.basename(actual)) |
| 116 |
| 117 if sys.platform == 'darwin': |
| 118 def test_native_case_symlink_wrong_case(self): |
| 119 base_dir = file_path.get_native_path_case(BASE_DIR) |
| 120 trace_inputs_dir = os.path.join(base_dir, 'trace_inputs') |
| 121 actual = file_path.get_native_path_case(trace_inputs_dir) |
| 122 self.assertEqual(trace_inputs_dir, actual) |
| 123 |
| 124 # Make sure the symlink is not resolved. |
| 125 data = os.path.join(trace_inputs_dir, 'Files2') |
| 126 actual = file_path.get_native_path_case(data) |
| 127 self.assertEqual( |
| 128 os.path.join(trace_inputs_dir, 'files2'), actual) |
| 129 |
| 130 data = os.path.join(trace_inputs_dir, 'Files2', '') |
| 131 actual = file_path.get_native_path_case(data) |
| 132 self.assertEqual( |
| 133 os.path.join(trace_inputs_dir, 'files2', ''), actual) |
| 134 |
| 135 data = os.path.join(trace_inputs_dir, 'Files2', 'Child1.py') |
| 136 actual = file_path.get_native_path_case(data) |
| 137 # TODO(maruel): Should be child1.py. |
| 138 self.assertEqual( |
| 139 os.path.join(trace_inputs_dir, 'files2', 'Child1.py'), actual) |
| 140 |
| 141 if sys.platform == 'win32': |
| 142 def test_native_case_alternate_datastream(self): |
| 143 # Create the file manually, since tempfile doesn't support ADS. |
| 144 tempdir = unicode(tempfile.mkdtemp(prefix='trace_inputs')) |
| 145 try: |
| 146 tempdir = file_path.get_native_path_case(tempdir) |
| 147 basename = 'foo.txt' |
| 148 filename = basename + ':Zone.Identifier' |
| 149 filepath = os.path.join(tempdir, filename) |
| 150 open(filepath, 'w').close() |
| 151 self.assertEqual(filepath, file_path.get_native_path_case(filepath)) |
| 152 data_suffix = ':$DATA' |
| 153 self.assertEqual( |
| 154 filepath + data_suffix, |
| 155 file_path.get_native_path_case(filepath + data_suffix)) |
| 156 |
| 157 open(filepath + '$DATA', 'w').close() |
| 158 self.assertEqual( |
| 159 filepath + data_suffix, |
| 160 file_path.get_native_path_case(filepath + data_suffix)) |
| 161 # Ensure the ADS weren't created as separate file. You love NTFS, don't |
| 162 # you? |
| 163 self.assertEqual([basename], os.listdir(tempdir)) |
| 164 finally: |
| 165 shutil.rmtree(tempdir) |
| 166 |
| 167 |
| 168 if __name__ == '__main__': |
| 169 logging.basicConfig( |
| 170 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
| 171 if '-v' in sys.argv: |
| 172 unittest.TestCase.maxDiff = None |
| 173 unittest.main() |
OLD | NEW |