# 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/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2


import sys, os, imp
import pytest
import basic, calculated_flow, prefix, hide_password, errors
from os.path import join as jp
from pytest import raises
from jenkinsflow.flow import parallel, JobControlFailException
from .framework import api_select
from .cfg import ApiType


def load_demo_jobs(demo, api_type):
    print("\nLoad jobs for demo:", demo.__name__)
    job_load_module_name = demo.__name__ + '_jobs'
    job_load = imp.load_source(job_load_module_name, jp(here, '../demo/jobs', job_load_module_name + '.py'))
    api = job_load.create_jobs(api_type)
    return api


def _test_demo(demo, api_type):
    load_demo_jobs(demo, api_type)

    with api_select.api(__file__, api_type, fixed_prefix="jenkinsflow_demo__") as api:
        api.job(demo.__name__ + "__0flow", max_fails=0, expect_invocations=1, expect_order=1)

        with parallel(api, timeout=70, job_name_prefix=api.job_name_prefix) as ctrl1:
            ctrl1.invoke(demo.__name__ + "__0flow")


@pytest.mark.not_apis(ApiType.SCRIPT)  # TODO: script api is not configured to run demos
def test_demos_basic(api_type):
    _test_demo(basic, api_type)


@pytest.mark.not_apis(ApiType.SCRIPT)  # TODO: script api is not configured to run demos
def test_demos_calculated_flow(api_type):
    _test_demo(calculated_flow, api_type)



# 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/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2


# Path: test/demos_test.py
import sys, os, imp
import pytest
import basic, calculated_flow, prefix, hide_password, errors
from os.path import join as jp
from pytest import raises
from jenkinsflow.flow import parallel, JobControlFailException
from .framework import api_select
from .cfg import ApiType



def load_demo_jobs(demo, api_type):
    print("\nLoad jobs for demo:", demo.__name__)
    job_load_module_name = demo.__name__ + '_jobs'
    job_load = imp.load_source(job_load_module_name, jp(here, '../demo/jobs', job_load_module_name + '.py'))
    api = job_load.create_jobs(api_type)
    return api


def _test_demo(demo, api_type):
    load_demo_jobs(demo, api_type)

    with api_select.api(__file__, api_type, fixed_prefix="jenkinsflow_demo__") as api:
        api.job(demo.__name__ + "__0flow", max_fails=0, expect_invocations=1, expect_order=1)

        with parallel(api, timeout=70, job_name_prefix=api.job_name_prefix) as ctrl1:
            ctrl1.invoke(demo.__name__ + "__0flow")


@pytest.mark.not_apis(ApiType.SCRIPT)  # TODO: script api is not configured to run demos
def test_demos_basic(api_type):
    _test_demo(basic, api_type)


@pytest.mark.not_apis(ApiType.SCRIPT)  # TODO: script api is not configured to run demos
def test_demos_calculated_flow(api_type):
    _test_demo(calculated_flow, api_type)



@pytest.mark.not_apis(ApiType.SCRIPT)  # TODO: script api is not configured to run demos