1import sys
2import os
3import sys
4sys.stderr.write("Warning: You are using test runners in legacy mode\n. "
5                 "That means you have 'python.tests.enableUniversalTests=false' in registry.\n"
6                 "This mode will be dropped in 2021. Consider removing this entry from registry and migrating to new test runners")
7
8helpers_dir = os.getenv("PYCHARM_HELPERS_DIR", sys.path[0])
9if sys.path[0] != helpers_dir:
10    sys.path.insert(0, helpers_dir)
11
12from nose_utils import TeamcityPlugin
13
14from pycharm_run_utils import debug, import_system_module
15from pycharm_run_utils import adjust_sys_path
16
17adjust_sys_path(False)
18
19shlex = import_system_module("shlex")
20
21try:
22  from nose.core import TestProgram
23  from nose.config import Config
24  from nose.plugins.manager import DefaultPluginManager
25except:
26  raise NameError("Please, install nosetests")
27
28teamcity_plugin = TeamcityPlugin()
29
30class MyConfig(Config):
31  def __init__(self, **kw):
32    super(MyConfig, self).__init__(**kw)
33
34  def __setstate__(self, state):
35    super(MyConfig, self).__setstate__(state)
36    self.plugins.addPlugin(teamcity_plugin)
37
38def process_args():
39  tests = []
40
41  opts = None
42  if sys.argv[-1].startswith("-"):
43    test_names = sys.argv[1:-1]
44    opts = sys.argv[-1]
45  else:
46    test_names = sys.argv[1:]
47
48  for arg in test_names:
49    arg = arg.strip()
50    if len(arg) == 0:
51      return
52
53    a = arg.split("::")
54    if len(a) == 1:
55      # From module or folder
56      a_splitted = a[0].split(";")
57      if len(a_splitted) != 1:
58        # means we have pattern to match against
59        if a_splitted[0].endswith("/"):
60          debug("/ from folder " + a_splitted[0] + ". Use pattern: " + a_splitted[1])
61          tests.append(a_splitted[0])
62      else:
63        if a[0].endswith("/"):
64          debug("/ from folder " + a[0])
65          tests.append(a[0])
66        else:
67          debug("/ from module " + a[0])
68          tests.append(a[0])
69
70    elif len(a) == 2:
71      # From testcase
72      debug("/ from testcase " + a[1] + " in " + a[0])
73      tests.append(a[0] + ":" + a[1])
74    else:
75      # From method in class or from function
76      debug("/ from method " + a[2] + " in testcase " +  a[1] + " in " + a[0])
77      if a[1] == "":
78        # test function, not method
79        tests.append(a[0] + ":" + a[2])
80      else:
81        tests.append(a[0] + ":" + a[1] + "." + a[2])
82
83  argv = ['nosetests']
84
85  argv.extend(tests)
86
87
88  if opts:
89    options = shlex.split(opts)
90    argv.extend(options)
91
92  manager = DefaultPluginManager()
93  manager.addPlugin(teamcity_plugin)
94  config = MyConfig(plugins=manager)
95  config.configure(argv)
96
97  TestProgram(argv=argv, config=config, exit=False)
98
99if __name__ == "__main__":
100  process_args()
101