|
1 | 1 | import json
|
2 | 2 | import logging
|
3 | 3 | import os
|
| 4 | +import shutil |
| 5 | +import stat |
4 | 6 | import subprocess
|
5 | 7 | import sys
|
6 | 8 | from io import TextIOWrapper
|
7 | 9 | from pathlib import Path
|
8 | 10 | from typing import Dict, List, Tuple
|
9 |
| -from unittest.mock import MagicMock |
| 11 | +from unittest.mock import MagicMock, create_autospec |
10 | 12 |
|
11 | 13 | import psutil
|
12 | 14 | import pytest
|
|
15 | 17 | from psutil import AccessDenied
|
16 | 18 | from pytest_mock import MockerFixture
|
17 | 19 |
|
18 |
| -from tox.execute.api import Outcome |
| 20 | +from tox.execute.api import ExecuteOptions, Outcome |
19 | 21 | from tox.execute.local_sub_process import SIG_INTERRUPT, LocalSubProcessExecuteInstance, LocalSubProcessExecutor
|
20 | 22 | from tox.execute.request import ExecuteRequest, StdinSource
|
21 | 23 | from tox.execute.stream import SyncWrite
|
@@ -298,3 +300,36 @@ def test_allow_list_external_ok(fake_exe_on_path: Path, mode: str) -> None:
|
298 | 300 | inst = LocalSubProcessExecuteInstance(request, MagicMock(), out=SyncWrite("out", None), err=SyncWrite("err", None))
|
299 | 301 |
|
300 | 302 | assert inst.cmd == [exe]
|
| 303 | + |
| 304 | + |
| 305 | +def test_shebang_limited_on(tmp_path: Path) -> None: |
| 306 | + exe, script, instance = _create_shebang_test(tmp_path, env={"TOX_LIMITED_SHEBANG": "1"}) |
| 307 | + if sys.platform == "win32": # pragma: win32 cover |
| 308 | + assert instance.cmd == [str(script), "--magic"] |
| 309 | + else: |
| 310 | + assert instance.cmd == [exe, "-s", str(script), "--magic"] |
| 311 | + |
| 312 | + |
| 313 | +@pytest.mark.parametrize("env", [{}, {"TOX_LIMITED_SHEBANG": ""}]) |
| 314 | +def test_shebang_limited_off(tmp_path: Path, env: Dict[str, str]) -> None: |
| 315 | + _, script, instance = _create_shebang_test(tmp_path, env=env) |
| 316 | + assert instance.cmd == [str(script), "--magic"] |
| 317 | + |
| 318 | + |
| 319 | +def test_shebang_failed_to_parse(tmp_path: Path) -> None: |
| 320 | + _, script, instance = _create_shebang_test(tmp_path, env={"TOX_LIMITED_SHEBANG": "yes"}) |
| 321 | + script.write_text("") |
| 322 | + assert instance.cmd == [str(script), "--magic"] |
| 323 | + |
| 324 | + |
| 325 | +def _create_shebang_test(tmp_path: Path, env: Dict[str, str]) -> Tuple[str, Path, LocalSubProcessExecuteInstance]: |
| 326 | + exe = shutil.which("python") |
| 327 | + assert exe is not None |
| 328 | + script = tmp_path / f"s{'.EXE' if sys.platform == 'win32' else ''}" |
| 329 | + script.write_text(f"#!{exe} -s") |
| 330 | + script.chmod(script.stat().st_mode | stat.S_IEXEC) # mark it executable |
| 331 | + env["PATH"] = str(script.parent) |
| 332 | + request = create_autospec(ExecuteRequest, cmd=["s", "--magic"], env=env, allow=None) |
| 333 | + writer = create_autospec(SyncWrite) |
| 334 | + instance = LocalSubProcessExecuteInstance(request, create_autospec(ExecuteOptions), writer, writer) |
| 335 | + return exe, script, instance |
0 commit comments