[SCM] WebKit Debian packaging branch, webkit-1.2, updated. upstream/1.1.90-6072-g9a69373

cjerdonek at webkit.org cjerdonek at webkit.org
Thu Apr 8 02:06:33 UTC 2010


The following commit has been merged in the webkit-1.2 branch:
commit 1602c3ed687e26f8cb85cb423d12afb347b1f4b3
Author: cjerdonek at webkit.org <cjerdonek at webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Date:   Tue Mar 2 17:25:52 2010 +0000

    Refactored the StyleChecker class's file-processing method
    _process_file().  This will make it easier to add new
    file-processing capabilities to check-webkit-style.
    
    Reviewed by Shinichiro Hamaji.
    
    https://bugs.webkit.org/show_bug.cgi?id=35490
    
    * Scripts/webkitpy/style/checker.py:
      - Added a _read_lines() method to the StyleChecker class
        that extracts the lines from a file.
      - Replaced part of _process_file() with a call to the new
        _read_lines() method.
      - Replaced another part of _process_file() with a call
        to the new CarriageReturnProcessor.process() method.
    
    * Scripts/webkitpy/style/processors/common.py:
      - Replaced the check_no_carriage_return() function with a
        new CarriageReturnProcessor class.
    
    * Scripts/webkitpy/style/processors/common_unittest.py:
      - Renamed the CarriageReturnTest class to
        CarriageReturnProcessorTest and updated it as necessary.
    
    git-svn-id: http://svn.webkit.org/repository/webkit/trunk@55411 268f45cc-cd09-0410-ab3c-d52691b4dbfc

diff --git a/WebKitTools/ChangeLog b/WebKitTools/ChangeLog
index 537f2ee..ed2d9ee 100644
--- a/WebKitTools/ChangeLog
+++ b/WebKitTools/ChangeLog
@@ -2,6 +2,32 @@
 
         Reviewed by Shinichiro Hamaji.
 
+        Refactored the StyleChecker class's file-processing method
+        _process_file().  This will make it easier to add new
+        file-processing capabilities to check-webkit-style.
+
+        https://bugs.webkit.org/show_bug.cgi?id=35490
+
+        * Scripts/webkitpy/style/checker.py:
+          - Added a _read_lines() method to the StyleChecker class
+            that extracts the lines from a file.
+          - Replaced part of _process_file() with a call to the new
+            _read_lines() method.
+          - Replaced another part of _process_file() with a call
+            to the new CarriageReturnProcessor.process() method.
+
+        * Scripts/webkitpy/style/processors/common.py:
+          - Replaced the check_no_carriage_return() function with a
+            new CarriageReturnProcessor class.
+
+        * Scripts/webkitpy/style/processors/common_unittest.py:
+          - Renamed the CarriageReturnTest class to
+            CarriageReturnProcessorTest and updated it as necessary.
+
+2010-03-02  Chris Jerdonek  <cjerdonek at webkit.org>
+
+        Reviewed by Shinichiro Hamaji.
+
         Started using the logging module in check-webkit-style.
         This provides more options for debugging and a more flexible,
         uniform way to report messages to the end-user.
diff --git a/WebKitTools/Scripts/webkitpy/style/checker.py b/WebKitTools/Scripts/webkitpy/style/checker.py
index c74aece..92d65d3 100644
--- a/WebKitTools/Scripts/webkitpy/style/checker.py
+++ b/WebKitTools/Scripts/webkitpy/style/checker.py
@@ -40,8 +40,8 @@ from error_handlers import PatchStyleErrorHandler
 from filter import FilterConfiguration
 from optparser import ArgumentParser
 from optparser import DefaultCommandOptionValues
-from processors.common import check_no_carriage_return
 from processors.common import categories as CommonCategories
+from processors.common import CarriageReturnProcessor
 from processors.cpp import CppProcessor
 from processors.text import TextProcessor
 
@@ -529,51 +529,48 @@ class StyleChecker(object):
         """Increment the total count of reported errors."""
         self.error_count += 1
 
