# 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 re
from pytest import raises
from jenkinsflow.flow import parallel, serial, FailedChildJobException, FailedChildJobsException
from .framework import api_select
from .framework.utils import lines_in
    with api_select.api(__file__, api_type) as api:
        api.flow_job()
        api.job('quick', max_fails=0, expect_invocations=1, expect_order=1, params=(('s1', '', 'desc'), ('c1', 'false', 'desc')))
        api.job('quick_fail', max_fails=1, expect_invocations=1, expect_order=1)
        api.job('wait10', max_fails=0, expect_invocations=1, expect_order=1, exec_time=10)
        api.job('wait10_fail', max_fails=1, expect_invocations=1, expect_order=1, exec_time=10)
        api.job('wait5', max_fails=0, expect_invocations=1, expect_order=1, exec_time=5)
        api.job('wait5_fail', max_fails=1, expect_invocations=1, expect_order=1, exec_time=5)

        with raises(FailedChildJobsException) as exinfo:
            with parallel(api, timeout=40, job_name_prefix=api.job_name_prefix, report_interval=3) as ctrl:
                ctrl.invoke('quick', s1='', c1=False)
                ctrl.invoke('quick_fail')
                ctrl.invoke('wait10')
                ctrl.invoke('wait10_fail')
                ctrl.invoke('wait5')
                ctrl.invoke('wait5_fail')

        assert "quick_fail" in str(exinfo.value)
        assert "wait10_fail" in str(exinfo.value)
        assert "wait5_fail" in str(exinfo.value)

        sout, _ = capsys.readouterr()
        assert lines_in(
            api_type, sout,
            re.compile("^FAILURE: 'jenkinsflow_test__single_level_errors_parallel__quick_fail' - build: .*/jenkinsflow_test__single_level_errors_parallel__quick_fail.* after:"),
        )


def test_single_level_errors_serial(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/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/single_level_errors_test.py
import re
from pytest import raises
from jenkinsflow.flow import parallel, serial, FailedChildJobException, FailedChildJobsException
from .framework import api_select
from .framework.utils import lines_in

    with api_select.api(__file__, api_type) as api:
        api.flow_job()
        api.job('quick', max_fails=0, expect_invocations=1, expect_order=1, params=(('s1', '', 'desc'), ('c1', 'false', 'desc')))
        api.job('quick_fail', max_fails=1, expect_invocations=1, expect_order=1)
        api.job('wait10', max_fails=0, expect_invocations=1, expect_order=1, exec_time=10)
        api.job('wait10_fail', max_fails=1, expect_invocations=1, expect_order=1, exec_time=10)
        api.job('wait5', max_fails=0, expect_invocations=1, expect_order=1, exec_time=5)
        api.job('wait5_fail', max_fails=1, expect_invocations=1, expect_order=1, exec_time=5)

        with raises(FailedChildJobsException) as exinfo:
            with parallel(api, timeout=40, job_name_prefix=api.job_name_prefix, report_interval=3) as ctrl:
                ctrl.invoke('quick', s1='', c1=False)
                ctrl.invoke('quick_fail')
                ctrl.invoke('wait10')
                ctrl.invoke('wait10_fail')
                ctrl.invoke('wait5')
                ctrl.invoke('wait5_fail')

        assert "quick_fail" in str(exinfo.value)
        assert "wait10_fail" in str(exinfo.value)
        assert "wait5_fail" in str(exinfo.value)

        sout, _ = capsys.readouterr()
        assert lines_in(
            api_type, sout,
            re.compile("^FAILURE: 'jenkinsflow_test__single_level_errors_parallel__quick_fail' - build: .*/jenkinsflow_test__single_level_errors_parallel__quick_fail.* after:"),
        )


def test_single_level_errors_serial(api_type):

    with api_select.api(__file__, api_type) as api: