tests/test-run-tests.py
author Simon Sapin <simon.sapin@octobus.net>
Fri, 21 Jan 2022 17:54:03 +0100
changeset 48745 94e36b230990
parent 47573 75b623801f6a
child 48875 6000f5b25c9b
permissions -rw-r--r--
status: prefer relative paths in Rust code … when the repository root is under the current directory, so the kernel needs to traverse fewer directory in every call to `read_dir` or `symlink_metadata`. Better yet would be to use libc functions like `openat` and `fstatat` to remove such repeated traversals entirely, but the standard library does not provide APIs based on those. Maybe with a crate like https://crates.io/crates/openat instead? Benchmarks of `rhg status` show that this patch is neutral in some configurations, and makes the command up to ~20% faster in others. Below is semi-arbitrary subset of results. The four numeric columns are: time (in seconds) with this changeset’s parent, time with this changeset, time difference (negative is better), time ratio (less than 1 is better). ``` mercurial-dirstate-v1 | default-plain-clean.no-iu.pbr | 0.0061 -> 0.0059: -0.0002 (0.97) mercurial-dirstate-v2 | default-plain-clean.no-iu.pbr | 0.0029 -> 0.0028: -0.0001 (0.97) mozilla-dirstate-v1 | default-plain-clean.no-iu.pbr | 0.2110 -> 0.2102: -0.0007 (1.00) mozilla-dirstate-v2 | default-copies-clean.ignored.pbr | 0.0489 -> 0.0401: -0.0088 (0.82) mozilla-dirstate-v2 | default-copies-clean.no-iu.pbr | 0.0479 -> 0.0393: -0.0085 (0.82) mozilla-dirstate-v2 | default-copies-large.all.pbr | 0.1262 -> 0.1210: -0.0051 (0.96) mozilla-dirstate-v2 | default-copies-small.ignored-unknown.pbr | 0.1262 -> 0.1200: -0.0062 (0.95) mozilla-dirstate-v2 | default-copies-small.ignored.pbr | 0.0536 -> 0.0417: -0.0119 (0.78) mozilla-dirstate-v2 | default-copies-small.no-iu.pbr | 0.0482 -> 0.0393: -0.0089 (0.81) mozilla-dirstate-v2 | default-plain-clean.ignored.pbr | 0.0518 -> 0.0402: -0.0116 (0.78) mozilla-dirstate-v2 | default-plain-clean.no-iu.pbr | 0.0481 -> 0.0392: -0.0088 (0.82) mozilla-dirstate-v2 | default-plain-large.all.pbr | 0.1271 -> 0.1218: -0.0052 (0.96) mozilla-dirstate-v2 | default-plain-small.ignored-unknown.pbr | 0.1225 -> 0.1202: -0.0022 (0.98) mozilla-dirstate-v2 | default-plain-small.ignored.pbr | 0.0510 -> 0.0418: -0.0092 (0.82) mozilla-dirstate-v2 | default-plain-small.no-iu.pbr | 0.0480 -> 0.0394: -0.0086 (0.82) netbeans-dirstate-v1 | default-plain-clean.no-iu.pbr | 0.1442 -> 0.1422: -0.0020 (0.99) netbeans-dirstate-v2 | default-plain-clean.no-iu.pbr | 0.0325 -> 0.0282: -0.0043 (0.87) ``` Differential Revision: https://phab.mercurial-scm.org/D12175
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
     1
"""test line matching with some failing examples and some which warn
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
     2
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
     3
run-test.t only checks positive matches and can not see warnings
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
     4
(both by design)
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
     5
"""
28917
f798ffe7cb08 tests: make test-run-tests use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 25061
diff changeset
     6
from __future__ import absolute_import, print_function
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
     7
28917
f798ffe7cb08 tests: make test-run-tests use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 25061
diff changeset
     8
import doctest
f798ffe7cb08 tests: make test-run-tests use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 25061
diff changeset
     9
import os
f798ffe7cb08 tests: make test-run-tests use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 25061
diff changeset
    10
