Changeset 57467 in webkit


Ignore:
Timestamp:
Apr 11, 2010 8:52:58 PM (14 years ago)
Author:
eric@webkit.org
Message:

2010-04-11 Chris Jerdonek <Chris Jerdonek>

Reviewed by Shinichiro Hamaji.

Refactored check-webkit-style so that the StyleChecker class
has no dependencies on patch-related concepts.

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

This patch is an intermediate step towards making the StyleChecker
class a generalized file processor that can do arbitary operations
on the files corresponding to a list of paths. This patch
also simplifies the unit-testing of patch-checking code.

  • Scripts/check-webkit-style:
    • Updated to use the new PatchChecker class.
  • Scripts/webkitpy/style/checker.py:
    • Refactored the StyleChecker.check_patch() method into the check() method of a new PatchChecker class.
  • Scripts/webkitpy/style/checker_unittest.py:
    • Refactored the unit tests as necessary, changing the StyleCheckerCheckPatchTest class to a PatchCheckerTest class.
Location:
trunk/WebKitTools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKitTools/ChangeLog

    r57466 r57467  
     12010-04-11  Chris Jerdonek  <cjerdonek@webkit.org>
     2
     3        Reviewed by Shinichiro Hamaji.
     4
     5        Refactored check-webkit-style so that the StyleChecker class
     6        has no dependencies on patch-related concepts.
     7
     8        https://bugs.webkit.org/show_bug.cgi?id=37065
     9
     10        This patch is an intermediate step towards making the StyleChecker
     11        class a generalized file processor that can do arbitary operations
     12        on the files corresponding to a list of paths.  This patch
     13        also simplifies the unit-testing of patch-checking code.
     14
     15        * Scripts/check-webkit-style:
     16          - Updated to use the new PatchChecker class.
     17
     18        * Scripts/webkitpy/style/checker.py:
     19          - Refactored the StyleChecker.check_patch() method into the
     20            check() method of a new PatchChecker class.
     21
     22        * Scripts/webkitpy/style/checker_unittest.py:
     23          - Refactored the unit tests as necessary, changing the
     24            StyleCheckerCheckPatchTest class to a PatchCheckerTest class.
     25
    1262010-04-11  Adam Barth  <abarth@webkit.org>
    227
  • trunk/WebKitTools/Scripts/check-webkit-style

    r57056 r57467  
    5050
    5151from webkitpy.style_references import detect_checkout
     52import webkitpy.style.checker as checker
     53from webkitpy.style.checker import PatchChecker
    5254from webkitpy.style.main import change_directory
    53 import webkitpy.style.checker as checker
    5455
    5556_log = logging.getLogger("check-webkit-style")
     
    114115        else:
    115116            patch = checkout.create_patch()
    116         style_checker.check_patch(patch)
     117        patch_checker = PatchChecker(style_checker)
     118        patch_checker.check(patch)
    117119
    118120    error_count = style_checker.error_count
  • trunk/WebKitTools/Scripts/webkitpy/style/checker.py

    r57119 r57467  
    708708        process_file(processor, file_path, handle_style_error)
    709709
    710     # FIXME: Eliminate this method and move its logic to style/main.py.
    711     #        Calls to check_patch() can be replaced by appropriate calls
    712     #        to check_file() using the optional line_numbers parameter.
    713     def check_patch(self, patch_string, mock_check_file=None):
    714         """Check style in the given patch.
     710
     711class PatchChecker(object):
     712
     713    """Supports checking style in patches."""
     714
     715    def __init__(self, style_checker):
     716        """Create a PatchChecker instance.
    715717
    716718        Args:
    717           patch_string: A string that is a patch string.
     719          style_checker: A StyleChecker instance.
    718720
    719721        """
    720         check_file = (self.check_file if mock_check_file is None else
    721                       mock_check_file)
    722 
     722        self._file_checker = style_checker
     723
     724    def check(self, patch_string):
     725        """Check style in the given patch."""
    723726        patch_files = parse_patch(patch_string)
    724727
    725728        # The diff variable is a DiffFile instance.
    726         for file_path, diff in patch_files.iteritems():
     729        for path, diff in patch_files.iteritems():
    727730            line_numbers = set()
    728731            for line in diff.lines:
     
    732735                    line_numbers.add(line[1])
    733736
    734             _log.debug('Found %s modified lines in patch for: %s'
    735                        % (len(line_numbers), file_path))
    736 
    737             check_file(file_path=file_path, line_numbers=line_numbers)
     737            _log.debug('Found %s new or modified lines in: %s'
     738                       % (len(line_numbers), path))
     739
     740            self._file_checker.check_file(file_path=path,
     741                                          line_numbers=line_numbers)
  • trunk/WebKitTools/Scripts/webkitpy/style/checker_unittest.py

    r57119 r57467  
    5151from checker import configure_logging
    5252from checker import ProcessorDispatcher
     53from checker import PatchChecker
    5354from checker import StyleChecker
    5455from checker import StyleCheckerConfiguration
     
    699700
    700701
    701 class StyleCheckerCheckPatchTest(StyleCheckerCheckFileBase):
    702 
    703     """Test the check_patch() method of the StyleChecker class.
    704 
    705     Internally, the check_patch() method calls StyleChecker.check_file() for
    706     each file that appears in the patch string.  This class passes a mock
    707     check_file() method to check_patch() to facilitate unit-testing.  The
    708     "got_*" attributes of this class are the parameters that check_patch()
    709     passed to check_file().  (We test only a single call.)  These attributes
    710     let us check that check_patch() is calling check_file() correctly.
    711 
    712     Attributes:
    713       got_file_path: The value that check_patch() passed as the file_path
    714                      parameter to the mock_check_file() function.
    715       got_line_numbers: The value that check_patch() passed as the line_numbers
    716                         parameter to the mock_check_file() function.
    717 
    718     """
    719 
    720     _file_path = "__init__.py"
    721 
    722     # The modified line_numbers array for this patch is: [2].
    723     _patch_string = """diff --git a/__init__.py b/__init__.py
    724 index ef65bee..e3db70e 100644
    725 --- a/__init__.py
    726 +++ b/__init__.py
    727 @@ -1,1 +1,2 @@
    728  # Required for Python to search this directory for module files
    729 +# New line
    730 """
    731 
    732     def setUp(self):
    733         StyleCheckerCheckFileBase.setUp(self)
    734         self._got_file_path = None
    735         self._got_line_numbers = None
    736 
    737     def _mock_check_file(self, file_path, line_numbers):
    738         self._got_file_path = file_path
    739         self._got_line_numbers = line_numbers
    740 
    741     def test_check_patch(self):
    742         patch_files = parse_patch(self._patch_string)
    743         diff = patch_files[self._file_path]
    744 
    745         configuration = self._style_checker_configuration()
    746 
    747         style_checker = StyleChecker(configuration)
    748 
    749         style_checker.check_patch(patch_string=self._patch_string,
    750                                   mock_check_file=self._mock_check_file)
    751 
    752         self.assertEquals(self._got_file_path, "__init__.py")
    753         self.assertEquals(self._got_line_numbers, set([2]))
    754 
    755 
    756702class StyleCheckerCheckPathsTest(unittest.TestCase):
    757703
     
    800746                           os.path.join("dir_path1", "file2"),
    801747                           os.path.join("dir_path2", "file3")])
     748
     749
     750class PatchCheckerTest(unittest.TestCase):
     751
     752    """Test the PatchChecker class."""
     753
     754    class MockStyleChecker(object):
     755
     756        def __init__(self):
     757            self.checked_files = []
     758            """A list of (file_path, line_numbers) pairs."""
     759
     760        def check_file(self, file_path, line_numbers):
     761            self.checked_files.append((file_path, line_numbers))
     762
     763    def setUp(self):
     764        style_checker = self.MockStyleChecker()
     765        self._style_checker = style_checker
     766        self._patch_checker = PatchChecker(style_checker)
     767
     768    def _call_check_patch(self, patch_string):
     769        self._patch_checker.check(patch_string)
     770
     771    def _assert_checked(self, checked_files):
     772        self.assertEquals(self._style_checker.checked_files, checked_files)
     773
     774    def test_check_patch(self):
     775        # The modified line_numbers array for this patch is: [2].
     776        self._call_check_patch("""diff --git a/__init__.py b/__init__.py
     777index ef65bee..e3db70e 100644
     778--- a/__init__.py
     779+++ b/__init__.py
     780@@ -1,1 +1,2 @@
     781 # Required for Python to search this directory for module files
     782+# New line
     783""")
     784        self._assert_checked([("__init__.py", set([2]))])
Note: See TracChangeset for help on using the changeset viewer.