+    def _read_lines(self, file_path):
+        """Read the file at a path, and return its lines.
+
+        Raises:
+          IOError: if the file does not exist or cannot be read.
+
+        """
+        # Support the UNIX convention of using "-" for stdin.
+        if file_path == '-':
+            file = codecs.StreamReaderWriter(sys.stdin,
+                                             codecs.getreader('utf8'),
+                                             codecs.getwriter('utf8'),
+                                             'replace')
+        else:
+            # We do not open the file with universal newline support
+            # (codecs does not support it anyway), so the resulting
+            # lines contain trailing "\r" characters if we are reading
+            # a file with CRLF endings.
+            file = codecs.open(file_path, 'r', 'utf8', 'replace')
+
+        contents = file.read()
+
+        lines = contents.split("\n")
+        return lines
+
     def _process_file(self, processor, file_path, handle_style_error):
-        """Process the file using the given processor."""
+        """Process the file using the given style processor."""
         try:
-            # Support the UNIX convention of using "-" for stdin.  Note that
-            # we are not opening the file with universal newline support
-            # (which codecs doesn't support anyway), so the resulting lines do
-            # contain trailing '\r' characters if we are reading a file that
-            # has CRLF endings.
-            # If after the split a trailing '\r' is present, it is removed
-            # below. If it is not expected to be present (i.e. os.linesep !=
-            # '\r\n' as in Windows), a warning is issued below if this file
-            # is processed.
-            if file_path == '-':
-                file = codecs.StreamReaderWriter(sys.stdin,
-                                                 codecs.getreader('utf8'),
-                                                 codecs.getwriter('utf8'),
-                                                 'replace')
-            else:
-                file = codecs.open(file_path, 'r', 'utf8', 'replace')
-
-            contents = file.read()
-
+            lines = self._read_lines(file_path)
         except IOError:
             message = 'Could not read file. Skipping: "%s"' % file_path
             _log.warn(message)
             return
 
-        lines = contents.split("\n")
-
-        # FIXME: Make a CarriageReturnProcessor for this logic, and put
-        #        it in processors.common.  The process() method should
-        #        return the lines with the carriage returns stripped.
-        for line_number in range(len(lines)):
-            # FIXME: We should probably use the SVN "eol-style" property
-            #        or a white list to decide whether or not to do
-            #        the carriage-return check. Originally, we did the
-            #        check only if (os.linesep != '\r\n').
-            #
-            # FIXME: As a minor optimization, we can have
-            #        check_no_carriage_return() return whether
-            #        the line ends with "\r".
-            check_no_carriage_return(lines[line_number], line_number,
-                                     handle_style_error)
-            if lines[line_number].endswith("\r"):
-                lines[line_number] = lines[line_number].rstrip("\r")
+        # Check for and remove trailing carriage returns ("\r").
+        #
+        # FIXME: We should probably use the SVN "eol-style" property
+        #        or a white list to decide whether or not to do
+        #        the carriage-return check. Originally, we did the
+        #        check only if (os.linesep != '\r\n').
+        carriage_return_processor = CarriageReturnProcessor(handle_style_error)
+        lines = carriage_return_processor.process(lines)
 
         processor.process(lines)
 
diff --git a/WebKitTools/Scripts/webkitpy/style/processors/common.py b/WebKitTools/Scripts/webkitpy/style/processors/common.py
index dbf4bea..30b8fed 100644
--- a/WebKitTools/Scripts/webkitpy/style/processors/common.py
+++ b/WebKitTools/Scripts/webkitpy/style/processors/common.py
@@ -20,7 +20,7 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-"""Supports style checking not specific to any one processor."""
+"""Supports style checking not specific to any one file type."""
 
 
 # FIXME: Test this list in the same way that the list of CppProcessor
@@ -33,27 +33,25 @@ categories = set([
 ])
 
 
-def check_no_carriage_return(line, line_number, error):
-    """Check that a line does not end with a carriage return.
+class CarriageReturnProcessor(object):
 
-    Returns true if the check is successful (i.e. if the line does not
-    end with a carriage return), and false otherwise.
+    """Supports checking for and handling carriage returns."""
 
-    Args:
-      line: A string that is the line to check.
-      line_number: The line number.
-      error: The function to call with any errors found.
+    def __init__(self, handle_style_error):
+        self._handle_style_error = handle_style_error
 
-    """
+    def process(self, lines):
+        """Check for and strip trailing carriage returns from lines."""
+        for line_number in range(len(lines)):
+            if not lines[line_number].endswith("\r"):
+                continue
 
