tests/test-doctest.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Fri, 05 Apr 2024 11:05:54 +0200
changeset 51576 de5bf3fe0233
parent 49873 40060267df22
permissions -rw-r--r--
revset: stop serializing node when using "%ln" Turning hundred of thousand of node from node to hex and back can be slow… what about we stop doing it? In many case were we are using node id we should be using revision id. However this is not a good reason to have a stupidly slow implementation of "%ln". This caught my attention again because the phase discovery during push make an extensive use of "%ln" or huge set. In absolute, that phase discovery probably should use "%ld" and need to improves its algorithmic complexity, but improving "%ln" seems simple and long overdue. This greatly speeds up `hg push` on repository with many drafts. Here are some relevant poulpe benchmarks: ### data-env-vars.name = mozilla-try-2023-03-22-zstd-sparse-revlog # benchmark.name = hg.command.push # bin-env-vars.hg.flavor = default # bin-env-vars.hg.py-re2-module = default # benchmark.variants.explicit-rev = all-out-heads # benchmark.variants.issue6528 = disabled # benchmark.variants.protocol = ssh # benchmark.variants.reuse-external-delta-parent = default ## benchmark.variants.revs = any-1-extra-rev before: 44.235070 after: 20.416329 (-53.85%, -23.82) ## benchmark.variants.revs = any-100-extra-rev before: 49.234697 after: 26.519829 (-46.14%, -22.71) ### benchmark.name = hg.command.bundle # bin-env-vars.hg.flavor = default # bin-env-vars.hg.py-re2-module = default # benchmark.variants.revs = all # benchmark.variants.type = none-streamv2 ## data-env-vars.name = heptapod-public-2024-03-25-zstd-sparse-revlog before: 10.138396 after: 7.750458 (-23.55%, -2.39) ## data-env-vars.name = mercurial-public-2024-03-22-zstd-sparse-revlog before: 1.263859 after: 0.700229 (-44.60%, -0.56) ## data-env-vars.name = mozilla-try-2023-03-22-zstd-sparse-revlog before: 399.484481 after: 346.5089 (-13.26%, -52.98) ## data-env-vars.name = pypy-2024-03-22-zstd-sparse-revlog before: 4.540080 after: 3.401700 (-25.07%, -1.14) ## data-env-vars.name = tryton-public-2024-03-22-zstd-sparse-revlog before: 2.975765 after: 1.870798 (-37.13%, -1.10)

# this is hack to make sure no escape characters are inserted into the output


import doctest
import os
import re
import subprocess
import sys

if 'TERM' in os.environ:
    del os.environ['TERM']


class py3docchecker(doctest.OutputChecker):
    def check_output(self, want, got, optionflags):
        want2 = re.sub(r'''\bu(['"])(.*?)\1''', r'\1\2\1', want)  # py2: u''
        got2 = re.sub(r'''\bb(['"])(.*?)\1''', r'\1\2\1', got)  # py3: b''
        # py3: <exc.name>: b'<msg>' -> <name>: <msg>
        #      <exc.name>: <others> -> <name>: <others>
        got2 = re.sub(
            r'''^mercurial\.\w+\.(\w+): (['"])(.*?)\2''',
            r'\1: \3',
            got2,
            re.MULTILINE,
        )
        got2 = re.sub(r'^mercurial\.\w+\.(\w+): ', r'\1: ', got2, re.MULTILINE)
        return any(
            doctest.OutputChecker.check_output(self, w, g, optionflags)
            for w, g in [(want, got), (want2, got2)]
        )


def testmod(name, optionflags=0, testtarget=None):
    __import__(name)
    mod = sys.modules[name]
    if testtarget is not None:
        mod = getattr(mod, testtarget)

    # minimal copy of doctest.testmod()
    finder = doctest.DocTestFinder()
    checker = py3docchecker()
    runner = doctest.DocTestRunner(checker=checker, optionflags=optionflags)
    for test in finder.find(mod, name):
        runner.run(test)
    runner.summarize()


DONT_RUN = []

# Exceptions to the defaults for a given detected module. The value for each
# module name is a list of dicts that specify the kwargs to pass to testmod.
# testmod is called once per item in the list, so an empty list will cause the
# module to not be tested.
testmod_arg_overrides = {
    'i18n.check-translation': DONT_RUN,  # may require extra installation
    'mercurial.dagparser': [{'optionflags': doctest.NORMALIZE_WHITESPACE}],
    'mercurial.keepalive': DONT_RUN,  # >>> is an example, not a doctest
    'mercurial.posix': DONT_RUN,  # run by mercurial.platform
    'mercurial.statprof': DONT_RUN,  # >>> is an example, not a doctest
    'mercurial.util': [{}, {'testtarget': 'platform'}],  # run twice!
    'mercurial.windows': DONT_RUN,  # run by mercurial.platform
    'tests.test-url': [{'optionflags': doctest.NORMALIZE_WHITESPACE}],
}

