1# coding=utf-8
2import sys
3
4import pytest
5from _pytest.config import get_plugin_manager
6
7from pkg_resources import iter_entry_points
8
9from _jb_runner_tools import jb_patch_separator, jb_doc_args, JB_DISABLE_BUFFERING, start_protocol, parse_arguments, \
10    set_parallel_mode
11from teamcity import pytest_plugin
12
13if __name__ == '__main__':
14    path, targets, additional_args = parse_arguments()
15    sys.argv += additional_args
16    joined_targets = jb_patch_separator(targets, fs_glue="/", python_glue="::", fs_to_python_glue=".py::")
17    # When file is launched in pytest it should be file.py: you can't provide it as bare module
18    joined_targets = [t + ".py" if ":" not in t else t for t in joined_targets]
19    sys.argv += [path] if path else joined_targets
20
21    # plugin is discovered automatically in 3, but not in 2
22    # to prevent "plugin already registered" problem we check it first
23    plugins_to_load = []
24    if not get_plugin_manager().hasplugin("pytest-teamcity"):
25        if "pytest-teamcity" not in map(lambda e: e.name, iter_entry_points(group='pytest11', name=None)):
26            plugins_to_load.append(pytest_plugin)
27
28    args = sys.argv[1:]
29    if JB_DISABLE_BUFFERING and "-s" not in args:
30        args += ["-s"]
31
32    jb_doc_args("pytest", args)
33
34
35    class Plugin:
36        @staticmethod
37        def pytest_configure(config):
38            if getattr(config.option, "numprocesses", None):
39                set_parallel_mode()
40            start_protocol()
41
42
43    sys.exit(pytest.main(args, plugins_to_load + [Plugin]))
44