-    if line.endswith("\r"):
-        error(line_number,
-              "whitespace/carriage_return",
-              1,
-              "One or more unexpected \\r (^M) found; "
-              "better to use only a \\n")
-        return False
-
-    return True
+            self._handle_style_error(line_number + 1,  # Correct for offset.
+                                     "whitespace/carriage_return",
+                                     1,
+                                     "One or more unexpected \\r (^M) found; "
+                                     "better to use only a \\n")
 
+            lines[line_number] = lines[line_number].rstrip("\r")
 
+        return lines
diff --git a/WebKitTools/Scripts/webkitpy/style/processors/common_unittest.py b/WebKitTools/Scripts/webkitpy/style/processors/common_unittest.py
index 9362b65..3dde7b9 100644
--- a/WebKitTools/Scripts/webkitpy/style/processors/common_unittest.py
+++ b/WebKitTools/Scripts/webkitpy/style/processors/common_unittest.py
@@ -22,10 +22,9 @@
 
 """Unit tests for common.py."""
 
-
 import unittest
 
-from common import check_no_carriage_return
+from common import CarriageReturnProcessor
 
 
 # FIXME: The unit tests for the cpp, text, and common processors should
@@ -33,13 +32,15 @@ from common import check_no_carriage_return
 #        mock style error handling code and the code to check that all
 #        of a processor's categories are covered by the unit tests.
 #        Such shared code can be located in a shared test file, perhaps
-#        ilke this one.
-class CarriageReturnTest(unittest.TestCase):
+#        even this file.
+class CarriageReturnProcessorTest(unittest.TestCase):
 
     """Tests check_no_carriage_return()."""
 
     _category = "whitespace/carriage_return"
     _confidence = 1
+    _expected_message = ("One or more unexpected \\r (^M) found; "
+                         "better to use only a \\n")
 
     def setUp(self):
         self._style_errors = [] # The list of accumulated style errors.
@@ -50,33 +51,44 @@ class CarriageReturnTest(unittest.TestCase):
         error = (line_number, category, confidence, message)
         self._style_errors.append(error)
 
-    def assert_carriage_return(self, line, is_error):
-        """Call check_no_carriage_return() and assert the result."""
-        line_number = 100
+    def assert_carriage_return(self, input_lines, expected_lines, error_lines):
+        """Process the given line and assert that the result is correct."""
         handle_style_error = self._mock_style_error_handler
 
-        check_no_carriage_return(line, line_number, handle_style_error)
+        processor = CarriageReturnProcessor(handle_style_error)
+        output_lines = processor.process(input_lines)
 
-        expected_message = ("One or more unexpected \\r (^M) found; "
-                            "better to use only a \\n")
+        # Check both the return value and error messages.
+        self.assertEquals(output_lines, expected_lines)
 
-        if is_error:
-            expected_errors = [(line_number, self._category, self._confidence,
-                                expected_message)]
-            self.assertEquals(self._style_errors, expected_errors)
-        else:
-            self.assertEquals(self._style_errors, [])
+        expected_errors = [(line_number, self._category, self._confidence,
+                            self._expected_message)
+                           for line_number in error_lines]
+        self.assertEquals(self._style_errors, expected_errors)
 
     def test_ends_with_carriage(self):
-        self.assert_carriage_return("carriage return\r", is_error=True)
+        self.assert_carriage_return(["carriage return\r"],
+                                    ["carriage return"],
+                                    [1])
 
     def test_ends_with_nothing(self):
-        self.assert_carriage_return("no carriage return", is_error=False)
+        self.assert_carriage_return(["no carriage return"],
+                                    ["no carriage return"],
+                                    [])
 
     def test_ends_with_newline(self):
-        self.assert_carriage_return("no carriage return\n", is_error=False)
-
-    def test_ends_with_carriage_newline(self):
-        # Check_no_carriage_return only() checks the final character.
-        self.assert_carriage_return("carriage\r in a string", is_error=False)
-
+        self.assert_carriage_return(["no carriage return\n"],
+                                    ["no carriage return\n"],
+                                    [])
+
+    def test_carriage_in_middle(self):
+        # The CarriageReturnProcessor checks only the final character
+        # of each line.
+        self.assert_carriage_return(["carriage\r in a string"],
+                                    ["carriage\r in a string"],
+                                    [])
+
+    def test_multiple_errors(self):
+        self.assert_carriage_return(["line1", "line2\r", "line3\r"],
+                                    ["line1", "line2", "line3"],
+                                    [2, 3])

-- 
WebKit Debian packaging



More information about the Pkg-webkit-commits mailing list