Enable blocked terms test to unblock multiple terms
The original logic was a bit off, it would only really work for a single term.
This fixes the logic and adds a unittest with two terms.
BUG=None
TEST=./pre-upload_unittest.py
Change-Id: I0a95e833b6343c5f2c3f0831ae80fefd342676e6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/repohooks/+/2391394
Commit-Queue: Daisuke Nojiri <dnojiri@chromium.org>
Tested-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/pre-upload.py b/pre-upload.py
index 4adb9a5..0b02750 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -616,15 +616,14 @@
for word in keywords:
m = re.search(word, line, flags=re.I)
if m:
- if opts.unblock:
- # Even though we remove the unblocked values from the keywords list
- # we walk through them here as well in the case that they are a
- # sub-regex of one of the common keyword values and not a direct
- # match.
- for unblocked in opts.unblock:
- if not re.search(unblocked, m.group(0)):
- return 'Matched ' + word
- else:
+ matched = True
+ # The unblock values supercede blocked values, so if any unblock
+ # regex matches a term found by the block list, we ignore it.
+ for unblocked in opts.unblock:
+ if re.search(unblocked, m.group(0)):
+ matched = False
+ break
+ if matched:
return 'Matched ' + word
return False
diff --git a/pre-upload_unittest.py b/pre-upload_unittest.py
index a318242..d5170ec 100755
--- a/pre-upload_unittest.py
+++ b/pre-upload_unittest.py
@@ -162,6 +162,14 @@
failures = pre_upload._check_keywords(ProjectNamed('PROJECT'),
'COMMIT', ['--unblock', 'scru.?fy'])
self.assertEqual(failures, [])
+ self.diff_mock.return_value = [
+ (1, 'Line with two unblocked terms scruffy big dog-pile'),
+ (2, 'Line with without any blocked terms'),
+ ]
+ failures = pre_upload._check_keywords(ProjectNamed('PROJECT'),
+ 'COMMIT', ['--unblock', 'dog.?pile',
+ '--unblock', 'scru.?fy'])
+ self.assertEqual(failures, [])
class CheckNoLongLinesTest(PreUploadTestCase):