File: util.py

package info (click to toggle)
wheel 0.46.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 444 kB
  • sloc: python: 2,503; makefile: 141; sh: 23
file content (43 lines) | stat: -rw-r--r-- 1,116 bytes parent folder | download | duplicates (2)
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
38
39
40
41
42
43
from __future__ import annotations

import sys
from io import StringIO
from os import PathLike
from subprocess import CalledProcessError
from unittest.mock import patch

import pytest

from wheel._commands import main


def run_command(
    command: str, *args: str | PathLike, catch_systemexit: bool = True
) -> str:
    returncode = 0
    stdout = StringIO()
    stderr = StringIO()
    args = ("wheel", command) + tuple(str(arg) for arg in args)
    with (
        patch.object(sys, "argv", args),
        patch.object(sys, "stdout", stdout),
        patch.object(sys, "stderr", stderr),
    ):
        try:
            main()
        except SystemExit as exc:
            if not catch_systemexit:
                raise CalledProcessError(
                    exc.code, args, stdout.getvalue(), stderr.getvalue()
                ) from exc

            returncode = exc.code

    if returncode:
        pytest.fail(
            f"'wheel {command}' exited with return code {returncode}\n"
            f"arguments: {args}\n"
            f"error output:\n{stderr.getvalue()}"
        )

    return stdout.getvalue()