| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from typing import TYPE_CHECKING |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
if TYPE_CHECKING: |
| 9 |
from pytest_mock import MockerFixture |
| 10 |
|
| 11 |
from tox.pytest import ToxProjectCreator |
| 12 |
|
| 13 |
|
| 14 |
def _tox_ini(extra: str = "") -> str: |
| 15 |
lines = ["[testenv:check]", "runner = virtualenv-pep-723", "script = check.py"] |
| 16 |
if extra: |
| 17 |
lines.append(extra) |
| 18 |
return "\n".join(lines) + "\n" |
| 19 |
|
| 20 |
|
| 21 |
def _run(project: ToxProjectCreator, files: dict[str, str], *extra_args: str) -> tuple: |
| 22 |
proj = project(files) |
| 23 |
execute_calls = proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 24 |
result = proj.run("r", "-e", "check", "--discover", sys.executable, *extra_args) |
| 25 |
return result, execute_calls |
| 26 |
|
| 27 |
|
| 28 |
def _py_ver() -> str: |
| 29 |
return f"{sys.version_info.major}.{sys.version_info.minor}" |
| 30 |
|
| 31 |
|
| 32 |
def test_deps_installed(tox_project: ToxProjectCreator) -> None: |
| 33 |
script = "# /// script\n# dependencies = [\"setuptools\"]\n# ///\nprint('ok')\n" |
| 34 |
result, execute_calls = _run(tox_project, {"tox.ini": _tox_ini(), "check.py": script}) |
| 35 |
result.assert_success() |
| 36 |
install_cmds = [i[0][3].cmd for i in execute_calls.call_args_list if "install" in i[0][3].run_id] |
| 37 |
assert any("setuptools" in arg for cmd in install_cmds for arg in cmd) |
| 38 |
|
| 39 |
|
| 40 |
def test_no_deps_when_only_requires_python(tox_project: ToxProjectCreator) -> None: |
| 41 |
script = f'# /// script\n# requires-python = ">={_py_ver()}"\n# ///\nprint("ok")\n' |
| 42 |
result, execute_calls = _run(tox_project, {"tox.ini": _tox_ini(), "check.py": script}) |
| 43 |
result.assert_success() |
| 44 |
assert not [i for i in execute_calls.call_args_list if "install" in i[0][3].run_id] |
| 45 |
|
| 46 |
|
| 47 |
def test_no_deps_when_no_metadata(tox_project: ToxProjectCreator) -> None: |
| 48 |
result, execute_calls = _run(tox_project, {"tox.ini": _tox_ini(), "check.py": 'print("ok")\n'}) |
| 49 |
result.assert_success() |
| 50 |
assert not [i for i in execute_calls.call_args_list if "install" in i[0][3].run_id] |
| 51 |
|
| 52 |
|
| 53 |
def test_custom_commands_override(tox_project: ToxProjectCreator) -> None: |
| 54 |
script = "# /// script\n# dependencies = [\"setuptools\"]\n# ///\nprint('ok')\n" |
| 55 |
result, execute_calls = _run( |
| 56 |
tox_project, |
| 57 |
{"tox.ini": _tox_ini("commands = python -c \"print('custom')\""), "check.py": script}, |
| 58 |
) |
| 59 |
result.assert_success() |
| 60 |
cmd_calls = [i[0][3].cmd for i in execute_calls.call_args_list if i[0][3].run_id == "commands[0]"] |
| 61 |
assert any("custom" in str(cmd) for cmd in cmd_calls) |
| 62 |
|
| 63 |
|
| 64 |
def test_default_commands_forward_posargs(tox_project: ToxProjectCreator) -> None: |
| 65 |
result, execute_calls = _run( |
| 66 |
tox_project, {"tox.ini": _tox_ini(), "check.py": 'print("ok")\n'}, "--", "arg1", "arg2" |
| 67 |
) |
| 68 |
result.assert_success() |
| 69 |
cmd = next(i[0][3].cmd for i in execute_calls.call_args_list if i[0][3].run_id == "commands[0]") |
| 70 |
assert "arg1" in cmd |
| 71 |
assert "arg2" in cmd |
| 72 |
|
| 73 |
|
| 74 |
def test_requires_python_satisfied(tox_project: ToxProjectCreator) -> None: |
| 75 |
script = f'# /// script\n# requires-python = ">={_py_ver()}"\n# ///\nprint("ok")\n' |
| 76 |
result, _ = _run(tox_project, {"tox.ini": _tox_ini(), "check.py": script}) |
| 77 |
result.assert_success() |
| 78 |
|
| 79 |
|
| 80 |
def test_requires_python_not_satisfied(tox_project: ToxProjectCreator) -> None: |
| 81 |
script = '# /// script\n# requires-python = ">=99.0"\n# ///\nprint("ok")\n' |
| 82 |
result, _ = _run(tox_project, {"tox.ini": _tox_ini(), "check.py": script}) |
| 83 |
result.assert_failed() |
| 84 |
assert "does not satisfy requires-python" in result.out |
| 85 |
|
| 86 |
|
| 87 |
def test_skip_env_install(tox_project: ToxProjectCreator) -> None: |
| 88 |
script = "# /// script\n# dependencies = [\"setuptools\"]\n# ///\nprint('ok')\n" |
| 89 |
result, execute_calls = _run(tox_project, {"tox.ini": _tox_ini(), "check.py": script}, "--skip-env-install") |
| 90 |
result.assert_success() |
| 91 |
assert not [i for i in execute_calls.call_args_list if "install" in i[0][3].run_id] |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.parametrize( |
| 95 |
("script", "error_fragment"), |
| 96 |
[ |
| 97 |
pytest.param( |
| 98 |
'# /// script\n# requires-python = ">=3.12"\n# ///\n\n# /// script\n# dependencies = []\n# ///\n', |
| 99 |
"multiple", |
| 100 |
id="multiple-script-blocks", |
| 101 |
), |
| 102 |
pytest.param( |
| 103 |
"# /// script\n# requires-python = not valid toml\n# ///\n", |
| 104 |
"Invalid", |
| 105 |
id="malformed-toml", |
| 106 |
), |
| 107 |
], |
| 108 |
) |
| 109 |
def test_invalid_script_metadata(tox_project: ToxProjectCreator, script: str, error_fragment: str) -> None: |
| 110 |
result, _ = _run(tox_project, {"tox.ini": _tox_ini(), "check.py": script}) |
| 111 |
result.assert_failed() |
| 112 |
assert error_fragment in result.out |
| 113 |
|
| 114 |
|
| 115 |
def test_missing_script_file(tox_project: ToxProjectCreator) -> None: |
| 116 |
proj = tox_project({"tox.ini": _tox_ini()}) |
| 117 |
result = proj.run("r", "-e", "check", "--discover", sys.executable) |
| 118 |
result.assert_failed() |
| 119 |
assert "script file not found" in result.out |
| 120 |
|
| 121 |
|
| 122 |
def test_base_python_rejected(tox_project: ToxProjectCreator) -> None: |
| 123 |
script = f'# /// script\n# requires-python = ">={_py_ver()}"\n# ///\n' |
| 124 |
result, _ = _run( |
| 125 |
tox_project, |
| 126 |
{"tox.ini": _tox_ini("base_python = python3"), "check.py": script}, |
| 127 |
) |
| 128 |
result.assert_failed() |
| 129 |
assert "cannot set base_python" in result.out |
| 130 |
|
| 131 |
|
| 132 |
def _tox_ini_with_script(script_value: str) -> str: |
| 133 |
return f"[testenv:check]\nrunner = virtualenv-pep-723\nscript = {script_value}\n" |
| 134 |
|
| 135 |
|
| 136 |
def test_script_path_outside_tox_root_rejected(tox_project: ToxProjectCreator) -> None: |
| 137 |
proj = tox_project({"tox.ini": _tox_ini_with_script("../escape.py")}) |
| 138 |
result = proj.run("r", "-e", "check", "--discover", sys.executable) |
| 139 |
result.assert_failed() |
| 140 |
assert "escapes tox_root" in result.out |
| 141 |
|
| 142 |
|
| 143 |
def test_script_path_traversal_via_dot_dot_rejected(tox_project: ToxProjectCreator) -> None: |
| 144 |
proj = tox_project({"tox.ini": _tox_ini_with_script("subdir/../../etc/passwd")}) |
| 145 |
result = proj.run("r", "-e", "check", "--discover", sys.executable) |
| 146 |
result.assert_failed() |
| 147 |
assert "escapes tox_root" in result.out |
| 148 |
|
| 149 |
|
| 150 |
def test_script_file_too_large_rejected(tox_project: ToxProjectCreator, mocker: MockerFixture) -> None: |
| 151 |
script = "# /// script\n# dependencies = []\n# ///\nprint('ok')\n" |
| 152 |
proj = tox_project({"tox.ini": _tox_ini(), "check.py": script}) |
| 153 |
proj.patch_execute(lambda r: 0 if "install" in r.run_id else None) |
| 154 |
mocker.patch("tox.tox_env.python.pep723._MAX_SCRIPT_BYTES", 1) |
| 155 |
result = proj.run("r", "-e", "check", "--discover", sys.executable) |
| 156 |
result.assert_failed() |
| 157 |
assert "exceeds the 1 byte limit" in result.out |