summaryrefslogtreecommitdiffstats
path: root/testing/language_helpers.py
blob: b9c538403dac86d8b3b8987261ff5a1439cff249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from __future__ import annotations

import os
from typing import Sequence

from pre_commit.languages.all import Language
from pre_commit.prefix import Prefix


def run_language(
        path: os.PathLike[str],
        language: Language,
        exe: str,
        args: Sequence[str] = (),
        file_args: Sequence[str] = (),
        version: str | None = None,
        deps: Sequence[str] = (),
        is_local: bool = False,
) -> tuple[int, bytes]:
    prefix = Prefix(str(path))
    version = version or language.get_default_version()

    language.install_environment(prefix, version, deps)
    health_error = language.health_check(prefix, version)
    assert health_error is None, health_error
    with language.in_env(prefix, version):
        ret, out = language.run_hook(
            prefix,
            exe,
            args,
            file_args,
            is_local=is_local,
            require_serial=True,
            color=False,
        )
        out = out.replace(b'\r\n', b'\n')
        return ret, out