# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2
# 
# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):
# 
# Path: test/framework/utils.py
# def lines_in(api_type, text, *expected_lines):
#     """Assert that `*expected_lines` occur in order in the lines of `text`.
# 
#     Args:
#         *expected_lines (str or RegexObject (hasattr `match`)): For each `expected_line` in expected_lines:
#             If `expected_line` has a match method it is called and must return True for a line in `text`.
#             Otherwise, if the `expected_line` starts with '^', a line in `text` must start with `expected_line[1:]`
#             Otherwise `expected line` must simply occur in a line in `text`
#     """
# 
#     assert isinstance(api_type, test_cfg.ApiType)
#     assert text
#     assert expected_lines
# 
#     return generic_lines_in(text, partial(replace_host_port, api_type), *expected_lines)


import os, multiprocessing
import time
import bottle
import requests
import pytest
from pytest import raises
from jenkinsflow import jenkins_api
from .cfg import ApiType
from .framework import api_select
from .framework.utils import lines_in
def _api():
    return bottle.static_file('which_ci_server.html', root=here)


_host = 'localhost'
_port = 8082


def _server():
    bottle.run(host=_host, port=_port, debug=True)


@pytest.mark.apis(ApiType.JENKINS)
def test_which_ci_server_not_ci(api_type):
    proc = None
    try:
        with api_select.api(__file__, api_type):
            proc = multiprocessing.Process(target=_server)
            proc.start()

            with raises(Exception) as exinfo:
                for _ in range(0, 10):
                    ex = None
                    try:
                        jenkins_api.Jenkins("http://" + _host + ':' + repr(_port), "dummy").poll()
                    except requests.exceptions.ConnectionError as ex:
                        # Wait for bottle to start
                        print(ex)
                        time.sleep(0.1)


# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2
# 
# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):
# 
# Path: test/framework/utils.py
# def lines_in(api_type, text, *expected_lines):
#     """Assert that `*expected_lines` occur in order in the lines of `text`.
# 
#     Args:
#         *expected_lines (str or RegexObject (hasattr `match`)): For each `expected_line` in expected_lines:
#             If `expected_line` has a match method it is called and must return True for a line in `text`.
#             Otherwise, if the `expected_line` starts with '^', a line in `text` must start with `expected_line[1:]`
#             Otherwise `expected line` must simply occur in a line in `text`
#     """
# 
#     assert isinstance(api_type, test_cfg.ApiType)
#     assert text
#     assert expected_lines
# 
#     return generic_lines_in(text, partial(replace_host_port, api_type), *expected_lines)


# Path: test/which_ci_server_test.py
import os, multiprocessing
import time
import bottle
import requests
import pytest
from pytest import raises
from jenkinsflow import jenkins_api
from .cfg import ApiType
from .framework import api_select
from .framework.utils import lines_in

def _api():
    return bottle.static_file('which_ci_server.html', root=here)


_host = 'localhost'
_port = 8082


def _server():
    bottle.run(host=_host, port=_port, debug=True)


@pytest.mark.apis(ApiType.JENKINS)
def test_which_ci_server_not_ci(api_type):
    proc = None
    try:
        with api_select.api(__file__, api_type):
            proc = multiprocessing.Process(target=_server)
            proc.start()

            with raises(Exception) as exinfo:
                for _ in range(0, 10):
                    ex = None
                    try:
                        jenkins_api.Jenkins("http://" + _host + ':' + repr(_port), "dummy").poll()
                    except requests.exceptions.ConnectionError as ex:
                        # Wait for bottle to start
                        print(ex)
                        time.sleep(0.1)


            assert lines_in(