Coverage for src/markdown_exec/formatters/bash.py: 50.00%
12 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-26 20:30 +0200
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-26 20:30 +0200
1"""Formatter for executing shell code."""
3from __future__ import annotations
5import subprocess
6from typing import Any
8from markdown_exec.formatters.base import ExecutionError, base_format
9from markdown_exec.rendering import code_block
12def _run_bash(
13 code: str,
14 returncode: int | None = None,
15 session: str | None = None, # noqa: ARG001
16 id: str | None = None, # noqa: A002,ARG001
17 **extra: str,
18) -> str:
19 process = subprocess.run(
20 ["bash", "-c", code], # noqa: S603,S607
21 stdout=subprocess.PIPE,
22 stderr=subprocess.STDOUT,
23 text=True,
24 )
25 if process.returncode != returncode:
26 raise ExecutionError(code_block("sh", process.stdout, **extra), process.returncode)
27 return process.stdout
30def _format_bash(**kwargs: Any) -> str:
31 return base_format(language="bash", run=_run_bash, **kwargs)