mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Handle stdout/stderr/BaseException in import test.
This commit is contained in:
parent
a4336feadc
commit
9c38f5b041
1 changed files with 57 additions and 2 deletions
|
@ -3,12 +3,18 @@
|
||||||
|
|
||||||
from __future__ import absolute_import, print_function
|
from __future__ import absolute_import, print_function
|
||||||
|
|
||||||
|
import contextlib
|
||||||
import imp
|
import imp
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
try:
|
||||||
|
from StringIO import StringIO
|
||||||
|
except ImportError:
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main program function."""
|
"""Main program function."""
|
||||||
|
@ -16,10 +22,16 @@ def main():
|
||||||
messages = set()
|
messages = set()
|
||||||
|
|
||||||
for path in sys.argv[1:]:
|
for path in sys.argv[1:]:
|
||||||
|
capture = Capture()
|
||||||
try:
|
try:
|
||||||
with open(path, 'r') as module_fd:
|
with open(path, 'r') as module_fd:
|
||||||
imp.load_module('module_import_test', module_fd, os.path.abspath(path), ('.py', 'r', imp.PY_SOURCE))
|
with capture_output(capture):
|
||||||
except Exception as ex: # pylint: disable=locally-disabled, broad-except
|
imp.load_module('module_import_test', module_fd, os.path.abspath(path), ('.py', 'r', imp.PY_SOURCE))
|
||||||
|
|
||||||
|
capture_report(path, capture, messages)
|
||||||
|
except BaseException as ex: # pylint: disable=locally-disabled, broad-except
|
||||||
|
capture_report(path, capture, messages)
|
||||||
|
|
||||||
exc_type, _, exc_tb = sys.exc_info()
|
exc_type, _, exc_tb = sys.exc_info()
|
||||||
message = str(ex)
|
message = str(ex)
|
||||||
results = list(reversed(traceback.extract_tb(exc_tb)))
|
results = list(reversed(traceback.extract_tb(exc_tb)))
|
||||||
|
@ -61,5 +73,48 @@ def main():
|
||||||
if messages:
|
if messages:
|
||||||
exit(10)
|
exit(10)
|
||||||
|
|
||||||
|
|
||||||
|
class Capture(object):
|
||||||
|
"""Captured output and/or exception."""
|
||||||
|
def __init__(self):
|
||||||
|
self.stdout = StringIO()
|
||||||
|
self.stderr = StringIO()
|
||||||
|
|
||||||
|
|
||||||
|
def capture_report(path, capture, messages):
|
||||||
|
"""Report on captured output.
|
||||||
|
:type path: str
|
||||||
|
:type capture: Capture
|
||||||
|
:type messages: set[str]
|
||||||
|
"""
|
||||||
|
if capture.stdout.getvalue():
|
||||||
|
message = '%s:%d:%d: %s: %s' % (path, 0, 0, 'Output', 'Import resulted in output to stdout.')
|
||||||
|
messages.add(message)
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
if capture.stderr.getvalue():
|
||||||
|
message = '%s:%d:%d: %s: %s' % (path, 0, 0, 'Output', 'Import resulted in output to stderr.')
|
||||||
|
messages.add(message)
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def capture_output(capture):
|
||||||
|
"""Capture sys.stdout and sys.stderr.
|
||||||
|
:type capture: Capture
|
||||||
|
"""
|
||||||
|
old_stdout = sys.stdout
|
||||||
|
old_stderr = sys.stderr
|
||||||
|
|
||||||
|
sys.stdout = capture.stdout
|
||||||
|
sys.stderr = capture.stderr
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
sys.stdout = old_stdout
|
||||||
|
sys.stderr = old_stderr
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue