1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
|
"""Test running of scripts with various modes and options."""
from __future__ import annotations
import contextlib
import importlib
import io
import os
import sys
from pathlib import Path
from subprocess import CalledProcessError
from types import ModuleType
from typing import Any, ContextManager
from unittest import mock
import pytest
from pytest_console_scripts import ScriptRunner
@pytest.fixture(params=['inprocess', 'subprocess'])
def launch_mode(request: pytest.FixtureRequest) -> str:
"""Launch mode: inprocess|subprocess."""
return str(request.param)
@pytest.fixture()
def console_script(tmp_path: Path) -> Path:
"""Python script to use in tests."""
script = tmp_path / 'script.py'
script.write_text('#!/usr/bin/env python3\nprint("foo")')
return script
@pytest.mark.script_launch_mode('both')
def test_not_installed(
console_script: Path, script_runner: ScriptRunner
) -> None:
result = script_runner.run(str(console_script))
assert result.success
assert result.stdout == 'foo\n'
assert result.stderr == ''
@pytest.mark.xfail(
sys.platform == "win32",
reason="Windows does not treat Python scripts as executables."
)
@pytest.mark.script_launch_mode('both')
def test_elsewhere_in_the_path(
console_script: Path, script_runner: ScriptRunner
) -> None:
console_script.chmod(0o777)
env = os.environ.copy()
env["PATH"] = f"{console_script.parent}{os.pathsep}{env['PATH']}"
result = script_runner.run(console_script.name, env=env)
assert result.success
assert result.stdout == 'foo\n'
assert result.stderr == ''
@pytest.mark.script_launch_mode('both')
def test_run_pytest(
tmp_path: Path,
console_script: Path,
script_runner: ScriptRunner,
launch_mode: str
) -> None:
console_script.write_text('import os;print(os.getpid())')
test = tmp_path / f'test_{launch_mode}.py'
compare = '==' if launch_mode == 'inprocess' else '!='
test.write_text(
f"""
import os
def test_script(script_runner):
result = script_runner.run(R'''{console_script}''')
assert result.success
assert result.stdout {compare} str(os.getpid()) + '\\n'
assert result.stderr == ''
"""
)
# Here we're testing two things:
#
# - pytest is a Python script that's installed in the test environment, so
# we'll use `script_runner` fixture to run it -- this tests execution of
# installed scripts from the path.
# - The pytest that we run will run a test that uses `script_runner`
# fixture to run another script. We're going to pass --script-launch-mode
# option to pytest and will check that the execution of the inner script
# is performed in accordance with its value.
#
# We're also testing all 4 combinations of inprocess/subprocess modes for
# inner and outer script runners.
result = script_runner.run(
['pytest', test, f'--script-launch-mode={launch_mode}']
)
assert result.success
@pytest.mark.script_launch_mode('inprocess')
def test_return_None(
console_script: Path, script_runner: ScriptRunner
) -> None:
"""Check that entry point function returning None is counted as success."""
# Many console_scripts entry point functions return 0 on success but not
# all of them do. Returning `None` is also allowed and would be translated
# to return code 0 when run normally via wrapper. This test checks that we
# handle this case properly in inprocess mode.
console_script.write_text(
"""
import sys
print("Foo")
sys.exit(None)
"""
)
result = script_runner.run(str(console_script))
assert result.success
assert 'Foo' in result.stdout
@pytest.mark.script_launch_mode('inprocess')
def test_return_code_uncommon(
console_script: Path, script_runner: ScriptRunner
) -> None:
"""Check uncommon return codes."""
console_script.write_text(
"""
import sys
sys.exit(2)
"""
)
assert script_runner.run(str(console_script)).returncode == 2
@pytest.mark.script_launch_mode('both')
def test_abnormal_exit(
console_script: Path, script_runner: ScriptRunner
) -> None:
console_script.write_text('import sys;sys.exit("boom")')
result = script_runner.run(str(console_script))
assert not result.success
assert result.stdout == ''
assert result.stderr == 'boom\n'
@pytest.mark.script_launch_mode('both')
def test_exception(console_script: Path, script_runner: ScriptRunner) -> None:
console_script.write_text('raise TypeError("boom")')
result = script_runner.run(str(console_script))
assert not result.success
assert result.stdout == ''
assert 'TypeError: boom' in result.stderr
def test_cwd(
console_script: Path,
script_runner: ScriptRunner,
tmp_path: Path,
) -> None:
"""Script starts in dir given by cwd arg and cwd changes are contained."""
dir1 = tmp_path / 'dir1'
dir1.mkdir()
dir2 = tmp_path / 'dir2'
dir2.mkdir()
console_script.write_text(
f"""
import os
print(os.getcwd())
os.chdir(R'''{dir2}''')
print(os.getcwd())
"""
)
mydir = os.getcwd()
result = script_runner.run(str(console_script), cwd=str(dir1))
assert result.success
assert result.stdout == f'{dir1}\n{dir2}\n'
assert os.getcwd() == mydir
@pytest.mark.script_launch_mode('both')
def test_env(console_script: Path, script_runner: ScriptRunner) -> None:
"""Script receives environment and env changes don't escape to test."""
console_script.write_text(
"""
import os
print(os.environ['FOO'])
os.environ['FOO'] = 'baz'
"""
)
env = os.environ.copy()
env['FOO'] = 'bar'
result = script_runner.run(str(console_script), env=env)
assert result.success
assert result.stdout == 'bar\n'
assert 'FOO' not in os.environ
@pytest.mark.script_launch_mode('both')
def test_stdin(console_script: Path, script_runner: ScriptRunner) -> None:
console_script.write_text(
"""
import sys
for line in sys.stdin:
sys.stdout.write('simon says ' + line)
sys.stderr.write('error says ' + line)
"""
)
stdin = io.StringIO('foo\nbar')
result = script_runner.run(str(console_script), stdin=stdin)
assert result.success
assert result.stdout == 'simon says foo\nsimon says bar'
assert result.stderr == 'error says foo\nerror says bar'
def test_logging(console_script: Path, script_runner: ScriptRunner) -> None:
"""Test that the script can perform logging initialization."""
console_script.write_text(
"""
import logging, sys
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
logging.debug('hidden')
logging.info('shown')
"""
)
result = script_runner.run(str(console_script))
assert result.success
assert result.stderr == 'INFO:root:shown\n'
@pytest.mark.parametrize('fail', [True, False])
@pytest.mark.parametrize('check', [True, False])
def test_print_stdio_on_error(
console_script: Path,
script_runner: ScriptRunner,
tmp_path: Path,
fail: bool,
check: bool,
launch_mode: str,
) -> None:
"""Output of the script is printed when the test fails."""
console_script.write_text('print("12345")\nraise Exception("54321")')
test = tmp_path / f'test_{fail}_{check}_{launch_mode}.py'
command = [str(console_script), 'arg']
test.write_text(
f"""
import subprocess
def test_fail(script_runner):
try:
ret = script_runner.run({command}, check={check})
except subprocess.CalledProcessError as exc:
assert (exc.returncode == 0) is {fail}
else:
assert ret.success is {fail}
"""
)
result = script_runner.run(
['pytest', test, f'--script-launch-mode={launch_mode}']
)
assert result.success != fail
if fail:
assert (f'# Running console script: {command}\n'
in result.stdout)
assert '# Script return code: 1\n' in result.stdout
assert '# Script stdout:\n12345\n' in result.stdout
assert '# Script stderr:\nTraceback' in result.stdout
assert 'Exception: 54321' in result.stdout
else:
assert '# Running console script' not in result.stdout
assert '12345' not in result.stdout
assert '54321' not in result.stdout
@pytest.mark.script_launch_mode('inprocess')
def test_mocking(
console_script: Path,
script_runner: ScriptRunner,
monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test mocking in of console scripts (in-process mode only).
Note: we can't mock objects in the script itself because it will not be
imported via normal import system but we can mock anything in the modules
that the script imports.
"""
console_script.write_text(
"""
import os
print(os.path.basename('foo'))
"""
)
monkeypatch.setattr(os.path, 'basename', lambda foo: 'bar')
result = script_runner.run(str(console_script))
assert result.success
assert result.stdout == 'bar\n'
def test_hide_run_result_arg(
tmp_path: Path, console_script: Path, script_runner: ScriptRunner
) -> None:
"""Disable printing of the RunResult to stdout with print_result=False."""
console_script.write_text('print("the answer is 42")')
test = tmp_path / 'test_hrra.py'
test.write_text(
f"""
import pytest
@pytest.mark.script_launch_mode('both')
def test_script(script_runner):
script_runner.run(R'''{console_script}''', print_result=False)
"""
)
result = script_runner.run(['pytest', '-s', test])
assert result.success
assert 'the answer is 42' not in result.stdout
assert 'Running console script' not in result.stdout
def test_hide_run_result_opt(
tmp_path: Path, console_script: Path, script_runner: ScriptRunner
) -> None:
"""Disable printing of the RunResult to stdout with print_result=False."""
console_script.write_text('print("the answer is 42")')
test = tmp_path / 'test_hrro.py'
test.write_text(
f"""
import pytest
@pytest.mark.script_launch_mode('both')
def test_script(script_runner):
script_runner.run(R'''{console_script}''')
"""
)
result = script_runner.run(['pytest', '-s', '--hide-run-results', test])
assert result.success
assert 'the answer is 42' not in result.stdout
assert 'Running console script' not in result.stdout
class MockEntryPoint:
module: ModuleType
def __init__(self, exec_path: str | Path):
self.exec_path = exec_path
def load(self) -> Any:
base, module = os.path.split(self.exec_path)
module_name, _ = os.path.splitext(module)
sys.path.append(base)
self.module = importlib.import_module(module_name)
sys.path.pop(-1)
return self.module.run
@pytest.mark.script_launch_mode('inprocess')
def test_global_logging(
tmp_path: Path, console_script: Path, script_runner: ScriptRunner
) -> None:
"""Load global values when executing from importlib.metadata"""
test = tmp_path / 'test_entry_point.py'
test.write_text(
"""
import logging
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)
def run() -> None:
LOGGER.debug('DEBUG')
LOGGER.info('INFO')
LOGGER.warning('WARNING')
"""
)
if sys.version_info < (3, 10):
patched_func = 'importlib_metadata.entry_points'
else:
patched_func = 'importlib.metadata.entry_points'
with mock.patch(
patched_func,
mock.MagicMock(return_value=[MockEntryPoint(str(test))]),
):
result = script_runner.run(str(console_script))
assert result.success
assert 'INFO:test_entry_point:INFO\n' in result.stderr
assert 'DEBUG\n' not in result.stderr
@pytest.mark.script_launch_mode('both')
def test_shell(
console_script: Path, script_runner: ScriptRunner
) -> None:
console_script.chmod(0o777)
result = script_runner.run(
f"{console_script} --test", shell=True, check=True
)
assert result.stdout == 'foo\n'
assert result.stderr == ''
result = script_runner.run(
[str(console_script), "--test"], shell=True, check=True
)
assert result.stdout == 'foo\n'
assert result.stderr == ''
@pytest.mark.script_launch_mode('both')
def test_deprecated_args(
console_script: Path, script_runner: ScriptRunner
) -> None:
console_script.write_text(
"""
import sys
print(sys.argv[1:])
"""
)
with pytest.warns(match=r".*multiple arguments."):
result = script_runner.run(console_script, 'A', 'B', check=True)
assert result.stdout == "['A', 'B']\n"
with pytest.warns(match=r".*multiple arguments."):
result = script_runner.run([console_script, 'C'], 'D', check=True)
assert result.stdout == "['C', 'D']\n"
@pytest.mark.script_launch_mode('both')
def test_check(
console_script: Path, script_runner: ScriptRunner
) -> None:
console_script.write_text("""import sys; sys.exit(1)""")
with pytest.raises(CalledProcessError, match='.*non-zero exit status 1'):
script_runner.run(str(console_script), check=True)
@pytest.mark.script_launch_mode('both')
def test_ignore_universal_newlines(
console_script: Path, script_runner: ScriptRunner
) -> None:
expectation: dict[str, ContextManager[Any]] = {
'inprocess': pytest.warns(match=r"Keyword argument .* was ignored"),
'subprocess': contextlib.nullcontext(),
}
with expectation[script_runner.launch_mode]:
result = script_runner.run(
str(console_script), check=True, universal_newlines=True
)
assert result.stdout == 'foo\n'
assert result.stderr == ''
@pytest.mark.script_launch_mode('subprocess')
def test_disable_universal_newlines(
console_script: Path, script_runner: ScriptRunner
) -> None:
result = script_runner.run(
str(console_script), check=True, universal_newlines=False
)
assert isinstance(result.stdout, bytes)
assert isinstance(result.stderr, bytes)
assert result.stdout.strip() == b'foo'
assert result.stderr == b''
@pytest.mark.script_launch_mode('both')
def test_run_path(
console_script: Path, script_runner: ScriptRunner
) -> None:
result = script_runner.run(console_script, check=True)
assert result.stdout == 'foo\n'
assert result.stderr == ''
console_script.chmod(0o777)
result = script_runner.run(console_script, check=True)
assert result.stdout == 'foo\n'
assert result.stderr == ''
@pytest.mark.script_launch_mode('both')
def test_run_script_codecs(
console_script: Path, script_runner: ScriptRunner
) -> None:
"""Check that non-UTF-8 scripts can load"""
console_script.write_text(
"""\
# -*- coding: cp437 -*-
import sys # Non UTF-8 characters -> ≡≡≡
print('foo')
""",
encoding="cp437",
)
result = script_runner.run(console_script, check=True)
assert result.stdout == 'foo\n'
assert result.stderr == ''
|