9d773dd9d80648ae0ca2ddd814d7e8e3c27d1f2a
[idea/community.git] / python / helpers / pycharm / _jb_tox_runner.py
1 # coding=utf-8
2 """
3 Runs tox from current directory.
4 It supports any runner, but well-known runners (py.test and unittest) are switched to our internal runners to provide better support
5 """
6 import os
7 import sys
8
9 from tox import config as tox_config, session as tox_session
10 from tox.session import Reporter
11
12 from tcmessages import TeamcityServiceMessages
13
14 helpers_dir = str(os.path.split(__file__)[0])
15
16 # Placeholder to insert arguments
17 __ARGUMENTS_KEY = "__jb_place_for_arguments"
18
19
20 # List of local runners to use
21 _RUNNERS = {"unit2": [os.path.join(helpers_dir, "utrunner.py"), os.getcwd(), __ARGUMENTS_KEY, "true"]}
22
23 teamcity = TeamcityServiceMessages()
24
25
26 class _Reporter(Reporter):
27     def logaction_start(self, action):
28         super(_Reporter, self).logaction_start(action)
29         if action.activity == "getenv":
30             teamcity.testSuiteStarted(action.id)
31             self.current_suite = action.id
32
33     def logaction_finish(self, action):
34         super(_Reporter, self).logaction_finish(action)
35         if action.activity == "runtests":
36             teamcity.testSuiteFinished(action.id)
37
38     def error(self, msg):
39         super(_Reporter, self).error(msg)
40         name = teamcity.current_test_name()
41         if name:
42             if name != teamcity.topmost_suite:
43                 teamcity.testFailed(name, msg)
44             else:
45                 teamcity.testFailed("ERROR", msg)
46                 teamcity.testSuiteFinished(name)
47         else:
48             sys.stderr.write(msg)
49
50     def skip(self, msg):
51         super(_Reporter, self).skip(msg)
52         name = teamcity.current_test_name()
53         if name:
54             teamcity.testFinished(name)
55
56
57 config = tox_config.parseconfig()
58 for env, tmp_config in config.envconfigs.items():
59     if not tmp_config.setenv:
60         tmp_config.setenv = dict()
61     tmp_config.setenv.update({"_jb_do_not_call_enter_matrix": "1"})
62     commands = tmp_config.commands
63     if isinstance(commands, list) and len(commands):
64         command_with_arguments = commands[0]
65         command = command_with_arguments[0]
66         arguments = command_with_arguments[1:]
67         if command in _RUNNERS:
68             command_with_arguments = list(_RUNNERS[command_with_arguments[0]])
69             index_key = command_with_arguments.index(__ARGUMENTS_KEY)
70             if index_key:
71                 if arguments:
72                     command_with_arguments[index_key:index_key + len(arguments)] = arguments
73                 else:
74                     command_with_arguments[index_key:index_key+1] = []
75             tmp_config.commands = [command_with_arguments]
76
77 session = tox_session.Session(config, Report=_Reporter)
78 teamcity.testMatrixEntered()
79 session.runcommand()