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