"""Find version of executable."""

from pathlib import Path
from subprocess import TimeoutExpired, run


def get_executable_version(executable: Path) -> str:
    """Return the version of the executable."""

    # print(f"get_executable_version {executable}")
    try:
        version_res = run([str(executable), "--version"], check=False, capture_output=True, timeout=15)  # noqa: S603
        if version_res.returncode != 0:
            return "<version fetch error>"
        return version_res.stdout.decode("utf-8").strip().replace("\n", " ")
    except TimeoutExpired:
        return "<version fetch timeout>"
