Title: [94618] trunk/Tools
Revision
94618
Author
[email protected]
Date
2011-09-06 18:43:51 -0700 (Tue, 06 Sep 2011)

Log Message

fix MockFileSystem.glob(), refactor filesystem tests
https://bugs.webkit.org/show_bug.cgi?id=67462

Reviewed by Eric Seidel.

In debugging why my proposed change for MockFileSystem.glob()
in bug 66228 didn't work, I stumbled across two real bugs
that cancelled each other out for the unit tests. I fixed those
(glob shouldn't recurse into subdirs, and we weren't populating
self.dirs from self.files properly).

I have also created a "mixin" class for tests that can be shared
between the real filesystem and the mock filesystem - MockFileSystem
is being used enough it needs its own tests.

* Scripts/webkitpy/common/system/filesystem_mock.py:
* Scripts/webkitpy/common/system/filesystem_mock_unittest.py: Added.
* Scripts/webkitpy/common/system/filesystem_unittest.py:

Modified Paths

Added Paths

Diff

Modified: trunk/Tools/ChangeLog (94617 => 94618)


--- trunk/Tools/ChangeLog	2011-09-07 01:25:26 UTC (rev 94617)
+++ trunk/Tools/ChangeLog	2011-09-07 01:43:51 UTC (rev 94618)
@@ -1,3 +1,24 @@
+2011-09-01  Dirk Pranke  <[email protected]>
+
+        fix MockFileSystem.glob(), refactor filesystem tests
+        https://bugs.webkit.org/show_bug.cgi?id=67462
+
+        Reviewed by Eric Seidel.
+
+        In debugging why my proposed change for MockFileSystem.glob()
+        in bug 66228 didn't work, I stumbled across two real bugs
+        that cancelled each other out for the unit tests. I fixed those
+        (glob shouldn't recurse into subdirs, and we weren't populating
+        self.dirs from self.files properly).
+
+        I have also created a "mixin" class for tests that can be shared
+        between the real filesystem and the mock filesystem - MockFileSystem
+        is being used enough it needs its own tests.
+
+        * Scripts/webkitpy/common/system/filesystem_mock.py:
+        * Scripts/webkitpy/common/system/filesystem_mock_unittest.py: Added.
+        * Scripts/webkitpy/common/system/filesystem_unittest.py:
+
 2011-09-06  Anders Carlsson  <[email protected]>
 
         Move NPAPI headers in bridge to plugins

Modified: trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py (94617 => 94618)


--- trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py	2011-09-07 01:25:26 UTC (rev 94617)
+++ trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py	2011-09-07 01:43:51 UTC (rev 94618)
@@ -50,7 +50,13 @@
         self._sep = '/'
         self.current_tmpno = 0
         self.cwd = cwd
-        self.dirs = dirs or set()
+        self.dirs = set(dirs or [])
+        self.dirs.add(cwd)
+        for f in self.files:
+            d = self.dirname(f)
+            while not d in self.dirs:
+                self.dirs.add(d)
+                d = self.dirname(d)
 
     def _get_sep(self):
         return self._sep
@@ -142,14 +148,11 @@
         return self.cwd
 
     def glob(self, glob_string):
-        # FIXME: This only handles the simplest of wildcard matches.
-        wildcard_index = glob_string.find('*')
-        if wildcard_index != -1:
-            before_wildcard = glob_string[:wildcard_index - 1]
-            after_wildcard = glob_string[wildcard_index + 1:]
-            path_filter = lambda path: path.startswith(before_wildcard) and path.endswith(after_wildcard)
-        else:
-            path_filter = lambda path: glob_string == path
+        # FIXME: This handles '*', but not '?', '[', or ']'.
+        glob_string = re.escape(glob_string)
+        glob_string = glob_string.replace('\\*', '[^\\/]*') + '$'
+        glob_string = glob_string.replace('\\/', '/')
+        path_filter = lambda path: re.match(glob_string, path)
 
         # We could use fnmatch.fnmatch, but that might not do the right thing on windows.
         existing_files = [path for path, contents in self.files.items() if contents is not None]

Added: trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock_unittest.py (0 => 94618)


--- trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock_unittest.py	                        (rev 0)
+++ trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock_unittest.py	2011-09-07 01:43:51 UTC (rev 94618)
@@ -0,0 +1,46 @@
+# Copyright (C) 2011 Google Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#    * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#    * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import unittest
+
+from webkitpy.common.system import filesystem_mock
+from webkitpy.common.system import filesystem_unittest
+
+
+class MockFileSystemTest(unittest.TestCase, filesystem_unittest.GenericFileSystemTests):
+    def setUp(self):
+        self.fs = filesystem_mock.MockFileSystem()
+        self.setup_generic_test_dir()
+
+    def tearDown(self):
+        self.teardown_generic_test_dir()
+        self.fs = None
+
+
+if __name__ == '__main__':
+    unittest.main()
Property changes on: trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock_unittest.py
___________________________________________________________________

Added: svn:eol-style

Modified: trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py (94617 => 94618)


--- trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py	2011-09-07 01:25:26 UTC (rev 94617)
+++ trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py	2011-09-07 01:43:51 UTC (rev 94618)
@@ -42,12 +42,53 @@
 from filesystem import FileSystem
 
 
-class FileSystemTest(unittest.TestCase):
+class GenericFileSystemTests(object):
+    """Tests that should pass on either a real or mock filesystem."""
+    def setup_generic_test_dir(self):
+        fs = self.fs
+        self.generic_test_dir = str(self.fs.mkdtemp())
+        self.orig_cwd = fs.getcwd()
+        fs.chdir(self.generic_test_dir)
+        fs.write_text_file('foo.txt', 'foo')
+        fs.write_text_file('foobar', 'foobar')
+        fs.maybe_make_directory('foodir')
+        fs.write_text_file(fs.join('foodir', 'baz'), 'baz')
+        fs.chdir(self.orig_cwd)
+
+    def teardown_generic_test_dir(self):
+        self.fs.rmtree(self.generic_test_dir)
+        self.fs.chdir(self.orig_cwd)
+        self.generic_test_dir = None
+
+    def test_glob__trailing_asterisk(self):
+        self.fs.chdir(self.generic_test_dir)
+        self.assertEquals(set(self.fs.glob('fo*')), set(['foo.txt', 'foobar', 'foodir']))
+
+    def test_glob__leading_asterisk(self):
+        self.fs.chdir(self.generic_test_dir)
+        self.assertEquals(set(self.fs.glob('*xt')), set(['foo.txt']))
+
+    def test_glob__middle_asterisk(self):
+        self.fs.chdir(self.generic_test_dir)
+        self.assertEquals(set(self.fs.glob('f*r')), set(['foobar', 'foodir']))
+
+    def test_glob__period_is_escaped(self):
+        self.fs.chdir(self.generic_test_dir)
+        self.assertEquals(set(self.fs.glob('foo.*')), set(['foo.txt']))
+
+class RealFileSystemTest(unittest.TestCase, GenericFileSystemTests):
     def setUp(self):
+        self.fs = FileSystem()
+        self.setup_generic_test_dir()
+
         self._this_dir = os.path.dirname(os.path.abspath(__file__))
         self._missing_file = os.path.join(self._this_dir, 'missing_file.py')
         self._this_file = os.path.join(self._this_dir, 'filesystem_unittest.py')
 
+    def tearDown(self):
+        self.teardown_generic_test_dir()
+        self.fs = None
+
     def test_chdir(self):
         fs = FileSystem()
         cwd = fs.getcwd()
@@ -195,11 +236,11 @@
         self.assertRaises(IOError, fs.read_text_file, self._missing_file)
 
     def test_remove_file_with_retry(self):
-        FileSystemTest._remove_failures = 2
+        RealFileSystemTest._remove_failures = 2
 
         def remove_with_exception(filename):
-            FileSystemTest._remove_failures -= 1
-            if FileSystemTest._remove_failures >= 0:
+            RealFileSystemTest._remove_failures -= 1
+            if RealFileSystemTest._remove_failures >= 0:
                 try:
                     raise WindowsError
                 except NameError:
@@ -207,7 +248,7 @@
 
         fs = FileSystem()
         self.assertTrue(fs.remove('filename', remove_with_exception))
-        self.assertEquals(-1, FileSystemTest._remove_failures)
+        self.assertEquals(-1, RealFileSystemTest._remove_failures)
 
     def test_sep(self):
         fs = FileSystem()
@@ -222,6 +263,5 @@
         fs = FileSystem()
         self.assertRaises(AttributeError, assign_sep)
 
-
 if __name__ == '__main__':
     unittest.main()
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to