watchlist: Suggest corrections for typos and improve error message consistency.
authorlevin@chromium.org <levin@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 28 Sep 2011 17:28:05 +0000 (17:28 +0000)
committerlevin@chromium.org <levin@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 28 Sep 2011 17:28:05 +0000 (17:28 +0000)
https://bugs.webkit.org/show_bug.cgi?id=68976

Reviewed by Adam Barth.

* Scripts/webkitpy/common/watchlist/watchlistparser.py: Add suggestions
for typos and improve consistency of the error messages.
* Scripts/webkitpy/common/watchlist/watchlistparser_unittest.py: Add tests
and improve the regex that were already present.

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

Tools/ChangeLog
Tools/Scripts/webkitpy/common/watchlist/watchlistparser.py
Tools/Scripts/webkitpy/common/watchlist/watchlistparser_unittest.py

index b046a6b..1f72e22 100644 (file)
@@ -1,5 +1,17 @@
 2011-09-28  David Levin  <levin@chromium.org>
 
+        watchlist: Suggest corrections for typos and improve error message consistency.
+        https://bugs.webkit.org/show_bug.cgi?id=68976
+
+        Reviewed by Adam Barth.
+
+        * Scripts/webkitpy/common/watchlist/watchlistparser.py: Add suggestions
+        for typos and improve consistency of the error messages.
+        * Scripts/webkitpy/common/watchlist/watchlistparser_unittest.py: Add tests
+        and improve the regex that were already present.
+
+2011-09-28  David Levin  <levin@chromium.org>
+
         watchlist: Add support for matching added or deleted lines.
         https://bugs.webkit.org/show_bug.cgi?id=68972
 
index 08d0bc2..e910898 100644 (file)
@@ -26,6 +26,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.
 
+import difflib
 import re
 from webkitpy.common.watchlist.changedlinepattern import ChangedLinePattern
 from webkitpy.common.watchlist.filenamepattern import FilenamePattern
@@ -61,7 +62,9 @@ class WatchListParser(object):
         for section in dictionary:
             parser = self._section_parsers.get(section)
             if not parser:
-                raise Exception('Unknown section "%s" in watch list.' % section)
+                raise Exception(('Unknown section "%s" in watch list.'
+                                 + self._suggest_words(section, self._section_parsers.keys()))
+                                % section)
             parser(dictionary[section], watch_list)
 
         return watch_list
@@ -69,6 +72,12 @@ class WatchListParser(object):
     def _eval_watch_list(self, watch_list_contents):
         return eval(watch_list_contents, {'__builtins__': None}, None)
 
+    def _suggest_words(self, invalid_word, valid_words):
+        close_matches = difflib.get_close_matches(invalid_word, valid_words)
+        if not close_matches:
+            return ''
+        return '\n\nPerhaps it should be %s.' % (' or '.join(close_matches))
+
     def _parse_definition_section(self, definition_section, watch_list):
         definitions = {}
         for name in definition_section:
@@ -81,7 +90,9 @@ class WatchListParser(object):
             for pattern_type in definition:
                 pattern_parser = self._definition_pattern_parsers.get(pattern_type)
                 if not pattern_parser:
-                    raise Exception('Invalid pattern type "%s" in definition "%s".' % (pattern_type, name))
+                    raise Exception(('Unknown pattern type "%s" in definition "%s".'
+                                     + self._suggest_words(pattern_type, self._definition_pattern_parsers.keys()))
+                                    % (pattern_type, name))
 
                 pattern = pattern_parser(definition[pattern_type])
                 definitions[name].append(pattern)
index e0a4394..178ea36 100644 (file)
@@ -53,6 +53,12 @@ class WatchListParserTest(unittest.TestCase):
         self.assertRaisesRegexp('Unknown section "FOO" in watch list.',
                                 self._watch_list_parser.parse, watch_list_with_bad_section)
 
+    def test_section_typo(self):
+        watch_list_with_bad_section = ('{"DEFINTIONS": {}}')
+        self.assertRaisesRegexp(r'Unknown section "DEFINTIONS" in watch list\.\s*'
+                              + r'Perhaps it should be DEFINITIONS\.',
+                                self._watch_list_parser.parse, watch_list_with_bad_section)
+
     def test_bad_definition(self):
         watch_list_with_bad_definition = (
             '{'
@@ -63,7 +69,7 @@ class WatchListParserTest(unittest.TestCase):
             '     },'
             '}')
 
-        self._verifyException('Invalid character "|" in definition "WatchList1|A".',
+        self._verifyException(r'Invalid character "\|" in definition "WatchList1\|A"\.',
                               self._watch_list_parser.parse, watch_list_with_bad_definition)
 
     def test_bad_match_type(self):
@@ -76,5 +82,19 @@ class WatchListParserTest(unittest.TestCase):
             '     },'
             '}')
 
-        self._verifyException('Invalid pattern type "nothing_matches_this" in definition "WatchList1".',
+        self._verifyException('Unknown pattern type "nothing_matches_this" in definition "WatchList1".',
+                              self._watch_list_parser.parse, watch_list_with_bad_match_type)
+
+    def test_match_type_typo(self):
+        watch_list_with_bad_match_type = (
+            '{'
+            '    "DEFINITIONS": {'
+            '        "WatchList1": {'
+            '            "iflename": r".*\\MyFileName\\.cpp",'
+            '        },'
+            '     },'
+            '}')
+
+        self._verifyException(r'Unknown pattern type "iflename" in definition "WatchList1"\.\s*'
+                              + r'Perhaps it should be filename\.',
                               self._watch_list_parser.parse, watch_list_with_bad_match_type)