contrib: factor out actual error check for file data of check-code.py
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Fri, 01 Mar 2019 02:53:09 +0900
changeset 41823 7a139fc60eb0
parent 41822 55ae5cd31f76
child 41824 519b2faea261
contrib: factor out actual error check for file data 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. Newly added _checkfiledata() will be useful to apply checks on code fragments embedded in *.t test scripts.
contrib/check-code.py
--- a/contrib/check-code.py	Fri Mar 01 02:53:09 2019 +0900
+++ b/contrib/check-code.py	Fri Mar 01 02:53:09 2019 +0900
@@ -635,7 +635,6 @@
 
     return True if no error is found, False otherwise.
     """
-    blamecache = None
     result = True
 
     try:
@@ -649,11 +648,12 @@
         print("Skipping %s, %s" % (f, str(e).split(':', 1)[0]))
         return result
 
+    # context information shared while single checkfile() invocation
+    context = {'blamecache': None}
+
     for name, match, magic, filters, pats in checks:
-        post = pre # discard filtering result of previous check
         if debug:
             print(name, f)
-        fc = 0
         if not (re.match(match, f) or (magic and re.search(magic, pre))):
             if debug:
                 print("Skipping %s for %s it doesn't match %s" % (
@@ -668,6 +668,42 @@
             # tests/test-check-code.t
             print("Skipping %s it has no-che?k-code (glob)" % f)
             return "Skip" # skip checking this file
+
+        if not _checkfiledata(name, f, pre, filters, pats, context,
+                              logfunc, maxerr, warnings, blame, debug, lineno):
+            result = False
+
+    return result
+
+def _checkfiledata(name, f, filedata, filters, pats, context,
+                   logfunc, maxerr, warnings, blame, debug, lineno):
+    """Execute actual error check for file data
+
+    :name: of the checking category
+    :f: filepath
+    :filedata: content of a file
+    :filters: to be applied before checking
+    :pats: to detect errors
+    :context: a dict of information shared while single checkfile() invocation
+              Valid keys: 'blamecache'.
+    :logfunc: function used to report error
+              logfunc(filename, linenumber, linecontent, errormessage)
+    :maxerr: number of error to display before aborting, or False to
+             report all errors
+    :warnings: whether warning level checks should be applied
+    :blame: whether blame information should be displayed at error reporting
+    :debug: whether debug information should be displayed
+    :lineno: whether lineno should be displayed at error reporting
+
+    return True if no error is found, False otherwise.
+    """
+    blamecache = context['blamecache']
+
+    fc = 0
+    pre = post = filedata
+    result = True
+
+    if True: # TODO: get rid of this redundant 'if' block
         for p, r in filters:
             post = re.sub(p, r, post)
         nerrs = len(pats[0]) # nerr elements are errors
@@ -715,8 +751,9 @@
                 bd = ""
                 if blame:
                     bd = 'working directory'
-                    if not blamecache:
+                    if blamecache is None:
                         blamecache = getblame(f)
+                        context['blamecache'] = blamecache
                     if n < len(blamecache):
                         bl, bu, br = blamecache[n]
                         if bl == l: