Commit c7b2b1a7 authored by Jason Madden's avatar Jason Madden

Normalize recent GNU ls output to match the expected BSD-style output present...

Normalize recent GNU ls output to match the expected BSD-style output present in the docstrings of subprocess.py under Python 3. Fixes #681.
parent 3d5c3dbc
from __future__ import print_function from __future__ import print_function
import sys import doctest
import functools
import os import os
import re import re
import doctest import sys
import unittest
import traceback import traceback
import unittest
import gevent import gevent
from gevent import socket from gevent import socket
from greentest import walk_modules from greentest import walk_modules
...@@ -16,6 +18,25 @@ def myfunction(*args, **kwargs): ...@@ -16,6 +18,25 @@ def myfunction(*args, **kwargs):
pass pass
class RENormalizingOutputChecker(doctest.OutputChecker):
"""
Pattern-normalizing output checker. Inspired by one used in zope.testing.
"""
def __init__(self, patterns):
self.transformers = [functools.partial(re.sub, replacement) for re, replacement in patterns]
def check_output(self, want, got, optionflags):
if got == want:
return True
for transformer in self.transformers:
want = transformer(want)
got = transformer(got)
return doctest.OutputChecker.check_output(self, want, got, optionflags)
if __name__ == '__main__': if __name__ == '__main__':
cwd = os.getcwd() cwd = os.getcwd()
try: try:
...@@ -42,6 +63,13 @@ if __name__ == '__main__': ...@@ -42,6 +63,13 @@ if __name__ == '__main__':
sys.exit('No modules found matching %s' % ' '.join(allowed_modules)) sys.exit('No modules found matching %s' % ' '.join(allowed_modules))
suite = unittest.TestSuite() suite = unittest.TestSuite()
checker = RENormalizingOutputChecker((
# Normalize subprocess.py: BSD ls is in the example, gnu ls outputs
# 'cannot access'
(re.compile('cannot access non_existent_file: No such file or directory'),
'non_existent_file: No such file or directory'),
))
tests_count = 0 tests_count = 0
modules_count = 0 modules_count = 0
for m, path in sorted(modules): for m, path in sorted(modules):
...@@ -49,7 +77,7 @@ if __name__ == '__main__': ...@@ -49,7 +77,7 @@ if __name__ == '__main__':
contents = f.read() contents = f.read()
if re.search(br'^\s*>>> ', contents, re.M): if re.search(br'^\s*>>> ', contents, re.M):
try: try:
s = doctest.DocTestSuite(m, extraglobs=globs) s = doctest.DocTestSuite(m, extraglobs=globs, checker=checker)
test_count = len(s._tests) # pylint: disable=W0212 test_count = len(s._tests) # pylint: disable=W0212
print('%s (from %s): %s tests' % (m, path, test_count)) print('%s (from %s): %s tests' % (m, path, test_count))
suite.addTest(s) suite.addTest(s)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment