Changeset 54206 in webkit
- Timestamp:
- Feb 1, 2010 10:41:28 PM (14 years ago)
- Location:
- trunk/WebKitTools
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/WebKitTools/ChangeLog
r54205 r54206 1 2010-02-01 Chris Jerdonek <cjerdonek@webkit.org> 2 3 Reviewed by Shinichiro Hamaji. 4 5 Addressed FIXME in check-webkit-style so that the carriage-return 6 check will work for patches. 7 8 https://bugs.webkit.org/show_bug.cgi?id=34260 9 10 Also added support for limiting the number of errors reported 11 per category, per file. 12 13 * Scripts/webkitpy/style/checker.py: 14 - Added new "whitespace/carriage_return" category from common.py. 15 - Added MAX_REPORTS_PER_CATEGORY dictionary. 16 - Added max_reports_per_category attribute to ProcessorOptions class. 17 - Refactored StyleChecker._process_file(). 18 19 * Scripts/webkitpy/style/checker_unittest.py: 20 - Updated ProcessorOptionsTest tests. 21 - Added test to check MAX_REPORTS_PER_CATEGORY. 22 23 * Scripts/webkitpy/style/error_handlers.py: 24 - Added support for suppressing the display of errors after 25 reaching a per-category maximum (from max_reports_per_category). 26 27 * Scripts/webkitpy/style/error_handlers_unittest.py: 28 - Added test for suppressing error display. 29 30 * Scripts/webkitpy/style/processors/common.py: Added. 31 - Moved carriage-return check to new file. 32 33 * Scripts/webkitpy/style/processors/common_unittest.py: Added. 34 - Added unit tests for carriage-return check. 35 36 * Scripts/webkitpy/style/unittests.py: 37 - Added reference to common_unittest.py. 38 1 39 2010-02-01 Shinichiro Hamaji <hamaji@chromium.org> 2 40 -
trunk/WebKitTools/Scripts/webkitpy/style/checker.py
r54192 r54206 38 38 from error_handlers import DefaultStyleErrorHandler 39 39 from error_handlers import PatchStyleErrorHandler 40 from processors.common import check_no_carriage_return 41 from processors.common import categories as CommonCategories 40 42 from processors.cpp import CppProcessor 41 43 from processors.text import TextProcessor … … 107 109 108 110 111 # FIXME: Check that the keys are in _style_categories(). 112 # 113 # The maximum number of errors to report per file, per category. 114 # If a category is not a key, then it has no maximum. 115 MAX_REPORTS_PER_CATEGORY = { 116 "whitespace/carriage_return": 1 117 } 118 119 109 120 def style_categories(): 110 121 """Return the set of all categories used by check-webkit-style.""" 111 # If other processors had categories, we would take their union here.112 return C ppProcessor.categories122 # Take the union across all processors. 123 return CommonCategories.union(CppProcessor.categories) 113 124 114 125 … … 291 302 """ 292 303 293 def __init__(self, output_format="emacs", verbosity=1, filter=None, 294 git_commit=None, extra_flag_values=None): 304 def __init__(self, 305 output_format="emacs", 306 verbosity=1, 307 filter=None, 308 max_reports_per_category=None, 309 git_commit=None, 310 extra_flag_values=None): 311 if extra_flag_values is None: 312 extra_flag_values = {} 295 313 if filter is None: 296 314 filter = CategoryFilter() 297 if extra_flag_valuesis None:298 extra_flag_values= {}315 if max_reports_per_category is None: 316 max_reports_per_category = {} 299 317 300 318 if output_format not in ("emacs", "vs7"): … … 308 326 'Value given: "%s".' % verbosity) 309 327 328 self.extra_flag_values = extra_flag_values 329 self.filter = filter 330 self.git_commit = git_commit 331 self.max_reports_per_category = max_reports_per_category 310 332 self.output_format = output_format 311 333 self.verbosity = verbosity 312 self.filter = filter313 self.git_commit = git_commit314 self.extra_flag_values = extra_flag_values315 334 316 335 # Useful for unit testing. 317 336 def __eq__(self, other): 318 337 """Return whether this ProcessorOptions instance is equal to another.""" 319 if self.output_format != other.output_format: 320 return False 321 if self.verbosity != other.verbosity: 338 if self.extra_flag_values != other.extra_flag_values: 322 339 return False 323 340 if self.filter != other.filter: … … 325 342 if self.git_commit != other.git_commit: 326 343 return False 327 if self.extra_flag_values != other.extra_flag_values: 344 if self.max_reports_per_category != other.max_reports_per_category: 345 return False 346 if self.output_format != other.output_format: 347 return False 348 if self.verbosity != other.verbosity: 328 349 return False 329 350 … … 569 590 filter = CategoryFilter(filter_rules) 570 591 571 options = ProcessorOptions(output_format, verbosity, filter, 572 git_commit, extra_flag_values) 592 options = ProcessorOptions(extra_flag_values=extra_flag_values, 593 filter=filter, 594 git_commit=git_commit, 595 max_reports_per_category=MAX_REPORTS_PER_CATEGORY, 596 output_format=output_format, 597 verbosity=verbosity) 573 598 574 599 return (filenames, options) … … 721 746 # is processed. 722 747 if file_path == '-': 723 lines= codecs.StreamReaderWriter(sys.stdin,724 725 726 'replace').read().split('\n')748 file = codecs.StreamReaderWriter(sys.stdin, 749 codecs.getreader('utf8'), 750 codecs.getwriter('utf8'), 751 'replace') 727 752 else: 728 lines = codecs.open(file_path, 'r', 'utf8', 'replace').read().split('\n') 729 730 carriage_return_found = False 731 # Remove trailing '\r'. 732 for line_number in range(len(lines)): 733 if lines[line_number].endswith('\r'): 734 lines[line_number] = lines[line_number].rstrip('\r') 735 carriage_return_found = True 753 file = codecs.open(file_path, 'r', 'utf8', 'replace') 754 755 contents = file.read() 736 756 737 757 except IOError: … … 739 759 return 740 760 761 lines = contents.split("\n") 762 763 for line_number in range(len(lines)): 764 # FIXME: We should probably use the SVN "eol-style" property 765 # or a white list to decide whether or not to do 766 # the carriage-return check. Originally, we did the 767 # check only if (os.linesep != '\r\n'). 768 # 769 # FIXME: As a minor optimization, we can have 770 # check_no_carriage_return() return whether 771 # the line ends with "\r". 772 check_no_carriage_return(lines[line_number], line_number, 773 handle_style_error) 774 if lines[line_number].endswith("\r"): 775 lines[line_number] = lines[line_number].rstrip("\r") 776 741 777 processor.process(lines) 742 743 if carriage_return_found and os.linesep != '\r\n':744 # FIXME: Make sure this error also shows up when checking745 # patches, if appropriate.746 #747 # Use 0 for line_number since outputting only one error for748 # potentially several lines.749 handle_style_error(0, 'whitespace/newline', 1,750 'One or more unexpected \\r (^M) found;'751 'better to use only a \\n')752 778 753 779 def check_file(self, file_path, handle_style_error=None, process_file=None): -
trunk/WebKitTools/Scripts/webkitpy/style/checker_unittest.py
r54126 r54206 111 111 self.assertEquals(options.filter, CategoryFilter()) 112 112 self.assertEquals(options.git_commit, None) 113 self.assertEquals(options.max_reports_per_category, {}) 113 114 self.assertEquals(options.output_format, "emacs") 114 115 self.assertEquals(options.verbosity, 1) … … 127 128 filter=CategoryFilter(["+"]), 128 129 git_commit="commit", 130 max_reports_per_category={"category": 3}, 129 131 output_format="vs7", 130 132 verbosity=3) … … 132 134 self.assertEquals(options.filter, CategoryFilter(["+"])) 133 135 self.assertEquals(options.git_commit, "commit") 136 self.assertEquals(options.max_reports_per_category, {"category": 3}) 134 137 self.assertEquals(options.output_format, "vs7") 135 138 self.assertEquals(options.verbosity, 3) … … 144 147 filter=CategoryFilter(["+"]), 145 148 git_commit="commit", 149 max_reports_per_category={"category": 3}, 146 150 output_format="vs7", 147 151 verbosity=1) … … 149 153 self.assertFalse(options == ProcessorOptions(filter=CategoryFilter(["-"]))) 150 154 self.assertFalse(options == ProcessorOptions(git_commit="commit2")) 155 self.assertFalse(options == ProcessorOptions(max_reports_per_category= 156 {"category": 2})) 151 157 self.assertFalse(options == ProcessorOptions(output_format="emacs")) 152 158 self.assertFalse(options == ProcessorOptions(verbosity=2)) … … 174 180 175 181 176 class WebKitArgumentDefaultsTest(unittest.TestCase):177 178 """Tests validity of default arguments used by check-webkit-style."""182 class GlobalVariablesTest(unittest.TestCase): 183 184 """Tests validity of the global variables.""" 179 185 180 186 def defaults(self): … … 207 213 parser.parse([]) # arguments valid: no error or SystemExit 208 214 215 def test_max_reports_per_category(self): 216 """Check that MAX_REPORTS_PER_CATEGORY is valid.""" 217 categories = style.style_categories() 218 for category in style.MAX_REPORTS_PER_CATEGORY.iterkeys(): 219 self.assertTrue(category in categories, 220 'Key "%s" is not a category' % category) 221 209 222 210 223 class ArgumentPrinterTest(unittest.TestCase): … … 218 231 extra_flag_values={}): 219 232 filter = CategoryFilter(filter_rules) 220 return style.ProcessorOptions(output_format, verbosity, filter, 221 git_commit, extra_flag_values) 233 return style.ProcessorOptions(extra_flag_values=extra_flag_values, 234 filter=filter, 235 git_commit=git_commit, 236 output_format=output_format, 237 verbosity=verbosity) 222 238 223 239 def test_to_flag_string(self): -
trunk/WebKitTools/Scripts/webkitpy/style/error_handlers.py
r54126 r54206 33 33 Handle the occurrence of a style error. 34 34 35 Check whether the error is reportable. If so, report the details. 35 Check whether the error is reportable. If so, increment the total 36 error count and report the details. Note that error reporting can 37 be suppressed after reaching a certain number of reports. 36 38 37 39 Args: … … 80 82 self._stderr_write = stderr_write 81 83 84 # A string to integer dictionary cache of the number of reportable 85 # errors per category passed to this instance. 86 self._category_totals = { } 87 88 def _add_reportable_error(self, category): 89 """Increment the error count and return the new category total.""" 90 self._increment_error_count() # Increment the total. 91 92 # Increment the category total. 93 if not category in self._category_totals: 94 self._category_totals[category] = 1 95 else: 96 self._category_totals[category] += 1 97 98 return self._category_totals[category] 99 100 def _max_reports(self, category): 101 """Return the maximum number of errors to report.""" 102 if not category in self._options.max_reports_per_category: 103 return None 104 return self._options.max_reports_per_category[category] 105 82 106 def __call__(self, line_number, category, confidence, message): 83 107 """Handle the occurrence of a style error. … … 89 113 return 90 114 91 self._increment_error_count() 115 category_total = self._add_reportable_error(category) 116 117 max_reports = self._max_reports(category) 118 119 if (max_reports is not None) and (category_total > max_reports): 120 # Then suppress displaying the error. 121 return 92 122 93 123 if self._options.output_format == 'vs7': … … 95 125 else: 96 126 format_string = "%s:%s: %s [%s] [%d]\n" 127 128 if category_total == max_reports: 129 format_string += ("Suppressing further [%s] reports for this " 130 "file.\n" % category) 97 131 98 132 self._stderr_write(format_string % (self._file_path, … … 131 165 for line in self._diff.lines: 132 166 # When deleted line is not set, it means that 133 # the line is newly added .167 # the line is newly added (or modified). 134 168 if not line[0]: 135 169 self._line_numbers.add(line[1]) … … 141 175 142 176 This function does not report errors occurring in lines not 143 m odified or added.177 marked as modified or added in the patch. 144 178 145 Args: see the DefaultStyleErrorHandler.__call__() documentation.179 See the docstring of this module for more information. 146 180 147 181 """ -
trunk/WebKitTools/Scripts/webkitpy/style/error_handlers_unittest.py
r54126 r54206 51 51 _category = "whitespace/tab" 52 52 53 def _options(self, output_format):54 return ProcessorOptions(verbosity=3, output_format=output_format)55 56 53 def _error_handler(self, options): 57 54 file_path = "foo.h" … … 61 58 self._mock_stderr_write) 62 59 63 def _prepare_call(self, output_format="emacs"): 64 """Return options after initializing.""" 65 options = self._options(output_format) 66 67 # Test that count is initialized to zero. 60 def _check_initialized(self): 61 """Check that count and error messages are initialized.""" 68 62 self.assertEquals(0, self._error_count) 69 63 self.assertEquals("", self._error_messages) 70 64 71 return options 72 73 def _call_error_handler(self, options, confidence): 74 """Handle an error with given confidence.""" 75 handle_error = self._error_handler(options) 76 65 def _call(self, handle_error, options, confidence): 66 """Handle an error with the given error handler.""" 77 67 line_number = 100 78 68 message = "message" … … 80 70 handle_error(line_number, self._category, confidence, message) 81 71 72 def _call_error_handler(self, options, confidence): 73 """Handle an error using a new error handler.""" 74 handle_error = self._error_handler(options) 75 self._call(handle_error, options, confidence) 76 82 77 def test_call_non_reportable(self): 83 78 """Test __call__() method with a non-reportable error.""" 84 79 confidence = 1 85 options = self._prepare_call() 80 options = ProcessorOptions(verbosity=3) 81 self._check_initialized() 86 82 87 83 # Confirm the error is not reportable. … … 96 92 """Test __call__() method with a reportable error and emacs format.""" 97 93 confidence = 5 98 options = self._prepare_call("emacs") 94 options = ProcessorOptions(verbosity=3, output_format="emacs") 95 self._check_initialized() 99 96 100 97 self._call_error_handler(options, confidence) … … 107 104 """Test __call__() method with a reportable error and vs7 format.""" 108 105 confidence = 5 109 options = self._prepare_call("vs7") 106 options = ProcessorOptions(verbosity=3, output_format="vs7") 107 self._check_initialized() 110 108 111 109 self._call_error_handler(options, confidence) … … 114 112 self.assertEquals(self._error_messages, 115 113 "foo.h(100): message [whitespace/tab] [5]\n") 114 115 def test_call_max_reports_per_category(self): 116 """Test error report suppression in __call__() method.""" 117 confidence = 5 118 options = ProcessorOptions(verbosity=3, 119 max_reports_per_category={self._category: 2}) 120 error_handler = self._error_handler(options) 121 122 self._check_initialized() 123 124 # First call: usual reporting. 125 self._call(error_handler, options, confidence) 126 self.assertEquals(1, self._error_count) 127 self.assertEquals(self._error_messages, 128 "foo.h:100: message [whitespace/tab] [5]\n") 129 130 # Second call: suppression message reported. 131 self._error_messages = "" 132 self._call(error_handler, options, confidence) 133 self.assertEquals(2, self._error_count) 134 self.assertEquals(self._error_messages, 135 "foo.h:100: message [whitespace/tab] [5]\n" 136 "Suppressing further [%s] reports for this file.\n" 137 % self._category) 138 139 # Third call: no report. 140 self._error_messages = "" 141 self._call(error_handler, options, confidence) 142 self.assertEquals(3, self._error_count) 143 self.assertEquals(self._error_messages, "") 116 144 117 145 -
trunk/WebKitTools/Scripts/webkitpy/style/unittests.py
r54126 r54206 38 38 from checker_unittest import * 39 39 from error_handlers_unittest import * 40 from processors.common_unittest import * 40 41 from processors.cpp_unittest import * 41 42 from processors.text_unittest import *
Note: See TracChangeset
for help on using the changeset viewer.