pre-submit: Modify 'sane' blocking regex

Modify 'sane' regex to '\bsane\b\d' to only block 'sane' when used as a
stand alone acronym or as an acronym with a number.
The regex as currently defined blocks any instances of words similar to
'asanEnabled' etc.

Modifying the regex to '\bsane(\b|\d)' limits pre-submit blocking to only
the acronym uses.

Add a unit test for 'sane' block and unblock.

Refactor blocked_terms_unittest.py to remove dependencies on the format
of the failure messages. Those are tested in pre-upload_unittest.py.

BUG=chromium:1135047
TEST=run pre-upload_unittest.py

Change-Id: Ic0c4744475eab43a741d385387d14fa98347546a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/repohooks/+/2517161
Tested-by: Laurent Chavey <chavey@google.com>
Commit-Queue: Laurent Chavey <chavey@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/blocked_terms.txt b/blocked_terms.txt
index 220bd18..cf620d0 100644
--- a/blocked_terms.txt
+++ b/blocked_terms.txt
@@ -26,7 +26,7 @@
 \bnative
 \bred.?line
 rtfm
-sane
+\bsane(\b|\d)
 sanity
 slave
 white.?glove
diff --git a/blocked_terms_unittest.py b/blocked_terms_unittest.py
index af80522..d50cf63 100755
--- a/blocked_terms_unittest.py
+++ b/blocked_terms_unittest.py
@@ -61,52 +61,65 @@
     self.project = pre_upload.Project(name='PROJECT', dir='./', remote=None)
 
   def CheckKeyword(self, test):
-    # Test a particular keyword.
-    #
-    #  Args:
-    #   { DIFF:[list of values to test keyword against],
-    #     FAILURE:[list of expected failures] or None }
-    #
+    """Test a particular keyword.
+
+    Args:
+      test: { DIFF: [(int, 'line to test keyword against'), ],
+              MATCH: number of matched terms or None, }
+    """
     def __check_keyword(unblocked):
       self.desc_mock.return_value = 'Commit message'
       self.diff_mock.return_value = test[self.DIFF]
       failures = pre_upload._check_keywords(self.project, 'COMMIT', ())
       if test[self.MATCH] and not unblocked:
+        # Check that the expected number of blocked lines are found.
         self.assertNotEqual(failures, [])
-        self.assertEqual('Found a blocked keyword in:', failures[0].msg)
-        self.assertEqual(test[self.MATCH], failures[0].items)
+        self.assertEqual(test[self.MATCH], len(failures[0].items))
       else:
         self.assertEqual(failures, [])
 
+    # Check blocked terms.
     self.rf_mock.side_effect = [self.common_keywords, str('')]
     __check_keyword(unblocked=False)
 
+    # Check unblocked terms.
     self.rf_mock.side_effect = [self.common_keywords, self.unblocked_keywords]
     __check_keyword(unblocked=True)
 
   def test_mitm_keyword(self):
-    # pylint: disable=C0301
-
-    # { DIFF:[list of values to test keyword against],
-    #   FAILURE:[list of expected failures] or None },}
-    #
-    test_instance = {
-        self.DIFF:[(1, 'Line with blocked mitm '),
-                   (2, 'Line with blocked (mitm)'),
-                   (3, 'Line with blocked .mitm'),
-                   (4, 'Line with blocked MITM'),
-                   (5, 'Line with blocked mitm1'),
-                   (6, 'Line with unblocked (commitment)'),
-                   (7, 'Line with unblocked DailyLimitMins'),],
-        self.MATCH:[
-            'x.ebuild, line 1: Matched "mitm" with regex of "\\bmitm(\\b|\\d)"',
-            'x.ebuild, line 2: Matched "mitm" with regex of "\\bmitm(\\b|\\d)"',
-            'x.ebuild, line 3: Matched "mitm" with regex of "\\bmitm(\\b|\\d)"',
-            'x.ebuild, line 4: Matched "MITM" with regex of "\\bmitm(\\b|\\d)"',
-            'x.ebuild, line 5: Matched "mitm1" with regex of "\\bmitm(\\b|\\d)"'],
+    test_instance_blocked = {
+        self.DIFF: [(1, 'blocked mitm '),
+                    (2, 'blocked (mitm)'),
+                    (3, 'blocked .mitm'),
+                    (4, 'blocked MITM'),
+                    (5, 'blocked mitm1'),],
+        self.MATCH: 5,
     }
+    self.CheckKeyword(test_instance_blocked)
 
-    self.CheckKeyword(test_instance)
+    test_instance_unblocked = {
+        self.DIFF: [(1, 'unblocked (commitment)'),
+                    (2, 'unblocked DailyLimitMins'),],
+        self.MATCH: 0,
+    }
+    self.CheckKeyword(test_instance_unblocked)
+
+  def test_sane_keyword(self):
+    test_instance_blocked = {
+        self.DIFF: [(1, 'blocked sane '),
+                    (2, 'blocked (sane)'),
+                    (3, 'blocked .sane'),
+                    (4, 'blocked SANE'),
+                    (5, 'blocked sane1'),],
+        self.MATCH: 5,
+    }
+    self.CheckKeyword(test_instance_blocked)
+
+    test_instance_unblocked = {
+        self.DIFF: [(1, 'unblocked asanEnabled'),],
+        self.MATCH: 0,
+    }
+    self.CheckKeyword(test_instance_unblocked)
 
 
 if __name__ == '__main__':
diff --git a/unblocked_terms.txt b/unblocked_terms.txt
index feee3a6..62b0f76 100644
--- a/unblocked_terms.txt
+++ b/unblocked_terms.txt
@@ -37,7 +37,7 @@
 \bnative
 \bred.?line
 rtfm
-sane
+\bsane(\b|\d)
 sanity
 slave
 # white.?glove