|
1
|
|
|
from contextlib import contextmanager |
|
2
|
|
|
import os |
|
3
|
|
|
import pip |
|
4
|
|
|
import pkg_resources |
|
5
|
|
|
import site |
|
6
|
|
|
import sys |
|
7
|
|
|
from unittest.case import SkipTest |
|
8
|
|
|
|
|
9
|
|
|
if sys.version_info < (3, 4): |
|
10
|
|
|
import imp as importlib |
|
11
|
|
|
else: |
|
12
|
|
|
import importlib |
|
13
|
|
|
|
|
14
|
|
|
from coalib.misc.ContextManagers import retrieve_stdout |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def execute_coala(func, binary, *args): |
|
18
|
|
|
""" |
|
19
|
|
|
Executes the main function with the given argument string from given module. |
|
20
|
|
|
|
|
21
|
|
|
:param function: A main function from coala_json, coala_ci module etc. |
|
22
|
|
|
:param binary: A binary to execute coala test |
|
23
|
|
|
:return: A tuple holding a return value first and |
|
24
|
|
|
a stdout output as second element. |
|
25
|
|
|
""" |
|
26
|
|
|
sys.argv = [binary] + list(args) |
|
27
|
|
|
with retrieve_stdout() as stdout: |
|
28
|
|
|
retval = func() |
|
29
|
|
|
return retval, stdout.getvalue() |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def teardown_bear_test_module(new_sys_path): |
|
33
|
|
|
""" |
|
34
|
|
|
Removes the coala bears test module from user directory and |
|
35
|
|
|
reimport pkg_resources. |
|
36
|
|
|
""" |
|
37
|
|
|
if pip.main(["uninstall", "-y", "coala-bears-test-module"]) == 0: |
|
38
|
|
|
importlib.reload(site) |
|
39
|
|
|
importlib.reload(pkg_resources) |
|
40
|
|
|
else: |
|
41
|
|
|
raise AssertionError("Unable to uninstall coala-bears-test-module") |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def setup_bear_test_module(): |
|
45
|
|
|
""" |
|
46
|
|
|
We install coala bears test module using pip and reload pkg_resource to |
|
47
|
|
|
reflect the change. |
|
48
|
|
|
We first try to install it to the normal directory which works when using |
|
49
|
|
|
pyenv or virtualenv. If this requires sudo or is not writable, we try |
|
50
|
|
|
installing with the --user flag. if neither of these works, we skip the |
|
51
|
|
|
test. |
|
52
|
|
|
""" |
|
53
|
|
|
bears_test_module = os.path.join(os.path.dirname(__file__), |
|
54
|
|
|
"coala_bears_test_module") |
|
55
|
|
|
if (pip.main(["install", "-I", bears_test_module]) == 0 or |
|
56
|
|
|
pip.main(["install", "--user", "-I", bears_test_module]) == 0): |
|
57
|
|
|
importlib.reload(site) # Update PATH if installed with --user |
|
58
|
|
|
importlib.reload(pkg_resources) # Update entrypoints |
|
59
|
|
|
else: |
|
60
|
|
|
raise SkipTest("Unable to install coala_bears_test_module") |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
@contextmanager |
|
64
|
|
|
def bear_test_module(): |
|
65
|
|
|
old_sys_path = setup_bear_test_module() |
|
66
|
|
|
try: |
|
67
|
|
|
yield |
|
68
|
|
|
finally: |
|
69
|
|
|
teardown_bear_test_module(old_sys_path) |
|
70
|
|
|
|