| 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 17 matching lines...) Expand all Loading... |
| 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 | 36 |
| 37 class MockFileSystem(object): | 37 class MockFileSystem(object): |
| 38 # pylint: disable=unused-argument |
| 39 |
| 38 sep = '/' | 40 sep = '/' |
| 39 pardir = '..' | 41 pardir = '..' |
| 40 | 42 |
| 41 def __init__(self, files=None, dirs=None, cwd='/'): | 43 def __init__(self, files=None, dirs=None, cwd='/'): |
| 42 """Initializes a "mock" filesystem that can be used to replace the | 44 """Initializes a "mock" filesystem that can be used to replace the |
| 43 FileSystem class in tests. | 45 FileSystem class in tests. |
| 44 | 46 |
| 45 Args: | 47 Args: |
| 46 files: A dictionary of filenames to file contents. A file contents | 48 files: A dictionary of filenames to file contents. A file contents |
| 47 value of None indicates that the file does not exist. | 49 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) | 381 rel_path = rel_path.lstrip(self.sep) |
| 380 elif not common_root == '/': | 382 elif not common_root == '/': |
| 381 # We are in the case typified by the following example: | 383 # We are in the case typified by the following example: |
| 382 # path = "/tmp/foobar", start = "/tmp/foo" -> rel_path = "bar" | 384 # path = "/tmp/foobar", start = "/tmp/foo" -> rel_path = "bar" |
| 383 common_root = self.dirname(common_root) | 385 common_root = self.dirname(common_root) |
| 384 dot_dot += '..' + self.sep | 386 dot_dot += '..' + self.sep |
| 385 rel_path = path[len(common_root) + 1:] | 387 rel_path = path[len(common_root) + 1:] |
| 386 | 388 |
| 387 return dot_dot + rel_path | 389 return dot_dot + rel_path |
| 388 | 390 |
| 389 def remove(self, path): | 391 def remove(self, path, retry=True): |
| 390 if self.files[path] is None: | 392 if self.files[path] is None: |
| 391 self._raise_not_found(path) | 393 self._raise_not_found(path) |
| 392 self.files[path] = None | 394 self.files[path] = None |
| 393 self.written_files[path] = None | 395 self.written_files[path] = None |
| 394 | 396 |
| 395 def rmtree(self, path_to_remove): | 397 def rmtree(self, path_to_remove, ignore_errors=True, onerror=None): |
| 396 path_to_remove = self.normpath(path_to_remove) | 398 path_to_remove = self.normpath(path_to_remove) |
| 397 | 399 |
| 398 for file_path in self.files: | 400 for file_path in self.files: |
| 399 # We need to add a trailing separator to path_to_remove to avoid mat
ching | 401 # 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'. | 402 # 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): | 403 if file_path == path_to_remove or file_path.startswith(path_to_remov
e + self.sep): |
| 402 self.files[file_path] = None | 404 self.files[file_path] = None |
| 403 | 405 |
| 404 def should_remove(directory): | 406 def should_remove(directory): |
| 405 return directory == path_to_remove or directory.startswith(path_to_r
emove + self.sep) | 407 return directory == path_to_remove or directory.startswith(path_to_r
emove + self.sep) |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 | 558 |
| 557 def assertFilesAdded(self, mock_filesystem, files): | 559 def assertFilesAdded(self, mock_filesystem, files): |
| 558 """Assert that the given files where added to the mock_filesystem. | 560 """Assert that the given files where added to the mock_filesystem. |
| 559 | 561 |
| 560 Use in a similar manner to self.assertRaises; | 562 Use in a similar manner to self.assertRaises; |
| 561 | 563 |
| 562 with self.assertFilesAdded(mock_filesystem, {'/newfile': 'contents'}): | 564 with self.assertFilesAdded(mock_filesystem, {'/newfile': 'contents'}): |
| 563 code(mock_filesystem) | 565 code(mock_filesystem) |
| 564 """ | 566 """ |
| 565 return self._AssertFilesAddedContext(self, mock_filesystem, files) | 567 return self._AssertFilesAddedContext(self, mock_filesystem, files) |
| OLD | NEW |