[PATCH 2 of 2] check-code: no-check-code must be on its own line and near top or bottom

Simon Heimberg simohe at besonet.ch
Mon Nov 11 17:40:11 CST 2013


# HG changeset patch
# User Simon Heimberg <simohe at besonet.ch>
# Date 1384004149 -3600
#      Sat Nov 09 14:35:49 2013 +0100
# Node ID 38ed395da3e1863fd6002527df50d5732dcdb2af
# Parent  82010fa54edb068ccd57373ab65fc9517d20b0d1
check-code: no-check-code must be on its own line and near top or bottom

no-check-code was used for ignoring a line (fixed in e033a7d444). Instead an
ignore pattern should have been added to the rule as 3rd argument (or
check-code-ignore appended to the failing line).
Prevent this from reoccurring by ignoring unlikely places and reporting them
clearly. A check-code pragma must now be in a comment on its own line and
near the start or the end of the file.

A normal check-code rule does not work because the file containing
"# no-check-code" would not be checked. A rule can not detect text in a comment
anyway.

diff -r 82010fa54edb -r 38ed395da3e1 contrib/check-code.py
--- a/contrib/check-code.py	Sat Nov 09 10:22:05 2013 +0100
+++ b/contrib/check-code.py	Sat Nov 09 14:35:49 2013 +0100
@@ -443,11 +443,22 @@
             continue
         pre = post = fp.read()
         fp.close()
+        errors = []
         if "# no-" "check-code" in pre:
-            if debug:
-                print "Skipping %s for %s it has no-" "check-code" % (
-                       name, f)
-            break
+            m = re.search(r"^[ ]*(.*)# no-" "check-code.*$", pre, re.MULTILINE)
+            msg = "no-" "check-code skips an entire file, "
+            if m.start() > 200 and len(pre) - m.end() > 20:
+                msg += "it must be near file start or end"
+            elif m.group(1):
+                msg += "it must be on on a separate line"
+            else:
+                if debug:
+                    print "Skipping %s for %s it has no-" "check-code" % (
+                           name, f)
+                break
+            l = m.group() # line is entire match
+            errors.append((f, lineno and -1, l, msg, ''))
+
         for p, r in filters:
             post = re.sub(p, r, post)
         nerrs = len(pats[0]) # nerr elements are errors
@@ -461,7 +472,6 @@
             print "Checking %s for %s" % (name, f)
 
         prelines = None
-        errors = []
         for i, pat in enumerate(pats):
             if len(pat) == 3:
                 p, msg, ignore = pat
diff -r 82010fa54edb -r 38ed395da3e1 tests/test-check-code.t
--- a/tests/test-check-code.t	Sat Nov 09 10:22:05 2013 +0100
+++ b/tests/test-check-code.t	Sat Nov 09 14:35:49 2013 +0100
@@ -133,6 +133,7 @@
    reduce is not available in Python 3+
   [1]
 
+  $ NOTHING=
   $ cat > is-op.py <<EOF
   > # is-operator comparing number or string literal
   > x = None
@@ -144,9 +145,13 @@
   > y = x is not "foo"
   > y = x is not 5346
   > y = x is not -6
+  > pass # no-${NOTHING}check-code
   > EOF
 
   $ "$check_code" ./is-op.py
+  ./is-op.py:-1:
+   > pass # no-chec?-code (glob)
+   *no-check-cod* separate line* (glob)
   ./is-op.py:3:
    > y = x is 'foo'
    object comparison with literal


More information about the Mercurial-devel mailing list