| OLD | NEW | 
| (Empty) |  | 
 |   1 # Copyright 2014 The Chromium Authors. All rights reserved. | 
 |   2 # Use of this source code is governed by a BSD-style license that can be | 
 |   3 # found in the LICENSE file. | 
 |   4  | 
 |   5 import os | 
 |   6  | 
 |   7  | 
 |   8 class FakeFilesystem(object): | 
 |   9   """A fake implementation of the Filesystem class in filesystem.py. | 
 |  10  | 
 |  11   This is a stripped-down version of the FakeHost class provided in the | 
 |  12   TYP (Test Your Project) framework: https://github.com/dpranke/typ. | 
 |  13   """ | 
 |  14   def __init__(self, files=None, cwd='/'): | 
 |  15     self.sep = '/' | 
 |  16     self.cwd = cwd | 
 |  17     self.files = files or {} | 
 |  18     self.dirs = set(self.dirname(f) for f in self.files) | 
 |  19  | 
 |  20   def abspath(self, relpath): | 
 |  21     if relpath.startswith(self.sep): | 
 |  22       return relpath | 
 |  23     return self.join(self.cwd, relpath) | 
 |  24  | 
 |  25   def basename(self, path): | 
 |  26     return path.split(self.sep)[-1] | 
 |  27  | 
 |  28   def dirname(self, path): | 
 |  29     return self.sep.join(path.split(self.sep)[:-1]) | 
 |  30  | 
 |  31   def exists(self, *comps): | 
 |  32     path = self.abspath(*comps) | 
 |  33     return ((path in self.files and self.files[path] is not None) or | 
 |  34              path in self.dirs) | 
 |  35  | 
 |  36   def join(self, *comps): | 
 |  37     p = '' | 
 |  38     for c in comps: | 
 |  39       if c in ('', '.'): | 
 |  40         continue | 
 |  41       elif c.startswith(self.sep): | 
 |  42         p = c | 
 |  43       elif p: | 
 |  44         p += self.sep + c | 
 |  45       else: | 
 |  46         p = c | 
 |  47  | 
 |  48     # Handle ./ | 
 |  49     p = p.replace('/./', self.sep) | 
 |  50  | 
 |  51     # Handle ../ | 
 |  52     while '../' in p: | 
 |  53       comps = p.split(self.sep) | 
 |  54       idx = comps.index('..') | 
 |  55       comps = comps[:idx-1] + comps[idx+1:] | 
 |  56       p = self.sep.join(comps) | 
 |  57     return p | 
 |  58  | 
 |  59   def listfiles(self, path): | 
 |  60     return [filepath.replace(path + self.sep, '') for filepath in self.files | 
 |  61             if (self.dirname(filepath) == path and | 
 |  62                 not self.basename(filepath).startswith('.'))] | 
 |  63  | 
 |  64   def read_text_file(self, path): | 
 |  65     return self.files[self.abspath(path)] | 
 |  66  | 
 |  67   def relpath(self, path, start='.'): | 
 |  68     full_path = self.abspath(path) | 
 |  69     full_start = self.abspath(start) | 
 |  70  | 
 |  71     # TODO: handle cases where path is not directly under start. | 
 |  72     assert full_start in full_path | 
 |  73     return full_path[len(full_start) + 1:] | 
 |  74  | 
 |  75   def write_text_file(self, path, contents): | 
 |  76     self.files[self.abspath(path)] = contents | 
| OLD | NEW |