fileset = 'set:(**.py)'

cwd = os.path.dirname(os.environ["TESTDIR"])

if not os.path.isdir(os.path.join(cwd, ".hg")):
    sys.exit(0)

files = subprocess.check_output(
    "hg files --print0 \"%s\"" % fileset,
    shell=True,
    cwd=cwd,
).split(b'\0')

if sys.version_info[0] >= 3:
    cwd = os.fsencode(cwd)

mods_tested = set()
for f in files:
    if not f:
        continue

    with open(os.path.join(cwd, f), "rb") as fh:
        if not re.search(br'\n\s*>>>', fh.read()):
            continue

    f = f.decode()

    modname = f.replace('.py', '').replace('\\', '.').replace('/', '.')

    # Third-party modules aren't our responsibility to test, and the modules in
    # contrib generally do not have doctests in a good state, plus they're hard
    # to import if this test is running with py2, so we just skip both for now.
    if modname.startswith('mercurial.thirdparty.') or modname.startswith(
        'contrib.'
    ):
        continue

    for kwargs in testmod_arg_overrides.get(modname, [{}]):
        mods_tested.add((modname, '%r' % (kwargs,)))
        if modname.startswith('tests.'):
            # On py2, we can't import from tests.foo, but it works on both py2
            # and py3 with the way that PYTHONPATH is setup to import without
            # the 'tests.' prefix, so we do that.
            modname = modname[len('tests.') :]

        testmod(modname, **kwargs)

# Meta-test: let's make sure that we actually ran what we expected to, above.
# Each item in the set is a 2-tuple of module name and stringified kwargs passed
# to testmod.
expected_mods_tested = set(
    [
        ('hgext.convert.convcmd', '{}'),
        ('hgext.convert.cvsps', '{}'),
        ('hgext.convert.filemap', '{}'),
        ('hgext.convert.p4', '{}'),
        ('hgext.convert.subversion', '{}'),
        ('hgext.fix', '{}'),
        ('hgext.mq', '{}'),
        ('mercurial.changelog', '{}'),
        ('mercurial.cmdutil', '{}'),
        ('mercurial.color', '{}'),
        ('mercurial.dagparser', "{'optionflags': 4}"),
        ('mercurial.dirstateutils.v2', '{}'),
        ('mercurial.encoding', '{}'),
        ('mercurial.fancyopts', '{}'),
        ('mercurial.formatter', '{}'),
        ('mercurial.hg', '{}'),
        ('mercurial.hgweb.hgwebdir_mod', '{}'),
        ('mercurial.match', '{}'),
        ('mercurial.mdiff', '{}'),
        ('mercurial.minirst', '{}'),
        ('mercurial.parser', '{}'),
        ('mercurial.patch', '{}'),
        ('mercurial.pathutil', '{}'),
        ('mercurial.pycompat', '{}'),
        ('mercurial.revlogutils.deltas', '{}'),
        ('mercurial.revset', '{}'),
        ('mercurial.revsetlang', '{}'),
        ('mercurial.simplemerge', '{}'),
        ('mercurial.smartset', '{}'),
        ('mercurial.store', '{}'),
        ('mercurial.subrepo', '{}'),
        ('mercurial.templater', '{}'),
        ('mercurial.ui', '{}'),
        ('mercurial.util', "{'testtarget': 'platform'}"),
        ('mercurial.util', '{}'),
        ('mercurial.utils.dateutil', '{}'),
        ('mercurial.utils.stringutil', '{}'),
        ('mercurial.utils.urlutil', '{}'),
        ('tests.drawdag', '{}'),
        ('tests.test-run-tests', '{}'),
        ('tests.test-url', "{'optionflags': 4}"),
    ]
)

unexpectedly_run = mods_tested.difference(expected_mods_tested)
not_run = expected_mods_tested.difference(mods_tested)

if unexpectedly_run:
    print('Unexpectedly ran (probably need to add to list):')
    for r in sorted(unexpectedly_run):
        print('  %r' % (r,))
if not_run:
    print('Expected to run, but was not run (doctest removed?):')
    for r in sorted(not_run):
        print('  %r' % (r,))