[PATCH 5 of 5 V3] tests: check importing modules in perf.py for historical portability
Yuya Nishihara
yuya at tcha.org
Sun Jul 17 06:06:47 EDT 2016
On Fri, 20 May 2016 10:01:03 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
> # Date 1463705255 -32400
> # Fri May 20 09:47:35 2016 +0900
> # Node ID 2572968304a6888277cc4278c7399bcf8c731e4a
> # Parent e9f592ae6c0b6f38e168d8dff0407415720861dc
> tests: check importing modules in perf.py for historical portability
Queued the series, thanks.
> +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
Better to comment why modules appear twice should be whitelisted, but I'd
rather making it separate inputs:
$ hg files -r 1.2 glob:mercurial/*.c glob:mercurial/*.py > a
$ hg files -r tip glob:mercurial/*.c glob:mercurial/*.py > b
$ check-perf-code.py --modules 1.2:a --modules tip:b
> 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"
> + ))
I guess this test would be well written by using ast, but that isn't a big deal.
More information about the Mercurial-devel
mailing list