Commit e310b9bbf1d35f3cce0aa414fda52e67caaf283b

Authored by Christian Herdtweck
1 parent e1d931de

unittest: remove OutputCapture (not needed any more)

The OutputCapture was a slightly hacky way to get output from scripts without
using subprocess. However, keeping it working started requiring reloads since
module's logs are global variables which were re-initialized which caused
trouble). That required reload of all submodules, so more hacks are needed
to keep the initial hack "alive". Therefore I am glad it is not needed any more
and can be removed.
tests/test_utils/__init__.py
1 -from .output_capture import OutputCapture  
2 -  
3 from os.path import dirname, join 1 from os.path import dirname, join
4 2
5 # Directory with test data, independent of current working directory 3 # Directory with test data, independent of current working directory
tests/test_utils/output_capture.py deleted
1 -""" class OutputCapture to test what scripts print to stdout """  
2 -  
3 -from __future__ import print_function  
4 -import sys  
5 -import logging  
6 -  
7 -  
8 -# python 2/3 version conflict:  
9 -if sys.version_info.major <= 2:  
10 - from StringIO import StringIO  
11 - # reload is a builtin  
12 -else:  
13 - from io import StringIO  
14 - if sys.version_info.minor < 4:  
15 - from imp import reload  
16 - else:  
17 - from importlib import reload  
18 -  
19 -  
20 -class OutputCapture:  
21 - """ context manager that captures stdout  
22 -  
23 - use as follows::  
24 -  
25 - with OutputCapture() as capturer:  
26 - run_my_script(some_args)  
27 -  
28 - # either test line-by-line ...  
29 - for line in capturer:  
30 - some_test(line)  
31 - # ...or test all output in one go  
32 - some_test(capturer.get_data())  
33 -  
34 - In order to solve issues with old logger instances still remembering closed  
35 - StringIO instances as "their" stdout, logging is shutdown and restarted  
36 - upon entering this Context Manager. This means that you may have to reload  
37 - your module, as well.  
38 - """  
39 -  
40 - def __init__(self):  
41 - self.buffer = StringIO()  
42 - self.orig_stdout = None  
43 - self.data = None  
44 -  
45 - def __enter__(self):  
46 - # Avoid problems with old logger instances that still remember an old  
47 - # closed StringIO as their sys.stdout  
48 - logging.shutdown()  
49 - reload(logging)  
50 -  
51 - # replace sys.stdout with own buffer.  
52 - self.orig_stdout = sys.stdout  
53 - sys.stdout = self.buffer  
54 - return self  
55 -  
56 - def __exit__(self, exc_type, exc_value, traceback):  
57 - sys.stdout = self.orig_stdout # re-set to original  
58 - self.data = self.buffer.getvalue()  
59 - self.buffer.close() # close buffer  
60 - self.buffer = None  
61 -  
62 - if exc_type: # there has been an error  
63 - print('Got error during output capture!')  
64 - print('Print captured output and re-raise:')  
65 - for line in self.data.splitlines():  
66 - print(line.rstrip()) # print output before re-raising  
67 -  
68 - def get_data(self):  
69 - """ retrieve all the captured data """  
70 - if self.buffer is not None:  
71 - return self.buffer.getvalue()  
72 - elif self.data is not None:  
73 - return self.data  
74 - else: # should not be possible  
75 - raise RuntimeError('programming error or someone messed with data!')  
76 -  
77 - def __iter__(self):  
78 - for line in self.get_data().splitlines():  
79 - yield line  
80 -  
81 - def reload_module(self, mod):  
82 - """ Wrapper around reload function for different python versions """  
83 - return reload(mod)