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


import re
import pytest
from pytest import raises
from jenkinsflow.flow import parallel, serial, MissingJobsException, FailedChildJobsException, FailedChildJobException, UnknownJobException
from .framework import api_select
from .framework.utils import lines_in
from .cfg import ApiType
                ctrl1.invoke('j1')
                ctrl1.invoke('missingA')
                with ctrl1.serial() as ctrl2:
                    ctrl2.invoke('missingB')
                    ctrl2.invoke('j2')
                ctrl1.invoke('missingC')


@pytest.mark.not_apis(ApiType.SCRIPT)  # TODO: Handle ApiType.SCRIPT
def test_missing_jobs_allowed_created_serial_parallel(api_type):
    with api_select.api(__file__, api_type) as api:
        with api.job_creator():
            api.flow_job()
            api.job('j1', max_fails=0, expect_invocations=1, expect_order=1, create_job='missingA')
            api.job('missingA', max_fails=0, expect_invocations=1, expect_order=2, flow_created=True, create_job='missingB')
            api.job('missingB', max_fails=0, expect_invocations=1, expect_order=3, flow_created=True)
            api.job('j2', max_fails=0, expect_invocations=1, expect_order=3, create_job='missingC')
            api.job('missingC', max_fails=0, expect_invocations=1, expect_order=4, flow_created=True)

        with serial(api, 20, job_name_prefix=api.job_name_prefix, allow_missing_jobs=True) as ctrl1:
            ctrl1.invoke('j1')
            ctrl1.invoke('missingA')
            with ctrl1.parallel() as ctrl2:
                ctrl2.invoke('missingB')
                ctrl2.invoke('j2')
            ctrl1.invoke('missingC')

        # TODO: Validate output



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


# Path: test/missing_jobs_test.py
import re
import pytest
from pytest import raises
from jenkinsflow.flow import parallel, serial, MissingJobsException, FailedChildJobsException, FailedChildJobException, UnknownJobException
from .framework import api_select
from .framework.utils import lines_in
from .cfg import ApiType

                ctrl1.invoke('j1')
                ctrl1.invoke('missingA')
                with ctrl1.serial() as ctrl2:
                    ctrl2.invoke('missingB')
                    ctrl2.invoke('j2')
                ctrl1.invoke('missingC')


@pytest.mark.not_apis(ApiType.SCRIPT)  # TODO: Handle ApiType.SCRIPT
def test_missing_jobs_allowed_created_serial_parallel(api_type):
    with api_select.api(__file__, api_type) as api:
        with api.job_creator():
            api.flow_job()
            api.job('j1', max_fails=0, expect_invocations=1, expect_order=1, create_job='missingA')
            api.job('missingA', max_fails=0, expect_invocations=1, expect_order=2, flow_created=True, create_job='missingB')
            api.job('missingB', max_fails=0, expect_invocations=1, expect_order=3, flow_created=True)
            api.job('j2', max_fails=0, expect_invocations=1, expect_order=3, create_job='missingC')
            api.job('missingC', max_fails=0, expect_invocations=1, expect_order=4, flow_created=True)

        with serial(api, 20, job_name_prefix=api.job_name_prefix, allow_missing_jobs=True) as ctrl1:
            ctrl1.invoke('j1')
            ctrl1.invoke('missingA')
            with ctrl1.parallel() as ctrl2:
                ctrl2.invoke('missingB')
                ctrl2.invoke('j2')
            ctrl1.invoke('missingC')

        # TODO: Validate output



@pytest.mark.not_apis(ApiType.MOCK, ApiType.SCRIPT)  # TODO: Handle ApiTypes