| 1 |
"""test line matching with some failing examples and some which warn |
| 2 |
|
| 3 |
run-test.t only checks positive matches and can not see warnings |
| 4 |
(both by design) |
| 5 |
""" |
| 6 |
|
| 7 |
import doctest |
| 8 |
import os |
| 9 |
import re |
| 10 |
import sys |
| 11 |
|
| 12 |
# this is hack to make sure no escape characters are inserted into the output |
| 13 |
if 'TERM' in os.environ: |
| 14 |
del os.environ['TERM'] |
| 15 |
run_tests = __import__('run-tests') |
| 16 |
|
| 17 |
|
| 18 |
def prn(ex): |
| 19 |
m = ex.args[0] |
| 20 |
if isinstance(m, str): |
| 21 |
print(m) |
| 22 |
else: |
| 23 |
print(m.decode('utf-8')) |
| 24 |
|
| 25 |
|
| 26 |
def lm(expected, output): |
| 27 |
r"""check if output matches expected |
| 28 |
|
| 29 |
does it generally work? |
| 30 |
>>> lm(b'H*e (glob)\n', b'Here\n') |
| 31 |
True |
| 32 |
|
| 33 |
fail on bad test data |
| 34 |
>>> try: lm(b'a\n',b'a') |
| 35 |
... except AssertionError as ex: print(ex) |
| 36 |
missing newline |
| 37 |
>>> try: lm(b'single backslash\n', b'single \backslash\n') |
| 38 |
... except AssertionError as ex: prn(ex) |
| 39 |
single backslash or unknown char |
| 40 |
""" |
| 41 |
assert expected.endswith(b'\n') and output.endswith( |
| 42 |
b'\n' |
| 43 |
), 'missing newline' |
| 44 |
assert not re.search( |
| 45 |
br'[^ \w\\/\r\n()*?]', expected + output |
| 46 |
), b'single backslash or unknown char' |
| 47 |
test = run_tests.TTest( |
| 48 |
b'test-run-test.t', b'.', b'.', python=sys.executable |
| 49 |
) |
| 50 |
match, exact = test.linematch(expected, output) |
| 51 |
if isinstance(match, str): |
| 52 |
return 'special: ' + match |
| 53 |
elif isinstance(match, bytes): |
| 54 |
return 'special: ' + match.decode('utf-8') |
| 55 |
else: |
| 56 |
return bool(match) # do not return match object |
| 57 |
|
| 58 |
|
| 59 |
def wintests(): |
| 60 |
r"""test matching like running on windows |
| 61 |
|
| 62 |
enable windows matching on any os |
| 63 |
>>> _osaltsep = os.altsep |
| 64 |
>>> os.altsep = True |
| 65 |
>>> _osname = os.name |
| 66 |
>>> os.name = 'nt' |
| 67 |
>>> _old_windows = run_tests.WINDOWS |
| 68 |
>>> run_tests.WINDOWS = True |
| 69 |
|
| 70 |
valid match on windows |
| 71 |
>>> lm(b'g/a*/d (glob)\n', b'g\\abc/d\n') |
| 72 |
True |
| 73 |
|
| 74 |
direct matching, glob unnecessary |
| 75 |
>>> lm(b'g/b (glob)\n', b'g/b\n') |
| 76 |
'special: -glob' |
| 77 |
|
| 78 |
missing glob |
| 79 |
>>> lm(b'/g/c/d/fg\n', b'\\g\\c\\d/fg\n') |
| 80 |
True |
| 81 |
>>> lm(b'/g/c/d/fg\n', b'\\g\\c\\d\\fg\r\n') |
| 82 |
True |
| 83 |
|
| 84 |
restore os.altsep |
| 85 |
>>> os.altsep = _osaltsep |
| 86 |
>>> os.name = _osname |
| 87 |
>>> run_tests.WINDOWS = _old_windows |
| 88 |
""" |
| 89 |
pass |
| 90 |
|
| 91 |
|
| 92 |
def otherostests(): |
| 93 |
r"""test matching like running on non-windows os |
| 94 |
|
| 95 |
disable windows matching on any os |
| 96 |
>>> _osaltsep = os.altsep |
| 97 |
>>> os.altsep = False |
| 98 |
>>> _osname = os.name |
| 99 |
>>> os.name = 'nt' |
| 100 |
|
| 101 |
backslash does not match slash |
| 102 |
>>> lm(b'h/a* (glob)\n', b'h\\ab\n') |
| 103 |
False |
| 104 |
|
| 105 |
direct matching glob can not be recognized |
| 106 |
>>> lm(b'h/b (glob)\n', b'h/b\n') |
| 107 |
True |
| 108 |
|
| 109 |
missing glob can not not be recognized |
| 110 |
>>> lm(b'/h/c/df/g/\n', b'\\h/c\\df/g\\\n') |
| 111 |
False |
| 112 |
|
| 113 |
restore os.altsep |
| 114 |
>>> os.altsep = _osaltsep |
| 115 |
>>> os.name = _osname |
| 116 |
""" |
| 117 |
pass |
| 118 |
|
| 119 |
|
| 120 |
if __name__ == '__main__': |
| 121 |
doctest.testmod() |