Changeset 83631 in webkit


Ignore:
Timestamp:
Apr 12, 2011 1:34:45 PM (13 years ago)
Author:
dpranke@chromium.org
Message:

2011-04-12 Dirk Pranke <dpranke@chromium.org>

Reviewed by Eric Seidel.

webkitpy: fix mock_filesystem abspath to handle relative paths
and add filesystem.chdir() and filesystem.getcwd() to be able
to test this and mock it out.

https://bugs.webkit.org/show_bug.cgi?id=58288

  • Scripts/webkitpy/common/system/filesystem.py:
  • Scripts/webkitpy/common/system/filesystem_mock.py:
  • Scripts/webkitpy/common/system/filesystem_unittest.py:
Location:
trunk/Tools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r83628 r83631  
     12011-04-12  Dirk Pranke  <dpranke@chromium.org>
     2
     3        Reviewed by Eric Seidel.
     4
     5        webkitpy: fix mock_filesystem abspath to handle relative paths
     6        and add filesystem.chdir() and filesystem.getcwd() to be able
     7        to test this and mock it out.
     8
     9        https://bugs.webkit.org/show_bug.cgi?id=58288
     10
     11        * Scripts/webkitpy/common/system/filesystem.py:
     12        * Scripts/webkitpy/common/system/filesystem_mock.py:
     13        * Scripts/webkitpy/common/system/filesystem_unittest.py:
     14
    1152011-04-12  Alice Liu  <alice.liu@apple.com>
    216
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem.py

    r82279 r83631  
    6262        return os.path.basename(path)
    6363
     64    def chdir(self, path):
     65        """Wraps os.chdir()."""
     66        return os.chdir(path)
     67
    6468    def copyfile(self, source, destination):
    6569        """Copies the contents of the file at the given path to the destination
     
    108112                    files.append(self.join(dirpath, filename))
    109113        return files
     114
     115    def getcwd(self):
     116        """Wraps os.getcwd()."""
     117        return os.getcwd()
    110118
    111119    def glob(self, path):
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py

    r82972 r83631  
    3636
    3737class MockFileSystem(object):
    38     def __init__(self, files=None):
     38    def __init__(self, files=None, cwd='/'):
    3939        """Initializes a "mock" filesystem that can be used to completely
    4040        stub out a filesystem.
     
    4949        self._sep = '/'
    5050        self.current_tmpno = 0
     51        self.cwd = cwd
     52        self.dirs = {}
    5153
    5254    def _get_sep(self):
     
    6264
    6365    def abspath(self, path):
    64         if path.endswith(self.sep):
    65             return path[:-1]
    66         return path
     66        if path.startswith(self.sep):
     67            return self.normpath(path)
     68        return self.abspath(self.join(self.cwd, path))
    6769
    6870    def basename(self, path):
    6971        return self._split(path)[1]
     72
     73    def chdir(self, path):
     74        path = self.normpath(path)
     75        if not self.isdir(path):
     76            raise OSError(errno.ENOENT, path, os.strerror(errno.ENOENT))
     77        self.cwd = path
    7078
    7179    def copyfile(self, source, destination):
     
    118126        return files
    119127
     128    def getcwd(self, path):
     129        return self.cwd
     130
    120131    def glob(self, path):
    121132        # FIXME: This only handles a wildcard '*' at the end of the path.
     
    135146        if path in self.files:
    136147            return False
    137         if not path.endswith(self.sep):
    138             path += self.sep
     148        path = self.normpath(path)
     149        if path in self.dirs:
     150            return True
    139151
    140152        # We need to use a copy of the keys here in order to avoid switching
     
    142154        # mid-iteration.
    143155        files = self.files.keys()[:]
    144         return any(f.startswith(path) for f in files)
     156        result = any(f.startswith(path) for f in files)
     157        if result:
     158            self.dirs[path] = True
     159        return result
    145160
    146161    def join(self, *comps):
     
    205220
    206221    def maybe_make_directory(self, *path):
    207         # FIXME: Implement such that subsequent calls to isdir() work?
    208         pass
     222        norm_path = self.normpath(self.join(*path))
     223        if not self.isdir(norm_path):
     224            self.dirs[norm_path] = True
    209225
    210226    def move(self, source, destination):
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py

    r77093 r83631  
    4949        self._this_file = os.path.join(self._this_dir, 'filesystem_unittest.py')
    5050
     51    def test_chdir(self):
     52        fs = FileSystem()
     53        cwd = fs.getcwd()
     54        newdir = '/'
     55        if sys.platform == 'win32':
     56            newdir = 'c:\\'
     57        fs.chdir(newdir)
     58        self.assertEquals(fs.getcwd(), newdir)
     59        fs.chdir(cwd)
     60
     61    def test_chdir__notexists(self):
     62        fs = FileSystem()
     63        newdir = '/dirdoesnotexist'
     64        if sys.platform == 'win32':
     65            newdir = 'c:\\dirdoesnotexist'
     66        self.assertRaises(OSError, fs.chdir, newdir)
     67
    5168    def test_exists__true(self):
    5269        fs = FileSystem()
     
    5673        fs = FileSystem()
    5774        self.assertFalse(fs.exists(self._missing_file))
     75
     76    def test_getcwd(self):
     77        fs = FileSystem()
     78        self.assertTrue(fs.exists(fs.getcwd()))
    5879
    5980    def test_isdir__true(self):
Note: See TracChangeset for help on using the changeset viewer.