Skip to content

Commit

Permalink
Report deprecated functions at the main function call.
Browse files Browse the repository at this point in the history
I've added stack level logic so that deprecated functions raise from the initial caller.
This helps developers fix these warnings in their own scripts.

Also cleaned up the deprecated calls in tests which don't test deprecated features,
and made tests which do have deprecations more strict.
  • Loading branch information
HexDecimal authored and kvas-it committed May 23, 2023
1 parent c7edb10 commit a50da30
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
32 changes: 25 additions & 7 deletions pytest_console_scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def _handle_command_args(
command: _Command,
*args: _StrOrPath,
shell: bool = False,
stacklevel: int = 1,
) -> Sequence[_StrOrPath]:
"""Return command arguments in a consistent list format.
Expand All @@ -146,7 +147,9 @@ def _handle_command_args(
if args or not isinstance(command, (str, os.PathLike)):
command = subprocess.list2cmdline(
str(arg)
for arg in _handle_command_args(command, *args, shell=False)
for arg in _handle_command_args(
command, *args, shell=False, stacklevel=stacklevel + 1
)
)
command = shlex.split(str(command), posix=os.name == 'posix')
args = ()
Expand All @@ -158,6 +161,7 @@ def _handle_command_args(
'\nReplace `script_runner.run(a, b, c)` calls with'
' `script_runner.run([a, b, c])`',
DeprecationWarning,
stacklevel=stacklevel + 1,
)
if not isinstance(command, (str, os.PathLike)):
return [*command, *args]
Expand Down Expand Up @@ -263,6 +267,7 @@ def run(
env=env,
stdin=stdin,
check=check,
_stacklevel=2,
**options,
)

Expand Down Expand Up @@ -326,14 +331,18 @@ def run_inprocess(
print_result: bool = True,
stdin: io.IOBase | None = None,
check: bool = False,
_stacklevel: int = 1,
**options: Any,
) -> RunResult:
for key in options:
warnings.warn(
f'Keyword argument {key!r} was ignored.'
'\nConsider using subprocess mode or raising an issue.'
'\nConsider using subprocess mode or raising an issue.',
stacklevel=_stacklevel + 1,
)
cmd_args = _handle_command_args(command, *arguments, shell=shell)
cmd_args = _handle_command_args(
command, *arguments, shell=shell, stacklevel=_stacklevel + 1
)
script = cls._load_script(cmd_args[0], cwd=cwd, env=env)
cmd_args = [str(cmd) for cmd in cmd_args]
stdin_stream = stdin if stdin is not None else StreamMock()
Expand Down Expand Up @@ -396,20 +405,29 @@ def run_subprocess(
stdin: io.IOBase | None = None,
check: bool = False,
universal_newlines: bool = True,
_stacklevel: int = 1,
**options: Any,
) -> RunResult:
stdin_input: str | bytes | None = None
if stdin is not None:
stdin_input = stdin.read()

script_path = cls._locate_script(_handle_command_args(
command, *arguments, shell=shell)[0], cwd=cwd, env=env
script_path = cls._locate_script(
_handle_command_args(
command, *arguments, shell=shell, stacklevel=_stacklevel + 1
)[0],
cwd=cwd,
env=env,
)
if arguments:
command = _handle_command_args(command, *arguments, shell=shell)
command = _handle_command_args(
command, *arguments, shell=shell, stacklevel=_stacklevel + 1
)

if _is_nonexecutable_python_file(script_path):
command = _handle_command_args(command, shell=shell)
command = _handle_command_args(
command, shell=shell, stacklevel=_stacklevel + 1
)
command = [sys.executable or 'python', *command]

cp = subprocess.run(
Expand Down
32 changes: 18 additions & 14 deletions tests/test_run_scripts.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""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
from typing import Any, ContextManager
from unittest import mock

import pytest
Expand Down Expand Up @@ -92,9 +93,7 @@ def test_script(script_runner):
# inner and outer script runners.

result = script_runner.run(
'pytest',
str(test),
'--script-launch-mode=' + launch_mode,
['pytest', test, f'--script-launch-mode={launch_mode}']
)
assert result.success

Expand Down Expand Up @@ -248,9 +247,7 @@ def test_fail(script_runner):
"""
)
result = script_runner.run(
'pytest',
str(test),
'--script-launch-mode=' + launch_mode,
['pytest', test, f'--script-launch-mode={launch_mode}']
)
assert result.success != fail
if fail:
Expand Down Expand Up @@ -306,7 +303,7 @@ def test_script(script_runner):
script_runner.run(R'''{console_script}''', print_result=False)
"""
)
result = script_runner.run('pytest', '-s', str(test))
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
Expand All @@ -327,7 +324,7 @@ def test_script(script_runner):
script_runner.run(R'''{console_script}''')
"""
)
result = script_runner.run('pytest', '-s', '--hide-run-results', str(test))
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
Expand Down Expand Up @@ -411,9 +408,11 @@ def test_deprecated_args(
print(sys.argv[1:])
"""
)
result = script_runner.run(console_script, 'A', 'B', check=True)
with pytest.warns(match=r".*multiple arguments."):
result = script_runner.run(console_script, 'A', 'B', check=True)
assert result.stdout == "['A', 'B']\n"
result = script_runner.run([console_script, 'C'], 'D', check=True)
with pytest.warns(match=r".*multiple arguments."):
result = script_runner.run([console_script, 'C'], 'D', check=True)
assert result.stdout == "['C', 'D']\n"


Expand All @@ -430,9 +429,14 @@ def test_check(
def test_ignore_universal_newlines(
console_script: Path, script_runner: ScriptRunner
) -> None:
result = script_runner.run(
str(console_script), check=True, universal_newlines=True
)
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 == ''

Expand Down

0 comments on commit a50da30

Please sign in to comment.