mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 00:04:42 +04:00
Merge pull request #3122 from glehmann/docker-tty
give docker a tty output when expecting color
This commit is contained in:
commit
5e11c266ae
3 changed files with 32 additions and 3 deletions
|
|
@ -108,10 +108,15 @@ def get_docker_user() -> tuple[str, ...]: # pragma: win32 no cover
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
|
|
||||||
def docker_cmd() -> tuple[str, ...]: # pragma: win32 no cover
|
def get_docker_tty(*, color: bool) -> tuple[str, ...]: # pragma: win32 no cover # noqa: E501
|
||||||
|
return (('--tty',) if color else ())
|
||||||
|
|
||||||
|
|
||||||
|
def docker_cmd(*, color: bool) -> tuple[str, ...]: # pragma: win32 no cover
|
||||||
return (
|
return (
|
||||||
'docker', 'run',
|
'docker', 'run',
|
||||||
'--rm',
|
'--rm',
|
||||||
|
*get_docker_tty(color=color),
|
||||||
*get_docker_user(),
|
*get_docker_user(),
|
||||||
# https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container-volumes-from
|
# https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container-volumes-from
|
||||||
# The `Z` option tells Docker to label the content with a private
|
# The `Z` option tells Docker to label the content with a private
|
||||||
|
|
@ -139,7 +144,7 @@ def run_hook(
|
||||||
|
|
||||||
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
|
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
|
||||||
return lang_base.run_xargs(
|
return lang_base.run_xargs(
|
||||||
(*docker_cmd(), *entry_tag, *cmd_rest),
|
(*docker_cmd(color=color), *entry_tag, *cmd_rest),
|
||||||
file_args,
|
file_args,
|
||||||
require_serial=require_serial,
|
require_serial=require_serial,
|
||||||
color=color,
|
color=color,
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ def run_hook(
|
||||||
require_serial: bool,
|
require_serial: bool,
|
||||||
color: bool,
|
color: bool,
|
||||||
) -> tuple[int, bytes]: # pragma: win32 no cover
|
) -> tuple[int, bytes]: # pragma: win32 no cover
|
||||||
cmd = docker_cmd() + lang_base.hook_cmd(entry, args)
|
cmd = docker_cmd(color=color) + lang_base.hook_cmd(entry, args)
|
||||||
return lang_base.run_xargs(
|
return lang_base.run_xargs(
|
||||||
cmd,
|
cmd,
|
||||||
file_args,
|
file_args,
|
||||||
|
|
|
||||||
|
|
@ -25,3 +25,27 @@ def test_docker_image_hook_via_args(tmp_path):
|
||||||
args=('hello hello world',),
|
args=('hello hello world',),
|
||||||
)
|
)
|
||||||
assert ret == (0, b'hello hello world\n')
|
assert ret == (0, b'hello hello world\n')
|
||||||
|
|
||||||
|
|
||||||
|
@xfailif_windows # pragma: win32 no cover
|
||||||
|
def test_docker_image_color_tty(tmp_path):
|
||||||
|
ret = run_language(
|
||||||
|
tmp_path,
|
||||||
|
docker_image,
|
||||||
|
'ubuntu:22.04',
|
||||||
|
args=('grep', '--color', 'root', '/etc/group'),
|
||||||
|
color=True,
|
||||||
|
)
|
||||||
|
assert ret == (0, b'\x1b[01;31m\x1b[Kroot\x1b[m\x1b[K:x:0:\n')
|
||||||
|
|
||||||
|
|
||||||
|
@xfailif_windows # pragma: win32 no cover
|
||||||
|
def test_docker_image_no_color_no_tty(tmp_path):
|
||||||
|
ret = run_language(
|
||||||
|
tmp_path,
|
||||||
|
docker_image,
|
||||||
|
'ubuntu:22.04',
|
||||||
|
args=('grep', '--color', 'root', '/etc/group'),
|
||||||
|
color=False,
|
||||||
|
)
|
||||||
|
assert ret == (0, b'root:x:0:\n')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue