| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # coding=utf-8 | 2 # coding=utf-8 |
| 3 # Copyright 2013 The Swarming Authors. All rights reserved. | 3 # Copyright 2013 The Swarming Authors. All rights reserved. |
| 4 # Use of this source code is governed under the Apache License, Version 2.0 that | 4 # Use of this source code is governed under the Apache License, Version 2.0 that |
| 5 # can be found in the LICENSE file. | 5 # can be found in the LICENSE file. |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import tempfile | 9 import tempfile |
| 10 import unittest | 10 import unittest |
| 11 import shutil | |
| 12 import StringIO | 11 import StringIO |
| 13 import subprocess | 12 import subprocess |
| 14 import sys | 13 import sys |
| 15 import time | 14 import time |
| 16 | 15 |
| 17 BASE_DIR = unicode(os.path.dirname(os.path.abspath(__file__))) | 16 BASE_DIR = unicode(os.path.dirname(os.path.abspath(__file__))) |
| 18 ROOT_DIR = os.path.dirname(BASE_DIR) | 17 ROOT_DIR = os.path.dirname(BASE_DIR) |
| 19 sys.path.insert(0, ROOT_DIR) | 18 sys.path.insert(0, ROOT_DIR) |
| 20 sys.path.insert(0, os.path.join(ROOT_DIR, 'third_party')) | 19 sys.path.insert(0, os.path.join(ROOT_DIR, 'third_party')) |
| 21 | 20 |
| 22 FILE_PATH = unicode(os.path.abspath(__file__)) | 21 FILE_PATH = unicode(os.path.abspath(__file__)) |
| 23 | 22 |
| 24 from depot_tools import auto_stub | 23 from depot_tools import auto_stub |
| 24 from depot_tools import fix_encoding |
| 25 import test_utils | 25 import test_utils |
| 26 from utils import file_path | 26 from utils import file_path |
| 27 | 27 |
| 28 | 28 |
| 29 def write_content(filepath, content): | 29 def write_content(filepath, content): |
| 30 with open(filepath, 'wb') as f: | 30 with open(filepath, 'wb') as f: |
| 31 f.write(content) | 31 f.write(content) |
| 32 | 32 |
| 33 | 33 |
| 34 class FilePathTest(auto_stub.TestCase): | 34 class FilePathTest(auto_stub.TestCase): |
| 35 def setUp(self): | 35 def setUp(self): |
| 36 super(FilePathTest, self).setUp() | 36 super(FilePathTest, self).setUp() |
| 37 self._tempdir = None | 37 self._tempdir = None |
| 38 | 38 |
| 39 def tearDown(self): | 39 def tearDown(self): |
| 40 if self._tempdir: | 40 try: |
| 41 for dirpath, dirnames, filenames in os.walk(self._tempdir, topdown=True): | 41 if self._tempdir: |
| 42 for filename in filenames: | 42 for dirpath, dirnames, filenames in os.walk( |
| 43 file_path.set_read_only(os.path.join(dirpath, filename), False) | 43 self._tempdir, topdown=True): |
| 44 for dirname in dirnames: | 44 for filename in filenames: |
| 45 file_path.set_read_only(os.path.join(dirpath, dirname), False) | 45 file_path.set_read_only(os.path.join(dirpath, filename), False) |
| 46 shutil.rmtree(self._tempdir) | 46 for dirname in dirnames: |
| 47 super(FilePathTest, self).tearDown() | 47 file_path.set_read_only(os.path.join(dirpath, dirname), False) |
| 48 file_path.rmtree(self._tempdir) |
| 49 finally: |
| 50 super(FilePathTest, self).tearDown() |
| 48 | 51 |
| 49 @property | 52 @property |
| 50 def tempdir(self): | 53 def tempdir(self): |
| 51 if not self._tempdir: | 54 if not self._tempdir: |
| 52 self._tempdir = tempfile.mkdtemp(prefix=u'run_isolated_test') | 55 self._tempdir = tempfile.mkdtemp(prefix=u'run_isolated_test') |
| 53 return self._tempdir | 56 return self._tempdir |
| 54 | 57 |
| 55 def assertFileMode(self, filepath, mode, umask=None): | 58 def assertFileMode(self, filepath, mode, umask=None): |
| 56 umask = test_utils.umask() if umask is None else umask | 59 umask = test_utils.umask() if umask is None else umask |
| 57 actual = os.stat(filepath).st_mode | 60 actual = os.stat(filepath).st_mode |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 file_path.get_native_path_case(filepath + data_suffix)) | 246 file_path.get_native_path_case(filepath + data_suffix)) |
| 244 | 247 |
| 245 open(filepath + '$DATA', 'w').close() | 248 open(filepath + '$DATA', 'w').close() |
| 246 self.assertEqual( | 249 self.assertEqual( |
| 247 filepath + data_suffix, | 250 filepath + data_suffix, |
| 248 file_path.get_native_path_case(filepath + data_suffix)) | 251 file_path.get_native_path_case(filepath + data_suffix)) |
| 249 # Ensure the ADS weren't created as separate file. You love NTFS, don't | 252 # Ensure the ADS weren't created as separate file. You love NTFS, don't |
| 250 # you? | 253 # you? |
| 251 self.assertEqual([basename], os.listdir(tempdir)) | 254 self.assertEqual([basename], os.listdir(tempdir)) |
| 252 finally: | 255 finally: |
| 253 shutil.rmtree(tempdir) | 256 file_path.rmtree(tempdir) |
| 254 | 257 |
| 255 def test_rmtree_win(self): | 258 def test_rmtree_win(self): |
| 256 # Mock our sleep for faster test case execution. | 259 # Mock our sleep for faster test case execution. |
| 257 sleeps = [] | 260 sleeps = [] |
| 258 self.mock(time, 'sleep', sleeps.append) | 261 self.mock(time, 'sleep', sleeps.append) |
| 259 self.mock(sys, 'stderr', StringIO.StringIO()) | 262 self.mock(sys, 'stderr', StringIO.StringIO()) |
| 260 | 263 |
| 261 # Open a child process, so the file is locked. | 264 # Open a child process, so the file is locked. |
| 262 subdir = os.path.join(self.tempdir, 'to_be_deleted') | 265 subdir = os.path.join(self.tempdir, 'to_be_deleted') |
| 263 os.mkdir(subdir) | 266 os.mkdir(subdir) |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 os.path.join(BASE_DIR, 'trace_inputs')) | 351 os.path.join(BASE_DIR, 'trace_inputs')) |
| 349 self.assertEqual('trace_inputs', os.path.basename(actual)) | 352 self.assertEqual('trace_inputs', os.path.basename(actual)) |
| 350 | 353 |
| 351 # Make sure the symlink is not resolved. | 354 # Make sure the symlink is not resolved. |
| 352 actual = file_path.get_native_path_case( | 355 actual = file_path.get_native_path_case( |
| 353 os.path.join(BASE_DIR, 'trace_inputs', 'files2')) | 356 os.path.join(BASE_DIR, 'trace_inputs', 'files2')) |
| 354 self.assertEqual('files2', os.path.basename(actual)) | 357 self.assertEqual('files2', os.path.basename(actual)) |
| 355 | 358 |
| 356 | 359 |
| 357 if __name__ == '__main__': | 360 if __name__ == '__main__': |
| 361 fix_encoding.fix_encoding() |
| 358 logging.basicConfig( | 362 logging.basicConfig( |
| 359 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) | 363 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
| 360 if '-v' in sys.argv: | 364 if '-v' in sys.argv: |
| 361 unittest.TestCase.maxDiff = None | 365 unittest.TestCase.maxDiff = None |
| 362 unittest.main() | 366 unittest.main() |
| OLD | NEW |