Coverage for src/markdown_exec/formatters/sh.py: 100.00%

12 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-26 20:30 +0200

1"""Formatter for executing shell code.""" 

2 

3from __future__ import annotations 

4 

5import subprocess 

6from typing import Any 

7 

8from markdown_exec.formatters.base import ExecutionError, base_format 

9from markdown_exec.rendering import code_block 

10 

11 

12def _run_sh( 

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 ["sh", "-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 

28 

29 

30def _format_sh(**kwargs: Any) -> str: 

31 return base_format(language="sh", run=_run_sh, **kwargs)