Patchwork [6,of,7] contrib: add line offset information to file check function of check-code.py

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Feb. 28, 2019, 6:16 p.m.
Message ID <e518c9701ab473a51e06.1551377776@footprints>
Download mbox | patch
Permalink /patch/38972/
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 e518c9701ab473a51e06fde20866fafe8864b960
# Parent  1b4340efc1c6e777f1a6fa9d1fe05e3ff7d07c05
# Available At https://bitbucket.org/foozy/mercurial-wip
#              hg pull https://bitbucket.org/foozy/mercurial-wip -r e518c9701ab4
# EXP-Topic tests-check-embedded-code
contrib: add line offset information to file check function 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.

This information will be useful to show correct line number in an
actual file for errors detected in code fragments embedded in *.t test
scripts.

Patch

diff --git a/contrib/check-code.py b/contrib/check-code.py
--- a/contrib/check-code.py
+++ b/contrib/check-code.py
@@ -677,7 +677,8 @@  def checkfile(f, logfunc=_defaultlogger.
     return result
 
 def _checkfiledata(name, f, filedata, filters, pats, context,
-                   logfunc, maxerr, warnings, blame, debug, lineno):
+                   logfunc, maxerr, warnings, blame, debug, lineno,
+                   offset=None):
     """Execute actual error check for file data
 
     :name: of the checking category
@@ -695,10 +696,17 @@  def _checkfiledata(name, f, filedata, fi
     :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
+    :offset: line number offset of 'filedata' in 'f' for checking
+             an embedded code fragment, or None (offset=0 is different
+             from offset=None)
 
     returns number of detected errors.
     """
     blamecache = context['blamecache']
+    if offset is None:
+        lineoffset = 0
+    else:
+        lineoffset = offset
 
     fc = 0
     pre = post = filedata
@@ -746,7 +754,7 @@  def _checkfiledata(name, f, filedata, fi
                 if ignore and re.search(ignore, l, re.MULTILINE):
                     if debug:
                         print("Skipping %s for %s:%s (ignore pattern)" % (
-                            name, f, n))
+                            name, f, (n + lineoffset)))
                     continue
                 bd = ""
                 if blame:
@@ -754,12 +762,22 @@  def _checkfiledata(name, f, filedata, fi
                     if blamecache is None:
                         blamecache = getblame(f)
                         context['blamecache'] = blamecache
-                    if n < len(blamecache):
-                        bl, bu, br = blamecache[n]
-                        if bl == l:
+                    if (n + lineoffset) < len(blamecache):
+                        bl, bu, br = blamecache[(n + lineoffset)]
+                        if offset is None and bl == l:
                             bd = '%s@%s' % (bu, br)
+                        elif offset is not None and bl.endswith(l):
+                            # "offset is not None" means "checking
+                            # embedded code fragment". In this case,
+                            # "l" does not have information about the
+                            # beginning of an *original* line in the
+                            # file (e.g. '  > ').
+                            # Therefore, use "str.endswith()", and
+                            # show "maybe" for a little loose
+                            # examination.
+                            bd = '%s@%s, maybe' % (bu, br)
 
-                errors.append((f, lineno and n + 1, l, msg, bd))
+                errors.append((f, lineno and (n + lineoffset + 1), l, msg, bd))
 
         errors.sort()
         for e in errors: