2011-01-19 Dirk Pranke <dpranke@chromium.org>
authordpranke@chromium.org <dpranke@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 19 Jan 2011 20:28:45 +0000 (20:28 +0000)
committerdpranke@chromium.org <dpranke@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 19 Jan 2011 20:28:45 +0000 (20:28 +0000)
        Reviewed by Tony Chang.

        Change more modules in the layout-tests code to use the
        filesystem wrapper for cleaner unit testing.

        This patch also adds the glob() wrapper to the filesystem
        abstraction.

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

        * Scripts/webkitpy/layout_tests/port/test_files.py:
        * Scripts/webkitpy/layout_tests/port/test_files_unittest.py:
        * Scripts/webkitpy/layout_tests/test_types/test_type_base.py:
        * Scripts/webkitpy/layout_tests/layout_package/dump_render_tree_thread.py:
        * Scripts/webkitpy/common/system/filesystem.py:
        * Scripts/webkitpy/common/system/filesystem_mock.py:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@76150 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/Scripts/webkitpy/common/system/filesystem.py
Tools/Scripts/webkitpy/common/system/filesystem_mock.py
Tools/Scripts/webkitpy/layout_tests/layout_package/dump_render_tree_thread.py
Tools/Scripts/webkitpy/layout_tests/port/test_files.py
Tools/Scripts/webkitpy/layout_tests/port/test_files_unittest.py
Tools/Scripts/webkitpy/layout_tests/test_types/test_type_base.py

index b8ffa8d6e317cb60fcd15ef8c7a0b29826addd56..80133e0b923f1d58586941d437e5ba4ea510d5ff 100644 (file)
@@ -1,3 +1,22 @@
+2011-01-19  Dirk Pranke  <dpranke@chromium.org>
+
+        Reviewed by Tony Chang.
+
+        Change more modules in the layout-tests code to use the
+        filesystem wrapper for cleaner unit testing.
+
+        This patch also adds the glob() wrapper to the filesystem
+        abstraction.
+        
+        https://bugs.webkit.org/show_bug.cgi?id=52604
+
+        * Scripts/webkitpy/layout_tests/port/test_files.py:
+        * Scripts/webkitpy/layout_tests/port/test_files_unittest.py:
+        * Scripts/webkitpy/layout_tests/test_types/test_type_base.py:
+        * Scripts/webkitpy/layout_tests/layout_package/dump_render_tree_thread.py:
+        * Scripts/webkitpy/common/system/filesystem.py:
+        * Scripts/webkitpy/common/system/filesystem_mock.py:
+
 2011-01-19  Levi Weintraub  <leviw@chromium.org>
 
         Unreviewed.
index 53e9796f15e357fff21925298444077ffdb2579a..d45d4c1b1f696f42e1e3ee348f4e221facf8d384 100644 (file)
@@ -33,6 +33,7 @@ from __future__ import with_statement
 import codecs
 import errno
 import exceptions
+import glob
 import os
 import shutil
 import tempfile
@@ -45,6 +46,7 @@ class FileSystem(object):
     or relative."""
 
     def basename(self, path):
+        """Wraps os.path.basename()."""
         return os.path.basename(path)
 
     def copyfile(self, source, destination):
@@ -53,6 +55,7 @@ class FileSystem(object):
         shutil.copyfile(source, destination)
 
     def dirname(self, path):
+        """Wraps os.path.dirname()."""
         return os.path.dirname(path)
 
     def exists(self, path):
@@ -65,6 +68,10 @@ class FileSystem(object):
             for (path_to_file, _, filenames) in os.walk(path)
             for filename in filenames]
 
+    def glob(self, path):
+        """Wraps glob.glob()."""
+        return glob.glob(path)
+
     def isfile(self, path):
         """Return whether the path refers to a file."""
         return os.path.isfile(path)
@@ -125,6 +132,7 @@ class FileSystem(object):
         shutil.move(src, dest)
 
     def normpath(self, path):
+        """Wraps os.path.normpath()."""
         return os.path.normpath(path)
 
     def open_binary_tempfile(self, suffix):
@@ -188,7 +196,7 @@ class FileSystem(object):
             return f.read()
 
     def splitext(self, path):
-        """Return (dirname, basename + ext)."""
+        """Return (dirname + os.sep + basename, '.' + ext)"""
         return os.path.splitext(path)
 
     def write_binary_file(self, path, contents):
index 10acc3b42deb50e54932c1c67a4723ddfe83284a..fd2438db2f585eac3037e8a3d2eec3f5c63c0882 100644 (file)
@@ -76,6 +76,14 @@ class MockFileSystem(object):
             path += '/'
         return [file for file in self.files if file.startswith(path)]
 
+    def glob(self, path):
+        # FIXME: This only handles a wildcard '*' at the end of the path.
+        # Maybe it should handle more?
+        if path[-1] == '*':
+            return [f for f in self.files if f.startswith(path[:-1])]
+        else:
+            return [f for f in self.files if f == path]
+
     def isfile(self, path):
         return path in self.files and self.files[path] is not None
 
index 2bb2d0272db99bb70eafd89c3fd31f6efe29fffd..4d6b5f6d5eccdb05cf0f98a021207c2859c7cb25 100644 (file)
@@ -86,6 +86,7 @@ def _process_output(port, options, test_input, test_types, test_args,
     Returns: a TestResult object
     """
     failures = []
+    fs = port._filesystem
 
     if test_output.crash:
         failures.append(test_failures.FailureCrash())
@@ -96,11 +97,10 @@ def _process_output(port, options, test_input, test_types, test_args,
     if test_output.crash:
         _log.debug("%s Stacktrace for %s:\n%s" % (worker_name, test_name,
                                                   test_output.error))
-        filename = os.path.join(options.results_directory, test_name)
-        filename = os.path.splitext(filename)[0] + "-stack.txt"
-        port.maybe_make_directory(os.path.split(filename)[0])
-        with codecs.open(filename, "wb", "utf-8") as file:
-            file.write(test_output.error)
+        filename = fs.join(options.results_directory, test_name)
+        filename = fs.splitext(filename)[0] + "-stack.txt"
+        fs.maybe_make_directory(fs.dirname(filename))
+        fs.write_text_file(filename, test_output.error)
     elif test_output.error:
         _log.debug("%s %s output stderr lines:\n%s" % (worker_name, test_name,
                                                        test_output.error))
@@ -385,7 +385,7 @@ class TestShellThread(WatchableThread):
 
         # Append tests we're running to the existing tests_run.txt file.
         # This is created in run_webkit_tests.py:_PrepareListsAndPrintOutput.
-        tests_run_filename = os.path.join(self._options.results_directory,
+        tests_run_filename = self._port._filesystem.join(self._options.results_directory,
                                           "tests_run.txt")
         tests_run_file = codecs.open(tests_run_filename, "a", "utf-8")
 
index 2c0a7b63bf671abc1bd824a81deed126f5fd9833..2901a9fd333396767191465b95c3b09684e7ab28 100644 (file)
@@ -58,6 +58,7 @@ def find(port, paths):
       paths: a list of command line paths relative to the layout_tests_dir()
           to limit the search to. glob patterns are ok.
     """
+    fs = port._filesystem
     gather_start_time = time.time()
     paths_to_walk = set()
     # if paths is empty, provide a pre-defined list.
@@ -65,9 +66,9 @@ def find(port, paths):
         _log.debug("Gathering tests from: %s relative to %s" % (paths, port.layout_tests_dir()))
         for path in paths:
             # If there's an * in the name, assume it's a glob pattern.
-            path = os.path.join(port.layout_tests_dir(), path)
+            path = fs.join(port.layout_tests_dir(), path)
             if path.find('*') > -1:
-                filenames = glob.glob(path)
+                filenames = fs.glob(path)
                 paths_to_walk.update(filenames)
             else:
                 paths_to_walk.add(path)
@@ -78,8 +79,8 @@ def find(port, paths):
     # Now walk all the paths passed in on the command line and get filenames
     test_files = set()
     for path in paths_to_walk:
-        if os.path.isfile(path) and _is_test_file(path):
-            test_files.add(os.path.normpath(path))
+        if fs.isfile(path) and _is_test_file(fs, path):
+            test_files.add(fs.normpath(path))
             continue
 
         for root, dirs, files in os.walk(path):
@@ -95,9 +96,9 @@ def find(port, paths):
                     dirs.remove(directory)
 
             for filename in files:
-                if _is_test_file(filename):
-                    filename = os.path.join(root, filename)
-                    filename = os.path.normpath(filename)
+                if _is_test_file(fs, filename):
+                    filename = fs.join(root, filename)
+                    filename = fs.normpath(filename)
                     test_files.add(filename)
 
     gather_time = time.time() - gather_start_time
@@ -106,10 +107,10 @@ def find(port, paths):
     return test_files
 
 
-def _has_supported_extension(filename):
+def _has_supported_extension(fs, filename):
     """Return true if filename is one of the file extensions we want to run a
     test on."""
-    extension = os.path.splitext(filename)[1]
+    extension = fs.splitext(filename)[1]
     return extension in _supported_file_extensions
 
 
@@ -122,7 +123,7 @@ def _is_reference_html_file(filename):
     return False
 
 
-def _is_test_file(filename):
+def _is_test_file(fs, filename):
     """Return true if the filename points to a test file."""
-    return (_has_supported_extension(filename) and
+    return (_has_supported_extension(fs, filename) and
             not _is_reference_html_file(filename))
index 83525c823f3942f37437f834375d1607043c3a41..56973911935a340839eaff8b479305090db34183 100644 (file)
@@ -64,11 +64,13 @@ class TestFilesTest(unittest.TestCase):
         self.assertEqual(tests, set([]))
 
     def test_is_test_file(self):
-        self.assertTrue(test_files._is_test_file('foo.html'))
-        self.assertTrue(test_files._is_test_file('foo.shtml'))
-        self.assertFalse(test_files._is_test_file('foo.png'))
-        self.assertFalse(test_files._is_test_file('foo-expected.html'))
-        self.assertFalse(test_files._is_test_file('foo-expected-mismatch.html'))
+        port = base.Port()
+        fs = port._filesystem
+        self.assertTrue(test_files._is_test_file(fs, 'foo.html'))
+        self.assertTrue(test_files._is_test_file(fs, 'foo.shtml'))
+        self.assertFalse(test_files._is_test_file(fs, 'foo.png'))
+        self.assertFalse(test_files._is_test_file(fs, 'foo-expected.html'))
+        self.assertFalse(test_files._is_test_file(fs, 'foo-expected-mismatch.html'))
 
 
 if __name__ == '__main__':
index 4b96b3abfe1b59512281150fd6feea0603544a93..ad65016ef254435e54dca19a26cef1609120fe90 100644 (file)
 Also defines the TestArguments "struct" to pass them additional arguments.
 """
 
-from __future__ import with_statement
-
-import codecs
 import cgi
 import errno
 import logging
-import os.path
 
 _log = logging.getLogger("webkitpy.layout_tests.test_types.test_type_base")
 
@@ -86,9 +82,10 @@ class TestTypeBase(object):
     def _make_output_directory(self, filename):
         """Creates the output directory (if needed) for a given test
         filename."""
-        output_filename = os.path.join(self._root_output_dir,
+        fs = self._port._filesystem
+        output_filename = fs.join(self._root_output_dir,
             self._port.relative_test_filename(filename))
-        self._port.maybe_make_directory(os.path.split(output_filename)[0])
+        fs.maybe_make_directory(fs.dirname(output_filename))
 
     def _save_baseline_data(self, filename, data, modifier, encoding,
                             generate_new_baseline=True):
@@ -106,21 +103,22 @@ class TestTypeBase(object):
             baseline, or update the existing one
         """
 
+        port = self._port
+        fs = self._port._filesystem
         if generate_new_baseline:
-            relative_dir = os.path.dirname(
-                self._port.relative_test_filename(filename))
-            baseline_path = self._port.baseline_path()
-            output_dir = os.path.join(baseline_path, relative_dir)
-            output_file = os.path.basename(os.path.splitext(filename)[0] +
+            relative_dir = fs.dirname(port.relative_test_filename(filename))
+            baseline_path = port.baseline_path()
+            output_dir = fs.join(baseline_path, relative_dir)
+            output_file = fs.basename(fs.splitext(filename)[0] +
                 self.FILENAME_SUFFIX_EXPECTED + modifier)
-            self._port.maybe_make_directory(output_dir)
-            output_path = os.path.join(output_dir, output_file)
+            fs.maybe_make_directory(output_dir)
+            output_path = fs.join(output_dir, output_file)
             _log.debug('writing new baseline result "%s"' % (output_path))
         else:
-            output_path = self._port.expected_filename(filename, modifier)
+            output_path = port.expected_filename(filename, modifier)
             _log.debug('resetting baseline result "%s"' % output_path)
 
-        self._port.update_baseline(output_path, data, encoding)
+        port.update_baseline(output_path, data, encoding)
 
     def output_filename(self, filename, modifier):
         """Returns a filename inside the output dir that contains modifier.
@@ -136,9 +134,10 @@ class TestTypeBase(object):
         Return:
           The absolute windows path to the output filename
         """
-        output_filename = os.path.join(self._root_output_dir,
+        fs = self._port._filesystem
+        output_filename = fs.join(self._root_output_dir,
             self._port.relative_test_filename(filename))
-        return os.path.splitext(output_filename)[0] + modifier
+        return fs.splitext(output_filename)[0] + modifier
 
     def compare_output(self, port, filename, test_args, actual_test_output,
                         expected_test_output):
@@ -165,11 +164,11 @@ class TestTypeBase(object):
     def _write_into_file_at_path(self, file_path, contents, encoding):
         """This method assumes that byte_array is already encoded
         into the right format."""
-        open_mode = 'w'
+        fs = self._port._filesystem
         if encoding is None:
-            open_mode = 'w+b'
-        with codecs.open(file_path, open_mode, encoding=encoding) as file:
-            file.write(contents)
+            fs.write_binary_file(file_path, contents)
+            return
+        fs.write_text_file(file_path, contents)
 
     def write_output_files(self, filename, file_type,
                            output, expected, encoding,