| OLD | NEW |
| 1 # Copyright (C) 2009 Google Inc. All rights reserved. | 1 # Copyright (C) 2009 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | 28 |
| 29 import errno | 29 import errno |
| 30 import hashlib | 30 import hashlib |
| 31 import os | 31 import os |
| 32 import re | 32 import re |
| 33 import StringIO | 33 import StringIO |
| 34 import unittest | 34 import unittest |
| 35 | 35 |
| 36 from webkitpy.common.system.filesystem import _remove_contents |
| 37 |
| 36 | 38 |
| 37 class MockFileSystem(object): | 39 class MockFileSystem(object): |
| 40 # pylint: disable=unused-argument |
| 41 |
| 38 sep = '/' | 42 sep = '/' |
| 39 pardir = '..' | 43 pardir = '..' |
| 40 | 44 |
| 41 def __init__(self, files=None, dirs=None, cwd='/'): | 45 def __init__(self, files=None, dirs=None, cwd='/'): |
| 42 """Initializes a "mock" filesystem that can be used to replace the | 46 """Initializes a "mock" filesystem that can be used to replace the |
| 43 FileSystem class in tests. | 47 FileSystem class in tests. |
| 44 | 48 |
| 45 Args: | 49 Args: |
| 46 files: A dictionary of filenames to file contents. A file contents | 50 files: A dictionary of filenames to file contents. A file contents |
| 47 value of None indicates that the file does not exist. | 51 value of None indicates that the file does not exist. |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 rel_path = rel_path.lstrip(self.sep) | 383 rel_path = rel_path.lstrip(self.sep) |
| 380 elif not common_root == '/': | 384 elif not common_root == '/': |
| 381 # We are in the case typified by the following example: | 385 # We are in the case typified by the following example: |
| 382 # path = "/tmp/foobar", start = "/tmp/foo" -> rel_path = "bar" | 386 # path = "/tmp/foobar", start = "/tmp/foo" -> rel_path = "bar" |
| 383 common_root = self.dirname(common_root) | 387 common_root = self.dirname(common_root) |
| 384 dot_dot += '..' + self.sep | 388 dot_dot += '..' + self.sep |
| 385 rel_path = path[len(common_root) + 1:] | 389 rel_path = path[len(common_root) + 1:] |
| 386 | 390 |
| 387 return dot_dot + rel_path | 391 return dot_dot + rel_path |
| 388 | 392 |
| 389 def remove(self, path): | 393 def remove(self, path, retry=True): |
| 390 if self.files[path] is None: | 394 if self.files[path] is None: |
| 391 self._raise_not_found(path) | 395 self._raise_not_found(path) |
| 392 self.files[path] = None | 396 self.files[path] = None |
| 393 self.written_files[path] = None | 397 self.written_files[path] = None |
| 394 | 398 |
| 395 def rmtree(self, path_to_remove): | 399 def rmtree(self, path_to_remove, ignore_errors=True, onerror=None): |
| 396 path_to_remove = self.normpath(path_to_remove) | 400 path_to_remove = self.normpath(path_to_remove) |
| 397 | 401 |
| 398 for file_path in self.files: | 402 for file_path in self.files: |
| 399 # We need to add a trailing separator to path_to_remove to avoid mat
ching | 403 # We need to add a trailing separator to path_to_remove to avoid mat
ching |
| 400 # cases like path_to_remove='/foo/b' and file_path='/foo/bar/baz'. | 404 # cases like path_to_remove='/foo/b' and file_path='/foo/bar/baz'. |
| 401 if file_path == path_to_remove or file_path.startswith(path_to_remov
e + self.sep): | 405 if file_path == path_to_remove or file_path.startswith(path_to_remov
e + self.sep): |
| 402 self.files[file_path] = None | 406 self.files[file_path] = None |
| 403 | 407 |
| 404 def should_remove(directory): | 408 def should_remove(directory): |
| 405 return directory == path_to_remove or directory.startswith(path_to_r
emove + self.sep) | 409 return directory == path_to_remove or directory.startswith(path_to_r
emove + self.sep) |
| 406 | 410 |
| 407 self.dirs = {d for d in self.dirs if not should_remove(d)} | 411 self.dirs = {d for d in self.dirs if not should_remove(d)} |
| 408 | 412 |
| 413 def remove_contents(self, dirname): |
| 414 return _remove_contents(self, dirname, sleep=lambda *args, **kw: None) |
| 415 |
| 409 def copytree(self, source, destination): | 416 def copytree(self, source, destination): |
| 410 source = self.normpath(source) | 417 source = self.normpath(source) |
| 411 destination = self.normpath(destination) | 418 destination = self.normpath(destination) |
| 412 | 419 |
| 413 for source_file in list(self.files): | 420 for source_file in list(self.files): |
| 414 if source_file.startswith(source): | 421 if source_file.startswith(source): |
| 415 destination_path = self.join(destination, self.relpath(source_fi
le, source)) | 422 destination_path = self.join(destination, self.relpath(source_fi
le, source)) |
| 416 self.maybe_make_directory(self.dirname(destination_path)) | 423 self.maybe_make_directory(self.dirname(destination_path)) |
| 417 self.files[destination_path] = self.files[source_file] | 424 self.files[destination_path] = self.files[source_file] |
| 418 | 425 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 | 563 |
| 557 def assertFilesAdded(self, mock_filesystem, files): | 564 def assertFilesAdded(self, mock_filesystem, files): |
| 558 """Assert that the given files where added to the mock_filesystem. | 565 """Assert that the given files where added to the mock_filesystem. |
| 559 | 566 |
| 560 Use in a similar manner to self.assertRaises; | 567 Use in a similar manner to self.assertRaises; |
| 561 | 568 |
| 562 with self.assertFilesAdded(mock_filesystem, {'/newfile': 'contents'}): | 569 with self.assertFilesAdded(mock_filesystem, {'/newfile': 'contents'}): |
| 563 code(mock_filesystem) | 570 code(mock_filesystem) |
| 564 """ | 571 """ |
| 565 return self._AssertFilesAddedContext(self, mock_filesystem, files) | 572 return self._AssertFilesAddedContext(self, mock_filesystem, files) |
| OLD | NEW |