1import os
 2import shutil
 3from pathlib import Path
 4import pytest
 5from pytest_postgresql import factories
 6
 7<<<hidden due to potential security issue>>>
 8psql_conn = factories.postgresql("postgresql_db_proc", db_name="integrationtest")
 9
10
11@pytest.fixture(autouse=True, scope="session")
12def ensure_du():
13    try:
14        os.environ["CHART_SETTINGS_MODULE"]
15    except KeyError:
16        os.environ["CHART_SETTINGS_MODULE"] = "chartepssg.project_settings"
17    from chart.project import settings
18
19    print(f"The used DU is: {settings.DU_DIR} ", end="")
20    if settings.DU_DIR.exists():
21        print(", using existing DU")
22        yield
23        return
24    test_dir = Path(Path(__file__).parent)
25    test_du = test_dir / "../chartepssg_du_for_integration_tests"
26    print(", using test DU: {test_du}")
27    shutil.copytree(test_du, settings.DU_DIR)
28    yield
29    shutil.rmtree(settings.DU_DIR)
30
31
32@pytest.fixture()
33def project_root():
34    return (Path(Path(__file__).parent) / "../../").resolve()
35
36
37@pytest.hookimpl(hookwrapper=True, tryfirst=True)
38def pytest_pyfunc_call(pyfuncitem):
39    outcome = yield
40    try:
41        outcome.get_result()
42    except Exception as e:
43        trace_dump = Path("trace.dump")
44        if trace_dump.exists():
45            trace_dump.rename(f"trace_{pyfuncitem.name}.dump")
46        raise e