Failed Conditions
Pull Request — master (#1596)
by Abdeali
01:45
created

coalib.tests.execute_coala()   A

Complexity

Conditions 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 13
rs 9.4285
1
from contextlib import contextmanager
2
import os
3
import pkg_resources
4
import sys
5
6
from coalib.misc.ContextManagers import retrieve_stdout
7
8
9
def execute_coala(func, binary, *args):
10
    """
11
    Executes the main function with the given argument string from given module.
12
13
    :param function: A main function from coala_json, coala_ci module etc.
14
    :param binary:   A binary to execute coala test
15
    :return:         A tuple holding a return value first and
16
                     a stdout output as second element.
17
    """
18
    sys.argv = [binary] + list(args)
19
    with retrieve_stdout() as stdout:
20
        retval = func()
21
        return retval, stdout.getvalue()
22
23
24
@contextmanager
25
def bear_test_module():
26
    """
27
    This function replaces the pkg_resources.iter_entry_points()
28
    to use the testing bear module we have. Hence, it doesn't test
29
    the collection of entry points.
30
    """
31
    old_iter = pkg_resources.iter_entry_points
32
    bears_test_module = os.path.join(os.path.dirname(__file__),
33
                                     "coala_bears_test_module",
34
                                     "__init__.py")
35
36
    def test_iter_entry_points(name):
37
        assert name == "coalabears"
38
39
        class EntryPoint:
40
41
            @staticmethod
42
            def load():
43
                class PseudoPlugin:
44
                    __file__ = bears_test_module
45
                return PseudoPlugin()
46
47
        return iter([EntryPoint()])
48
49
    pkg_resources.iter_entry_points = test_iter_entry_points
50
    try:
51
        yield
52
    finally:
53
        pkg_resources.iter_entry_points = old_iter
54