changeset 5350:73cd416a1653

tests: replace check-compat-strings.sh with a Python script Previously it was a bash script (using BASH_REMATCH, for example) that was used in tests, and we want our tests as portable as possible, so mostly that is the reason for this rewrite. It's also easier to add a cache to the Python version, which reduces the run time: from ~10s to ~5.5s. Another change worth mentioning is filtering the list of files to be only .py files. We don't currently have compat comments in any other files.
author Anton Shestakov <av6@dwimlabs.net>
date Fri, 29 May 2020 13:23:45 +0800
parents c3d87d106fbe
children dd88c127ad81
files tests/test-check-compat-strings.t tests/testlib/check-compat-strings.py tests/testlib/check-compat-strings.sh
diffstat 3 files changed, 64 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/tests/test-check-compat-strings.t	Sat Jun 06 20:37:10 2020 +0800
+++ b/tests/test-check-compat-strings.t	Fri May 29 13:23:45 2020 +0800
@@ -5,4 +5,5 @@
   > evolution = all
   > EOF
 
-  $ $TESTDIR/testlib/check-compat-strings.sh "$TESTDIR/../hgext3rd/" "$RUNTESTDIR/.."
+  $ "$PYTHON" "$TESTDIR/testlib/check-compat-strings.py" \
+  > "$TESTDIR/../hgext3rd/" "$RUNTESTDIR/.."
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/testlib/check-compat-strings.py	Fri May 29 13:23:45 2020 +0800
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+"""
+This script finds compatibility-related comments with a node hash specified
+in all files in a given directory (. by default) and looks up the hash in a
+repo (~/hg by default) to determine if each of the comments is correct and,
+if not, it suggests the correct release. This can prevent accidentally
+removing a piece of code that was misattributed to a different (earlier)
+release of core hg.
+
+Usage: $0 WDIR HGREPO where WDIR is usually evolve/hgext3rd/ and HGREPO is
+the place with core Mercurial repo (not just checkout). Said repo has to be
+sufficiently up-to-date, otherwise this script may not work correctly.
+"""
+
+from __future__ import print_function
+
+import argparse
+import os
+import re
+from subprocess import check_output
+
+def grepall(workdir, linere):
+    for root, dirs, files in os.walk(workdir):
+        for fname in files:
+            if not fname.endswith('.py'):
+                continue
+            path = os.path.join(root, fname)
+            with open(path, 'r') as src:
+                for lineno, line in enumerate(src, 1):
+                    for groups in linere.findall(line):
+                        yield path, lineno, line, groups
+
+def main():
+    ap = argparse.ArgumentParser()
+    ap.add_argument('workdir', nargs='?', default='.')
+    ap.add_argument('hgdir', nargs='?', default=os.path.expanduser('~/hg'))
+
+    opts = ap.parse_args()
+
+    linere = re.compile(r'hg <= ([0-9.]+) \(([0-9a-f+]+)\)')
+    basecmd = ['hg', '--cwd', opts.hgdir, 'log', '-T', '{tags}']
+    hgenv = {'HGPLAIN': '1', 'HGENCODING': 'UTF-8'}
+    relcache = {}
+
+    for path, lineno, line, match in grepall(opts.workdir, linere):
+        expected, revset = match
+
+        if revset not in relcache:
+            tagrevset = 'max(tag("re:^[0-9]\\.[0-9]$") - (%s)::)' % revset
+            cmd = basecmd + ['-r', tagrevset]
+            relcache[revset] = check_output(cmd, env=hgenv).decode('UTF-8')
+
+        lastrel = relcache[revset]
+
+        if lastrel != expected:
+            print('%s:%d:%s' % (path, lineno, line.rstrip('\r\n')))
+            print('\\ actual last major release without %s is %s'
+                  % (revset, lastrel))
+            print()
+
+if __name__ == '__main__':
+    main()
--- a/tests/testlib/check-compat-strings.sh	Sat Jun 06 20:37:10 2020 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-unset GREP_OPTIONS
-
-# This script finds compatibility-related comments with a node hash specified
-# in all files in a given directory (. by default) and looks up the hash in a
-# repo (~/hg by default) to determine if each of the comments is correct and,
-# if not, it suggests the correct release. This can prevent accidentally
-# removing a piece of code that was misattributed to a different (earlier)
-# release of core hg.
-
-# Usage: $0 WDIR HGREPO where WDIR is usually evolve/hgext3rd/ and HGREPO is
-# the place with core Mercurial repo (not just checkout). Said repo has to be
-# sufficiently up-to-date, otherwise this script may not work correctly.
-
-workdir=${1:-'.'}
-hgdir=${2:-~/hg}
-grep -Ern 'hg <= [0-9.]+ \([0-9a-f+]+\)' "$workdir" | while read -r line; do
-    bashre='hg <= ([0-9.]+) \(([0-9a-f+]+)\)'
-    if [[ $line =~ $bashre ]]; then
-        expected=${BASH_REMATCH[1]}
-        revset=${BASH_REMATCH[2]}
-        tagrevset="max(tag('re:^[0-9]\\.[0-9]$') - ($revset)::)"
-        lastrel=$(HGPLAIN=1 hg --cwd "$hgdir" log -r "$tagrevset" -T '{tags}')
-        if [[ "$lastrel" != "$expected" ]]; then
-            echo "$line"
-            echo "actual last major release without $revset is $lastrel"
-            echo
-        fi
-    fi
-done