tests/test-doctest.py
changeset 44565 0af56d3ee24c
parent 44564 529cb23155bc
child 44656 15aef805619d
equal deleted inserted replaced
44564:529cb23155bc 44565:0af56d3ee24c
     1 # this is hack to make sure no escape characters are inserted into the output
     1 # this is hack to make sure no escape characters are inserted into the output
     2 
     2 
     3 from __future__ import absolute_import
     3 from __future__ import absolute_import
       
     4 from __future__ import print_function
     4 
     5 
     5 import doctest
     6 import doctest
     6 import os
     7 import os
     7 import re
     8 import re
       
     9 import subprocess
     8 import sys
    10 import sys
     9 
    11 
    10 ispy3 = sys.version_info[0] >= 3
    12 ispy3 = sys.version_info[0] >= 3
    11 
    13 
    12 if 'TERM' in os.environ:
    14 if 'TERM' in os.environ:
    47     for test in finder.find(mod, name):
    49     for test in finder.find(mod, name):
    48         runner.run(test)
    50         runner.run(test)
    49     runner.summarize()
    51     runner.summarize()
    50 
    52 
    51 
    53 
    52 testmod('mercurial.changelog')
    54 DONT_RUN = []
    53 testmod('mercurial.cmdutil')
       
    54 testmod('mercurial.color')
       
    55 testmod('mercurial.config')
       
    56 testmod('mercurial.dagparser', optionflags=doctest.NORMALIZE_WHITESPACE)
       
    57 testmod('mercurial.encoding')
       
    58 testmod('mercurial.fancyopts')
       
    59 testmod('mercurial.formatter')
       
    60 testmod('mercurial.hg')
       
    61 testmod('mercurial.hgweb.hgwebdir_mod')
       
    62 testmod('mercurial.match')
       
    63 testmod('mercurial.mdiff')
       
    64 testmod('mercurial.minirst')
       
    65 testmod('mercurial.parser')
       
    66 testmod('mercurial.patch')
       
    67 testmod('mercurial.pathutil')
       
    68 testmod('mercurial.pycompat')
       
    69 testmod('mercurial.revlogutils.deltas')
       
    70 testmod('mercurial.revset')
       
    71 testmod('mercurial.revsetlang')
       
    72 testmod('mercurial.simplemerge')
       
    73 testmod('mercurial.smartset')
       
    74 testmod('mercurial.store')
       
    75 testmod('mercurial.subrepo')
       
    76 testmod('mercurial.templater')
       
    77 testmod('mercurial.ui')
       
    78 testmod('mercurial.util')
       
    79 testmod('mercurial.util', testtarget='platform')  # windows.py or posix.py
       
    80 testmod('mercurial.utils.dateutil')
       
    81 testmod('mercurial.utils.stringutil')
       
    82 testmod('hgext.convert.convcmd')
       
    83 testmod('hgext.convert.cvsps')
       
    84 testmod('hgext.convert.filemap')
       
    85 testmod('hgext.convert.p4')
       
    86 testmod('hgext.convert.subversion')
       
    87 testmod('hgext.fix')
       
    88 testmod('hgext.mq')
       
    89 # Helper scripts in tests/ that have doctests:
       
    90 testmod('drawdag')
       
    91 testmod('test-run-tests')
       
    92 
    55 
    93 # Disabled since it requires extra modules that might not be installed.
    56 # Exceptions to the defaults for a given detected module. The value for each
    94 # testmod('i18n.check-translation')
    57 # module name is a list of dicts that specify the kwargs to pass to testmod.
       
    58 # testmod is called once per item in the list, so an empty list will cause the
       
    59 # module to not be tested.
       
    60 testmod_arg_overrides = {
       
    61     'i18n.check-translation': DONT_RUN,  # may require extra installation
       
    62     'mercurial.dagparser': [{'optionflags': doctest.NORMALIZE_WHITESPACE}],
       
    63     'mercurial.keepalive': DONT_RUN,  # >>> is an example, not a doctest
       
    64     'mercurial.posix': DONT_RUN,  # run by mercurial.platform
       
    65     'mercurial.statprof': DONT_RUN,  # >>> is an example, not a doctest
       
    66     'mercurial.util': [{}, {'testtarget': 'platform'}],  # run twice!
       
    67     'mercurial.windows': DONT_RUN,  # run by mercurial.platform
       
    68     'tests.test-url': [{'optionflags': doctest.NORMALIZE_WHITESPACE}],
       
    69 }
       
    70 
       
    71 doctest_indicator = '\n\\s*>>> '
       
    72 fileset = 'set:(**.py and grep("%s"))' % doctest_indicator
       
    73 
       
    74 files = subprocess.check_output(
       
    75     "hg files --print0 '%s'" % fileset,
       
    76     shell=True,
       
    77     cwd=os.path.dirname(os.environ['TESTDIR']),
       
    78 ).split(b'\0')
       
    79 
       
    80 mods_tested = set()
       
    81 for f in files:
       
    82     if not f:
       
    83         continue
       
    84 
       
    85     if ispy3:
       
    86         f = f.decode()
       
    87 
       
    88     modname = f.replace('.py', '').replace('\\', '.').replace('/', '.')
       
    89 
       
    90     # Third-party modules aren't our responsibility to test, and the modules in
       
    91     # contrib generally do not have doctests in a good state, plus they're hard
       
    92     # to import if this test is running with py2, so we just skip both for now.
       
    93     if modname.startswith('mercurial.thirdparty.') or modname.startswith(
       
    94         'contrib.'
       
    95     ):
       
    96         continue
       
    97 
       
    98     for kwargs in testmod_arg_overrides.get(modname, [{}]):
       
    99         mods_tested.add((modname, '%r' % (kwargs,)))
       
   100         if modname.startswith('tests.'):
       
   101             # On py2, we can't import from tests.foo, but it works on both py2
       
   102             # and py3 with the way that PYTHONPATH is setup to import without
       
   103             # the 'tests.' prefix, so we do that.
       
   104             modname = modname[len('tests.') :]
       
   105 
       
   106         testmod(modname, **kwargs)
       
   107 
       
   108 # Meta-test: let's make sure that we actually ran what we expected to, above.
       
   109 # Each item in the set is a 2-tuple of module name and stringified kwargs passed
       
   110 # to testmod.
       
   111 expected_mods_tested = set(
       
   112     [
       
   113         ('hgext.convert.convcmd', '{}'),
       
   114         ('hgext.convert.cvsps', '{}'),
       
   115         ('hgext.convert.filemap', '{}'),
       
   116         ('hgext.convert.p4', '{}'),
       
   117         ('hgext.convert.subversion', '{}'),
       
   118         ('hgext.fix', '{}'),
       
   119         ('hgext.mq', '{}'),
       
   120         ('mercurial.changelog', '{}'),
       
   121         ('mercurial.cmdutil', '{}'),
       
   122         ('mercurial.color', '{}'),
       
   123         ('mercurial.config', '{}'),
       
   124         ('mercurial.dagparser', "{'optionflags': 4}"),
       
   125         ('mercurial.encoding', '{}'),
       
   126         ('mercurial.fancyopts', '{}'),
       
   127         ('mercurial.formatter', '{}'),
       
   128         ('mercurial.hg', '{}'),
       
   129         ('mercurial.hgweb.hgwebdir_mod', '{}'),
       
   130         ('mercurial.match', '{}'),
       
   131         ('mercurial.mdiff', '{}'),
       
   132         ('mercurial.minirst', '{}'),
       
   133         ('mercurial.parser', '{}'),
       
   134         ('mercurial.patch', '{}'),
       
   135         ('mercurial.pathutil', '{}'),
       
   136         ('mercurial.pycompat', '{}'),
       
   137         ('mercurial.revlogutils.deltas', '{}'),
       
   138         ('mercurial.revset', '{}'),
       
   139         ('mercurial.revsetlang', '{}'),
       
   140         ('mercurial.simplemerge', '{}'),
       
   141         ('mercurial.smartset', '{}'),
       
   142         ('mercurial.store', '{}'),
       
   143         ('mercurial.subrepo', '{}'),
       
   144         ('mercurial.templater', '{}'),
       
   145         ('mercurial.ui', '{}'),
       
   146         ('mercurial.util', "{'testtarget': 'platform'}"),
       
   147         ('mercurial.util', '{}'),
       
   148         ('mercurial.utils.dateutil', '{}'),
       
   149         ('mercurial.utils.stringutil', '{}'),
       
   150         ('tests.drawdag', '{}'),
       
   151         ('tests.test-run-tests', '{}'),
       
   152         ('tests.test-url', "{'optionflags': 4}"),
       
   153     ]
       
   154 )
       
   155 
       
   156 unexpectedly_run = mods_tested.difference(expected_mods_tested)
       
   157 not_run = expected_mods_tested.difference(mods_tested)
       
   158 
       
   159 if unexpectedly_run:
       
   160     print('Unexpectedly ran (probably need to add to list):')
       
   161     for r in sorted(unexpectedly_run):
       
   162         print('  %r' % (r,))
       
   163 if not_run:
       
   164     print('Expected to run, but was not run (doctest removed?):')
       
   165     for r in sorted(not_run):
       
   166         print('  %r' % (r,))