# Copyright cocotb contributors
# Licensed under the Revised BSD License, see LICENSE for details.
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations

from cocotb_tools import _env


def start_cocotb_library_coverage() -> None:  # pragma: no cover
    if not _env.get_bool("COCOTB_LIBRARY_COVERAGE"):
        return
    try:
        import coverage  # noqa: PLC0415
    except (ImportError, ModuleNotFoundError):
        raise RuntimeError(
            "cocotb library coverage collection requested but coverage package not available. Install it using `pip install coverage`."
        ) from None
    else:
        library_coverage = coverage.coverage(
            data_file=".coverage.cocotb",
            config_file=False,
            branch=True,
            source=["cocotb"],
        )
        library_coverage.load()
        library_coverage.start()

        def stop_library_coverage() -> None:
            library_coverage.stop()
            library_coverage.save()  # pragma: no cover

        # This must come after `library_coverage.start()` to ensure coverage is being
        # collected on the cocotb library before importing from it.
        import cocotb._shutdown  # noqa: PLC0415

        cocotb._shutdown.register(stop_library_coverage)
