diff options
Diffstat (limited to 'Tools/Scripts/webkitpy/common/system/filesystem_mock.py')
-rw-r--r-- | Tools/Scripts/webkitpy/common/system/filesystem_mock.py | 71 |
1 files changed, 44 insertions, 27 deletions
diff --git a/Tools/Scripts/webkitpy/common/system/filesystem_mock.py b/Tools/Scripts/webkitpy/common/system/filesystem_mock.py index 0004944..aa79a8c 100644 --- a/Tools/Scripts/webkitpy/common/system/filesystem_mock.py +++ b/Tools/Scripts/webkitpy/common/system/filesystem_mock.py @@ -28,9 +28,11 @@ import errno import os -import path import re +from webkitpy.common.system import path +from webkitpy.common.system import ospath + class MockFileSystem(object): def __init__(self, files=None): @@ -44,17 +46,23 @@ class MockFileSystem(object): """ self.files = files or {} self.written_files = {} - self.sep = '/' + self._sep = '/' self.current_tmpno = 0 + def _get_sep(self): + return self._sep + + sep = property(_get_sep, doc="pathname separator") + def _raise_not_found(self, path): raise IOError(errno.ENOENT, path, os.strerror(errno.ENOENT)) def _split(self, path): - idx = path.rfind('/') - return (path[0:idx], path[idx + 1:]) + return path.rsplit(self.sep, 1) def abspath(self, path): + if path.endswith(self.sep): + return path[:-1] return path def basename(self, path): @@ -69,6 +77,7 @@ class MockFileSystem(object): raise IOError(errno.EISDIR, destination, os.strerror(errno.ISDIR)) self.files[destination] = self.files[source] + self.written_files[destination] = self.files[source] def dirname(self, path): return self._split(path)[0] @@ -90,10 +99,10 @@ class MockFileSystem(object): if self.basename(path) in dirs_to_skip: return [] - if not path.endswith('/'): - path += '/' + if not path.endswith(self.sep): + path += self.sep - dir_substrings = ['/' + d + '/' for d in dirs_to_skip] + dir_substrings = [self.sep + d + self.sep for d in dirs_to_skip] for filename in self.files: if not filename.startswith(path): continue @@ -117,7 +126,7 @@ class MockFileSystem(object): return [f for f in self.files if f == path] def isabs(self, path): - return path.startswith('/') + return path.startswith(self.sep) def isfile(self, path): return path in self.files and self.files[path] is not None @@ -125,8 +134,8 @@ class MockFileSystem(object): def isdir(self, path): if path in self.files: return False - if not path.endswith('/'): - path += '/' + if not path.endswith(self.sep): + path += self.sep # We need to use a copy of the keys here in order to avoid switching # to a different thread and potentially modifying the dict in @@ -135,22 +144,24 @@ class MockFileSystem(object): return any(f.startswith(path) for f in files) def join(self, *comps): - return re.sub(re.escape(os.path.sep), '/', os.path.join(*comps)) + # FIXME: might want tests for this and/or a better comment about how + # it works. + return re.sub(re.escape(os.path.sep), self.sep, os.path.join(*comps)) def listdir(self, path): if not self.isdir(path): raise OSError("%s is not a directory" % path) - if not path.endswith('/'): - path += '/' + if not path.endswith(self.sep): + path += self.sep dirs = [] files = [] for f in self.files: if self.exists(f) and f.startswith(path): remaining = f[len(path):] - if '/' in remaining: - dir = remaining[:remaining.index('/')] + if self.sep in remaining: + dir = remaining[:remaining.index(self.sep)] if not dir in dirs: dirs.append(dir) else: @@ -164,7 +175,7 @@ class MockFileSystem(object): def _mktemp(self, suffix='', prefix='tmp', dir=None, **kwargs): if dir is None: - dir = '/__im_tmp' + dir = self.sep + '__im_tmp' curno = self.current_tmpno self.current_tmpno += 1 return self.join(dir, "%s_%u_%s" % (prefix, curno, suffix)) @@ -196,24 +207,26 @@ class MockFileSystem(object): # FIXME: Implement such that subsequent calls to isdir() work? pass - def move(self, src, dst): - if self.files[src] is None: - self._raise_not_found(src) - self.files[dst] = self.files[src] - self.files[src] = None + def move(self, source, destination): + if self.files[source] is None: + self._raise_not_found(source) + self.files[destination] = self.files[source] + self.written_files[destination] = self.files[destination] + self.files[source] = None + self.written_files[source] = None def normpath(self, path): return path - def open_binary_tempfile(self, suffix): + def open_binary_tempfile(self, suffix=''): path = self._mktemp(suffix) - return WritableFileObject(self, path), path + return (WritableFileObject(self, path), path) def open_text_file_for_writing(self, path, append=False): return WritableFileObject(self, path, append) def read_text_file(self, path): - return self.read_binary_file(path) + return self.read_binary_file(path).decode('utf-8') def read_binary_file(self, path): # Intentionally raises KeyError if we don't recognize the path. @@ -221,14 +234,18 @@ class MockFileSystem(object): self._raise_not_found(path) return self.files[path] + def relpath(self, path, start='.'): + return ospath.relpath(path, start, self.abspath, self.sep) + def remove(self, path): if self.files[path] is None: self._raise_not_found(path) self.files[path] = None + self.written_files[path] = None def rmtree(self, path): - if not path.endswith('/'): - path += '/' + if not path.endswith(self.sep): + path += self.sep for f in self.files: if f.startswith(path): @@ -241,7 +258,7 @@ class MockFileSystem(object): return (path[0:idx], path[idx:]) def write_text_file(self, path, contents): - return self.write_binary_file(path, contents) + return self.write_binary_file(path, contents.encode('utf-8')) def write_binary_file(self, path, contents): self.files[path] = contents |