| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from tox.execute.request import ExecuteRequest, StdinSource |
| 10 |
|
| 11 |
|
| 12 |
def test_execute_request_raise_on_empty_cmd(os_env: dict[str, str]) -> None: |
| 13 |
with pytest.raises(ValueError, match="cannot execute an empty command"): |
| 14 |
ExecuteRequest(cmd=[], cwd=Path().absolute(), env=os_env, stdin=StdinSource.OFF, run_id="") |
| 15 |
|
| 16 |
|
| 17 |
def test_request_allow_star_is_none() -> None: |
| 18 |
request = ExecuteRequest( |
| 19 |
cmd=[sys.executable], |
| 20 |
cwd=Path.cwd(), |
| 21 |
env={"PATH": os.environ["PATH"]}, |
| 22 |
stdin=StdinSource.OFF, |
| 23 |
run_id="run-id", |
| 24 |
allow=["*", "magic"], |
| 25 |
) |
| 26 |
assert request.allow is None |
| 27 |
|
| 28 |
|
| 29 |
def test_shell_cmd_redacted_masks_secret_flag_value() -> None: |
| 30 |
request = ExecuteRequest( |
| 31 |
cmd=["pytest", "--token=hunter2", "tests"], |
| 32 |
cwd=Path.cwd(), |
| 33 |
env={}, |
| 34 |
stdin=StdinSource.OFF, |
| 35 |
run_id="", |
| 36 |
) |
| 37 |
assert "hunter2" not in request.shell_cmd_redacted |
| 38 |
assert "--token=*******" in request.shell_cmd_redacted |
| 39 |
|
| 40 |
|
| 41 |
def test_shell_cmd_unredacted_preserves_secret_value() -> None: |
| 42 |
request = ExecuteRequest( |
| 43 |
cmd=["pytest", "--token=hunter2"], |
| 44 |
cwd=Path.cwd(), |
| 45 |
env={}, |
| 46 |
stdin=StdinSource.OFF, |
| 47 |
run_id="", |
| 48 |
) |
| 49 |
assert "hunter2" in request.shell_cmd |
| 50 |
|
| 51 |
|
| 52 |
def test_shell_cmd_redacted_leaves_innocuous_args_alone() -> None: |
| 53 |
request = ExecuteRequest( |
| 54 |
cmd=["pytest", "-k", "test_token"], |
| 55 |
cwd=Path.cwd(), |
| 56 |
env={}, |
| 57 |
stdin=StdinSource.OFF, |
| 58 |
run_id="", |
| 59 |
) |
| 60 |
assert request.shell_cmd_redacted == request.shell_cmd |