import re
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    11
20284
e1e6ddaef299 tests: fix test-run-tests.py on OS X
Simon Heimberg <simohe@besonet.ch>
parents: 20274
diff changeset
    12
# this is hack to make sure no escape characters are inserted into the output
e1e6ddaef299 tests: fix test-run-tests.py on OS X
Simon Heimberg <simohe@besonet.ch>
parents: 20274
diff changeset
    13
if 'TERM' in os.environ:
e1e6ddaef299 tests: fix test-run-tests.py on OS X
Simon Heimberg <simohe@besonet.ch>
parents: 20274
diff changeset
    14
    del os.environ['TERM']
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    15
run_tests = __import__('run-tests')
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    16
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    17
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    18
def prn(ex):
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    19
    m = ex.args[0]
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    20
    if isinstance(m, str):
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    21
        print(m)
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    22
    else:
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    23
        print(m.decode('utf-8'))
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    24
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    25
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    26
def lm(expected, output):
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    27
    r"""check if output matches expected
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    28
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    29
    does it generally work?
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    30
        >>> lm(b'H*e (glob)\n', b'Here\n')
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    31
        True
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    32
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    33
    fail on bad test data
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    34
        >>> try: lm(b'a\n',b'a')
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    35
        ... except AssertionError as ex: print(ex)
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    36
        missing newline
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    37
        >>> try: lm(b'single backslash\n', b'single \backslash\n')
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    38
        ... except AssertionError as ex: prn(ex)
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    39
        single backslash or unknown char
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    40
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    41
    assert expected.endswith(b'\n') and output.endswith(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    42
        b'\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    43
    ), 'missing newline'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    44
    assert not re.search(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    45
        br'[^ \w\\/\r\n()*?]', expected + output
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    46
    ), b'single backslash or unknown char'
33710
2e43c5cd57a7 tests: fix up test-run-tests failures on Python 3.6
Augie Fackler <augie@google.com>
parents: 33695
diff changeset
    47
    test = run_tests.TTest(b'test-run-test.t', b'.', b'.')
38554
f83600efa1ca tests: don't allow reodering of glob/re lines across non-glob/re lines
Martin von Zweigbergk <martinvonz@google.com>
parents: 35382
diff changeset
    48
    match, exact = test.linematch(expected, output)
20273
d9d6cbbeef0d run-tests: suggest to append glob when only path sep does not match
Simon Heimberg <simohe@besonet.ch>
parents: 20271
diff changeset
    49
    if isinstance(match, str):
d9d6cbbeef0d run-tests: suggest to append glob when only path sep does not match
Simon Heimberg <simohe@besonet.ch>
parents: 20271
diff changeset
    50
        return 'special: ' + match
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    51
    elif isinstance(match, bytes):
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    52
        return 'special: ' + match.decode('utf-8')
20273
d9d6cbbeef0d run-tests: suggest to append glob when only path sep does not match
Simon Heimberg <simohe@besonet.ch>
parents: 20271
diff changeset
    53
    else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    54
        return bool(match)  # do not return match object
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    55
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    56
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    57
def wintests():
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    58
    r"""test matching like running on windows
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    59
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    60
    enable windows matching on any os
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    61
        >>> _osaltsep = os.altsep
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    62
        >>> os.altsep = True
35381
14fd435763ee run-tests: suggest a (glob) for os.path.sep mismatches with '\r\n' EOL too
Matt Harbison <matt_harbison@yahoo.com>
parents: 33710
diff changeset
    63
        >>> _osname = os.name
14fd435763ee run-tests: suggest a (glob) for os.path.sep mismatches with '\r\n' EOL too
Matt Harbison <matt_harbison@yahoo.com>
parents: 33710
diff changeset
    64
        >>> os.name = 'nt'
47573
75b623801f6a run-tests: use a global WINDOWS constant instead of multiple tests
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    65
        >>> _old_windows = run_tests.WINDOWS
75b623801f6a run-tests: use a global WINDOWS constant instead of multiple tests
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    66
        >>> run_tests.WINDOWS = True
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    67
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    68
    valid match on windows
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    69
        >>> lm(b'g/a*/d (glob)\n', b'g\\abc/d\n')
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    70
        True
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    71
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    72
    direct matching, glob unnecessary
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    73
        >>> lm(b'g/b (glob)\n', b'g/b\n')
20274
7a259dfe24f7 run-tests: print more information on unnecessary glob matching
Simon Heimberg <simohe@besonet.ch>
parents: 20273
diff changeset
    74
        'special: -glob'
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    75
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    76
    missing glob
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
    77
        >>> lm(b'/g/c/d/fg\n', b'\\g\\c\\d/fg\n')
35382
dfae14354660 run-tests: accept '\' vs '/' path differences without '(glob)'
Matt Harbison <matt_harbison@yahoo.com>
parents: 35381
diff changeset
    78
        True
35381
14fd435763ee run-tests: suggest a (glob) for os.path.sep mismatches with '\r\n' EOL too
Matt Harbison <matt_harbison@yahoo.com>
parents: 33710
diff changeset
    79
        >>> lm(b'/g/c/d/fg\n', b'\\g\\c\\d\\fg\r\n')
35382
dfae14354660 run-tests: accept '\' vs '/' path differences without '(glob)'
Matt Harbison <matt_harbison@yahoo.com>
parents: 35381
diff changeset
    80
        True
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    81
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    82
    restore os.altsep
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    83
        >>> os.altsep = _osaltsep
35381
14fd435763ee run-tests: suggest a (glob) for os.path.sep mismatches with '\r\n' EOL too
Matt Harbison <matt_harbison@yahoo.com>
parents: 33710
diff changeset
    84
        >>> os.name = _osname
47573
75b623801f6a run-tests: use a global WINDOWS constant instead of multiple tests
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43076
diff changeset
    85
        >>> run_tests.WINDOWS = _old_windows
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    86
    """
20284
e1e6ddaef299 tests: fix test-run-tests.py on OS X
Simon Heimberg <simohe@besonet.ch>
parents: 20274
diff changeset
    87
    pass
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    88
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
    89
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    90
def otherostests():
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    91
    r"""test matching like running on non-windows os
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    92
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    93
    disable windows matching on any os
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    94
        >>> _osaltsep = os.altsep
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    95
        >>> os.altsep = False
35381
14fd435763ee run-tests: suggest a (glob) for os.path.sep mismatches with '\r\n' EOL too
Matt Harbison <matt_harbison@yahoo.com>
parents: 33710
diff changeset
    96
        >>> _osname = os.name
14fd435763ee run-tests: suggest a (glob) for os.path.sep mismatches with '\r\n' EOL too
Matt Harbison <matt_harbison@yahoo.com>
parents: 33710
diff changeset
    97
        >>> os.name = 'nt'
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    98
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
    99
    backslash does not match slash
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
   100
        >>> lm(b'h/a* (glob)\n', b'h\\ab\n')
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   101
        False
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   102
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   103
    direct matching glob can not be recognized
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
   104
        >>> lm(b'h/b (glob)\n', b'h/b\n')
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   105
        True
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   106
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   107
    missing glob can not not be recognized
25061
625dd917f04f test-run-tests: fix for Python 3.5
Augie Fackler <augie@google.com>
parents: 21315
diff changeset
   108
        >>> lm(b'/h/c/df/g/\n', b'\\h/c\\df/g\\\n')
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   109
        False
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   110
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   111
    restore os.altsep
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   112
        >>> os.altsep = _osaltsep
35381
14fd435763ee run-tests: suggest a (glob) for os.path.sep mismatches with '\r\n' EOL too
Matt Harbison <matt_harbison@yahoo.com>
parents: 33710
diff changeset
   113
        >>> os.name = _osname
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   114
    """
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   115
    pass
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   116
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41759
diff changeset
   117
20271
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   118
if __name__ == '__main__':
4453d08a616a tests: new test for line matching functions in run-tests
Simon Heimberg <simohe@besonet.ch>
parents:
diff changeset
   119
    doctest.testmod()