Patchwork [3,of,7] contrib: refactor preparation logic for patterns of check-code.py

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Feb. 28, 2019, 6:16 p.m.
Message ID <26e86857082b83cc45ab.1551377773@footprints>
Download mbox | patch
Permalink /patch/38975/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - Feb. 28, 2019, 6:16 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1551376389 -32400
#      Fri Mar 01 02:53:09 2019 +0900
# Node ID 26e86857082b83cc45ab452a83775a661adc6363
# Parent  bbc5686632c8fe35372c3095e3cedb1823472831
# Available At https://bitbucket.org/foozy/mercurial-wip
#              hg pull https://bitbucket.org/foozy/mercurial-wip -r 26e86857082b
# EXP-Topic tests-check-embedded-code
contrib: refactor preparation logic for patterns of check-code.py

This is a part of preparation to apply checking with check-code.py on
code fragments embedded in *.t test scripts.

Before this patch, preparation logic in _preparepats() of
check-code.py is not reusable. It can handle only module global list
"checks".

This patch splits preparation logic into small internal functions, and
add the loop to invoke them, in order to increase reusability of the
logic.

"c[-2]" is equivalent to "c[3]" for "checks". This patch uses the
former, because it will be more reusable for subsequent patch than the
latter.

Patch

diff --git a/contrib/check-code.py b/contrib/check-code.py
--- a/contrib/check-code.py
+++ b/contrib/check-code.py
@@ -561,8 +561,7 @@  checks = [
 ]
 
 def _preparepats():
-    for c in checks:
-        failandwarn = c[-1]
+    def preparefailandwarn(failandwarn):
         for pats in failandwarn:
             for i, pseq in enumerate(pats):
                 # fix-up regexes for multi-line searches
@@ -576,10 +575,19 @@  def _preparepats():
                 p = re.sub(r'(?<!\\)\[\^', r'[^\\n', p)
 
                 pats[i] = (re.compile(p, re.MULTILINE),) + pseq[1:]
-        filters = c[3]
+
+    def preparefilters(filters):
         for i, flt in enumerate(filters):
             filters[i] = re.compile(flt[0]), flt[1]
 
+    for cs in (checks,):
+        for c in cs:
+            failandwarn = c[-1]
+            preparefailandwarn(failandwarn)
+
+            filters = c[-2]
+            preparefilters(filters)
+
 class norepeatlogger(object):
     def __init__(self):
         self._lastseen = None