tests: check importing modules in perf.py for historical portability
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Fri, 20 May 2016 09:47:35 +0900
changeset 29571 d1a7d9c279bb
parent 29570 cbd240188e4e
child 29572 d86b54d9bb0c
tests: check importing modules in perf.py for historical portability To check importing modules in perf.py for historical portability, this patch lists up files by "hg files" both for "1.2" and tip, and builds up "module whitelist" check from those files. This patch uses "1.2" as earlier side version of "module whitelist", because "mercurial.error" module is a blocker for loading perf.py with Mercurial earlier than 1.2, and just importing "mercurial.error" separately isn't enough.
tests/check-perf-code.py
tests/test-contrib-perf.t
--- a/tests/check-perf-code.py	Fri May 20 09:47:35 2016 +0900
+++ b/tests/check-perf-code.py	Fri May 20 09:47:35 2016 +0900
@@ -16,7 +16,50 @@
   ]
 ]
 
+def modulewhitelist(names):
+    replacement = [('.py', ''), ('.c', ''), # trim suffix
+                   ('mercurial%s' % (os.sep), ''), # trim "mercurial/" path
+                  ]
+    ignored = set(['__init__'])
+    modules = {}
+
+    # convert from file name to module name, and count # of appearances
+    for name in names:
+        name = name.strip()
+        for old, new in replacement:
+            name = name.replace(old, new)
+        if name not in ignored:
+            modules[name] = modules.get(name, 0) + 1
+
+    # list up module names, which appear multiple times
+    whitelist = []
+    for name, count in modules.items():
+        if count > 1:
+            whitelist.append(name)
+
+    return whitelist
+
 if __name__ == "__main__":
+    # in this case, it is assumed that result of "hg files" at
+    # multiple revisions is given via stdin
+    whitelist = modulewhitelist(sys.stdin)
+    assert whitelist, "module whitelist is empty"
+
+    # build up module whitelist check from file names given at runtime
+    perfpypats[0].append(
+        # this matching pattern assumes importing modules from
+        # "mercurial" package in the current style below, for simplicity
+        #
+        #    from mercurial import (
+        #        foo,
+        #        bar,
+        #        baz
+        #    )
+        ((r'from mercurial import [(][a-z0-9, \n#]*\n(?! *%s,|^[ #]*\n|[)])'
+          % ',| *'.join(whitelist)),
+         "import newer module separately in try clause for early Mercurial"
+         ))
+
     # import contrib/check-code.py as checkcode
     assert 'RUNTESTDIR' in os.environ, "use check-perf-code.py in *.t script"
     contribpath = os.path.join(os.environ['RUNTESTDIR'], '..', 'contrib')
--- a/tests/test-contrib-perf.t	Fri May 20 09:47:35 2016 +0900
+++ b/tests/test-contrib-perf.t	Fri May 20 09:47:35 2016 +0900
@@ -152,4 +152,6 @@
 
   $ cd "$TESTDIR/.."
 
-  $ "$TESTDIR"/check-perf-code.py contrib/perf.py
+  $ (hg files -r 1.2 glob:mercurial/*.c glob:mercurial/*.py;
+  >  hg files -r tip glob:mercurial/*.c glob:mercurial/*.py) |
+  > "$TESTDIR"/check-perf-code.py contrib/perf.py