Erste Version
This commit is contained in:
commit
782b1850a3
1641 files changed
+512713
No files matched your search
Generated
+10
@@ -0,0 +1,10 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Ignored default folder with query files
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
Generated
+14
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="~/PycharmProjects/Funktional/.venv" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
<component name="PackageRequirementsSettings" />
|
||||
<component name="PyDocumentationSettings" />
|
||||
<component name="ReSTService" />
|
||||
<component name="TestRunnerService" />
|
||||
</module>
|
||||
Generated
+10
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.system.id="pyproject.toml" type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$/aufgaben/einstellung">
|
||||
<excludeFolder url="file://$MODULE_DIR$/aufgaben/einstellung/.venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="~/PycharmProjects/Funktional/aufgaben/einstellung/.venv" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
||||
Generated
+4
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="~/PycharmProjects/Funktional/.venv" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
Generated
+9
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/Funktional.iml" filepath="$PROJECT_DIR$/.idea/Funktional.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/Funktional@1.iml" filepath="$PROJECT_DIR$/.idea/Funktional@1.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/aufgaben/hausaufgaben" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,6 @@
|
||||
# created by virtualenv automatically
|
||||
.venv/
|
||||
__pycache__/
|
||||
.pytest_cache/
|
||||
.idea/
|
||||
input.txt
|
||||
@@ -0,0 +1,4 @@
|
||||
Signature: 8a477f597d28d172789f06886806bc55
|
||||
# This file is a cache directory tag created by Python virtualenv.
|
||||
# For information about cache directory tags, see:
|
||||
# https://bford.info/cachedir/
|
||||
@@ -0,0 +1,132 @@
|
||||
# This file must be used with "source bin/activate" *from bash*
|
||||
# you cannot run it directly
|
||||
|
||||
|
||||
if [ "${BASH_SOURCE-}" = "$0" ]; then
|
||||
echo "You must source this script: \$ source $0" >&2
|
||||
exit 33
|
||||
fi
|
||||
|
||||
deactivate () {
|
||||
unset -f pydoc >/dev/null 2>&1 || true
|
||||
|
||||
# reset old environment variables
|
||||
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
|
||||
PATH="$_OLD_VIRTUAL_PATH"
|
||||
export PATH
|
||||
unset _OLD_VIRTUAL_PATH
|
||||
fi
|
||||
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
|
||||
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
|
||||
export PYTHONHOME
|
||||
unset _OLD_VIRTUAL_PYTHONHOME
|
||||
fi
|
||||
|
||||
if [ -n "${_OLD_VIRTUAL_TCL_LIBRARY:-}" ]; then
|
||||
TCL_LIBRARY="$_OLD_VIRTUAL_TCL_LIBRARY"
|
||||
export TCL_LIBRARY
|
||||
unset _OLD_VIRTUAL_TCL_LIBRARY
|
||||
fi
|
||||
if [ -n "${_OLD_VIRTUAL_TK_LIBRARY:-}" ]; then
|
||||
TK_LIBRARY="$_OLD_VIRTUAL_TK_LIBRARY"
|
||||
export TK_LIBRARY
|
||||
unset _OLD_VIRTUAL_TK_LIBRARY
|
||||
fi
|
||||
if [ -n "${_OLD_PKG_CONFIG_PATH:-}" ]; then
|
||||
PKG_CONFIG_PATH="$_OLD_PKG_CONFIG_PATH"
|
||||
export PKG_CONFIG_PATH
|
||||
unset _OLD_PKG_CONFIG_PATH
|
||||
fi
|
||||
|
||||
# The hash command must be called to get it to forget past
|
||||
# commands. Without forgetting past commands the $PATH changes
|
||||
# we made may not be respected
|
||||
hash -r 2>/dev/null
|
||||
|
||||
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
|
||||
PS1="$_OLD_VIRTUAL_PS1"
|
||||
export PS1
|
||||
unset _OLD_VIRTUAL_PS1
|
||||
fi
|
||||
|
||||
unset VIRTUAL_ENV
|
||||
unset VIRTUAL_ENV_PROMPT
|
||||
if [ ! "${1-}" = "nondestructive" ] ; then
|
||||
# Self destruct!
|
||||
unset -f deactivate
|
||||
fi
|
||||
}
|
||||
|
||||
# unset irrelevant variables
|
||||
deactivate nondestructive
|
||||
|
||||
if [ ! -d /home/zlata/PycharmProjects/Funktional/.venv ]; then
|
||||
echo "Virtual environment directory /home/zlata/PycharmProjects/Funktional/.venv does not exist!" >&2
|
||||
CURRENT_PATH=$(realpath "${0}")
|
||||
CURRENT_DIR=$(dirname "${CURRENT_PATH}")
|
||||
VIRTUAL_ENV="$(realpath "${CURRENT_DIR}/../")"
|
||||
else
|
||||
VIRTUAL_ENV=/home/zlata/PycharmProjects/Funktional/.venv
|
||||
fi
|
||||
|
||||
case "$(uname)" in
|
||||
CYGWIN*|MSYS*|MINGW*)
|
||||
VIRTUAL_ENV=$(cygpath "$VIRTUAL_ENV")
|
||||
;;
|
||||
esac
|
||||
export VIRTUAL_ENV
|
||||
|
||||
_OLD_VIRTUAL_PATH="$PATH"
|
||||
PATH="$VIRTUAL_ENV/"bin":$PATH"
|
||||
export PATH
|
||||
|
||||
_OLD_PKG_CONFIG_PATH="${PKG_CONFIG_PATH:-}"
|
||||
PKG_CONFIG_PATH="${VIRTUAL_ENV}/lib/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}"
|
||||
export PKG_CONFIG_PATH
|
||||
|
||||
if [ "x"'' != x ] ; then
|
||||
VIRTUAL_ENV_PROMPT=''
|
||||
else
|
||||
VIRTUAL_ENV_PROMPT=$(basename "$VIRTUAL_ENV")
|
||||
fi
|
||||
export VIRTUAL_ENV_PROMPT
|
||||
|
||||
# unset PYTHONHOME if set
|
||||
if [ -n "${PYTHONHOME:-}" ] ; then
|
||||
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
|
||||
unset PYTHONHOME
|
||||
fi
|
||||
|
||||
if [ '' != "" ]; then
|
||||
if [ -n "${TCL_LIBRARY:-}" ] ; then
|
||||
_OLD_VIRTUAL_TCL_LIBRARY="$TCL_LIBRARY"
|
||||
fi
|
||||
TCL_LIBRARY=''
|
||||
export TCL_LIBRARY
|
||||
fi
|
||||
|
||||
if [ '' != "" ]; then
|
||||
if [ -n "${TK_LIBRARY:-}" ] ; then
|
||||
_OLD_VIRTUAL_TK_LIBRARY="$TK_LIBRARY"
|
||||
fi
|
||||
TK_LIBRARY=''
|
||||
export TK_LIBRARY
|
||||
fi
|
||||
|
||||
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
|
||||
_OLD_VIRTUAL_PS1="${PS1-}"
|
||||
PS1="(${VIRTUAL_ENV_PROMPT}) ${PS1-}"
|
||||
export PS1
|
||||
fi
|
||||
|
||||
# Make sure to unalias pydoc if it's already there
|
||||
alias pydoc 2>/dev/null >/dev/null && unalias pydoc || true
|
||||
|
||||
pydoc () {
|
||||
python -m pydoc "$@"
|
||||
}
|
||||
|
||||
# The hash command must be called to get it to forget past
|
||||
# commands. Without forgetting past commands the $PATH changes
|
||||
# we made may not be respected
|
||||
hash -r 2>/dev/null || true
|
||||
@@ -0,0 +1,74 @@
|
||||
# This file must be used with "source bin/activate.csh" *from csh*.
|
||||
# You cannot run it directly.
|
||||
# Created by Davide Di Blasi <davidedb@gmail.com>.
|
||||
|
||||
set newline='\
|
||||
'
|
||||
|
||||
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH:q" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_TCL_LIBRARY != 0 && setenv TCL_LIBRARY "$_OLD_VIRTUAL_TCL_LIBRARY:q" && unset _OLD_VIRTUAL_TCL_LIBRARY || unsetenv TCL_LIBRARY; test $?_OLD_VIRTUAL_TK_LIBRARY != 0 && setenv TK_LIBRARY "$_OLD_VIRTUAL_TK_LIBRARY:q" && unset _OLD_VIRTUAL_TK_LIBRARY || unsetenv TK_LIBRARY; test $?_OLD_PKG_CONFIG_PATH != 0 && setenv PKG_CONFIG_PATH "$_OLD_PKG_CONFIG_PATH:q" && unset _OLD_PKG_CONFIG_PATH || unsetenv PKG_CONFIG_PATH; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT:q" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate && unalias pydoc'
|
||||
|
||||
# Unset irrelevant variables.
|
||||
deactivate nondestructive
|
||||
|
||||
setenv VIRTUAL_ENV /home/zlata/PycharmProjects/Funktional/.venv
|
||||
|
||||
set _OLD_VIRTUAL_PATH="$PATH:q"
|
||||
setenv PATH "$VIRTUAL_ENV:q/"bin":$PATH:q"
|
||||
|
||||
if ($?PKG_CONFIG_PATH) then
|
||||
set _OLD_PKG_CONFIG_PATH="$PKG_CONFIG_PATH"
|
||||
setenv PKG_CONFIG_PATH "${VIRTUAL_ENV}/lib/pkgconfig:${PKG_CONFIG_PATH}"
|
||||
else
|
||||
setenv PKG_CONFIG_PATH "${VIRTUAL_ENV}/lib/pkgconfig"
|
||||
endif
|
||||
|
||||
if ('' != "") then
|
||||
if ($?TCL_LIBRARY) then
|
||||
set _OLD_VIRTUAL_TCL_LIBRARY="$TCL_LIBRARY"
|
||||
endif
|
||||
setenv TCL_LIBRARY ''
|
||||
endif
|
||||
|
||||
if ('' != "") then
|
||||
if ($?TK_LIBRARY) then
|
||||
set _OLD_VIRTUAL_TK_LIBRARY="$TK_LIBRARY"
|
||||
endif
|
||||
setenv TK_LIBRARY ''
|
||||
endif
|
||||
|
||||
if ('' != "") then
|
||||
setenv VIRTUAL_ENV_PROMPT ''
|
||||
else
|
||||
setenv VIRTUAL_ENV_PROMPT "$VIRTUAL_ENV:t:q"
|
||||
endif
|
||||
|
||||
if ( $?VIRTUAL_ENV_DISABLE_PROMPT ) then
|
||||
if ( $VIRTUAL_ENV_DISABLE_PROMPT == "" ) then
|
||||
set do_prompt = "1"
|
||||
else
|
||||
set do_prompt = "0"
|
||||
endif
|
||||
else
|
||||
set do_prompt = "1"
|
||||
endif
|
||||
|
||||
if ( $do_prompt == "1" ) then
|
||||
# Could be in a non-interactive environment,
|
||||
# in which case, $prompt is undefined and we wouldn't
|
||||
# care about the prompt anyway.
|
||||
if ( $?prompt ) then
|
||||
set _OLD_VIRTUAL_PROMPT="$prompt:q"
|
||||
if ( "$prompt:q" =~ *"$newline:q"* ) then
|
||||
:
|
||||
else
|
||||
set prompt = '('"$VIRTUAL_ENV_PROMPT:q"') '"$prompt:q"
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
unset env_name
|
||||
unset do_prompt
|
||||
|
||||
alias pydoc python -m pydoc
|
||||
|
||||
rehash
|
||||
@@ -0,0 +1,118 @@
|
||||
# This file must be used using `source bin/activate.fish` *within a running fish ( http://fishshell.com ) session*.
|
||||
# Do not run it directly.
|
||||
|
||||
function deactivate -d 'Exit virtualenv mode and return to the normal environment.'
|
||||
# reset old environment variables
|
||||
if test -n "$_OLD_VIRTUAL_PATH"
|
||||
set -gx PATH $_OLD_VIRTUAL_PATH
|
||||
set -e _OLD_VIRTUAL_PATH
|
||||
end
|
||||
|
||||
if test -n ''
|
||||
if test -n "$_OLD_VIRTUAL_TCL_LIBRARY";
|
||||
set -gx TCL_LIBRARY "$_OLD_VIRTUAL_TCL_LIBRARY";
|
||||
set -e _OLD_VIRTUAL_TCL_LIBRARY;
|
||||
else;
|
||||
set -e TCL_LIBRARY;
|
||||
end
|
||||
end
|
||||
if test -n ''
|
||||
if test -n "$_OLD_VIRTUAL_TK_LIBRARY";
|
||||
set -gx TK_LIBRARY "$_OLD_VIRTUAL_TK_LIBRARY";
|
||||
set -e _OLD_VIRTUAL_TK_LIBRARY;
|
||||
else;
|
||||
set -e TK_LIBRARY;
|
||||
end
|
||||
end
|
||||
|
||||
if test -n "$_OLD_PKG_CONFIG_PATH"
|
||||
set -gx PKG_CONFIG_PATH "$_OLD_PKG_CONFIG_PATH"
|
||||
set -e _OLD_PKG_CONFIG_PATH
|
||||
end
|
||||
|
||||
if test -n "$_OLD_VIRTUAL_PYTHONHOME"
|
||||
set -gx PYTHONHOME "$_OLD_VIRTUAL_PYTHONHOME"
|
||||
set -e _OLD_VIRTUAL_PYTHONHOME
|
||||
end
|
||||
|
||||
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
|
||||
and functions -q _old_fish_prompt
|
||||
# Set an empty local `$fish_function_path` to allow the removal of `fish_prompt` using `functions -e`.
|
||||
set -l fish_function_path
|
||||
|
||||
# Erase virtualenv's `fish_prompt` and restore the original.
|
||||
functions -e fish_prompt
|
||||
functions -c _old_fish_prompt fish_prompt
|
||||
functions -e _old_fish_prompt
|
||||
set -e _OLD_FISH_PROMPT_OVERRIDE
|
||||
end
|
||||
|
||||
set -e VIRTUAL_ENV
|
||||
set -e VIRTUAL_ENV_PROMPT
|
||||
|
||||
if test "$argv[1]" != 'nondestructive'
|
||||
# Self-destruct!
|
||||
functions -e pydoc
|
||||
functions -e deactivate
|
||||
end
|
||||
end
|
||||
|
||||
# Unset irrelevant variables.
|
||||
deactivate nondestructive
|
||||
|
||||
set -gx VIRTUAL_ENV /home/zlata/PycharmProjects/Funktional/.venv
|
||||
|
||||
set -gx _OLD_PKG_CONFIG_PATH "$PKG_CONFIG_PATH"
|
||||
set -gx PKG_CONFIG_PATH "$VIRTUAL_ENV/lib/pkgconfig:$PKG_CONFIG_PATH"
|
||||
|
||||
set -gx _OLD_VIRTUAL_PATH $PATH
|
||||
set -gx PATH "$VIRTUAL_ENV"'/'bin $PATH
|
||||
|
||||
if test -n ''
|
||||
if set -q TCL_LIBRARY;
|
||||
set -gx _OLD_VIRTUAL_TCL_LIBRARY $TCL_LIBRARY;
|
||||
end
|
||||
set -gx TCL_LIBRARY ''''
|
||||
end
|
||||
if test -n ''
|
||||
if set -q TK_LIBRARY;
|
||||
set -gx _OLD_VIRTUAL_TK_LIBRARY $TK_LIBRARY;
|
||||
end
|
||||
set -gx TK_LIBRARY ''''
|
||||
end
|
||||
|
||||
# Prompt override provided?
|
||||
# If not, just use the environment name.
|
||||
if test -n ''
|
||||
set -gx VIRTUAL_ENV_PROMPT ''
|
||||
else
|
||||
set -gx VIRTUAL_ENV_PROMPT (basename "$VIRTUAL_ENV")
|
||||
end
|
||||
|
||||
# Unset `$PYTHONHOME` if set.
|
||||
if set -q PYTHONHOME
|
||||
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
|
||||
set -e PYTHONHOME
|
||||
end
|
||||
|
||||
function pydoc
|
||||
python -m pydoc $argv
|
||||
end
|
||||
|
||||
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
|
||||
# Copy the current `fish_prompt` function as `_old_fish_prompt`.
|
||||
functions -c fish_prompt _old_fish_prompt
|
||||
|
||||
function fish_prompt
|
||||
set -l old_status $status
|
||||
# Run the user's prompt first; it might depend on (pipe)status.
|
||||
set -l prompt (_old_fish_prompt)
|
||||
|
||||
printf '(%s) ' $VIRTUAL_ENV_PROMPT
|
||||
|
||||
string join -- \n $prompt # handle multi-line prompts
|
||||
echo "exit $old_status" | .
|
||||
end
|
||||
|
||||
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
|
||||
end
|
||||
@@ -0,0 +1,89 @@
|
||||
# virtualenv activation module:
|
||||
# - Activate with `overlay use activate.nu`
|
||||
# - Deactivate with `deactivate`, as usual
|
||||
#
|
||||
# To customize the overlay name, you can call `overlay use activate.nu as foo`, but then simply `deactivate` won't work
|
||||
# because it is just an alias to hide the "activate" overlay. You'd need to call `overlay hide foo` manually.
|
||||
|
||||
module warning {
|
||||
export-env {
|
||||
const file = path self
|
||||
error make -u {
|
||||
msg: $"`($file | path basename)` is meant to be used with `overlay use`, not `source`"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
use warning
|
||||
|
||||
export-env {
|
||||
|
||||
let nu_ver = (version | get version | split row '.' | take 2 | each { into int })
|
||||
if $nu_ver.0 == 0 and $nu_ver.1 < 106 {
|
||||
error make {
|
||||
msg: 'virtualenv Nushell activation requires Nushell 0.106 or greater.'
|
||||
}
|
||||
}
|
||||
|
||||
def is-string [x] {
|
||||
($x | describe) == 'string'
|
||||
}
|
||||
|
||||
def has-env [...names] {
|
||||
$names | each {|n| $n in $env } | all {|i| $i }
|
||||
}
|
||||
|
||||
def is-env-true [name: string] {
|
||||
if (has-env $name) {
|
||||
let val = ($env | get --optional $name)
|
||||
if ($val | describe) == 'bool' {
|
||||
$val
|
||||
} else {
|
||||
not ($val | is-empty)
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
let virtual_env = r#'/home/zlata/PycharmProjects/Funktional/.venv'#
|
||||
let bin = r#'bin'#
|
||||
let path_name = if (has-env 'Path') { 'Path' } else { 'PATH' }
|
||||
let venv_path = ([$virtual_env $bin] | path join)
|
||||
let new_path = ($env | get $path_name | prepend $venv_path)
|
||||
let virtual_env_prompt = if (r#''# | is-empty) {
|
||||
($virtual_env | path basename)
|
||||
} else {
|
||||
r#''#
|
||||
}
|
||||
let old_pkg_config_path = if (has-env 'PKG_CONFIG_PATH') { $env.PKG_CONFIG_PATH } else { '' }
|
||||
let new_pkg_config_path = $'($virtual_env)/lib/pkgconfig:($old_pkg_config_path)'
|
||||
let new_env = { $path_name: $new_path VIRTUAL_ENV: $virtual_env VIRTUAL_ENV_PROMPT: $virtual_env_prompt PKG_CONFIG_PATH: $new_pkg_config_path }
|
||||
if (has-env 'TCL_LIBRARY') {
|
||||
let $new_env = $new_env | insert TCL_LIBRARY r#''#
|
||||
}
|
||||
if (has-env 'TK_LIBRARY') {
|
||||
let $new_env = $new_env | insert TK_LIBRARY r#''#
|
||||
}
|
||||
let old_prompt_command = if (has-env 'PROMPT_COMMAND') { $env.PROMPT_COMMAND } else { '' }
|
||||
let new_env = if (is-env-true 'VIRTUAL_ENV_DISABLE_PROMPT') {
|
||||
$new_env
|
||||
} else {
|
||||
let virtual_prefix = $'(char lparen)($virtual_env_prompt)(char rparen) '
|
||||
let new_prompt = if (has-env 'PROMPT_COMMAND') {
|
||||
if ('closure' in ($old_prompt_command | describe)) {
|
||||
{|| $'($virtual_prefix)(do $old_prompt_command)' }
|
||||
} else {
|
||||
{|| $'($virtual_prefix)($old_prompt_command)' }
|
||||
}
|
||||
} else {
|
||||
{|| $'($virtual_prefix)' }
|
||||
}
|
||||
$new_env | merge { PROMPT_COMMAND: $new_prompt VIRTUAL_PREFIX: $virtual_prefix }
|
||||
}
|
||||
load-env $new_env
|
||||
}
|
||||
|
||||
export alias pydoc = python -m pydoc
|
||||
export alias deactivate = overlay hide activate
|
||||
@@ -0,0 +1,213 @@
|
||||
<#
|
||||
.Synopsis
|
||||
Activate a Python virtual environment for the current PowerShell session.
|
||||
|
||||
.Description
|
||||
Pushes the python executable for a virtual environment to the front of the
|
||||
$Env:PATH environment variable and sets the prompt to signify that you are
|
||||
in a Python virtual environment. Makes use of the command line switches as
|
||||
well as the `pyvenv.cfg` file values present in the virtual environment.
|
||||
|
||||
.Parameter VenvDir
|
||||
Path to the directory that contains the virtual environment to activate. The
|
||||
default value for this is the parent of the directory that the activate.ps1
|
||||
script is located within.
|
||||
|
||||
.Parameter Prompt
|
||||
The prompt prefix to display when this virtual environment is activated. By
|
||||
default, this prompt is the name of the virtual environment folder (VenvDir)
|
||||
surrounded by parentheses and followed by a single space (ie. '(.venv) ').
|
||||
|
||||
.Example
|
||||
activate.ps1
|
||||
Activates the Python virtual environment that contains the activate.ps1 script.
|
||||
|
||||
.Example
|
||||
activate.ps1 -Verbose
|
||||
Activates the Python virtual environment that contains the activate.ps1 script,
|
||||
and shows extra information about the activation as it executes.
|
||||
|
||||
.Example
|
||||
activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv
|
||||
Activates the Python virtual environment located in the specified location.
|
||||
|
||||
.Example
|
||||
activate.ps1 -Prompt "MyPython"
|
||||
Activates the Python virtual environment that contains the activate.ps1 script,
|
||||
and prefixes the current prompt with the specified string (surrounded in
|
||||
parentheses) while the virtual environment is active.
|
||||
|
||||
.Notes
|
||||
On Windows, it may be required to enable this activate.ps1 script by setting the
|
||||
execution policy for the user. You can do this by issuing the following PowerShell
|
||||
command:
|
||||
|
||||
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
||||
|
||||
For more information on Execution Policies:
|
||||
https://go.microsoft.com/fwlink/?LinkID=135170
|
||||
|
||||
#>
|
||||
Param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[String]
|
||||
$VenvDir,
|
||||
[Parameter(Mandatory = $false)]
|
||||
[String]
|
||||
$Prompt
|
||||
)
|
||||
|
||||
function Get-PyVenvConfig(
|
||||
[String]
|
||||
$ConfigDir
|
||||
) {
|
||||
Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
|
||||
|
||||
$pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
|
||||
|
||||
$pyvenvConfig = @{ }
|
||||
|
||||
if ($pyvenvConfigPath) {
|
||||
Write-Verbose "File exists, parse ``key = value`` lines"
|
||||
$pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
|
||||
|
||||
$pyvenvConfigContent | ForEach-Object {
|
||||
$keyval = $PSItem -split "\s*=\s*", 2
|
||||
if ($keyval[0] -and $keyval[1]) {
|
||||
$val = $keyval[1]
|
||||
|
||||
if ("'""".Contains($val.Substring(0, 1))) {
|
||||
$val = $val.Substring(1, $val.Length - 2)
|
||||
}
|
||||
|
||||
$pyvenvConfig[$keyval[0]] = $val
|
||||
Write-Verbose "Adding Key: '$($keyval[0])'='$val'"
|
||||
}
|
||||
}
|
||||
}
|
||||
return $pyvenvConfig
|
||||
}
|
||||
|
||||
function global:deactivate([switch] $NonDestructive) {
|
||||
if (Test-Path variable:_OLD_VIRTUAL_PATH) {
|
||||
$env:PATH = $variable:_OLD_VIRTUAL_PATH
|
||||
Remove-Variable "_OLD_VIRTUAL_PATH" -Scope global
|
||||
}
|
||||
|
||||
if (Test-Path variable:_OLD_VIRTUAL_TCL_LIBRARY) {
|
||||
$env:TCL_LIBRARY = $variable:_OLD_VIRTUAL_TCL_LIBRARY
|
||||
Remove-Variable "_OLD_VIRTUAL_TCL_LIBRARY" -Scope global
|
||||
} else {
|
||||
if (Test-Path env:TCL_LIBRARY) {
|
||||
Remove-Item env:TCL_LIBRARY -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
if (Test-Path variable:_OLD_VIRTUAL_TK_LIBRARY) {
|
||||
$env:TK_LIBRARY = $variable:_OLD_VIRTUAL_TK_LIBRARY
|
||||
Remove-Variable "_OLD_VIRTUAL_TK_LIBRARY" -Scope global
|
||||
} else {
|
||||
if (Test-Path env:TK_LIBRARY) {
|
||||
Remove-Item env:TK_LIBRARY -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
if (Test-Path variable:_OLD_PKG_CONFIG_PATH) {
|
||||
$env:PKG_CONFIG_PATH = $variable:_OLD_PKG_CONFIG_PATH
|
||||
Remove-Variable "_OLD_PKG_CONFIG_PATH" -Scope global
|
||||
}
|
||||
|
||||
if (Test-Path function:_old_virtual_prompt) {
|
||||
$function:prompt = $function:_old_virtual_prompt
|
||||
Remove-Item function:\_old_virtual_prompt
|
||||
}
|
||||
|
||||
if ($env:VIRTUAL_ENV) {
|
||||
Remove-Item env:VIRTUAL_ENV -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
if ($env:VIRTUAL_ENV_PROMPT) {
|
||||
Remove-Item env:VIRTUAL_ENV_PROMPT -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
|
||||
Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
|
||||
}
|
||||
|
||||
if (!$NonDestructive) {
|
||||
Remove-Item function:deactivate
|
||||
Remove-Item function:pydoc
|
||||
}
|
||||
}
|
||||
|
||||
function global:pydoc {
|
||||
& python -m pydoc $args
|
||||
}
|
||||
|
||||
$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
||||
$VenvExecDir = Get-Item -Path $VenvExecPath
|
||||
|
||||
Write-Verbose "Activation script is located in path: '$VenvExecPath'"
|
||||
|
||||
if ($VenvDir) {
|
||||
Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
|
||||
} else {
|
||||
$VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
|
||||
Write-Verbose "VenvDir=$VenvDir"
|
||||
}
|
||||
|
||||
$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
|
||||
|
||||
if ($Prompt) {
|
||||
Write-Verbose "Prompt specified as argument, using '$Prompt'"
|
||||
} else {
|
||||
if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
|
||||
Write-Verbose "Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
|
||||
$Prompt = $pyvenvCfg['prompt']
|
||||
} elseif ('' -ne "") {
|
||||
$Prompt = ''
|
||||
} else {
|
||||
$Prompt = Split-Path -Path $VenvDir -Leaf
|
||||
}
|
||||
}
|
||||
|
||||
deactivate -nondestructive
|
||||
|
||||
$env:VIRTUAL_ENV = $VenvDir
|
||||
$env:VIRTUAL_ENV_PROMPT = $Prompt
|
||||
|
||||
if ('' -ne "") {
|
||||
if (Test-Path env:TCL_LIBRARY) {
|
||||
New-Variable -Scope global -Name _OLD_VIRTUAL_TCL_LIBRARY -Value $env:TCL_LIBRARY
|
||||
}
|
||||
$env:TCL_LIBRARY = ''
|
||||
}
|
||||
|
||||
if ('' -ne "") {
|
||||
if (Test-Path env:TK_LIBRARY) {
|
||||
New-Variable -Scope global -Name _OLD_VIRTUAL_TK_LIBRARY -Value $env:TK_LIBRARY
|
||||
}
|
||||
$env:TK_LIBRARY = ''
|
||||
}
|
||||
|
||||
New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH
|
||||
|
||||
if (Test-Path env:PKG_CONFIG_PATH) {
|
||||
New-Variable -Scope global -Name _OLD_PKG_CONFIG_PATH -Value $env:PKG_CONFIG_PATH
|
||||
}
|
||||
$env:PKG_CONFIG_PATH = "$env:VIRTUAL_ENV\lib\pkgconfig;$env:PKG_CONFIG_PATH"
|
||||
|
||||
$env:PATH = "$env:VIRTUAL_ENV/" + 'bin' + ':' + $env:PATH
|
||||
|
||||
if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
|
||||
function global:_old_virtual_prompt {
|
||||
""
|
||||
}
|
||||
$function:_old_virtual_prompt = $function:prompt
|
||||
New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt
|
||||
|
||||
function global:prompt {
|
||||
Write-Host -NoNewline -ForegroundColor Green "($($_PYTHON_VENV_PROMPT_PREFIX)) "
|
||||
_old_virtual_prompt
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
"""Activate virtualenv for current interpreter:
|
||||
|
||||
import runpy runpy.run_path(this_file)
|
||||
|
||||
This can be used when you must use an existing Python interpreter, not the virtualenv bin/python.
|
||||
|
||||
""" # noqa: D415
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import site
|
||||
import sys
|
||||
|
||||
try:
|
||||
abs_file = os.path.abspath(__file__)
|
||||
except NameError as exc:
|
||||
msg = "You must use import runpy; runpy.run_path(this_file)"
|
||||
raise AssertionError(msg) from exc
|
||||
|
||||
bin_dir = os.path.dirname(abs_file)
|
||||
base = bin_dir[: -len('bin') - 1] # ty: ignore[unresolved-reference]
|
||||
|
||||
# prepend bin to PATH (this file is inside the bin directory)
|
||||
os.environ["PATH"] = os.pathsep.join([bin_dir, *os.environ.get("PATH", "").split(os.pathsep)])
|
||||
os.environ["VIRTUAL_ENV"] = base # virtual env is right above bin directory
|
||||
os.environ["VIRTUAL_ENV_PROMPT"] = '' or os.path.basename(base) # ty: ignore[unresolved-reference]
|
||||
|
||||
# Set PKG_CONFIG_PATH to include the virtualenv's pkgconfig directory
|
||||
pkg_config_path = os.path.join(base, "lib", "pkgconfig")
|
||||
existing_pkg_config_path = os.environ.get("PKG_CONFIG_PATH", "")
|
||||
if existing_pkg_config_path:
|
||||
os.environ["PKG_CONFIG_PATH"] = os.pathsep.join([pkg_config_path, existing_pkg_config_path])
|
||||
else:
|
||||
os.environ["PKG_CONFIG_PATH"] = pkg_config_path
|
||||
|
||||
# add the virtual environments libraries to the host python import mechanism
|
||||
prev_length = len(sys.path)
|
||||
for lib in '../lib/python3.14/site-packages'.split(os.pathsep): # ty: ignore[unresolved-reference]
|
||||
path = os.path.realpath(os.path.join(bin_dir, lib))
|
||||
site.addsitedir(path.decode("utf-8") if '' else path) # ty: ignore[unresolved-reference,unresolved-attribute]
|
||||
sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]
|
||||
|
||||
sys.real_prefix = sys.prefix # ty: ignore[unresolved-attribute]
|
||||
sys.prefix = base
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
import sys
|
||||
from coverage.cmdline import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = sys.argv[0].removesuffix('.exe')
|
||||
sys.exit(main())
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
import sys
|
||||
from coverage.cmdline import main_deprecated
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = sys.argv[0].removesuffix('.exe')
|
||||
sys.exit(main_deprecated())
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
import sys
|
||||
from coverage.cmdline import main_deprecated
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = sys.argv[0].removesuffix('.exe')
|
||||
sys.exit(main_deprecated())
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
if __name__ == '__main__':
|
||||
from pip._internal.cli.main import main
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
if __name__ == '__main__':
|
||||
from pip._internal.cli.main import main
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
if __name__ == '__main__':
|
||||
from pip._internal.cli.main import main
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
if __name__ == '__main__':
|
||||
from pip._internal.cli.main import main
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
import sys
|
||||
from _pytest.config import _console_main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = sys.argv[0].removesuffix('.exe')
|
||||
sys.exit(_console_main())
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
import sys
|
||||
from pygments.cmdline import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = sys.argv[0].removesuffix('.exe')
|
||||
sys.exit(main())
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/home/zlata/PycharmProjects/Funktional/.venv/bin/python
|
||||
import sys
|
||||
from _pytest.config import _console_main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = sys.argv[0].removesuffix('.exe')
|
||||
sys.exit(_console_main())
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
/usr/bin/python3
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
python
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
python
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
__all__ = ["__version__", "version_tuple"]
|
||||
|
||||
try:
|
||||
from ._version import version as __version__
|
||||
from ._version import version_tuple
|
||||
except ImportError: # pragma: no cover
|
||||
# broken installation, we don't even try
|
||||
# unknown only works because we do poor mans version compare
|
||||
__version__ = "unknown"
|
||||
version_tuple = (0, 0, "unknown")
|
||||
@@ -0,0 +1,117 @@
|
||||
"""Allow bash-completion for argparse with argcomplete if installed.
|
||||
|
||||
Needs argcomplete>=0.5.6 for python 3.2/3.3 (older versions fail
|
||||
to find the magic string, so _ARGCOMPLETE env. var is never set, and
|
||||
this does not need special code).
|
||||
|
||||
Function try_argcomplete(parser) should be called directly before
|
||||
the call to ArgumentParser.parse_args().
|
||||
|
||||
The filescompleter is what you normally would use on the positional
|
||||
arguments specification, in order to get "dirname/" after "dirn<TAB>"
|
||||
instead of the default "dirname ":
|
||||
|
||||
optparser.add_argument(Config._file_or_dir, nargs='*').completer=filescompleter
|
||||
|
||||
Other, application specific, completers should go in the file
|
||||
doing the add_argument calls as they need to be specified as .completer
|
||||
attributes as well. (If argcomplete is not installed, the function the
|
||||
attribute points to will not be used).
|
||||
|
||||
SPEEDUP
|
||||
=======
|
||||
|
||||
The generic argcomplete script for bash-completion
|
||||
(/etc/bash_completion.d/python-argcomplete.sh)
|
||||
uses a python program to determine startup script generated by pip.
|
||||
You can speed up completion somewhat by changing this script to include
|
||||
# PYTHON_ARGCOMPLETE_OK
|
||||
so the python-argcomplete-check-easy-install-script does not
|
||||
need to be called to find the entry point of the code and see if that is
|
||||
marked with PYTHON_ARGCOMPLETE_OK.
|
||||
|
||||
INSTALL/DEBUGGING
|
||||
=================
|
||||
|
||||
To include this support in another application that has setup.py generated
|
||||
scripts:
|
||||
|
||||
- Add the line:
|
||||
# PYTHON_ARGCOMPLETE_OK
|
||||
near the top of the main python entry point.
|
||||
|
||||
- Include in the file calling parse_args():
|
||||
from _argcomplete import try_argcomplete, filescompleter
|
||||
Call try_argcomplete just before parse_args(), and optionally add
|
||||
filescompleter to the positional arguments' add_argument().
|
||||
|
||||
If things do not work right away:
|
||||
|
||||
- Switch on argcomplete debugging with (also helpful when doing custom
|
||||
completers):
|
||||
export _ARC_DEBUG=1
|
||||
|
||||
- Run:
|
||||
python-argcomplete-check-easy-install-script $(which appname)
|
||||
echo $?
|
||||
will echo 0 if the magic line has been found, 1 if not.
|
||||
|
||||
- Sometimes it helps to find early on errors using:
|
||||
_ARGCOMPLETE=1 _ARC_DEBUG=1 appname
|
||||
which should throw a KeyError: 'COMPLINE' (which is properly set by the
|
||||
global argcomplete script).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
|
||||
class FastFilesCompleter:
|
||||
"""Fast file completer class."""
|
||||
|
||||
def __init__(self, directories: bool = True) -> None:
|
||||
self.directories = directories
|
||||
|
||||
def __call__(self, prefix: str, **kwargs: Any) -> list[str]:
|
||||
# Only called on non option completions.
|
||||
if os.sep in prefix[1:]:
|
||||
prefix_dir = len(os.path.dirname(prefix) + os.sep)
|
||||
else:
|
||||
prefix_dir = 0
|
||||
completion = []
|
||||
globbed = []
|
||||
if "*" not in prefix and "?" not in prefix:
|
||||
# We are on unix, otherwise no bash.
|
||||
if not prefix or prefix[-1] == os.sep:
|
||||
globbed.extend(glob(prefix + ".*"))
|
||||
prefix += "*"
|
||||
globbed.extend(glob(prefix))
|
||||
for x in sorted(globbed):
|
||||
if os.path.isdir(x):
|
||||
x += "/"
|
||||
# Append stripping the prefix (like bash, not like compgen).
|
||||
completion.append(x[prefix_dir:])
|
||||
return completion
|
||||
|
||||
|
||||
if os.environ.get("_ARGCOMPLETE"):
|
||||
try:
|
||||
import argcomplete.completers
|
||||
except ImportError:
|
||||
sys.exit(-1)
|
||||
filescompleter: FastFilesCompleter | None = FastFilesCompleter()
|
||||
|
||||
def try_argcomplete(parser: argparse.ArgumentParser) -> None:
|
||||
argcomplete.autocomplete(parser, always_complete_options=False)
|
||||
|
||||
else:
|
||||
|
||||
def try_argcomplete(parser: argparse.ArgumentParser) -> None:
|
||||
pass
|
||||
|
||||
filescompleter = None
|
||||
@@ -0,0 +1,26 @@
|
||||
"""Python inspection/code generation API."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from .code import Code
|
||||
from .code import ExceptionInfo
|
||||
from .code import filter_traceback
|
||||
from .code import Frame
|
||||
from .code import getfslineno
|
||||
from .code import Traceback
|
||||
from .code import TracebackEntry
|
||||
from .source import getrawcode
|
||||
from .source import Source
|
||||
|
||||
|
||||
__all__ = [
|
||||
"Code",
|
||||
"ExceptionInfo",
|
||||
"Frame",
|
||||
"Source",
|
||||
"Traceback",
|
||||
"TracebackEntry",
|
||||
"filter_traceback",
|
||||
"getfslineno",
|
||||
"getrawcode",
|
||||
]
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,262 @@
|
||||
# mypy: allow-untyped-defs
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
from bisect import bisect_right
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Iterator
|
||||
import inspect
|
||||
import linecache
|
||||
import textwrap
|
||||
import tokenize
|
||||
import types
|
||||
from typing import overload
|
||||
import warnings
|
||||
|
||||
|
||||
class Source:
|
||||
"""An immutable object holding a source code fragment.
|
||||
|
||||
When using Source(...), the source lines are deindented.
|
||||
"""
|
||||
|
||||
def __init__(self, obj: object = None) -> None:
|
||||
if not obj:
|
||||
self.lines: list[str] = []
|
||||
self.raw_lines: list[str] = []
|
||||
return
|
||||
match obj:
|
||||
case Source():
|
||||
self.lines = obj.lines
|
||||
self.raw_lines = obj.raw_lines
|
||||
case tuple() | list():
|
||||
self.lines = deindent(x.rstrip("\n") for x in obj)
|
||||
self.raw_lines = [x.rstrip("\n") for x in obj]
|
||||
case str():
|
||||
self.lines = deindent(obj.split("\n"))
|
||||
self.raw_lines = obj.split("\n")
|
||||
case _:
|
||||
try:
|
||||
rawcode = getrawcode(obj)
|
||||
src = inspect.getsource(rawcode)
|
||||
except TypeError:
|
||||
src = inspect.getsource(obj) # type: ignore[arg-type]
|
||||
self.lines = deindent(src.split("\n"))
|
||||
self.raw_lines = src.split("\n")
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if not isinstance(other, Source):
|
||||
return NotImplemented
|
||||
return self.lines == other.lines
|
||||
|
||||
# Ignore type because of https://github.com/python/mypy/issues/4266.
|
||||
__hash__ = None # type: ignore
|
||||
|
||||
@overload
|
||||
def __getitem__(self, key: int) -> str: ...
|
||||
|
||||
@overload
|
||||
def __getitem__(self, key: slice) -> Source: ...
|
||||
|
||||
def __getitem__(self, key: int | slice) -> str | Source:
|
||||
if isinstance(key, int):
|
||||
return self.lines[key]
|
||||
else:
|
||||
if key.step not in (None, 1):
|
||||
raise IndexError("cannot slice a Source with a step")
|
||||
newsource = Source()
|
||||
newsource.lines = self.lines[key.start : key.stop]
|
||||
newsource.raw_lines = self.raw_lines[key.start : key.stop]
|
||||
return newsource
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(self.lines)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.lines)
|
||||
|
||||
def strip(self) -> Source:
|
||||
"""Return new Source object with trailing and leading blank lines removed."""
|
||||
start, end = 0, len(self)
|
||||
while start < end and not self.lines[start].strip():
|
||||
start += 1
|
||||
while end > start and not self.lines[end - 1].strip():
|
||||
end -= 1
|
||||
source = Source()
|
||||
source.raw_lines = self.raw_lines
|
||||
source.lines[:] = self.lines[start:end]
|
||||
return source
|
||||
|
||||
def indent(self, indent: str = " " * 4) -> Source:
|
||||
"""Return a copy of the source object with all lines indented by the
|
||||
given indent-string."""
|
||||
newsource = Source()
|
||||
newsource.raw_lines = self.raw_lines
|
||||
newsource.lines = [(indent + line) for line in self.lines]
|
||||
return newsource
|
||||
|
||||
def getstatement(self, lineno: int) -> Source:
|
||||
"""Return Source statement which contains the given linenumber
|
||||
(counted from 0)."""
|
||||
start, end = self.getstatementrange(lineno)
|
||||
return self[start:end]
|
||||
|
||||
def getstatementrange(self, lineno: int) -> tuple[int, int]:
|
||||
"""Return (start, end) tuple which spans the minimal statement region
|
||||
which containing the given lineno."""
|
||||
if not (0 <= lineno < len(self)):
|
||||
raise IndexError("lineno out of range")
|
||||
_ast, start, end = getstatementrange_ast(lineno, self)
|
||||
return start, end
|
||||
|
||||
def deindent(self) -> Source:
|
||||
"""Return a new Source object deindented."""
|
||||
newsource = Source()
|
||||
newsource.lines[:] = deindent(self.lines)
|
||||
newsource.raw_lines = self.raw_lines
|
||||
return newsource
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "\n".join(self.lines)
|
||||
|
||||
|
||||
#
|
||||
# helper functions
|
||||
#
|
||||
|
||||
|
||||
def findsource(obj) -> tuple[Source | None, int]:
|
||||
try:
|
||||
sourcelines, lineno = inspect.findsource(obj)
|
||||
except Exception:
|
||||
# inspect.findsource() fails for code objects whose co_filename is
|
||||
# relative to a directory other than the cwd, e.g. compiled Cython
|
||||
# modules when running pytest from a subdirectory (#1139).
|
||||
# Fall back to linecache, which also searches sys.path for bare
|
||||
# filenames, as the standard traceback module does.
|
||||
code = (
|
||||
obj if isinstance(obj, types.CodeType) else getattr(obj, "__code__", None)
|
||||
)
|
||||
if code is None:
|
||||
return None, -1
|
||||
sourcelines = linecache.getlines(code.co_filename)
|
||||
if not sourcelines:
|
||||
return None, -1
|
||||
lineno = code.co_firstlineno - 1
|
||||
source = Source()
|
||||
source.lines = [line.rstrip() for line in sourcelines]
|
||||
source.raw_lines = sourcelines
|
||||
return source, lineno
|
||||
|
||||
|
||||
def getrawcode(obj: object, trycall: bool = True) -> types.CodeType:
|
||||
"""Return code object for given function."""
|
||||
try:
|
||||
return obj.__code__ # type: ignore[attr-defined,no-any-return]
|
||||
except AttributeError:
|
||||
pass
|
||||
if trycall:
|
||||
call = getattr(obj, "__call__", None)
|
||||
if call and not isinstance(obj, type):
|
||||
return getrawcode(call, trycall=False)
|
||||
raise TypeError(f"could not get code object for {obj!r}")
|
||||
|
||||
|
||||
def deindent(lines: Iterable[str]) -> list[str]:
|
||||
return textwrap.dedent("\n".join(lines)).splitlines()
|
||||
|
||||
|
||||
def _statement_linenos(node: ast.AST) -> list[int]:
|
||||
"""Sorted 0-based line numbers of all statements below ``node``.
|
||||
|
||||
Walking the tree is proportional to the size of the whole module, while a
|
||||
traceback asks for many line numbers of the same module -- so the result is
|
||||
memoized on the node, which shares the lifetime of the caller's ast cache.
|
||||
"""
|
||||
values: list[int] | None = getattr(node, "_pytest_statement_linenos", None)
|
||||
if values is not None:
|
||||
return values
|
||||
# Flatten all statements and except handlers into one lineno-list.
|
||||
# AST's line numbers start indexing at 1.
|
||||
values = []
|
||||
for x in ast.walk(node):
|
||||
if isinstance(x, ast.stmt | ast.ExceptHandler):
|
||||
# The lineno points to the class/def, so need to include the decorators.
|
||||
if isinstance(x, ast.ClassDef | ast.FunctionDef | ast.AsyncFunctionDef):
|
||||
for d in x.decorator_list:
|
||||
values.append(d.lineno - 1)
|
||||
values.append(x.lineno - 1)
|
||||
for name in ("finalbody", "orelse"):
|
||||
val: list[ast.stmt] | None = getattr(x, name, None)
|
||||
if val:
|
||||
# Treat the finally/orelse part as its own statement.
|
||||
values.append(val[0].lineno - 1 - 1)
|
||||
values.sort()
|
||||
try:
|
||||
node._pytest_statement_linenos = values # type: ignore[attr-defined]
|
||||
except AttributeError: # pragma: no cover - defensive
|
||||
pass
|
||||
return values
|
||||
|
||||
|
||||
def get_statement_startend2(lineno: int, node: ast.AST) -> tuple[int, int | None]:
|
||||
values = _statement_linenos(node)
|
||||
insert_index = bisect_right(values, lineno)
|
||||
if insert_index == 0:
|
||||
return 0, None
|
||||
start = values[insert_index - 1]
|
||||
if insert_index >= len(values):
|
||||
end = None
|
||||
else:
|
||||
end = values[insert_index]
|
||||
return start, end
|
||||
|
||||
|
||||
def getstatementrange_ast(
|
||||
lineno: int,
|
||||
source: Source,
|
||||
assertion: bool = False,
|
||||
astnode: ast.AST | None = None,
|
||||
) -> tuple[ast.AST, int, int]:
|
||||
if astnode is None:
|
||||
content = str(source)
|
||||
# See #4260:
|
||||
# Don't produce duplicate warnings when compiling source to find AST.
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
astnode = ast.parse(content, "source", "exec")
|
||||
|
||||
start, end = get_statement_startend2(lineno, astnode)
|
||||
# We need to correct the end:
|
||||
# - ast-parsing strips comments
|
||||
# - there might be empty lines
|
||||
# - we might have lesser indented code blocks at the end
|
||||
if end is None:
|
||||
end = len(source.lines)
|
||||
|
||||
if end > start + 1:
|
||||
# Make sure we don't span differently indented code blocks
|
||||
# by using the BlockFinder helper used which inspect.getsource() uses itself.
|
||||
block_finder = inspect.BlockFinder()
|
||||
# If we start with an indented line, put blockfinder to "started" mode.
|
||||
block_finder.started = (
|
||||
bool(source.lines[start]) and source.lines[start][0].isspace()
|
||||
)
|
||||
it = ((x + "\n") for x in source.lines[start:end])
|
||||
try:
|
||||
for tok in tokenize.generate_tokens(lambda: next(it)):
|
||||
block_finder.tokeneater(*tok)
|
||||
except (inspect.EndOfBlock, IndentationError):
|
||||
end = block_finder.last + start
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# The end might still point to a comment or empty line, correct it.
|
||||
end = min(end, len(source.lines))
|
||||
while end:
|
||||
line = source.lines[end - 1].lstrip()
|
||||
if line.startswith("#") or not line:
|
||||
end -= 1
|
||||
else:
|
||||
break
|
||||
return astnode, start, end
|
||||
@@ -0,0 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .terminalwriter import get_terminal_width
|
||||
from .terminalwriter import TerminalWriter
|
||||
|
||||
|
||||
__all__ = [
|
||||
"TerminalWriter",
|
||||
"get_terminal_width",
|
||||
]
|
||||
@@ -0,0 +1,719 @@
|
||||
# mypy: allow-untyped-defs
|
||||
# This module was imported from the cpython standard library
|
||||
# (https://github.com/python/cpython/) at commit
|
||||
# c5140945c723ae6c4b7ee81ff720ac8ea4b52cfd (python3.12).
|
||||
#
|
||||
# It has since been adapted to emit its output lazily as a stream of
|
||||
# string chunks (``_format`` and the per-type helpers are generators)
|
||||
# rather than writing to a file-like object. This lets ``pformat_lines``
|
||||
# stop formatting as soon as a line/char budget is reached, so a huge
|
||||
# collection a caller is going to truncate anyway is never fully built.
|
||||
# As a result this copy has diverged structurally from upstream and is
|
||||
# no longer kept in sync with it.
|
||||
#
|
||||
#
|
||||
# Original Author: Fred L. Drake, Jr.
|
||||
# fdrake@acm.org
|
||||
#
|
||||
# This is a simple little module I wrote to make life easier. I didn't
|
||||
# see anything quite like it in the library, though I may have overlooked
|
||||
# something. I wrote this when I was trying to read some heavily nested
|
||||
# tuples with fairly non-descriptive content. This is modeled very much
|
||||
# after Lisp/Scheme - style pretty-printing of lists. If you find it
|
||||
# useful, thank small children who sleep at night.
|
||||
from __future__ import annotations
|
||||
|
||||
import collections as _collections
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Iterator
|
||||
import dataclasses as _dataclasses
|
||||
import re
|
||||
import sys
|
||||
import types as _types
|
||||
from typing import Any
|
||||
|
||||
|
||||
class _safe_key:
|
||||
"""Helper function for key functions when sorting unorderable objects.
|
||||
|
||||
The wrapped-object will fallback to a Py2.x style comparison for
|
||||
unorderable types (sorting first comparing the type name and then by
|
||||
the obj ids). Does not work recursively, so dict.items() must have
|
||||
_safe_key applied to both the key and the value.
|
||||
|
||||
"""
|
||||
|
||||
__slots__ = ["obj"]
|
||||
|
||||
def __init__(self, obj):
|
||||
self.obj = obj
|
||||
|
||||
def __lt__(self, other):
|
||||
try:
|
||||
return self.obj < other.obj
|
||||
except TypeError:
|
||||
return (str(type(self.obj)), id(self.obj)) < (
|
||||
str(type(other.obj)),
|
||||
id(other.obj),
|
||||
)
|
||||
|
||||
|
||||
def _safe_tuple(t):
|
||||
"""Helper function for comparing 2-tuples"""
|
||||
return _safe_key(t[0]), _safe_key(t[1])
|
||||
|
||||
|
||||
class PrettyPrinter:
|
||||
def __init__(
|
||||
self,
|
||||
indent: int = 4,
|
||||
width: int = 80,
|
||||
depth: int | None = None,
|
||||
) -> None:
|
||||
"""Handle pretty printing operations onto a stream using a set of
|
||||
configured parameters.
|
||||
|
||||
indent
|
||||
Number of spaces to indent for each level of nesting.
|
||||
|
||||
width
|
||||
Attempted maximum number of columns in the output.
|
||||
|
||||
depth
|
||||
The maximum depth to print out nested structures.
|
||||
|
||||
"""
|
||||
if indent < 0:
|
||||
raise ValueError("indent must be >= 0")
|
||||
if depth is not None and depth <= 0:
|
||||
raise ValueError("depth must be > 0")
|
||||
if not width:
|
||||
raise ValueError("width must be != 0")
|
||||
self._depth = depth
|
||||
self._indent_per_level = indent
|
||||
self._width = width
|
||||
|
||||
def pformat(self, object: Any) -> str:
|
||||
return "".join(self._format(object, 0, 0, set(), 0))
|
||||
|
||||
def pformat_lines(
|
||||
self,
|
||||
object: Any,
|
||||
*,
|
||||
max_lines: int | None = None,
|
||||
max_chars: int | None = None,
|
||||
) -> list[str]:
|
||||
"""Pretty-print ``object`` and return its lines.
|
||||
|
||||
``max_lines`` / ``max_chars`` bound the two output dimensions
|
||||
independently; either may be ``None`` to leave that dimension
|
||||
unbounded, and with both ``None`` the whole object is formatted.
|
||||
When a bound is given the object is only formatted far enough to
|
||||
reach it, so a huge object costs O(budget) rather than O(N).
|
||||
|
||||
The budget is a stopping condition, not a precise cut: formatting
|
||||
stops on the first piece of output that reaches it, so the result
|
||||
may slightly overshoot the bound.
|
||||
"""
|
||||
if max_lines is None and max_chars is None:
|
||||
return self.pformat(object).splitlines()
|
||||
n_lines = 0
|
||||
n_chars = 0
|
||||
chunks: list[str] = []
|
||||
for chunk in self._format(object, 0, 0, set(), 0):
|
||||
chunks.append(chunk)
|
||||
if max_chars is not None:
|
||||
n_chars += len(chunk)
|
||||
if max_lines is not None and "\n" in chunk:
|
||||
# Guard the count: most chunks (brackets, indents, item
|
||||
# reprs) have no newline, and skipping the call on them
|
||||
# is meaningfully cheaper than counting every chunk.
|
||||
n_lines += chunk.count("\n")
|
||||
if (max_lines is not None and n_lines >= max_lines) or (
|
||||
max_chars is not None and n_chars >= max_chars
|
||||
):
|
||||
break
|
||||
return "".join(chunks).splitlines()
|
||||
|
||||
def _format(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
objid = id(object)
|
||||
if objid in context:
|
||||
yield _recursion(object)
|
||||
return
|
||||
|
||||
p = self._dispatch.get(type(object).__repr__, None)
|
||||
if p is not None:
|
||||
context.add(objid)
|
||||
yield from p(self, object, indent, allowance, context, level + 1)
|
||||
context.remove(objid)
|
||||
elif (
|
||||
_dataclasses.is_dataclass(object)
|
||||
and not isinstance(object, type)
|
||||
and object.__dataclass_params__.repr # type:ignore[attr-defined]
|
||||
and
|
||||
# Check dataclass has generated repr method.
|
||||
hasattr(object.__repr__, "__wrapped__")
|
||||
and "__create_fn__" in object.__repr__.__wrapped__.__qualname__
|
||||
):
|
||||
context.add(objid)
|
||||
yield from self._pprint_dataclass(
|
||||
object, indent, allowance, context, level + 1
|
||||
)
|
||||
context.remove(objid)
|
||||
else:
|
||||
yield self._repr(object, context, level)
|
||||
|
||||
def _pprint_dataclass(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
cls_name = object.__class__.__name__
|
||||
items = [
|
||||
(f.name, getattr(object, f.name))
|
||||
for f in _dataclasses.fields(object)
|
||||
if f.repr
|
||||
]
|
||||
yield cls_name + "("
|
||||
yield from self._format_namespace_items(
|
||||
items, indent, allowance, context, level
|
||||
)
|
||||
yield ")"
|
||||
|
||||
_dispatch: dict[
|
||||
Callable[..., str],
|
||||
Callable[[PrettyPrinter, Any, int, int, set[int], int], Iterator[str]],
|
||||
] = {}
|
||||
|
||||
def _pprint_dict(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield "{"
|
||||
yield from self._format_dict_items(
|
||||
object.items(), indent, allowance, context, level
|
||||
)
|
||||
yield "}"
|
||||
|
||||
_dispatch[dict.__repr__] = _pprint_dict
|
||||
|
||||
def _pprint_dict_with_cls(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
if not len(object):
|
||||
yield repr(object)
|
||||
return
|
||||
cls = object.__class__
|
||||
yield cls.__name__ + "("
|
||||
yield from self._pprint_dict(object, indent, allowance, context, level)
|
||||
yield ")"
|
||||
|
||||
_dispatch[_collections.OrderedDict.__repr__] = _pprint_dict_with_cls
|
||||
if sys.version_info >= (3, 15):
|
||||
_dispatch[frozendict.__repr__] = _pprint_dict_with_cls # noqa: F821
|
||||
|
||||
def _pprint_list(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield "["
|
||||
yield from self._format_items(object, indent, allowance, context, level)
|
||||
yield "]"
|
||||
|
||||
_dispatch[list.__repr__] = _pprint_list
|
||||
|
||||
def _pprint_tuple(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield "("
|
||||
yield from self._format_items(object, indent, allowance, context, level)
|
||||
yield ")"
|
||||
|
||||
_dispatch[tuple.__repr__] = _pprint_tuple
|
||||
|
||||
def _pprint_set(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
if not len(object):
|
||||
yield repr(object)
|
||||
return
|
||||
typ = object.__class__
|
||||
if typ is set:
|
||||
yield "{"
|
||||
endchar = "}"
|
||||
else:
|
||||
yield typ.__name__ + "({"
|
||||
endchar = "})"
|
||||
try:
|
||||
# Try a direct sort first; it is faster than the fallback and
|
||||
# works for the common homogeneous, orderable case.
|
||||
object = sorted(object)
|
||||
except TypeError:
|
||||
# Heterogeneous element types — fall back to a key that
|
||||
# tolerates unorderable pairs by string-comparing their types.
|
||||
object = sorted(object, key=_safe_key)
|
||||
yield from self._format_items(object, indent, allowance, context, level)
|
||||
yield endchar
|
||||
|
||||
_dispatch[set.__repr__] = _pprint_set
|
||||
_dispatch[frozenset.__repr__] = _pprint_set
|
||||
|
||||
def _pprint_str(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
if not len(object):
|
||||
yield repr(object)
|
||||
return
|
||||
chunks = []
|
||||
lines = object.splitlines(True)
|
||||
if level == 1:
|
||||
indent += 1
|
||||
allowance += 1
|
||||
max_width1 = max_width = self._width - indent
|
||||
for i, line in enumerate(lines):
|
||||
rep = repr(line)
|
||||
if i == len(lines) - 1:
|
||||
max_width1 -= allowance
|
||||
if len(rep) <= max_width1:
|
||||
chunks.append(rep)
|
||||
else:
|
||||
# A list of alternating (non-space, space) strings
|
||||
parts = re.findall(r"\S*\s*", line)
|
||||
assert parts
|
||||
assert not parts[-1]
|
||||
parts.pop() # drop empty last part
|
||||
max_width2 = max_width
|
||||
current = ""
|
||||
for j, part in enumerate(parts):
|
||||
candidate = current + part
|
||||
if j == len(parts) - 1 and i == len(lines) - 1:
|
||||
max_width2 -= allowance
|
||||
if len(repr(candidate)) > max_width2:
|
||||
if current:
|
||||
chunks.append(repr(current))
|
||||
current = part
|
||||
else:
|
||||
current = candidate
|
||||
if current: # pragma: no branch - a wrapped line always ends with a pending chunk
|
||||
chunks.append(repr(current))
|
||||
if len(chunks) == 1:
|
||||
yield rep
|
||||
return
|
||||
if level == 1:
|
||||
yield "("
|
||||
for i, rep in enumerate(chunks):
|
||||
if i > 0:
|
||||
yield "\n" + " " * indent
|
||||
yield rep
|
||||
if level == 1:
|
||||
yield ")"
|
||||
|
||||
_dispatch[str.__repr__] = _pprint_str
|
||||
|
||||
def _pprint_bytes(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
if len(object) <= 4:
|
||||
yield repr(object)
|
||||
return
|
||||
parens = level == 1
|
||||
if parens:
|
||||
indent += 1
|
||||
allowance += 1
|
||||
yield "("
|
||||
delim = ""
|
||||
for rep in _wrap_bytes_repr(object, self._width - indent, allowance):
|
||||
yield delim
|
||||
yield rep
|
||||
if not delim:
|
||||
delim = "\n" + " " * indent
|
||||
if parens:
|
||||
yield ")"
|
||||
|
||||
_dispatch[bytes.__repr__] = _pprint_bytes
|
||||
|
||||
def _pprint_bytearray(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield "bytearray("
|
||||
yield from self._pprint_bytes(
|
||||
bytes(object), indent + 10, allowance + 1, context, level + 1
|
||||
)
|
||||
yield ")"
|
||||
|
||||
_dispatch[bytearray.__repr__] = _pprint_bytearray
|
||||
|
||||
def _pprint_mappingproxy(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield "mappingproxy("
|
||||
yield from self._format(object.copy(), indent, allowance, context, level)
|
||||
yield ")"
|
||||
|
||||
_dispatch[_types.MappingProxyType.__repr__] = _pprint_mappingproxy
|
||||
|
||||
def _pprint_simplenamespace(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
if type(object) is _types.SimpleNamespace:
|
||||
# The SimpleNamespace repr is "namespace" instead of the class
|
||||
# name, so we do the same here. For subclasses; use the class name.
|
||||
cls_name = "namespace"
|
||||
else:
|
||||
cls_name = object.__class__.__name__
|
||||
items = object.__dict__.items()
|
||||
yield cls_name + "("
|
||||
yield from self._format_namespace_items(
|
||||
items, indent, allowance, context, level
|
||||
)
|
||||
yield ")"
|
||||
|
||||
_dispatch[_types.SimpleNamespace.__repr__] = _pprint_simplenamespace
|
||||
|
||||
def _format_dict_items(
|
||||
self,
|
||||
items: Iterable[tuple[Any, Any]],
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
item_indent = indent + self._indent_per_level
|
||||
delimnl = "\n" + " " * item_indent
|
||||
emitted = False
|
||||
for key, ent in items:
|
||||
emitted = True
|
||||
yield delimnl
|
||||
yield self._repr(key, context, level)
|
||||
yield ": "
|
||||
yield from self._format(ent, item_indent, 1, context, level)
|
||||
yield ","
|
||||
|
||||
if emitted:
|
||||
yield "\n" + " " * indent
|
||||
|
||||
def _format_namespace_items(
|
||||
self,
|
||||
items: Iterable[tuple[Any, Any]],
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
item_indent = indent + self._indent_per_level
|
||||
delimnl = "\n" + " " * item_indent
|
||||
emitted = False
|
||||
for key, ent in items:
|
||||
emitted = True
|
||||
yield delimnl
|
||||
yield key
|
||||
yield "="
|
||||
if id(ent) in context:
|
||||
# Special-case representation of recursion to match standard
|
||||
# recursive dataclass repr.
|
||||
yield "..."
|
||||
else:
|
||||
yield from self._format(
|
||||
ent,
|
||||
item_indent + len(key) + 1,
|
||||
1,
|
||||
context,
|
||||
level,
|
||||
)
|
||||
|
||||
yield ","
|
||||
|
||||
if emitted:
|
||||
yield "\n" + " " * indent
|
||||
|
||||
def _format_items(
|
||||
self,
|
||||
items: Iterable[Any],
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
item_indent = indent + self._indent_per_level
|
||||
delimnl = "\n" + " " * item_indent
|
||||
emitted = False
|
||||
for item in items:
|
||||
emitted = True
|
||||
yield delimnl
|
||||
yield from self._format(item, item_indent, 1, context, level)
|
||||
yield ","
|
||||
|
||||
if emitted:
|
||||
yield "\n" + " " * indent
|
||||
|
||||
def _repr(self, object: Any, context: set[int], level: int) -> str:
|
||||
return self._safe_repr(object, context.copy(), self._depth, level)
|
||||
|
||||
def _pprint_default_dict(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
rdf = self._repr(object.default_factory, context, level)
|
||||
yield f"{object.__class__.__name__}({rdf}, "
|
||||
yield from self._pprint_dict(object, indent, allowance, context, level)
|
||||
yield ")"
|
||||
|
||||
_dispatch[_collections.defaultdict.__repr__] = _pprint_default_dict
|
||||
|
||||
def _pprint_counter(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield object.__class__.__name__ + "("
|
||||
|
||||
if object:
|
||||
yield "{"
|
||||
items = object.most_common()
|
||||
yield from self._format_dict_items(items, indent, allowance, context, level)
|
||||
yield "}"
|
||||
|
||||
yield ")"
|
||||
|
||||
_dispatch[_collections.Counter.__repr__] = _pprint_counter
|
||||
|
||||
def _pprint_chain_map(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
if not len(object.maps) or (len(object.maps) == 1 and not len(object.maps[0])):
|
||||
yield repr(object)
|
||||
return
|
||||
|
||||
yield object.__class__.__name__ + "("
|
||||
yield from self._format_items(object.maps, indent, allowance, context, level)
|
||||
yield ")"
|
||||
|
||||
_dispatch[_collections.ChainMap.__repr__] = _pprint_chain_map
|
||||
|
||||
def _pprint_deque(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield object.__class__.__name__ + "("
|
||||
if object.maxlen is not None:
|
||||
yield f"maxlen={object.maxlen}, "
|
||||
yield "["
|
||||
|
||||
yield from self._format_items(object, indent, allowance + 1, context, level)
|
||||
yield "])"
|
||||
|
||||
_dispatch[_collections.deque.__repr__] = _pprint_deque
|
||||
|
||||
def _pprint_user_dict(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield from self._format(object.data, indent, allowance, context, level - 1)
|
||||
|
||||
_dispatch[_collections.UserDict.__repr__] = _pprint_user_dict
|
||||
|
||||
def _pprint_user_list(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield from self._format(object.data, indent, allowance, context, level - 1)
|
||||
|
||||
_dispatch[_collections.UserList.__repr__] = _pprint_user_list
|
||||
|
||||
def _pprint_user_string(
|
||||
self,
|
||||
object: Any,
|
||||
indent: int,
|
||||
allowance: int,
|
||||
context: set[int],
|
||||
level: int,
|
||||
) -> Iterator[str]:
|
||||
yield from self._format(object.data, indent, allowance, context, level - 1)
|
||||
|
||||
_dispatch[_collections.UserString.__repr__] = _pprint_user_string
|
||||
|
||||
def _safe_repr(
|
||||
self, object: Any, context: set[int], maxlevels: int | None, level: int
|
||||
) -> str:
|
||||
typ = type(object)
|
||||
if typ in _builtin_scalars:
|
||||
return repr(object)
|
||||
|
||||
r = getattr(typ, "__repr__", None)
|
||||
|
||||
if sys.version_info >= (3, 15):
|
||||
is_frozendict = (
|
||||
issubclass(typ, frozendict) # noqa: F821
|
||||
and r is frozendict.__repr__ # noqa: F821
|
||||
)
|
||||
else:
|
||||
is_frozendict = False
|
||||
|
||||
if (issubclass(typ, dict) and r is dict.__repr__) or is_frozendict:
|
||||
if not object:
|
||||
if is_frozendict:
|
||||
return f"{typ.__name__}()"
|
||||
else:
|
||||
return "{}"
|
||||
objid = id(object)
|
||||
if maxlevels and level >= maxlevels:
|
||||
if is_frozendict:
|
||||
return f"{typ.__name__}({{...}})"
|
||||
else:
|
||||
return "{...}"
|
||||
if objid in context:
|
||||
return _recursion(object)
|
||||
context.add(objid)
|
||||
components: list[str] = []
|
||||
append = components.append
|
||||
level += 1
|
||||
for k, v in object.items():
|
||||
krepr = self._safe_repr(k, context, maxlevels, level)
|
||||
vrepr = self._safe_repr(v, context, maxlevels, level)
|
||||
append(f"{krepr}: {vrepr}")
|
||||
context.remove(objid)
|
||||
rep = "{{{}}}".format(", ".join(components))
|
||||
if is_frozendict:
|
||||
return f"{typ.__name__}({rep})"
|
||||
else:
|
||||
return rep
|
||||
|
||||
if (issubclass(typ, list) and r is list.__repr__) or (
|
||||
issubclass(typ, tuple) and r is tuple.__repr__
|
||||
):
|
||||
if issubclass(typ, list):
|
||||
if not object:
|
||||
return "[]"
|
||||
format = "[%s]"
|
||||
elif len(object) == 1:
|
||||
format = "(%s,)"
|
||||
else:
|
||||
if not object:
|
||||
return "()"
|
||||
format = "(%s)"
|
||||
objid = id(object)
|
||||
if maxlevels and level >= maxlevels:
|
||||
return format % "..."
|
||||
if objid in context:
|
||||
return _recursion(object)
|
||||
context.add(objid)
|
||||
components = []
|
||||
append = components.append
|
||||
level += 1
|
||||
for o in object:
|
||||
orepr = self._safe_repr(o, context, maxlevels, level)
|
||||
append(orepr)
|
||||
context.remove(objid)
|
||||
return format % ", ".join(components)
|
||||
|
||||
return repr(object)
|
||||
|
||||
|
||||
_builtin_scalars = frozenset(
|
||||
{str, bytes, bytearray, float, complex, bool, type(None), int}
|
||||
)
|
||||
|
||||
|
||||
def _recursion(object: Any) -> str:
|
||||
return f"<Recursion on {type(object).__name__} with id={id(object)}>"
|
||||
|
||||
|
||||
def _wrap_bytes_repr(object: Any, width: int, allowance: int) -> Iterator[str]:
|
||||
current = b""
|
||||
last = len(object) // 4 * 4
|
||||
for i in range(0, len(object), 4):
|
||||
part = object[i : i + 4]
|
||||
candidate = current + part
|
||||
if i == last:
|
||||
width -= allowance
|
||||
if len(repr(candidate)) > width:
|
||||
if current:
|
||||
yield repr(current)
|
||||
current = part
|
||||
else:
|
||||
current = candidate
|
||||
if current:
|
||||
yield repr(current)
|
||||
@@ -0,0 +1,155 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from itertools import islice
|
||||
import pprint
|
||||
import reprlib
|
||||
|
||||
|
||||
def _try_repr_or_str(obj: object) -> str:
|
||||
try:
|
||||
return repr(obj)
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
raise
|
||||
except BaseException:
|
||||
return f'{type(obj).__name__}("{obj}")'
|
||||
|
||||
|
||||
def _format_repr_exception(exc: BaseException, obj: object) -> str:
|
||||
try:
|
||||
exc_info = _try_repr_or_str(exc)
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
raise
|
||||
except BaseException as inner_exc:
|
||||
exc_info = f"unpresentable exception ({_try_repr_or_str(inner_exc)})"
|
||||
return (
|
||||
f"<[{exc_info} raised in repr()] {type(obj).__name__} object at 0x{id(obj):x}>"
|
||||
)
|
||||
|
||||
|
||||
def _ellipsize(s: str, maxsize: int) -> str:
|
||||
if len(s) > maxsize:
|
||||
i = max(0, (maxsize - 3) // 2)
|
||||
j = max(0, maxsize - 3 - i)
|
||||
return s[:i] + "..." + s[len(s) - j :]
|
||||
return s
|
||||
|
||||
|
||||
class SafeRepr(reprlib.Repr):
|
||||
"""
|
||||
repr.Repr that limits the resulting size of repr() and includes
|
||||
information on exceptions raised during the call.
|
||||
"""
|
||||
|
||||
def __init__(self, maxsize: int | None, use_ascii: bool = False) -> None:
|
||||
"""
|
||||
:param maxsize:
|
||||
If not None, will truncate the resulting repr to that specific size, using ellipsis
|
||||
somewhere in the middle to hide the extra text.
|
||||
If None, will not impose any size limits on the returning repr.
|
||||
"""
|
||||
super().__init__()
|
||||
# ``maxstring`` is used by the superclass, and needs to be an int; using a
|
||||
# very large number in case maxsize is None, meaning we want to disable
|
||||
# truncation.
|
||||
self.maxstring = maxsize if maxsize is not None else 1_000_000_000
|
||||
self.maxsize = maxsize
|
||||
self.use_ascii = use_ascii
|
||||
|
||||
def repr(self, x: object) -> str:
|
||||
try:
|
||||
if self.use_ascii:
|
||||
s = ascii(x)
|
||||
else:
|
||||
s = super().repr(x)
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
raise
|
||||
except BaseException as exc:
|
||||
s = _format_repr_exception(exc, x)
|
||||
if self.maxsize is not None:
|
||||
s = _ellipsize(s, self.maxsize)
|
||||
return s
|
||||
|
||||
def repr_instance(self, x: object, level: int) -> str:
|
||||
try:
|
||||
s = repr(x)
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
raise
|
||||
except BaseException as exc:
|
||||
s = _format_repr_exception(exc, x)
|
||||
if self.maxsize is not None:
|
||||
s = _ellipsize(s, self.maxsize)
|
||||
|
||||
return s
|
||||
|
||||
def repr_dict(self, x: dict[object, object], level: int) -> str:
|
||||
"""Represent a dict while preserving its insertion order.
|
||||
|
||||
Differs from ``reprlib.Repr.repr_dict`` by iterating directly over ``x``
|
||||
rather than using the stdlib's sorting helper.
|
||||
"""
|
||||
fillvalue = "..."
|
||||
n = len(x)
|
||||
if n == 0:
|
||||
return "{}"
|
||||
if level <= 0:
|
||||
return "{" + fillvalue + "}"
|
||||
newlevel = level - 1
|
||||
repr1 = self.repr1
|
||||
pieces = []
|
||||
for key in islice(x, self.maxdict):
|
||||
keyrepr = repr1(key, newlevel)
|
||||
valrepr = repr1(x[key], newlevel)
|
||||
pieces.append(f"{keyrepr}: {valrepr}")
|
||||
if n > self.maxdict:
|
||||
pieces.append(fillvalue)
|
||||
return "{" + ", ".join(pieces) + "}"
|
||||
|
||||
|
||||
def safeformat(obj: object) -> str:
|
||||
"""Return a pretty printed string for the given object.
|
||||
|
||||
Failing __repr__ functions of user instances will be represented
|
||||
with a short exception info.
|
||||
"""
|
||||
try:
|
||||
return pprint.pformat(obj)
|
||||
except Exception as exc:
|
||||
return _format_repr_exception(exc, obj)
|
||||
|
||||
|
||||
# Maximum size of overall repr of objects to display during assertion errors.
|
||||
DEFAULT_REPR_MAX_SIZE = 240
|
||||
|
||||
|
||||
def saferepr(
|
||||
obj: object, maxsize: int | None = DEFAULT_REPR_MAX_SIZE, use_ascii: bool = False
|
||||
) -> str:
|
||||
"""Return a size-limited safe repr-string for the given object.
|
||||
|
||||
Failing __repr__ functions of user instances will be represented
|
||||
with a short exception info and 'saferepr' generally takes
|
||||
care to never raise exceptions itself.
|
||||
|
||||
This function is a wrapper around the Repr/reprlib functionality of the
|
||||
stdlib.
|
||||
"""
|
||||
return SafeRepr(maxsize, use_ascii).repr(obj)
|
||||
|
||||
|
||||
def saferepr_unlimited(obj: object, use_ascii: bool = True) -> str:
|
||||
"""Return an unlimited-size safe repr-string for the given object.
|
||||
|
||||
As with saferepr, failing __repr__ functions of user instances
|
||||
will be represented with a short exception info.
|
||||
|
||||
This function is a wrapper around simple repr.
|
||||
|
||||
Note: a cleaner solution would be to alter ``saferepr``this way
|
||||
when maxsize=None, but that might affect some other code.
|
||||
"""
|
||||
try:
|
||||
if use_ascii:
|
||||
return ascii(obj)
|
||||
return repr(obj)
|
||||
except Exception as exc:
|
||||
return _format_repr_exception(exc, obj)
|
||||
@@ -0,0 +1,258 @@
|
||||
"""Helper functions for writing to terminals and files."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Sequence
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from typing import final
|
||||
from typing import Literal
|
||||
from typing import TextIO
|
||||
|
||||
import pygments
|
||||
from pygments.formatters.terminal import TerminalFormatter
|
||||
from pygments.lexer import Lexer
|
||||
from pygments.lexers.diff import DiffLexer
|
||||
from pygments.lexers.python import PythonLexer
|
||||
|
||||
from ..compat import assert_never
|
||||
from .wcwidth import wcswidth
|
||||
|
||||
|
||||
# This code was initially copied from py 1.8.1, file _io/terminalwriter.py.
|
||||
|
||||
|
||||
def get_terminal_width() -> int:
|
||||
width, _ = shutil.get_terminal_size(fallback=(80, 24))
|
||||
|
||||
# The Windows get_terminal_size may be bogus, let's sanify a bit.
|
||||
if width < 40:
|
||||
width = 80
|
||||
|
||||
return width
|
||||
|
||||
|
||||
def should_do_markup(file: TextIO) -> bool:
|
||||
if os.environ.get("PY_COLORS") == "1":
|
||||
return True
|
||||
if os.environ.get("PY_COLORS") == "0":
|
||||
return False
|
||||
if os.environ.get("NO_COLOR"):
|
||||
return False
|
||||
if os.environ.get("FORCE_COLOR"):
|
||||
return True
|
||||
return (
|
||||
hasattr(file, "isatty") and file.isatty() and os.environ.get("TERM") != "dumb"
|
||||
)
|
||||
|
||||
|
||||
@final
|
||||
class TerminalWriter:
|
||||
_esctable = dict(
|
||||
black=30,
|
||||
red=31,
|
||||
green=32,
|
||||
yellow=33,
|
||||
blue=34,
|
||||
purple=35,
|
||||
cyan=36,
|
||||
white=37,
|
||||
Black=40,
|
||||
Red=41,
|
||||
Green=42,
|
||||
Yellow=43,
|
||||
Blue=44,
|
||||
Purple=45,
|
||||
Cyan=46,
|
||||
White=47,
|
||||
bold=1,
|
||||
light=2,
|
||||
blink=5,
|
||||
invert=7,
|
||||
)
|
||||
|
||||
def __init__(self, file: TextIO | None = None) -> None:
|
||||
if file is None:
|
||||
file = sys.stdout
|
||||
if hasattr(file, "isatty") and file.isatty() and sys.platform == "win32":
|
||||
try:
|
||||
import colorama
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
file = colorama.AnsiToWin32(file).stream
|
||||
assert file is not None
|
||||
self._file = file
|
||||
self.hasmarkup = should_do_markup(file)
|
||||
self._current_line = ""
|
||||
self._terminal_width: int | None = None
|
||||
self.code_highlight = True
|
||||
|
||||
@property
|
||||
def fullwidth(self) -> int:
|
||||
if self._terminal_width is not None:
|
||||
return self._terminal_width
|
||||
return get_terminal_width()
|
||||
|
||||
@fullwidth.setter
|
||||
def fullwidth(self, value: int) -> None:
|
||||
self._terminal_width = value
|
||||
|
||||
@property
|
||||
def width_of_current_line(self) -> int:
|
||||
"""Return an estimate of the width so far in the current line."""
|
||||
return wcswidth(self._current_line)
|
||||
|
||||
def markup(self, text: str, **markup: bool) -> str:
|
||||
for name in markup:
|
||||
if name not in self._esctable:
|
||||
raise ValueError(f"unknown markup: {name!r}")
|
||||
if self.hasmarkup:
|
||||
esc = [self._esctable[name] for name, on in markup.items() if on]
|
||||
if esc:
|
||||
text = "".join(f"\x1b[{cod}m" for cod in esc) + text + "\x1b[0m"
|
||||
return text
|
||||
|
||||
def sep(
|
||||
self,
|
||||
sepchar: str,
|
||||
title: str | None = None,
|
||||
fullwidth: int | None = None,
|
||||
**markup: bool,
|
||||
) -> None:
|
||||
if fullwidth is None:
|
||||
fullwidth = self.fullwidth
|
||||
# The goal is to have the line be as long as possible
|
||||
# under the condition that len(line) <= fullwidth.
|
||||
if sys.platform == "win32":
|
||||
# If we print in the last column on windows we are on a
|
||||
# new line but there is no way to verify/neutralize this
|
||||
# (we may not know the exact line width).
|
||||
# So let's be defensive to avoid empty lines in the output.
|
||||
fullwidth -= 1
|
||||
if title is not None:
|
||||
# we want 2 + 2*len(fill) + len(title) <= fullwidth
|
||||
# i.e. 2 + 2*len(sepchar)*N + len(title) <= fullwidth
|
||||
# 2*len(sepchar)*N <= fullwidth - len(title) - 2
|
||||
# N <= (fullwidth - len(title) - 2) // (2*len(sepchar))
|
||||
N = max((fullwidth - len(title) - 2) // (2 * len(sepchar)), 1)
|
||||
fill = sepchar * N
|
||||
line = f"{fill} {title} {fill}"
|
||||
else:
|
||||
# we want len(sepchar)*N <= fullwidth
|
||||
# i.e. N <= fullwidth // len(sepchar)
|
||||
line = sepchar * (fullwidth // len(sepchar))
|
||||
# In some situations there is room for an extra sepchar at the right,
|
||||
# in particular if we consider that with a sepchar like "_ " the
|
||||
# trailing space is not important at the end of the line.
|
||||
if len(line) + len(sepchar.rstrip()) <= fullwidth:
|
||||
line += sepchar.rstrip()
|
||||
|
||||
self.line(line, **markup)
|
||||
|
||||
def write(self, msg: str, *, flush: bool = False, **markup: bool) -> None:
|
||||
if msg:
|
||||
current_line = msg.rsplit("\n", 1)[-1]
|
||||
if "\n" in msg:
|
||||
self._current_line = current_line
|
||||
else:
|
||||
self._current_line += current_line
|
||||
|
||||
msg = self.markup(msg, **markup)
|
||||
|
||||
self.write_raw(msg, flush=flush)
|
||||
|
||||
def write_raw(self, msg: str, *, flush: bool = False) -> None:
|
||||
try:
|
||||
self._file.write(msg)
|
||||
except UnicodeEncodeError:
|
||||
# Some environments don't support printing general Unicode
|
||||
# strings, due to misconfiguration or otherwise; in that case,
|
||||
# print the string escaped to ASCII.
|
||||
# When the Unicode situation improves we should consider
|
||||
# letting the error propagate instead of masking it (see #7475
|
||||
# for one brief attempt).
|
||||
msg = msg.encode("unicode-escape").decode("ascii")
|
||||
self._file.write(msg)
|
||||
|
||||
if flush:
|
||||
self.flush()
|
||||
|
||||
def line(self, s: str = "", **markup: bool) -> None:
|
||||
self.write(s, **markup)
|
||||
self.write("\n")
|
||||
|
||||
def flush(self) -> None:
|
||||
self._file.flush()
|
||||
|
||||
def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> None:
|
||||
"""Write lines of source code possibly highlighted.
|
||||
|
||||
Keeping this private for now because the API is clunky. We should discuss how
|
||||
to evolve the terminal writer so we can have more precise color support, for example
|
||||
being able to write part of a line in one color and the rest in another, and so on.
|
||||
"""
|
||||
if indents and len(indents) != len(lines):
|
||||
raise ValueError(
|
||||
f"indents size ({len(indents)}) should have same size as lines ({len(lines)})"
|
||||
)
|
||||
if not indents:
|
||||
indents = [""] * len(lines)
|
||||
source = "\n".join(lines)
|
||||
new_lines = self._highlight(source).splitlines()
|
||||
# Would be better to strict=True but that fails some CI jobs.
|
||||
for indent, new_line in zip(indents, new_lines, strict=False):
|
||||
self.line(indent + new_line)
|
||||
|
||||
def _get_pygments_lexer(self, lexer: Literal["python", "diff"]) -> Lexer:
|
||||
if lexer == "python":
|
||||
return PythonLexer()
|
||||
elif lexer == "diff":
|
||||
return DiffLexer()
|
||||
else:
|
||||
assert_never(lexer)
|
||||
|
||||
def _get_pygments_formatter(self) -> TerminalFormatter:
|
||||
from _pytest.config.exceptions import UsageError
|
||||
|
||||
theme = os.getenv("PYTEST_THEME")
|
||||
theme_mode = os.getenv("PYTEST_THEME_MODE", "dark")
|
||||
|
||||
try:
|
||||
return TerminalFormatter(bg=theme_mode, style=theme)
|
||||
except pygments.util.ClassNotFound as e:
|
||||
raise UsageError(
|
||||
f"PYTEST_THEME environment variable has an invalid value: '{theme}'. "
|
||||
"Hint: See available pygments styles with `pygmentize -L styles`."
|
||||
) from e
|
||||
except pygments.util.OptionError as e:
|
||||
raise UsageError(
|
||||
f"PYTEST_THEME_MODE environment variable has an invalid value: '{theme_mode}'. "
|
||||
"The allowed values are 'dark' (default) and 'light'."
|
||||
) from e
|
||||
|
||||
def _highlight(
|
||||
self, source: str, lexer: Literal["diff", "python"] = "python"
|
||||
) -> str:
|
||||
"""Highlight the given source if we have markup support."""
|
||||
if not source or not self.hasmarkup or not self.code_highlight:
|
||||
return source
|
||||
|
||||
pygments_lexer = self._get_pygments_lexer(lexer)
|
||||
pygments_formatter = self._get_pygments_formatter()
|
||||
|
||||
highlighted: str = pygments.highlight(
|
||||
source, pygments_lexer, pygments_formatter
|
||||
)
|
||||
# pygments terminal formatter may add a newline when there wasn't one.
|
||||
# We don't want this, remove.
|
||||
if highlighted[-1] == "\n" and source[-1] != "\n":
|
||||
highlighted = highlighted[:-1]
|
||||
|
||||
# Some lexers will not set the initial color explicitly
|
||||
# which may lead to the previous color being propagated to the
|
||||
# start of the expression, so reset first.
|
||||
highlighted = "\x1b[0m" + highlighted
|
||||
|
||||
return highlighted
|
||||
@@ -0,0 +1,57 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
import unicodedata
|
||||
|
||||
|
||||
@lru_cache(100)
|
||||
def wcwidth(c: str) -> int:
|
||||
"""Determine how many columns are needed to display a character in a terminal.
|
||||
|
||||
Returns -1 if the character is not printable.
|
||||
Returns 0, 1 or 2 for other characters.
|
||||
"""
|
||||
o = ord(c)
|
||||
|
||||
# ASCII fast path.
|
||||
if 0x20 <= o < 0x07F:
|
||||
return 1
|
||||
|
||||
# Some Cf/Zp/Zl characters which should be zero-width.
|
||||
if (
|
||||
o == 0x0000
|
||||
or 0x200B <= o <= 0x200F
|
||||
or 0x2028 <= o <= 0x202E
|
||||
or 0x2060 <= o <= 0x2063
|
||||
):
|
||||
return 0
|
||||
|
||||
category = unicodedata.category(c)
|
||||
|
||||
# Control characters.
|
||||
if category == "Cc":
|
||||
return -1
|
||||
|
||||
# Combining characters with zero width.
|
||||
if category in ("Me", "Mn"):
|
||||
return 0
|
||||
|
||||
# Full/Wide east asian characters.
|
||||
if unicodedata.east_asian_width(c) in ("F", "W"):
|
||||
return 2
|
||||
|
||||
return 1
|
||||
|
||||
|
||||
def wcswidth(s: str) -> int:
|
||||
"""Determine how many columns are needed to display a string in a terminal.
|
||||
|
||||
Returns -1 if the string contains non-printable characters.
|
||||
"""
|
||||
width = 0
|
||||
for c in unicodedata.normalize("NFC", s):
|
||||
wc = wcwidth(c)
|
||||
if wc < 0:
|
||||
return -1
|
||||
width += wc
|
||||
return width
|
||||
Whitespace-only changes.
@@ -0,0 +1,119 @@
|
||||
"""create errno-specific classes for IO or os calls."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
import errno
|
||||
import os
|
||||
import sys
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TypeVar
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import ParamSpec
|
||||
|
||||
P = ParamSpec("P")
|
||||
|
||||
R = TypeVar("R")
|
||||
|
||||
|
||||
class Error(EnvironmentError):
|
||||
def __repr__(self) -> str:
|
||||
return "{}.{} {!r}: {} ".format(
|
||||
self.__class__.__module__,
|
||||
self.__class__.__name__,
|
||||
self.__class__.__doc__,
|
||||
" ".join(map(str, self.args)),
|
||||
# repr(self.args)
|
||||
)
|
||||
|
||||
def __str__(self) -> str:
|
||||
s = "[{}]: {}".format(
|
||||
self.__class__.__doc__,
|
||||
" ".join(map(str, self.args)),
|
||||
)
|
||||
return s
|
||||
|
||||
|
||||
_winerrnomap = {
|
||||
2: errno.ENOENT,
|
||||
3: errno.ENOENT,
|
||||
17: errno.EEXIST,
|
||||
18: errno.EXDEV,
|
||||
13: errno.EBUSY, # empty cd drive, but ENOMEDIUM seems unavailable
|
||||
22: errno.ENOTDIR,
|
||||
20: errno.ENOTDIR,
|
||||
267: errno.ENOTDIR,
|
||||
5: errno.EACCES, # anything better?
|
||||
}
|
||||
|
||||
|
||||
class ErrorMaker:
|
||||
"""lazily provides Exception classes for each possible POSIX errno
|
||||
(as defined per the 'errno' module). All such instances
|
||||
subclass EnvironmentError.
|
||||
"""
|
||||
|
||||
_errno2class: dict[int, type[Error]] = {}
|
||||
|
||||
def __getattr__(self, name: str) -> type[Error]:
|
||||
if name[0] == "_":
|
||||
raise AttributeError(name)
|
||||
eno = getattr(errno, name)
|
||||
cls = self._geterrnoclass(eno)
|
||||
setattr(self, name, cls)
|
||||
return cls
|
||||
|
||||
def _geterrnoclass(self, eno: int) -> type[Error]:
|
||||
try:
|
||||
return self._errno2class[eno]
|
||||
except KeyError:
|
||||
clsname = errno.errorcode.get(eno, f"UnknownErrno{eno}")
|
||||
errorcls = type(
|
||||
clsname,
|
||||
(Error,),
|
||||
{"__module__": "py.error", "__doc__": os.strerror(eno)},
|
||||
)
|
||||
self._errno2class[eno] = errorcls
|
||||
return errorcls
|
||||
|
||||
def checked_call(
|
||||
self, func: Callable[P, R], *args: P.args, **kwargs: P.kwargs
|
||||
) -> R:
|
||||
"""Call a function and raise an errno-exception if applicable."""
|
||||
__tracebackhide__ = True
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except Error:
|
||||
raise
|
||||
except OSError as value:
|
||||
if not hasattr(value, "errno"):
|
||||
raise
|
||||
if sys.platform == "win32":
|
||||
try:
|
||||
# error: Invalid index type "Optional[int]" for "dict[int, int]"; expected type "int" [index]
|
||||
# OK to ignore because we catch the KeyError below.
|
||||
cls = self._geterrnoclass(_winerrnomap[value.errno]) # type:ignore[index]
|
||||
except KeyError:
|
||||
raise value
|
||||
else:
|
||||
# we are not on Windows, or we got a proper OSError
|
||||
if value.errno is None:
|
||||
cls = type(
|
||||
"UnknownErrnoNone",
|
||||
(Error,),
|
||||
{"__module__": "py.error", "__doc__": None},
|
||||
)
|
||||
else:
|
||||
cls = self._geterrnoclass(value.errno)
|
||||
|
||||
raise cls(f"{func.__name__}{args!r}")
|
||||
|
||||
|
||||
_error_maker = ErrorMaker()
|
||||
checked_call = _error_maker.checked_call
|
||||
|
||||
|
||||
def __getattr__(attr: str) -> type[Error]:
|
||||
return getattr(_error_maker, attr) # type: ignore[no-any-return]
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,24 @@
|
||||
# file generated by vcs-versioning
|
||||
# don't change, don't track in version control
|
||||
from __future__ import annotations
|
||||
|
||||
__all__ = [
|
||||
"__version__",
|
||||
"__version_tuple__",
|
||||
"version",
|
||||
"version_tuple",
|
||||
"__commit_id__",
|
||||
"commit_id",
|
||||
]
|
||||
|
||||
version: str
|
||||
__version__: str
|
||||
__version_tuple__: tuple[int | str, ...]
|
||||
version_tuple: tuple[int | str, ...]
|
||||
commit_id: str | None
|
||||
__commit_id__: str | None
|
||||
|
||||
__version__ = version = '9.2.0'
|
||||
__version_tuple__ = version_tuple = (9, 2, 0)
|
||||
|
||||
__commit_id__ = commit_id = None
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,258 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Support for presenting detailed information in failing assertions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing import Protocol
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from _pytest.assertion import rewrite
|
||||
from _pytest.assertion import truncate
|
||||
from _pytest.assertion import util
|
||||
from _pytest.assertion._typing import _AssertionTextDiffStyle
|
||||
from _pytest.assertion._typing import NO_TRUNCATION_BUDGET
|
||||
from _pytest.assertion._typing import TruncationBudget
|
||||
from _pytest.assertion.rewrite import assertstate_key
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import hookimpl
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.nodes import Item
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from _pytest.main import Session
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("debugconfig")
|
||||
group.addoption(
|
||||
"--assert",
|
||||
action="store",
|
||||
dest="assertmode",
|
||||
choices=("rewrite", "plain"),
|
||||
default="rewrite",
|
||||
metavar="MODE",
|
||||
help=(
|
||||
"Control assertion debugging tools.\n"
|
||||
"'plain' performs no assertion debugging.\n"
|
||||
"'rewrite' (the default) rewrites assert statements in test modules"
|
||||
" on import to provide assert expression information."
|
||||
),
|
||||
)
|
||||
parser.addini(
|
||||
"enable_assertion_pass_hook",
|
||||
type="bool",
|
||||
default=False,
|
||||
help="Enables the pytest_assertion_pass hook. "
|
||||
"Make sure to delete any previously generated pyc cache files.",
|
||||
)
|
||||
|
||||
# ``int | str`` (not plain ``int``) for backward compatibility: INI files
|
||||
# and ``-o`` overrides provide the value as a string.
|
||||
parser.addini(
|
||||
"truncation_limit_lines",
|
||||
type=int | str,
|
||||
default=None,
|
||||
help="Set threshold of LINES after which truncation will take effect",
|
||||
)
|
||||
parser.addini(
|
||||
"truncation_limit_chars",
|
||||
type=int | str,
|
||||
default=None,
|
||||
help=("Set threshold of CHARS after which truncation will take effect"),
|
||||
)
|
||||
parser.addini(
|
||||
"assertion_text_diff_style",
|
||||
type=_AssertionTextDiffStyle,
|
||||
default="ndiff",
|
||||
help="Choose how pytest renders diffs for string equality assertions",
|
||||
)
|
||||
|
||||
Config._add_verbosity_ini(
|
||||
parser,
|
||||
Config.VERBOSITY_ASSERTIONS,
|
||||
help=(
|
||||
"Specify a verbosity level for assertions, overriding the main level. "
|
||||
"Higher levels will provide more detailed explanation when an assertion fails."
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def pytest_configure(config: Config) -> None:
|
||||
# Eagerly validate the value; it is only read lazily when an assertion fails.
|
||||
config.getini("assertion_text_diff_style")
|
||||
|
||||
|
||||
def register_assert_rewrite(*names: str) -> None:
|
||||
"""Register one or more module names to be rewritten on import.
|
||||
|
||||
This function will make sure that this module or all modules inside
|
||||
the package will get their assert statements rewritten.
|
||||
Thus you should make sure to call this before the module is
|
||||
actually imported, usually in your __init__.py if you are a plugin
|
||||
using a package.
|
||||
|
||||
:param names: The module names to register.
|
||||
"""
|
||||
for name in names:
|
||||
if not isinstance(name, str):
|
||||
msg = "expected module names as *args, got {0} instead" # type: ignore[unreachable]
|
||||
raise TypeError(msg.format(repr(names)))
|
||||
rewrite_hook: RewriteHook
|
||||
for hook in sys.meta_path:
|
||||
if isinstance(hook, rewrite.AssertionRewritingHook):
|
||||
rewrite_hook = hook
|
||||
break
|
||||
else:
|
||||
rewrite_hook = DummyRewriteHook()
|
||||
rewrite_hook.mark_rewrite(*names)
|
||||
|
||||
|
||||
class RewriteHook(Protocol):
|
||||
def mark_rewrite(self, *names: str) -> None: ...
|
||||
|
||||
|
||||
class DummyRewriteHook:
|
||||
"""A no-op import hook for when rewriting is disabled."""
|
||||
|
||||
def mark_rewrite(self, *names: str) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class AssertionState:
|
||||
"""State for the assertion plugin."""
|
||||
|
||||
def __init__(self, config: Config, mode) -> None:
|
||||
self.mode = mode
|
||||
self.trace = config.trace.root.get("assertion")
|
||||
self.hook: rewrite.AssertionRewritingHook | None = None
|
||||
|
||||
|
||||
def install_importhook(config: Config) -> rewrite.AssertionRewritingHook:
|
||||
"""Try to install the rewrite hook, raise SystemError if it fails."""
|
||||
config.stash[assertstate_key] = AssertionState(config, "rewrite")
|
||||
config.stash[assertstate_key].hook = hook = rewrite.AssertionRewritingHook(config)
|
||||
sys.meta_path.insert(0, hook)
|
||||
config.stash[assertstate_key].trace("installed rewrite import hook")
|
||||
|
||||
def undo() -> None:
|
||||
hook = config.stash[assertstate_key].hook
|
||||
if hook is not None and hook in sys.meta_path:
|
||||
sys.meta_path.remove(hook)
|
||||
|
||||
config.add_cleanup(undo)
|
||||
return hook
|
||||
|
||||
|
||||
def pytest_collection(session: Session) -> None:
|
||||
# This hook is only called when test modules are collected
|
||||
# so for example not in the managing process of pytest-xdist
|
||||
# (which does not collect test modules).
|
||||
assertstate = session.config.stash.get(assertstate_key, None)
|
||||
if assertstate:
|
||||
if assertstate.hook is not None:
|
||||
assertstate.hook.set_session(session)
|
||||
|
||||
|
||||
@hookimpl(wrapper=True, tryfirst=True)
|
||||
def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]:
|
||||
"""Setup the pytest_assertrepr_compare and pytest_assertion_pass hooks.
|
||||
|
||||
The rewrite module will use util._reprcompare if it exists to use custom
|
||||
reporting via the pytest_assertrepr_compare hook. This sets up this custom
|
||||
comparison for the test.
|
||||
"""
|
||||
ihook = item.ihook
|
||||
|
||||
def callbinrepr(op, left: object, right: object) -> str | None:
|
||||
"""Call the pytest_assertrepr_compare hook and prepare the result.
|
||||
|
||||
This uses the first result from the hook and then ensures the
|
||||
following:
|
||||
* Overly verbose explanations are truncated unless configured otherwise
|
||||
(eg. if running in verbose mode).
|
||||
* Embedded newlines are escaped to help util.format_explanation()
|
||||
later.
|
||||
* If the rewrite mode is used embedded %-characters are replaced
|
||||
to protect later % formatting.
|
||||
|
||||
The result can be formatted by util.format_explanation() for
|
||||
pretty printing.
|
||||
"""
|
||||
hook_result = ihook.pytest_assertrepr_compare(
|
||||
config=item.config, op=op, left=left, right=right
|
||||
)
|
||||
for new_expl in hook_result:
|
||||
if new_expl:
|
||||
new_expl = truncate.materialize_with_truncation(new_expl, item.config)
|
||||
# A truthy-but-empty iterable materialises to [], so re-check.
|
||||
if new_expl:
|
||||
new_expl = [line.replace("\n", "\\n") for line in new_expl]
|
||||
res = "\n~".join(new_expl)
|
||||
if item.config.getvalue("assertmode") == "rewrite":
|
||||
res = res.replace("%", "%%")
|
||||
return res
|
||||
return None
|
||||
|
||||
saved_assert_hooks = util._reprcompare, util._assertion_pass
|
||||
util._reprcompare = callbinrepr
|
||||
util._config = item.config
|
||||
|
||||
if ihook.pytest_assertion_pass.get_hookimpls():
|
||||
|
||||
def call_assertion_pass_hook(lineno: int, orig: str, expl: str) -> None:
|
||||
ihook.pytest_assertion_pass(item=item, lineno=lineno, orig=orig, expl=expl)
|
||||
|
||||
util._assertion_pass = call_assertion_pass_hook
|
||||
|
||||
try:
|
||||
return (yield)
|
||||
finally:
|
||||
util._reprcompare, util._assertion_pass = saved_assert_hooks
|
||||
util._config = None
|
||||
|
||||
|
||||
def pytest_sessionfinish(session: Session) -> None:
|
||||
assertstate = session.config.stash.get(assertstate_key, None)
|
||||
if assertstate:
|
||||
if assertstate.hook is not None:
|
||||
assertstate.hook.set_session(None)
|
||||
|
||||
|
||||
def pytest_assertrepr_compare(
|
||||
config: Config, op: str, left: Any, right: Any
|
||||
) -> list[str] | None:
|
||||
if config.pluginmanager.has_plugin("terminalreporter"):
|
||||
highlighter = config.get_terminal_writer()._highlight
|
||||
else:
|
||||
# Keep it plaintext when not using terminalrepoterer (#14377).
|
||||
highlighter = util.dummy_highlighter
|
||||
# When truncation is going to clip the explanation downstream, cap the
|
||||
# comparison helpers' formatting at what the truncator will actually pull
|
||||
# (the raw limits plus the footer slack) so no effort is spent formatting
|
||||
# lines/chars that would be dropped anyway.
|
||||
should_truncate, base_budget = truncate._get_truncation_parameters(config)
|
||||
if should_truncate:
|
||||
truncation_budget = TruncationBudget(
|
||||
max_lines=base_budget.max_lines + truncate.TRUNCATION_FOOTER_LINES + 1
|
||||
if base_budget.max_lines > 0
|
||||
else 0,
|
||||
max_chars=base_budget.max_chars + truncate.TRUNCATION_FOOTER_CHARS
|
||||
if base_budget.max_chars > 0
|
||||
else 0,
|
||||
)
|
||||
else:
|
||||
truncation_budget = NO_TRUNCATION_BUDGET
|
||||
lines = util.assertrepr_compare(
|
||||
op=op,
|
||||
left=left,
|
||||
right=right,
|
||||
verbose=config.get_verbosity(Config.VERBOSITY_ASSERTIONS),
|
||||
highlighter=highlighter,
|
||||
assertion_text_diff_style=config.getini("assertion_text_diff_style"),
|
||||
truncation_budget=truncation_budget,
|
||||
)
|
||||
return truncate.materialize_with_truncation(lines, config) or None
|
||||
@@ -0,0 +1,139 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator
|
||||
import dataclasses
|
||||
import pprint
|
||||
|
||||
from _pytest.assertion._compare_mapping import _compare_eq_mapping
|
||||
from _pytest.assertion._compare_sequence import _compare_eq_iterable
|
||||
from _pytest.assertion._compare_sequence import _compare_eq_sequence
|
||||
from _pytest.assertion._compare_set import _compare_eq_set
|
||||
from _pytest.assertion._guards import has_default_eq
|
||||
from _pytest.assertion._guards import isattrs
|
||||
from _pytest.assertion._guards import isdatacls
|
||||
from _pytest.assertion._guards import isiterable
|
||||
from _pytest.assertion._guards import ismapping
|
||||
from _pytest.assertion._guards import isnamedtuple
|
||||
from _pytest.assertion._guards import issequence
|
||||
from _pytest.assertion._guards import isset
|
||||
from _pytest.assertion._guards import istext
|
||||
from _pytest.assertion._typing import _AssertionTextDiffStyle
|
||||
from _pytest.assertion._typing import _HighlightFunc
|
||||
from _pytest.assertion._typing import NO_TRUNCATION_BUDGET
|
||||
from _pytest.assertion._typing import TruncationBudget
|
||||
from _pytest.assertion.compare_text import _compare_eq_text
|
||||
|
||||
|
||||
def _compare_eq_any(
|
||||
left: object,
|
||||
right: object,
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int,
|
||||
assertion_text_diff_style: _AssertionTextDiffStyle,
|
||||
truncation_budget: TruncationBudget = NO_TRUNCATION_BUDGET,
|
||||
) -> Iterator[str]:
|
||||
"""Yield the per-line explanation for ``left == right`` (without summary).
|
||||
|
||||
Yields nothing when no specialised explanation applies, so consumers
|
||||
can stream the output and bail out early (e.g. for truncation) without
|
||||
materialising the entire diff first.
|
||||
"""
|
||||
if istext(left) and istext(right):
|
||||
yield from _compare_eq_text(
|
||||
left,
|
||||
right,
|
||||
highlighter,
|
||||
verbose,
|
||||
assertion_text_diff_style,
|
||||
truncation_budget,
|
||||
)
|
||||
else:
|
||||
from _pytest.approx import Approx
|
||||
|
||||
# Although the common order should be obtained == approx(...), allow both ways.
|
||||
if isinstance(right, Approx):
|
||||
yield from right._repr_compare(left)
|
||||
elif isinstance(left, Approx):
|
||||
yield from left._repr_compare(right)
|
||||
elif type(left) is type(right) and (
|
||||
isdatacls(left) or isattrs(left) or isnamedtuple(left)
|
||||
):
|
||||
# Note: unlike dataclasses/attrs, namedtuples compare only the
|
||||
# field values, not the type or field names. But this branch
|
||||
# intentionally only handles the same-type case, which was often
|
||||
# used in older code bases before dataclasses/attrs were available.
|
||||
yield from _compare_eq_cls(
|
||||
left,
|
||||
right,
|
||||
highlighter,
|
||||
verbose,
|
||||
assertion_text_diff_style,
|
||||
)
|
||||
elif issequence(left) and issequence(right):
|
||||
yield from _compare_eq_sequence(left, right, highlighter, verbose)
|
||||
elif isset(left) and isset(right):
|
||||
yield from _compare_eq_set(left, right, highlighter, verbose)
|
||||
elif ismapping(left) and ismapping(right):
|
||||
yield from _compare_eq_mapping(
|
||||
left, right, highlighter, verbose, truncation_budget
|
||||
)
|
||||
|
||||
if isiterable(left) and isiterable(right):
|
||||
yield from _compare_eq_iterable(
|
||||
left, right, highlighter, verbose, truncation_budget
|
||||
)
|
||||
|
||||
|
||||
def _compare_eq_cls(
|
||||
left: object,
|
||||
right: object,
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int,
|
||||
assertion_text_diff_style: _AssertionTextDiffStyle,
|
||||
) -> Iterator[str]:
|
||||
if not has_default_eq(left):
|
||||
return
|
||||
if isdatacls(left):
|
||||
all_fields = dataclasses.fields(left)
|
||||
fields_to_check = [info.name for info in all_fields if info.compare]
|
||||
elif isattrs(left):
|
||||
all_fields = left.__attrs_attrs__ # type: ignore[attr-defined]
|
||||
fields_to_check = [field.name for field in all_fields if getattr(field, "eq")]
|
||||
elif isnamedtuple(left):
|
||||
fields_to_check = left._fields # type: ignore[attr-defined]
|
||||
else:
|
||||
assert False
|
||||
|
||||
indent = " "
|
||||
same = []
|
||||
diff = []
|
||||
for field in fields_to_check:
|
||||
if getattr(left, field) == getattr(right, field):
|
||||
same.append(field)
|
||||
else:
|
||||
diff.append(field)
|
||||
|
||||
if same or diff:
|
||||
yield ""
|
||||
if same and verbose < 2:
|
||||
yield f"Omitting {len(same)} identical items, use -vv to show"
|
||||
elif same:
|
||||
yield "Matching attributes:"
|
||||
yield from highlighter(pprint.pformat(same)).splitlines()
|
||||
if diff:
|
||||
yield "Differing attributes:"
|
||||
yield from highlighter(pprint.pformat(diff)).splitlines()
|
||||
for field in diff:
|
||||
field_left = getattr(left, field)
|
||||
field_right = getattr(right, field)
|
||||
yield ""
|
||||
yield f"Drill down into differing attribute {field}:"
|
||||
yield f"{indent}{field}: {highlighter(repr(field_left))} != {highlighter(repr(field_right))}"
|
||||
for line in _compare_eq_any(
|
||||
field_left,
|
||||
field_right,
|
||||
highlighter,
|
||||
verbose,
|
||||
assertion_text_diff_style,
|
||||
):
|
||||
yield indent + line
|
||||
@@ -0,0 +1,76 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Collection
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import Mapping
|
||||
import heapq
|
||||
import pprint
|
||||
|
||||
from _pytest._io.pprint import _safe_key
|
||||
from _pytest._io.saferepr import saferepr
|
||||
from _pytest.assertion._typing import _HighlightFunc
|
||||
from _pytest.assertion._typing import NO_TRUNCATION_BUDGET
|
||||
from _pytest.assertion._typing import TruncationBudget
|
||||
|
||||
|
||||
def _compare_eq_mapping(
|
||||
left: Mapping[object, object],
|
||||
right: Mapping[object, object],
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
truncation_budget: TruncationBudget = NO_TRUNCATION_BUDGET,
|
||||
) -> Iterator[str]:
|
||||
# Use the key views rather than ``set(left)``/``set(right)``: their set
|
||||
# operations resolve membership through the mappings themselves, so a
|
||||
# mapping with its own key equivalence (case-insensitive keys, for
|
||||
# example) is compared the way it compares itself.
|
||||
keys_left = left.keys()
|
||||
keys_right = right.keys()
|
||||
common = keys_left & keys_right
|
||||
same = {k: left[k] for k in common if left[k] == right[k]}
|
||||
if same and verbose < 2:
|
||||
yield f"Omitting {len(same)} identical items, use -vv to show"
|
||||
elif same:
|
||||
yield "Common items:"
|
||||
yield from highlighter(pprint.pformat(same)).splitlines()
|
||||
diff = {k for k in common if left[k] != right[k]}
|
||||
if diff:
|
||||
yield "Differing items:"
|
||||
for k in diff:
|
||||
yield (
|
||||
highlighter(saferepr({k: left[k]}))
|
||||
+ " != "
|
||||
+ highlighter(saferepr({k: right[k]}))
|
||||
)
|
||||
extra_left = keys_left - keys_right
|
||||
len_extra_left = len(extra_left)
|
||||
if len_extra_left:
|
||||
yield f"Left contains {len_extra_left} more item{'' if len_extra_left == 1 else 's'}:"
|
||||
yield from _format_extra_items(left, extra_left, highlighter, truncation_budget)
|
||||
extra_right = keys_right - keys_left
|
||||
len_extra_right = len(extra_right)
|
||||
if len_extra_right:
|
||||
yield f"Right contains {len_extra_right} more item{'' if len_extra_right == 1 else 's'}:"
|
||||
yield from _format_extra_items(
|
||||
right, extra_right, highlighter, truncation_budget
|
||||
)
|
||||
|
||||
|
||||
def _format_extra_items(
|
||||
mapping: Mapping[object, object],
|
||||
keys: Collection[object],
|
||||
highlighter: _HighlightFunc,
|
||||
truncation_budget: TruncationBudget,
|
||||
) -> Iterator[str]:
|
||||
"""Render the "X contains N more items" subdict."""
|
||||
max_lines = truncation_budget.max_lines
|
||||
if max_lines == 0 or len(keys) <= max_lines:
|
||||
# If no need to truncate, let pprint handle it.
|
||||
yield from highlighter(
|
||||
pprint.pformat({k: mapping[k] for k in keys})
|
||||
).splitlines()
|
||||
else:
|
||||
# To avoid spending effort on formatting entries that would be truncated,
|
||||
# only format the needed entries, keeping the sorting that pprint would use.
|
||||
for k in heapq.nsmallest(max_lines, keys, key=_safe_key):
|
||||
yield highlighter(saferepr({k: mapping[k]}))
|
||||
@@ -0,0 +1,96 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import Sequence
|
||||
|
||||
from _pytest._io.pprint import PrettyPrinter
|
||||
from _pytest._io.saferepr import saferepr
|
||||
from _pytest.assertion._typing import _HighlightFunc
|
||||
from _pytest.assertion._typing import NO_TRUNCATION_BUDGET
|
||||
from _pytest.assertion._typing import TruncationBudget
|
||||
from _pytest.compat import running_on_ci
|
||||
|
||||
|
||||
def _compare_eq_iterable(
|
||||
left: Iterable[object],
|
||||
right: Iterable[object],
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
truncation_budget: TruncationBudget = NO_TRUNCATION_BUDGET,
|
||||
) -> Iterator[str]:
|
||||
if verbose <= 0 and not running_on_ci():
|
||||
yield "Use -v to get more diff"
|
||||
return
|
||||
# dynamic import to speedup pytest
|
||||
import difflib
|
||||
|
||||
pp = PrettyPrinter()
|
||||
# ``pformat_lines`` spells an unbounded dimension ``None``; the budget spells it ``0``.
|
||||
max_lines = truncation_budget.max_lines or None
|
||||
max_chars = truncation_budget.max_chars or None
|
||||
left_formatting = pp.pformat_lines(left, max_lines=max_lines, max_chars=max_chars)
|
||||
right_formatting = pp.pformat_lines(right, max_lines=max_lines, max_chars=max_chars)
|
||||
|
||||
yield ""
|
||||
yield "Full diff: (-: missing in left side, +: extra in left side)"
|
||||
# "right" is the expected base against which we compare "left",
|
||||
# see https://github.com/pytest-dev/pytest/issues/3333
|
||||
# Highlight each ndiff line individually so the streaming truncator can
|
||||
# stop pulling from ``difflib.ndiff`` once its budget is full; the diff
|
||||
# lexer is line-oriented, so per-line highlighting is equivalent.
|
||||
for line in difflib.ndiff(right_formatting, left_formatting):
|
||||
yield highlighter(line.rstrip(), lexer="diff")
|
||||
|
||||
|
||||
def _compare_eq_sequence(
|
||||
left: Sequence[object],
|
||||
right: Sequence[object],
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
) -> Iterator[str]:
|
||||
comparing_bytes = isinstance(left, bytes) and isinstance(right, bytes)
|
||||
len_left = len(left)
|
||||
len_right = len(right)
|
||||
for i in range(min(len_left, len_right)):
|
||||
if left[i] != right[i]:
|
||||
if comparing_bytes:
|
||||
# when comparing bytes, we want to see their ascii representation
|
||||
# instead of their numeric values (#5260)
|
||||
# using a slice gives us the ascii representation:
|
||||
# >>> s = b'foo'
|
||||
# >>> s[0]
|
||||
# 102
|
||||
# >>> s[0:1]
|
||||
# b'f'
|
||||
left_value: object = left[i : i + 1]
|
||||
right_value: object = right[i : i + 1]
|
||||
else:
|
||||
left_value = left[i]
|
||||
right_value = right[i]
|
||||
|
||||
yield (
|
||||
f"At index {i} diff:"
|
||||
f" {highlighter(repr(left_value))} != {highlighter(repr(right_value))}"
|
||||
)
|
||||
break
|
||||
|
||||
if comparing_bytes:
|
||||
# when comparing bytes, it doesn't help to show the "sides contain one or more
|
||||
# items" longer explanation, so skip it
|
||||
return
|
||||
|
||||
len_diff = len_left - len_right
|
||||
if len_diff:
|
||||
if len_diff > 0:
|
||||
dir_with_more = "Left"
|
||||
extra = saferepr(left[len_right])
|
||||
else:
|
||||
len_diff = 0 - len_diff
|
||||
dir_with_more = "Right"
|
||||
extra = saferepr(right[len_left])
|
||||
|
||||
if len_diff == 1:
|
||||
yield f"{dir_with_more} contains one more item: {highlighter(extra)}"
|
||||
else:
|
||||
yield f"{dir_with_more} contains {len_diff} more items, first extra item: {highlighter(extra)}"
|
||||
@@ -0,0 +1,101 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import Set as AbstractSet
|
||||
from typing import TypeAlias
|
||||
|
||||
from _pytest._io.saferepr import saferepr
|
||||
from _pytest.assertion._typing import _HighlightFunc
|
||||
|
||||
|
||||
def _set_one_sided_diff(
|
||||
posn: str,
|
||||
set1: AbstractSet[object],
|
||||
set2: AbstractSet[object],
|
||||
highlighter: _HighlightFunc,
|
||||
) -> Iterator[str]:
|
||||
diff = set1 - set2
|
||||
if diff:
|
||||
yield f"Extra items in the {posn} set:"
|
||||
for item in diff:
|
||||
yield highlighter(saferepr(item))
|
||||
|
||||
|
||||
def _compare_eq_set(
|
||||
left: AbstractSet[object],
|
||||
right: AbstractSet[object],
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
) -> Iterator[str]:
|
||||
yield from _set_one_sided_diff("left", left, right, highlighter)
|
||||
yield from _set_one_sided_diff("right", right, left, highlighter)
|
||||
|
||||
|
||||
def _compare_gte_set(
|
||||
left: AbstractSet[object],
|
||||
right: AbstractSet[object],
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
) -> Iterator[str]:
|
||||
yield from _set_one_sided_diff("right", right, left, highlighter)
|
||||
|
||||
|
||||
def _compare_lte_set(
|
||||
left: AbstractSet[object],
|
||||
right: AbstractSet[object],
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
) -> Iterator[str]:
|
||||
yield from _set_one_sided_diff("left", left, right, highlighter)
|
||||
|
||||
|
||||
def _compare_gt_set(
|
||||
left: AbstractSet[object],
|
||||
right: AbstractSet[object],
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
) -> Iterator[str]:
|
||||
if left == right:
|
||||
yield "Both sets are equal"
|
||||
else:
|
||||
yield from _set_one_sided_diff("right", right, left, highlighter)
|
||||
|
||||
|
||||
def _compare_lt_set(
|
||||
left: AbstractSet[object],
|
||||
right: AbstractSet[object],
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
) -> Iterator[str]:
|
||||
if left == right:
|
||||
yield "Both sets are equal"
|
||||
else:
|
||||
yield from _set_one_sided_diff("left", left, right, highlighter)
|
||||
|
||||
|
||||
SetComparisonFunction: TypeAlias = Callable[
|
||||
[AbstractSet[object], AbstractSet[object], _HighlightFunc, int],
|
||||
Iterator[str],
|
||||
]
|
||||
|
||||
|
||||
def _both_sets_are_equal(
|
||||
left: AbstractSet[object],
|
||||
right: AbstractSet[object],
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
) -> Iterator[str]:
|
||||
yield "Both sets are equal"
|
||||
|
||||
|
||||
SET_COMPARISON_FUNCTIONS: dict[str, SetComparisonFunction] = {
|
||||
# == can't be done here without a prior refactor because there's an additional
|
||||
# explanation for iterable in _compare_eq_any
|
||||
# "==": _compare_eq_set,
|
||||
"!=": _both_sets_are_equal,
|
||||
">=": _compare_gte_set,
|
||||
"<=": _compare_lte_set,
|
||||
">": _compare_gt_set,
|
||||
"<": _compare_lt_set,
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import collections.abc
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Set as AbstractSet
|
||||
import dataclasses
|
||||
from typing import TypeGuard
|
||||
|
||||
|
||||
def issequence(x: object) -> TypeGuard[collections.abc.Sequence[object]]:
|
||||
return isinstance(x, collections.abc.Sequence) and not isinstance(x, str)
|
||||
|
||||
|
||||
def istext(x: object) -> TypeGuard[str]:
|
||||
return isinstance(x, str)
|
||||
|
||||
|
||||
def ismapping(x: object) -> TypeGuard[Mapping[object, object]]:
|
||||
return isinstance(x, Mapping)
|
||||
|
||||
|
||||
def isset(x: object) -> TypeGuard[AbstractSet[object]]:
|
||||
return isinstance(x, AbstractSet)
|
||||
|
||||
|
||||
def isnamedtuple(obj: object) -> bool:
|
||||
return isinstance(obj, tuple) and getattr(obj, "_fields", None) is not None
|
||||
|
||||
|
||||
isdatacls = dataclasses.is_dataclass
|
||||
|
||||
|
||||
def isattrs(obj: object) -> bool:
|
||||
return getattr(obj, "__attrs_attrs__", None) is not None
|
||||
|
||||
|
||||
def isiterable(obj: object) -> TypeGuard[collections.abc.Iterable[object]]:
|
||||
try:
|
||||
iter(obj) # type: ignore[call-overload]
|
||||
return not istext(obj)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def has_default_eq(obj: object) -> bool:
|
||||
"""Check if an instance of an object contains the default eq
|
||||
|
||||
First, we check if the object's __eq__ attribute has __code__,
|
||||
if so, we check the equally of the method code filename (__code__.co_filename)
|
||||
to the default one generated by the dataclass and attr module
|
||||
for dataclasses the default co_filename is <string>, for attrs class, the __eq__ should contain "attrs eq generated"
|
||||
"""
|
||||
# inspired from https://github.com/willmcgugan/rich/blob/07d51ffc1aee6f16bd2e5a25b4e82850fb9ed778/rich/pretty.py#L68
|
||||
if hasattr(obj.__eq__, "__code__") and hasattr(obj.__eq__.__code__, "co_filename"):
|
||||
code_filename = obj.__eq__.__code__.co_filename
|
||||
|
||||
if isattrs(obj):
|
||||
return "attrs generated " in code_filename
|
||||
|
||||
return code_filename == "<string>" # data class
|
||||
return True
|
||||
@@ -0,0 +1,35 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import ClassVar
|
||||
from typing import Literal
|
||||
from typing import Protocol
|
||||
|
||||
|
||||
_AssertionTextDiffStyle = Literal["ndiff", "block"]
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True, slots=True)
|
||||
class TruncationBudget:
|
||||
"""Per-explanation budget for truncating assertion output.
|
||||
|
||||
``max_lines`` / ``max_chars`` mirror the ``truncation_limit_lines`` /
|
||||
``truncation_limit_chars`` ini values: a positive limit bounds that
|
||||
dimension; ``0`` leaves it unbounded (the limit is disabled).
|
||||
"""
|
||||
|
||||
#: Default limits applied when the corresponding ini option is left unset.
|
||||
DEFAULT_MAX_LINES: ClassVar[int] = 8
|
||||
DEFAULT_MAX_CHARS: ClassVar[int] = DEFAULT_MAX_LINES * 80
|
||||
|
||||
max_lines: int = DEFAULT_MAX_LINES
|
||||
max_chars: int = DEFAULT_MAX_CHARS
|
||||
|
||||
|
||||
# Reusable "no cap" budget, used as a default argument (B008).
|
||||
NO_TRUNCATION_BUDGET = TruncationBudget(max_lines=0, max_chars=0)
|
||||
|
||||
|
||||
class _HighlightFunc(Protocol): # noqa: PYI046
|
||||
def __call__(self, source: str, lexer: Literal["diff", "python"] = "python") -> str:
|
||||
"""Apply highlighting to the given source."""
|
||||
@@ -0,0 +1,134 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator
|
||||
|
||||
from _pytest._io.saferepr import saferepr
|
||||
from _pytest.assertion._typing import _AssertionTextDiffStyle
|
||||
from _pytest.assertion._typing import _HighlightFunc
|
||||
from _pytest.assertion._typing import NO_TRUNCATION_BUDGET
|
||||
from _pytest.assertion._typing import TruncationBudget
|
||||
from _pytest.assertion.highlight import dummy_highlighter
|
||||
from _pytest.compat import assert_never
|
||||
|
||||
|
||||
def _compare_eq_text(
|
||||
left: str,
|
||||
right: str,
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int,
|
||||
assertion_text_diff_style: _AssertionTextDiffStyle,
|
||||
truncation_budget: TruncationBudget = NO_TRUNCATION_BUDGET,
|
||||
) -> Iterator[str]:
|
||||
match assertion_text_diff_style:
|
||||
case "block":
|
||||
yield from _diff_text_block(left, right)
|
||||
case "ndiff":
|
||||
yield from _diff_text(left, right, highlighter, verbose, truncation_budget)
|
||||
case unreachable:
|
||||
assert_never(unreachable)
|
||||
|
||||
|
||||
def _diff_text_block(left: str, right: str) -> Iterator[str]:
|
||||
yield "Left:"
|
||||
yield from _format_text_block_lines(left)
|
||||
yield ""
|
||||
yield "Right:"
|
||||
yield from _format_text_block_lines(right)
|
||||
|
||||
|
||||
def _format_text_block_lines(text: str) -> Iterator[str]:
|
||||
for line in text.split("\n"):
|
||||
yield f" {line}"
|
||||
|
||||
|
||||
def _diff_text(
|
||||
left: str,
|
||||
right: str,
|
||||
highlighter: _HighlightFunc,
|
||||
verbose: int = 0,
|
||||
truncation_budget: TruncationBudget = NO_TRUNCATION_BUDGET,
|
||||
) -> Iterator[str]:
|
||||
"""Yield the explanation for the diff between text.
|
||||
|
||||
Unless --verbose is used this will skip leading and trailing
|
||||
characters which are identical to keep the diff minimal.
|
||||
|
||||
When a truncation budget is set, the inputs to ``ndiff`` are capped
|
||||
first, so the truncated head may differ from the head of the
|
||||
unbounded diff.
|
||||
"""
|
||||
from difflib import ndiff
|
||||
|
||||
if verbose < 1:
|
||||
i = 0 # just in case left or right has zero length
|
||||
for i in range(min(len(left), len(right))):
|
||||
if left[i] != right[i]:
|
||||
break
|
||||
if i > 42:
|
||||
i -= 10 # Provide some context
|
||||
yield f"Skipping {i} identical leading characters in diff, use -v to show"
|
||||
left = left[i:]
|
||||
right = right[i:]
|
||||
if len(left) == len(right):
|
||||
for i in range(1, len(left) + 1):
|
||||
if left[-i] != right[-i]:
|
||||
break
|
||||
if i > 42:
|
||||
i -= 10 # Provide some context
|
||||
yield (
|
||||
f"Skipping {i} identical trailing "
|
||||
"characters in diff, use -v to show"
|
||||
)
|
||||
left = left[:-i]
|
||||
right = right[:-i]
|
||||
keepends = True
|
||||
if left.isspace() or right.isspace():
|
||||
left = repr(str(left))
|
||||
right = repr(str(right))
|
||||
yield "Strings contain only whitespace, escaping them using repr()"
|
||||
left_lines = _cap_ndiff_input(left, keepends, truncation_budget)
|
||||
right_lines = _cap_ndiff_input(right, keepends, truncation_budget)
|
||||
# "right" is the expected base against which we compare "left",
|
||||
# see https://github.com/pytest-dev/pytest/issues/3333
|
||||
yield from highlighter(
|
||||
"\n".join(line.strip("\n") for line in ndiff(right_lines, left_lines)),
|
||||
lexer="diff",
|
||||
).splitlines()
|
||||
|
||||
|
||||
def _cap_ndiff_input(text: str, keepends: bool, budget: TruncationBudget) -> list[str]:
|
||||
"""Cap an ``ndiff`` input to the truncation budget, as split lines.
|
||||
|
||||
A char slice first (bounds a few huge lines, whose intraline diff is
|
||||
O(len^2)), then a line slice (bounds many lines). A ``0`` limit leaves
|
||||
that dimension unbounded.
|
||||
"""
|
||||
if budget.max_chars > 0:
|
||||
text = text[: budget.max_chars]
|
||||
lines = text.splitlines(keepends)
|
||||
if budget.max_lines > 0:
|
||||
lines = lines[: budget.max_lines]
|
||||
return lines
|
||||
|
||||
|
||||
def _notin_text(
|
||||
term: str,
|
||||
text: str,
|
||||
verbose: int = 0,
|
||||
truncation_budget: TruncationBudget = NO_TRUNCATION_BUDGET,
|
||||
) -> Iterator[str]:
|
||||
index = text.find(term)
|
||||
head = text[:index]
|
||||
tail = text[index + len(term) :]
|
||||
correct_text = head + tail
|
||||
diff = _diff_text(text, correct_text, dummy_highlighter, verbose, truncation_budget)
|
||||
yield f"{saferepr(term, maxsize=42)} is contained here:"
|
||||
for line in diff:
|
||||
if line.startswith("Skipping"):
|
||||
continue
|
||||
if line.startswith("- "):
|
||||
continue
|
||||
if line.startswith("+ "):
|
||||
yield " " + line[2:]
|
||||
else:
|
||||
yield line
|
||||
@@ -0,0 +1,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Literal
|
||||
|
||||
|
||||
def dummy_highlighter(source: str, lexer: Literal["diff", "python"] = "python") -> str:
|
||||
"""Dummy highlighter that returns the text unprocessed.
|
||||
|
||||
Needed for _notin_text, as the diff gets post-processed to only show the "+" part.
|
||||
"""
|
||||
return source
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,159 @@
|
||||
"""Utilities for truncating assertion output.
|
||||
|
||||
Current default behaviour is to truncate assertion explanations at
|
||||
terminal lines, unless running with an assertions verbosity level of at least 2 or running on CI.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable
|
||||
|
||||
from _pytest.assertion._typing import NO_TRUNCATION_BUDGET
|
||||
from _pytest.assertion._typing import TruncationBudget
|
||||
from _pytest.compat import running_on_ci
|
||||
from _pytest.config import Config
|
||||
|
||||
|
||||
USAGE_MSG = "use '-vv' to show"
|
||||
TRUNCATION_MSG = f"...Full output truncated, {USAGE_MSG}"
|
||||
|
||||
# Truncating appends a footer: ``...`` on the last kept line, a blank line,
|
||||
# then ``TRUNCATION_MSG``. A body may exceed the raw budget by the footer's
|
||||
# cost before truncating, so a body that nearly fits is not cut just to
|
||||
# make room for the footer.
|
||||
TRUNCATION_FOOTER_LINES = 2 # blank separator + message line
|
||||
TRUNCATION_FOOTER_CHARS = len("...") + len(TRUNCATION_MSG)
|
||||
|
||||
|
||||
def materialize_with_truncation(lines: Iterable[str], config: Config) -> list[str]:
|
||||
"""Materialise a streaming explanation, applying truncation lazily.
|
||||
|
||||
Pulls from ``lines`` only until the truncation threshold is reached;
|
||||
the rest of the iterator is dropped without being consumed.
|
||||
"""
|
||||
should_truncate, budget = _get_truncation_parameters(config)
|
||||
if not should_truncate:
|
||||
return list(lines)
|
||||
|
||||
tolerable_max_chars = budget.max_chars + TRUNCATION_FOOTER_CHARS
|
||||
# Pull one line past the point where ``_truncate_explanation`` keeps the
|
||||
# body whole (``max_lines + TRUNCATION_FOOTER_LINES``) so it can detect the
|
||||
# overflow, without us materialising more than we need.
|
||||
line_cap = (
|
||||
budget.max_lines + TRUNCATION_FOOTER_LINES + 1 if budget.max_lines > 0 else None
|
||||
)
|
||||
buffered: list[str] = []
|
||||
char_count = 0
|
||||
for line in lines:
|
||||
buffered.append(line)
|
||||
char_count += len(line)
|
||||
if line_cap is not None and len(buffered) >= line_cap:
|
||||
break
|
||||
if budget.max_chars > 0 and char_count > tolerable_max_chars:
|
||||
break
|
||||
else:
|
||||
# Iterator exhausted within limits — nothing to truncate.
|
||||
return buffered
|
||||
|
||||
return _truncate_explanation(buffered, budget)
|
||||
|
||||
|
||||
def _get_truncation_parameters(config: Config) -> tuple[bool, TruncationBudget]:
|
||||
"""Return the truncation parameters from the given config, as (should truncate, budget)."""
|
||||
# We do not need to truncate if one of these conditions is met:
|
||||
# 1. Verbosity level is 2 or more;
|
||||
# 2. Test is being run in CI environment;
|
||||
# 3. Both truncation_limit_lines and truncation_limit_chars
|
||||
# are set to 0 explicitly.
|
||||
verbose = config.get_verbosity(Config.VERBOSITY_ASSERTIONS)
|
||||
if verbose >= 2 or running_on_ci():
|
||||
return False, NO_TRUNCATION_BUDGET
|
||||
|
||||
budget = _budget_from_config(config)
|
||||
should_truncate = budget.max_lines > 0 or budget.max_chars > 0
|
||||
return should_truncate, budget
|
||||
|
||||
|
||||
def _budget_from_config(config: Config) -> TruncationBudget:
|
||||
"""Build a budget from the ``truncation_limit_*`` ini options.
|
||||
|
||||
``getini`` returns ``None`` when an option is unset, which falls back to
|
||||
the default limit.
|
||||
"""
|
||||
max_lines = config.getini("truncation_limit_lines")
|
||||
max_chars = config.getini("truncation_limit_chars")
|
||||
return TruncationBudget(
|
||||
max_lines=(
|
||||
TruncationBudget.DEFAULT_MAX_LINES if max_lines is None else int(max_lines)
|
||||
),
|
||||
max_chars=(
|
||||
TruncationBudget.DEFAULT_MAX_CHARS if max_chars is None else int(max_chars)
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _truncate_explanation(
|
||||
input_lines: list[str],
|
||||
budget: TruncationBudget,
|
||||
) -> list[str]:
|
||||
"""Truncate given list of strings that makes up the assertion explanation.
|
||||
|
||||
Truncates to either ``budget.max_lines`` or ``budget.max_chars`` -
|
||||
whichever the input reaches first, taking the truncation explanation into
|
||||
account. The remaining lines will be replaced by a usage message.
|
||||
|
||||
If max_chars=0, no truncation by character count is performed.
|
||||
If max_lines=0, no truncation by line count is performed.
|
||||
|
||||
When this function is launched we know max_lines > 0 or max_chars > 0
|
||||
because _get_truncation_parameters was called first.
|
||||
"""
|
||||
# ``max_chars`` bounds the body only; the footer slack is added on top
|
||||
# (see ``TRUNCATION_FOOTER_CHARS``).
|
||||
tolerable_max_chars = budget.max_chars + TRUNCATION_FOOTER_CHARS
|
||||
if (
|
||||
budget.max_lines == 0
|
||||
or len(input_lines) <= budget.max_lines + TRUNCATION_FOOTER_LINES
|
||||
):
|
||||
if (
|
||||
budget.max_chars == 0
|
||||
or sum(len(s) for s in input_lines) <= tolerable_max_chars
|
||||
):
|
||||
return input_lines
|
||||
truncated_explanation = input_lines
|
||||
else:
|
||||
# Truncate first to max_lines, and then truncate to max_chars if necessary
|
||||
truncated_explanation = input_lines[: budget.max_lines]
|
||||
# We reevaluate the need to truncate chars following removal of some lines
|
||||
if (
|
||||
budget.max_chars > 0
|
||||
and sum(len(e) for e in truncated_explanation) > tolerable_max_chars
|
||||
):
|
||||
truncated_explanation = _truncate_by_char_count(
|
||||
truncated_explanation, budget.max_chars
|
||||
)
|
||||
# Something was truncated, adding '...' at the end to show that
|
||||
truncated_explanation[-1] += "..."
|
||||
return [
|
||||
*truncated_explanation,
|
||||
"",
|
||||
TRUNCATION_MSG,
|
||||
]
|
||||
|
||||
|
||||
def _truncate_by_char_count(input_lines: list[str], max_chars: int) -> list[str]:
|
||||
# Find point at which input length exceeds total allowed length
|
||||
iterated_char_count = 0
|
||||
for iterated_index, input_line in enumerate(input_lines):
|
||||
if iterated_char_count + len(input_line) > max_chars:
|
||||
break
|
||||
iterated_char_count += len(input_line)
|
||||
|
||||
# Create truncated explanation with modified final line
|
||||
truncated_result = input_lines[:iterated_index]
|
||||
final_line = input_lines[iterated_index]
|
||||
if final_line:
|
||||
final_line_truncate_point = max_chars - iterated_char_count
|
||||
final_line = final_line[:final_line_truncate_point]
|
||||
truncated_result.append(final_line)
|
||||
return truncated_result
|
||||
@@ -0,0 +1,191 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Utilities for assertion debugging."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import Sequence
|
||||
from collections.abc import Set as AbstractSet
|
||||
from unicodedata import normalize
|
||||
|
||||
from _pytest import outcomes
|
||||
import _pytest._code
|
||||
from _pytest._io.saferepr import saferepr
|
||||
from _pytest._io.saferepr import saferepr_unlimited
|
||||
from _pytest.assertion._compare_any import _compare_eq_any
|
||||
from _pytest.assertion._compare_set import SET_COMPARISON_FUNCTIONS
|
||||
from _pytest.assertion._typing import _AssertionTextDiffStyle
|
||||
from _pytest.assertion._typing import _HighlightFunc
|
||||
from _pytest.assertion._typing import NO_TRUNCATION_BUDGET
|
||||
from _pytest.assertion._typing import TruncationBudget
|
||||
from _pytest.assertion.compare_text import _notin_text
|
||||
from _pytest.assertion.highlight import dummy_highlighter as dummy_highlighter
|
||||
from _pytest.config import Config
|
||||
|
||||
|
||||
# The _reprcompare attribute on the util module is used by the new assertion
|
||||
# interpretation code and assertion rewriter to detect this plugin was
|
||||
# loaded and in turn call the hooks defined here as part of the
|
||||
# DebugInterpreter.
|
||||
_reprcompare: Callable[[str, object, object], str | None] | None = None
|
||||
|
||||
# Works similarly as _reprcompare attribute. Is populated with the hook call
|
||||
# when pytest_runtest_setup is called.
|
||||
_assertion_pass: Callable[[int, str, str], None] | None = None
|
||||
|
||||
# Config object which is assigned during pytest_runtest_protocol.
|
||||
_config: Config | None = None
|
||||
|
||||
|
||||
def format_explanation(explanation: str) -> str:
|
||||
r"""Format an explanation.
|
||||
|
||||
Normally all embedded newlines are escaped, however there are
|
||||
three exceptions: \n{, \n} and \n~. The first two are intended
|
||||
cover nested explanations, see function and attribute explanations
|
||||
for examples (.visit_Call(), visit_Attribute()). The last one is
|
||||
for when one explanation needs to span multiple lines, e.g. when
|
||||
displaying diffs.
|
||||
"""
|
||||
lines = _split_explanation(explanation)
|
||||
result = _format_lines(lines)
|
||||
return "\n".join(result)
|
||||
|
||||
|
||||
def _split_explanation(explanation: str) -> list[str]:
|
||||
r"""Return a list of individual lines in the explanation.
|
||||
|
||||
This will return a list of lines split on '\n{', '\n}' and '\n~'.
|
||||
Any other newlines will be escaped and appear in the line as the
|
||||
literal '\n' characters.
|
||||
"""
|
||||
raw_lines = (explanation or "").split("\n")
|
||||
lines = [raw_lines[0]]
|
||||
for values in raw_lines[1:]:
|
||||
if values and values[0] in ["{", "}", "~", ">"]:
|
||||
lines.append(values)
|
||||
else:
|
||||
lines[-1] += "\\n" + values
|
||||
return lines
|
||||
|
||||
|
||||
def _format_lines(lines: Sequence[str]) -> list[str]:
|
||||
"""Format the individual lines.
|
||||
|
||||
This will replace the '{', '}' and '~' characters of our mini formatting
|
||||
language with the proper 'where ...', 'and ...' and ' + ...' text, taking
|
||||
care of indentation along the way.
|
||||
|
||||
Return a list of formatted lines.
|
||||
"""
|
||||
result = list(lines[:1])
|
||||
stack = [0]
|
||||
stackcnt = [0]
|
||||
for line in lines[1:]:
|
||||
if line.startswith("{"):
|
||||
if stackcnt[-1]:
|
||||
s = "and "
|
||||
else:
|
||||
s = "where "
|
||||
stack.append(len(result))
|
||||
stackcnt[-1] += 1
|
||||
stackcnt.append(0)
|
||||
result.append(" +" + " " * (len(stack) - 1) + s + line[1:])
|
||||
elif line.startswith("}"):
|
||||
stack.pop()
|
||||
stackcnt.pop()
|
||||
result[stack[-1]] += line[1:]
|
||||
else:
|
||||
assert line[0] in ["~", ">"]
|
||||
stack[-1] += 1
|
||||
indent = len(stack) if line.startswith("~") else len(stack) - 1
|
||||
result.append(" " * indent + line[1:])
|
||||
assert len(stack) == 1
|
||||
return result
|
||||
|
||||
|
||||
def assertrepr_compare(
|
||||
op: str,
|
||||
left: object,
|
||||
right: object,
|
||||
*,
|
||||
verbose: int,
|
||||
highlighter: _HighlightFunc,
|
||||
assertion_text_diff_style: _AssertionTextDiffStyle,
|
||||
truncation_budget: TruncationBudget = NO_TRUNCATION_BUDGET,
|
||||
) -> Iterator[str]:
|
||||
"""Yield specialised explanations for some operators/operands.
|
||||
|
||||
The first line yielded is always the summary (``left op right``);
|
||||
subsequent lines are the detailed explanation. Yields nothing when no
|
||||
specialised explanation applies, which lets consumers map an empty
|
||||
iterator to "no explanation" without materialising anything.
|
||||
|
||||
The iterator is lazy on purpose: a streaming consumer can stop pulling
|
||||
lines as soon as it has enough to show, so an enormous diff doesn't
|
||||
have to be built in full just to be thrown away.
|
||||
"""
|
||||
# Strings which normalize equal are often hard to distinguish when printed; use ascii() to make this easier.
|
||||
# See issue #3246.
|
||||
use_ascii = (
|
||||
isinstance(left, str)
|
||||
and isinstance(right, str)
|
||||
and normalize("NFD", left) == normalize("NFD", right)
|
||||
)
|
||||
|
||||
if verbose > 1:
|
||||
left_repr = saferepr_unlimited(left, use_ascii=use_ascii)
|
||||
right_repr = saferepr_unlimited(right, use_ascii=use_ascii)
|
||||
else:
|
||||
# XXX: "15 chars indentation" is wrong
|
||||
# ("E AssertionError: assert "); should use term width.
|
||||
maxsize = (
|
||||
80 - 15 - len(op) - 2
|
||||
) // 2 # 15 chars indentation, 1 space around op
|
||||
|
||||
left_repr = saferepr(left, maxsize=maxsize, use_ascii=use_ascii)
|
||||
right_repr = saferepr(right, maxsize=maxsize, use_ascii=use_ascii)
|
||||
|
||||
summary = f"{left_repr} {op} {right_repr}"
|
||||
|
||||
try:
|
||||
match (left, op, right):
|
||||
case (_, "==", _):
|
||||
source = _compare_eq_any(
|
||||
left,
|
||||
right,
|
||||
highlighter,
|
||||
verbose,
|
||||
assertion_text_diff_style,
|
||||
truncation_budget,
|
||||
)
|
||||
case (str(), "not in", str()):
|
||||
source = _notin_text(left, right, verbose, truncation_budget)
|
||||
case (AbstractSet(), "!=" | ">=" | "<=" | ">" | "<", AbstractSet()):
|
||||
source = SET_COMPARISON_FUNCTIONS[op](left, right, highlighter, verbose)
|
||||
case _:
|
||||
source = iter(())
|
||||
|
||||
# Only yield the summary if there is a detailed explanation.
|
||||
# Make sure there's a separating empty line after the summary.
|
||||
summary_yielded = False
|
||||
for line in source:
|
||||
if not summary_yielded:
|
||||
yield summary
|
||||
if line != "":
|
||||
yield ""
|
||||
summary_yielded = True
|
||||
yield line
|
||||
except outcomes.Exit:
|
||||
raise
|
||||
except Exception:
|
||||
repr_crash = _pytest._code.ExceptionInfo.from_current()._getreprcrash()
|
||||
if not summary_yielded:
|
||||
yield summary
|
||||
yield ""
|
||||
summary_yielded = True
|
||||
yield (
|
||||
f"(pytest_assertion plugin: representation of details failed: {repr_crash}."
|
||||
)
|
||||
yield " Probably an object has a faulty __repr__.)"
|
||||
@@ -0,0 +1,663 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Implementation of the cache provider."""
|
||||
|
||||
# This plugin was not named "cache" to avoid conflicts with the external
|
||||
# pytest-cache version.
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
from collections.abc import Iterable
|
||||
import dataclasses
|
||||
import errno
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import tempfile
|
||||
from typing import final
|
||||
|
||||
from .pathlib import resolve_from_str
|
||||
from .pathlib import rm_rf
|
||||
from .reports import CollectReport
|
||||
from _pytest import nodes
|
||||
from _pytest._io import TerminalWriter
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import ExitCode
|
||||
from _pytest.config import hookimpl
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.deprecated import check_ispytest
|
||||
from _pytest.fixtures import fixture
|
||||
from _pytest.fixtures import FixtureRequest
|
||||
from _pytest.main import Session
|
||||
from _pytest.nodeid import NodeId
|
||||
from _pytest.nodes import Directory
|
||||
from _pytest.nodes import File
|
||||
from _pytest.reports import TestReport
|
||||
|
||||
|
||||
CACHEDIR_FILES: dict[str, bytes] = {
|
||||
"README.md": b"""\
|
||||
# pytest cache directory #
|
||||
|
||||
This directory contains data from the pytest's cache plugin,
|
||||
which provides the `--lf` and `--ff` options, as well as the `cache` fixture.
|
||||
|
||||
**Do not** commit this to version control.
|
||||
|
||||
See [the docs](https://docs.pytest.org/en/stable/how-to/cache.html) for more information.
|
||||
""",
|
||||
".gitignore": b"# Created by pytest automatically.\n*\n",
|
||||
"CACHEDIR.TAG": b"""\
|
||||
Signature: 8a477f597d28d172789f06886806bc55
|
||||
# This file is a cache directory tag created by pytest.
|
||||
# For information about cache directory tags, see:
|
||||
# https://bford.info/cachedir/spec.html
|
||||
""",
|
||||
}
|
||||
|
||||
|
||||
def _make_cachedir(target: Path) -> None:
|
||||
"""Create the pytest cache directory atomically with supporting files.
|
||||
|
||||
Creates a temporary directory with README.md, .gitignore, and CACHEDIR.TAG,
|
||||
then atomically renames it to the target location. If another process wins
|
||||
the race, the temporary directory is cleaned up.
|
||||
"""
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
path = Path(tempfile.mkdtemp(prefix="pytest-cache-files-", dir=target.parent))
|
||||
try:
|
||||
# Reset permissions to the default, see #12308.
|
||||
# Note: there's no way to get the current umask atomically, eek.
|
||||
umask = os.umask(0o022)
|
||||
os.umask(umask)
|
||||
path.chmod(0o777 - umask)
|
||||
|
||||
for name, content in CACHEDIR_FILES.items():
|
||||
path.joinpath(name).write_bytes(content)
|
||||
|
||||
path.rename(target)
|
||||
except OSError as e:
|
||||
# If 2 concurrent pytests both race to the rename, the loser
|
||||
# gets "Directory not empty" from the rename. In this case,
|
||||
# everything is handled so just continue after cleanup.
|
||||
# On Windows, the error is a FileExistsError which translates to EEXIST.
|
||||
if e.errno not in (errno.ENOTEMPTY, errno.EEXIST):
|
||||
raise
|
||||
finally:
|
||||
shutil.rmtree(path, ignore_errors=True)
|
||||
|
||||
|
||||
@final
|
||||
@dataclasses.dataclass
|
||||
class Cache:
|
||||
"""Instance of the `cache` fixture."""
|
||||
|
||||
_cachedir: Path = dataclasses.field(repr=False)
|
||||
_config: Config = dataclasses.field(repr=False)
|
||||
|
||||
# Sub-directory under cache-dir for directories created by `mkdir()`.
|
||||
_CACHE_PREFIX_DIRS = "d"
|
||||
|
||||
# Sub-directory under cache-dir for values created by `set()`.
|
||||
_CACHE_PREFIX_VALUES = "v"
|
||||
|
||||
def __init__(
|
||||
self, cachedir: Path, config: Config, *, _ispytest: bool = False
|
||||
) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
self._cachedir = cachedir
|
||||
self._config = config
|
||||
|
||||
@classmethod
|
||||
def for_config(cls, config: Config, *, _ispytest: bool = False) -> Cache:
|
||||
"""Create the Cache instance for a Config.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
check_ispytest(_ispytest)
|
||||
cachedir = cls.cache_dir_from_config(config, _ispytest=True)
|
||||
if config.getoption("cacheclear") and cachedir.is_dir():
|
||||
cls.clear_cache(cachedir, _ispytest=True)
|
||||
return cls(cachedir, config, _ispytest=True)
|
||||
|
||||
@classmethod
|
||||
def clear_cache(cls, cachedir: Path, _ispytest: bool = False) -> None:
|
||||
"""Clear the sub-directories used to hold cached directories and values.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
check_ispytest(_ispytest)
|
||||
for prefix in (cls._CACHE_PREFIX_DIRS, cls._CACHE_PREFIX_VALUES):
|
||||
d = cachedir / prefix
|
||||
if d.is_dir():
|
||||
rm_rf(d)
|
||||
|
||||
@staticmethod
|
||||
def cache_dir_from_config(config: Config, *, _ispytest: bool = False) -> Path:
|
||||
"""Get the path to the cache directory for a Config.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
check_ispytest(_ispytest)
|
||||
return resolve_from_str(config.getini("cache_dir"), config.rootpath)
|
||||
|
||||
def warn(self, fmt: str, *, _ispytest: bool = False, **args: object) -> None:
|
||||
"""Issue a cache warning.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
check_ispytest(_ispytest)
|
||||
import warnings
|
||||
|
||||
from _pytest.warning_types import PytestCacheWarning
|
||||
|
||||
warnings.warn(
|
||||
PytestCacheWarning(fmt.format(**args) if args else fmt),
|
||||
self._config.hook,
|
||||
stacklevel=3,
|
||||
)
|
||||
|
||||
def _mkdir(self, path: Path) -> None:
|
||||
self._ensure_cache_dir_and_supporting_files()
|
||||
path.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
@staticmethod
|
||||
def _join_within(base: Path, name: str) -> Path:
|
||||
"""Join ``name`` onto ``base``, keeping the result inside ``base``.
|
||||
|
||||
``joinpath()`` lets an absolute or drive-qualified ``name`` replace
|
||||
``base`` outright, and keeps ``..`` segments verbatim. Normalizing
|
||||
lexically and re-checking containment rejects both, while still
|
||||
allowing a ``..`` that cancels out within ``base``.
|
||||
"""
|
||||
path = Path(os.path.normpath(base.joinpath(name)))
|
||||
if not path.is_relative_to(base):
|
||||
raise ValueError(f"{name!r} is not allowed to escape the cache directory")
|
||||
return path
|
||||
|
||||
def mkdir(self, name: str) -> Path:
|
||||
"""Return a directory path object with the given name.
|
||||
|
||||
If the directory does not yet exist, it will be created. You can use
|
||||
it to manage files to e.g. store/retrieve database dumps across test
|
||||
sessions.
|
||||
|
||||
.. versionadded:: 7.0
|
||||
|
||||
:param name:
|
||||
Must be a string not containing a ``/`` separator.
|
||||
Make sure the name contains your plugin or application
|
||||
identifiers to prevent clashes with other cache users.
|
||||
"""
|
||||
if len(Path(name).parts) > 1:
|
||||
raise ValueError("name is not allowed to contain path separators")
|
||||
res = self._join_within(self._cachedir / self._CACHE_PREFIX_DIRS, name)
|
||||
self._mkdir(res)
|
||||
return res
|
||||
|
||||
def _getvaluepath(self, key: str) -> Path:
|
||||
return self._join_within(self._cachedir / self._CACHE_PREFIX_VALUES, key)
|
||||
|
||||
def get(self, key: str, default):
|
||||
"""Return the cached value for the given key.
|
||||
|
||||
If no value was yet cached or the value cannot be read, the specified
|
||||
default is returned.
|
||||
|
||||
:param key:
|
||||
Must be a ``/`` separated value that does not resolve outside
|
||||
the cache directory. Usually the first
|
||||
name is the name of your plugin or your application.
|
||||
:param default:
|
||||
The value to return in case of a cache-miss or invalid cache value.
|
||||
"""
|
||||
path = self._getvaluepath(key)
|
||||
try:
|
||||
with path.open("r", encoding="UTF-8") as f:
|
||||
return json.load(f)
|
||||
except (ValueError, OSError):
|
||||
return default
|
||||
|
||||
def set(self, key: str, value: object) -> None:
|
||||
"""Save value for the given key.
|
||||
|
||||
:param key:
|
||||
Must be a ``/`` separated value that does not resolve outside
|
||||
the cache directory. Usually the first
|
||||
name is the name of your plugin or your application.
|
||||
:param value:
|
||||
Must be of any combination of basic python types,
|
||||
including nested types like lists of dictionaries.
|
||||
"""
|
||||
path = self._getvaluepath(key)
|
||||
try:
|
||||
self._mkdir(path.parent)
|
||||
except OSError as exc:
|
||||
self.warn(
|
||||
f"could not create cache path {path}: {exc}",
|
||||
_ispytest=True,
|
||||
)
|
||||
return
|
||||
data = json.dumps(value, ensure_ascii=False, indent=2)
|
||||
try:
|
||||
path.write_text(data, encoding="UTF-8")
|
||||
except OSError as exc:
|
||||
self.warn(
|
||||
f"cache could not write path {path}: {exc}",
|
||||
_ispytest=True,
|
||||
)
|
||||
|
||||
def _ensure_cache_dir_and_supporting_files(self) -> None:
|
||||
"""Create the cache dir and its supporting files."""
|
||||
if not self._cachedir.is_dir():
|
||||
_make_cachedir(self._cachedir)
|
||||
|
||||
|
||||
class LFPluginCollWrapper:
|
||||
def __init__(self, lfplugin: LFPlugin) -> None:
|
||||
self.lfplugin = lfplugin
|
||||
self._collected_at_least_one_failure = False
|
||||
|
||||
@hookimpl(wrapper=True)
|
||||
def pytest_make_collect_report(
|
||||
self, collector: nodes.Collector
|
||||
) -> Generator[None, CollectReport, CollectReport]:
|
||||
res = yield
|
||||
if isinstance(collector, Session | Directory):
|
||||
# Sort any lf-paths to the beginning.
|
||||
lf_paths = self.lfplugin._last_failed_paths
|
||||
|
||||
# Use stable sort to prioritize last failed.
|
||||
def sort_key(node: nodes.Item | nodes.Collector) -> bool:
|
||||
return node.path in lf_paths
|
||||
|
||||
res.result = sorted(
|
||||
res.result,
|
||||
key=sort_key,
|
||||
reverse=True,
|
||||
)
|
||||
|
||||
elif isinstance(collector, File):
|
||||
if collector.path in self.lfplugin._last_failed_paths:
|
||||
result = res.result
|
||||
lastfailed = self.lfplugin.lastfailed
|
||||
|
||||
# Only filter with known failures.
|
||||
if not self._collected_at_least_one_failure:
|
||||
if not any(x.id in lastfailed for x in result):
|
||||
return res
|
||||
self.lfplugin.config.pluginmanager.register(
|
||||
LFPluginCollSkipfiles(self.lfplugin), "lfplugin-collskip"
|
||||
)
|
||||
self._collected_at_least_one_failure = True
|
||||
|
||||
session = collector.session
|
||||
result[:] = [
|
||||
x
|
||||
for x in result
|
||||
if x.id in lastfailed
|
||||
# Include any passed arguments (not trivial to filter).
|
||||
or session.isinitpath(x.path)
|
||||
# Keep all sub-collectors.
|
||||
or isinstance(x, nodes.Collector)
|
||||
]
|
||||
|
||||
return res
|
||||
|
||||
|
||||
class LFPluginCollSkipfiles:
|
||||
def __init__(self, lfplugin: LFPlugin) -> None:
|
||||
self.lfplugin = lfplugin
|
||||
|
||||
@hookimpl
|
||||
def pytest_make_collect_report(
|
||||
self, collector: nodes.Collector
|
||||
) -> CollectReport | None:
|
||||
if isinstance(collector, File):
|
||||
if collector.path not in self.lfplugin._last_failed_paths:
|
||||
self.lfplugin._skipped_files += 1
|
||||
|
||||
return CollectReport(collector.id, "passed", longrepr=None, result=[])
|
||||
return None
|
||||
|
||||
|
||||
class LFPlugin:
|
||||
"""Plugin which implements the --lf (run last-failing) option."""
|
||||
|
||||
def __init__(self, config: Config) -> None:
|
||||
self.config = config
|
||||
active_keys = "lf", "failedfirst"
|
||||
self.active = any(config.getoption(key) for key in active_keys)
|
||||
assert config.cache
|
||||
self.lastfailed: dict[NodeId, bool] = {
|
||||
NodeId.parse(k): v
|
||||
for k, v in config.cache.get("cache/lastfailed", {}).items()
|
||||
}
|
||||
self._previously_failed_count: int | None = None
|
||||
self._report_status: str | None = None
|
||||
self._skipped_files = 0 # count skipped files during collection due to --lf
|
||||
|
||||
if config.getoption("lf"):
|
||||
self._last_failed_paths = self.get_last_failed_paths()
|
||||
config.pluginmanager.register(
|
||||
LFPluginCollWrapper(self), "lfplugin-collwrapper"
|
||||
)
|
||||
|
||||
def get_last_failed_paths(self) -> set[Path]:
|
||||
"""Return a set with all Paths of the previously failed nodeids and
|
||||
their parents."""
|
||||
rootpath = self.config.rootpath
|
||||
result = set()
|
||||
for nodeid in self.lastfailed:
|
||||
path = rootpath / nodeid.path
|
||||
result.add(path)
|
||||
result.update(path.parents)
|
||||
return {x for x in result if x.exists()}
|
||||
|
||||
def pytest_report_collectionfinish(self) -> str | None:
|
||||
if self.active and self.config.get_verbosity() >= 0:
|
||||
return f"run-last-failure: {self._report_status}"
|
||||
return None
|
||||
|
||||
def pytest_runtest_logreport(self, report: TestReport) -> None:
|
||||
if (report.when == "call" and report.passed) or report.skipped:
|
||||
self.lastfailed.pop(report.id, None)
|
||||
elif report.failed:
|
||||
self.lastfailed[report.id] = True
|
||||
|
||||
def pytest_collectreport(self, report: CollectReport) -> None:
|
||||
passed = report.outcome in ("passed", "skipped")
|
||||
if passed:
|
||||
report_id = report.id
|
||||
if report_id in self.lastfailed:
|
||||
self.lastfailed.pop(report_id)
|
||||
self.lastfailed.update((item.id, True) for item in report.result)
|
||||
else:
|
||||
self.lastfailed[report.id] = True
|
||||
|
||||
@hookimpl(wrapper=True, tryfirst=True)
|
||||
def pytest_collection_modifyitems(
|
||||
self, config: Config, items: list[nodes.Item]
|
||||
) -> Generator[None]:
|
||||
res = yield
|
||||
|
||||
if not self.active:
|
||||
return res
|
||||
|
||||
if self.lastfailed:
|
||||
previously_failed = []
|
||||
previously_passed = []
|
||||
for item in items:
|
||||
if item.id in self.lastfailed:
|
||||
previously_failed.append(item)
|
||||
else:
|
||||
previously_passed.append(item)
|
||||
self._previously_failed_count = len(previously_failed)
|
||||
|
||||
if not previously_failed:
|
||||
# Running a subset of all tests with recorded failures
|
||||
# only outside of it.
|
||||
self._report_status = (
|
||||
f"{len(self.lastfailed)} known failures not in selected tests"
|
||||
)
|
||||
else:
|
||||
if self.config.getoption("lf"):
|
||||
items[:] = previously_failed
|
||||
config.hook.pytest_deselected(items=previously_passed)
|
||||
else: # --failedfirst
|
||||
items[:] = previously_failed + previously_passed
|
||||
|
||||
noun = "failure" if self._previously_failed_count == 1 else "failures"
|
||||
suffix = " first" if self.config.getoption("failedfirst") else ""
|
||||
self._report_status = (
|
||||
f"rerun previous {self._previously_failed_count} {noun}{suffix}"
|
||||
)
|
||||
|
||||
if self._skipped_files > 0:
|
||||
files_noun = "file" if self._skipped_files == 1 else "files"
|
||||
self._report_status += f" (skipped {self._skipped_files} {files_noun})"
|
||||
else:
|
||||
self._report_status = "no previously failed tests, "
|
||||
if self.config.getoption("last_failed_no_failures") == "none":
|
||||
self._report_status += "deselecting all items."
|
||||
config.hook.pytest_deselected(items=items[:])
|
||||
items[:] = []
|
||||
else:
|
||||
self._report_status += "not deselecting items."
|
||||
|
||||
return res
|
||||
|
||||
def pytest_sessionfinish(self, session: Session) -> None:
|
||||
config = self.config
|
||||
if config.getoption("cacheshow") or hasattr(config, "workerinput"):
|
||||
return
|
||||
|
||||
assert config.cache is not None
|
||||
current_lastfailed = {str(k): v for k, v in self.lastfailed.items()}
|
||||
saved_lastfailed = config.cache.get("cache/lastfailed", {})
|
||||
if saved_lastfailed != current_lastfailed:
|
||||
config.cache.set("cache/lastfailed", current_lastfailed)
|
||||
|
||||
|
||||
class NFPlugin:
|
||||
"""Plugin which implements the --nf (run new-first) option."""
|
||||
|
||||
def __init__(self, config: Config) -> None:
|
||||
self.config = config
|
||||
self.active = config.option.newfirst
|
||||
assert config.cache is not None
|
||||
self.cached_nodeids: set[NodeId] = {
|
||||
NodeId.parse(s) for s in config.cache.get("cache/nodeids", [])
|
||||
}
|
||||
|
||||
@hookimpl(wrapper=True, tryfirst=True)
|
||||
def pytest_collection_modifyitems(self, items: list[nodes.Item]) -> Generator[None]:
|
||||
res = yield
|
||||
|
||||
if self.active:
|
||||
new_items: dict[NodeId, nodes.Item] = {}
|
||||
other_items: dict[NodeId, nodes.Item] = {}
|
||||
for item in items:
|
||||
if item.id not in self.cached_nodeids:
|
||||
new_items[item.id] = item
|
||||
else:
|
||||
other_items[item.id] = item
|
||||
|
||||
items[:] = self._get_increasing_order(
|
||||
new_items.values()
|
||||
) + self._get_increasing_order(other_items.values())
|
||||
self.cached_nodeids.update(new_items)
|
||||
else:
|
||||
self.cached_nodeids.update(item.id for item in items)
|
||||
|
||||
return res
|
||||
|
||||
def _get_increasing_order(self, items: Iterable[nodes.Item]) -> list[nodes.Item]:
|
||||
return sorted(items, key=lambda item: item.path.stat().st_mtime, reverse=True)
|
||||
|
||||
def pytest_sessionfinish(self) -> None:
|
||||
config = self.config
|
||||
if config.getoption("cacheshow") or hasattr(config, "workerinput"):
|
||||
return
|
||||
|
||||
if config.getoption("collectonly"):
|
||||
return
|
||||
|
||||
assert config.cache is not None
|
||||
config.cache.set("cache/nodeids", sorted(str(n) for n in self.cached_nodeids))
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
"""Add command-line options for cache functionality.
|
||||
|
||||
:param parser: Parser object to add command-line options to.
|
||||
"""
|
||||
group = parser.getgroup("general")
|
||||
group.addoption(
|
||||
"--lf",
|
||||
"--last-failed",
|
||||
action="store_true",
|
||||
dest="lf",
|
||||
help="Rerun only the tests that failed at the last run (or all if none failed)",
|
||||
)
|
||||
group.addoption(
|
||||
"--ff",
|
||||
"--failed-first",
|
||||
action="store_true",
|
||||
dest="failedfirst",
|
||||
help="Run all tests, but run the last failures first. "
|
||||
"This may re-order tests and thus lead to "
|
||||
"repeated fixture setup/teardown.",
|
||||
)
|
||||
group.addoption(
|
||||
"--nf",
|
||||
"--new-first",
|
||||
action="store_true",
|
||||
dest="newfirst",
|
||||
help="Run tests from new files first, then the rest of the tests "
|
||||
"sorted by file mtime",
|
||||
)
|
||||
group.addoption(
|
||||
"--cache-show",
|
||||
action="append",
|
||||
nargs="?",
|
||||
dest="cacheshow",
|
||||
help=(
|
||||
"Show cache contents, don't perform collection or tests. "
|
||||
"Optional argument: glob (default: '*')."
|
||||
),
|
||||
)
|
||||
group.addoption(
|
||||
"--cache-clear",
|
||||
action="store_true",
|
||||
dest="cacheclear",
|
||||
help="Remove all cache contents at start of test run",
|
||||
)
|
||||
cache_dir_default = ".pytest_cache"
|
||||
if "TOX_ENV_DIR" in os.environ:
|
||||
cache_dir_default = os.path.join(os.environ["TOX_ENV_DIR"], cache_dir_default)
|
||||
parser.addini("cache_dir", default=cache_dir_default, help="Cache directory path")
|
||||
group.addoption(
|
||||
"--lfnf",
|
||||
"--last-failed-no-failures",
|
||||
action="store",
|
||||
dest="last_failed_no_failures",
|
||||
choices=("all", "none"),
|
||||
default="all",
|
||||
help="With ``--lf``, determines whether to execute tests when there "
|
||||
"are no previously (known) failures or when no "
|
||||
"cached ``lastfailed`` data was found. "
|
||||
"``all`` (the default) runs the full test suite again. "
|
||||
"``none`` just emits a message about no known failures and exits successfully.",
|
||||
)
|
||||
|
||||
|
||||
def pytest_cmdline_main(config: Config) -> int | ExitCode | None:
|
||||
if config.option.cacheshow and not config.option.help:
|
||||
from _pytest.main import wrap_session
|
||||
|
||||
return wrap_session(config, cacheshow)
|
||||
return None
|
||||
|
||||
|
||||
@hookimpl(tryfirst=True)
|
||||
def pytest_configure(config: Config) -> None:
|
||||
"""Configure cache system and register related plugins.
|
||||
|
||||
Creates the Cache instance and registers the last-failed (LFPlugin)
|
||||
and new-first (NFPlugin) plugins with the plugin manager.
|
||||
|
||||
:param config: pytest configuration object.
|
||||
"""
|
||||
config.cache = Cache.for_config(config, _ispytest=True)
|
||||
config.pluginmanager.register(LFPlugin(config), "lfplugin")
|
||||
config.pluginmanager.register(NFPlugin(config), "nfplugin")
|
||||
|
||||
|
||||
@fixture
|
||||
def cache(request: FixtureRequest) -> Cache:
|
||||
"""Return a cache object that can persist state between testing sessions.
|
||||
|
||||
cache.get(key, default)
|
||||
cache.set(key, value)
|
||||
|
||||
Keys must be ``/`` separated strings, where the first part is usually the
|
||||
name of your plugin or application to avoid clashes with other cache users.
|
||||
|
||||
Values can be any object handled by the json stdlib module.
|
||||
"""
|
||||
assert request.config.cache is not None
|
||||
return request.config.cache
|
||||
|
||||
|
||||
def pytest_report_header(config: Config) -> str | None:
|
||||
"""Display cachedir with --cache-show and if non-default."""
|
||||
if config.option.verbose > 0 or config.getini("cache_dir") != ".pytest_cache":
|
||||
assert config.cache is not None
|
||||
cachedir = config.cache._cachedir
|
||||
# TODO: evaluate generating upward relative paths
|
||||
# starting with .., ../.. if sensible
|
||||
|
||||
try:
|
||||
displaypath = cachedir.relative_to(config.rootpath)
|
||||
except ValueError:
|
||||
displaypath = cachedir
|
||||
return f"cachedir: {displaypath}"
|
||||
return None
|
||||
|
||||
|
||||
def cacheshow(config: Config, session: Session) -> int:
|
||||
"""Display cache contents when --cache-show is used.
|
||||
|
||||
Shows cached values and directories matching the specified glob pattern
|
||||
(default: '*'). Displays cache location, cached test results, and
|
||||
any cached directories created by plugins.
|
||||
|
||||
:param config: pytest configuration object.
|
||||
:param session: pytest session object.
|
||||
:returns: Exit code (0 for success).
|
||||
"""
|
||||
from pprint import pformat
|
||||
|
||||
assert config.cache is not None
|
||||
|
||||
tw = TerminalWriter()
|
||||
tw.line("cachedir: " + str(config.cache._cachedir))
|
||||
if not config.cache._cachedir.is_dir():
|
||||
tw.line("cache is empty")
|
||||
return 0
|
||||
|
||||
glob = config.option.cacheshow[0]
|
||||
if glob is None:
|
||||
glob = "*"
|
||||
|
||||
def globfiles(base: Path) -> Iterable[Path]:
|
||||
"""Glob for files under `base`, discarding matches that escape it.
|
||||
|
||||
A glob may contain `..` segments, which `rglob()` happily follows.
|
||||
"""
|
||||
for x in base.rglob(glob):
|
||||
if x.is_file() and Path(os.path.normpath(x)).is_relative_to(base):
|
||||
yield x
|
||||
|
||||
dummy = object()
|
||||
basedir = config.cache._cachedir
|
||||
vdir = basedir / Cache._CACHE_PREFIX_VALUES
|
||||
tw.sep("-", f"cache values for {glob!r}")
|
||||
for valpath in sorted(globfiles(vdir)):
|
||||
key = str(valpath.relative_to(vdir))
|
||||
val = config.cache.get(key, dummy)
|
||||
if val is dummy:
|
||||
tw.line(f"{key} contains unreadable content, will be ignored")
|
||||
else:
|
||||
tw.line(f"{key} contains:")
|
||||
for line in pformat(val).splitlines():
|
||||
tw.line(" " + line)
|
||||
|
||||
ddir = basedir / Cache._CACHE_PREFIX_DIRS
|
||||
if ddir.is_dir():
|
||||
tw.sep("-", f"cache directories for {glob!r}")
|
||||
for p in sorted(globfiles(ddir)):
|
||||
key = str(p.relative_to(basedir))
|
||||
tw.line(f"{key} is a file of length {p.stat().st_size}")
|
||||
return 0
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,330 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Python version compatibility code and random general utilities."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
import enum
|
||||
import functools
|
||||
import inspect
|
||||
from inspect import Parameter
|
||||
from inspect import Signature
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing import Final
|
||||
from typing import NoReturn
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import py
|
||||
|
||||
|
||||
if sys.version_info >= (3, 14):
|
||||
from annotationlib import Format
|
||||
|
||||
|
||||
#: constant to prepare valuing pylib path replacements/lazy proxies later on
|
||||
# intended for removal in pytest 8.0 or 9.0
|
||||
|
||||
# fmt: off
|
||||
# intentional space to create a fake difference for the verification
|
||||
LEGACY_PATH = py.path. local
|
||||
# fmt: on
|
||||
|
||||
|
||||
def legacy_path(path: str | os.PathLike[str]) -> LEGACY_PATH:
|
||||
"""Internal wrapper to prepare lazy proxies for legacy_path instances"""
|
||||
return LEGACY_PATH(path)
|
||||
|
||||
|
||||
# fmt: off
|
||||
# Singleton type for NOTSET, as described in:
|
||||
# https://www.python.org/dev/peps/pep-0484/#support-for-singleton-types-in-unions
|
||||
class NotSetType(enum.Enum):
|
||||
token = 0
|
||||
NOTSET: Final = NotSetType.token
|
||||
# fmt: on
|
||||
|
||||
|
||||
def iscoroutinefunction(func: object) -> bool:
|
||||
"""Return True if func is a coroutine function (a function defined with async
|
||||
def syntax, and doesn't contain yield), or a function decorated with
|
||||
@asyncio.coroutine.
|
||||
|
||||
Note: copied and modified from Python 3.5's builtin coroutines.py to avoid
|
||||
importing asyncio directly, which in turns also initializes the "logging"
|
||||
module as a side-effect (see issue #8).
|
||||
"""
|
||||
return inspect.iscoroutinefunction(func) or getattr(func, "_is_coroutine", False)
|
||||
|
||||
|
||||
def is_async_function(func: object) -> bool:
|
||||
"""Return True if the given function seems to be an async function or
|
||||
an async generator."""
|
||||
return iscoroutinefunction(func) or inspect.isasyncgenfunction(func)
|
||||
|
||||
|
||||
def signature(obj: Callable[..., Any]) -> Signature:
|
||||
"""Return signature without evaluating annotations."""
|
||||
if sys.version_info >= (3, 14):
|
||||
return inspect.signature(obj, annotation_format=Format.STRING)
|
||||
return inspect.signature(obj)
|
||||
|
||||
|
||||
def getlocation(function, curdir: str | os.PathLike[str] | None = None) -> str:
|
||||
function = get_real_func(function)
|
||||
fn = Path(inspect.getfile(function))
|
||||
lineno = function.__code__.co_firstlineno
|
||||
if curdir is not None:
|
||||
try:
|
||||
relfn = fn.relative_to(curdir)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
return f"{relfn}:{lineno}"
|
||||
return f"{fn}:{lineno}"
|
||||
|
||||
|
||||
def num_mock_patch_args(function) -> int:
|
||||
"""Return number of arguments used up by mock arguments (if any)."""
|
||||
patchings = getattr(function, "patchings", None)
|
||||
if not patchings:
|
||||
return 0
|
||||
|
||||
mock_sentinel = getattr(sys.modules.get("mock"), "DEFAULT", object())
|
||||
ut_mock_sentinel = getattr(sys.modules.get("unittest.mock"), "DEFAULT", object())
|
||||
|
||||
return len(
|
||||
[
|
||||
p
|
||||
for p in patchings
|
||||
if not p.attribute_name
|
||||
and (p.new is mock_sentinel or p.new is ut_mock_sentinel)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def getfuncargnames(
|
||||
function: Callable[..., object],
|
||||
*,
|
||||
name: str = "",
|
||||
cls: type | None = None,
|
||||
) -> tuple[str, ...]:
|
||||
"""Return the names of a function's mandatory arguments.
|
||||
|
||||
Should return the names of all function arguments that:
|
||||
* Aren't bound to an instance or type as in instance or class methods.
|
||||
* Don't have default values.
|
||||
* Aren't bound with functools.partial.
|
||||
* Aren't replaced with mocks.
|
||||
|
||||
The cls arguments indicate that the function should be treated as a bound
|
||||
method even though it's not unless the function is a static method.
|
||||
|
||||
The name parameter should be the original name in which the function was collected.
|
||||
"""
|
||||
# TODO(RonnyPfannschmidt): This function should be refactored when we
|
||||
# revisit fixtures. The fixture mechanism should ask the node for
|
||||
# the fixture names, and not try to obtain directly from the
|
||||
# function object well after collection has occurred.
|
||||
|
||||
# The parameters attribute of a Signature object contains an
|
||||
# ordered mapping of parameter names to Parameter instances. This
|
||||
# creates a tuple of the names of the parameters that don't have
|
||||
# defaults.
|
||||
try:
|
||||
parameters = signature(function).parameters.values()
|
||||
except (ValueError, TypeError) as e:
|
||||
from _pytest.outcomes import fail
|
||||
|
||||
fail(
|
||||
f"Could not determine arguments of {function!r}: {e}",
|
||||
pytrace=False,
|
||||
)
|
||||
|
||||
arg_names = tuple(
|
||||
p.name
|
||||
for p in parameters
|
||||
if (
|
||||
p.kind is Parameter.POSITIONAL_OR_KEYWORD
|
||||
or p.kind is Parameter.KEYWORD_ONLY
|
||||
)
|
||||
and p.default is Parameter.empty
|
||||
)
|
||||
if not name:
|
||||
name = function.__name__
|
||||
|
||||
# If this function should be treated as a bound method even though
|
||||
# it's passed as an unbound method or function, and its first parameter
|
||||
# wasn't defined as positional only, remove the first parameter name.
|
||||
if not any(p.kind is Parameter.POSITIONAL_ONLY for p in parameters) and (
|
||||
# Not using `getattr` because we don't want to resolve the staticmethod.
|
||||
# Not using `cls.__dict__` because we want to check the entire MRO.
|
||||
cls
|
||||
and not inspect.ismethod(function)
|
||||
and not isinstance(
|
||||
inspect.getattr_static(cls, name, default=None), staticmethod
|
||||
)
|
||||
):
|
||||
arg_names = arg_names[1:]
|
||||
# Remove any names that will be replaced with mocks.
|
||||
if hasattr(function, "__wrapped__"):
|
||||
arg_names = arg_names[num_mock_patch_args(function) :]
|
||||
return arg_names
|
||||
|
||||
|
||||
def get_default_arg_names(function: Callable[..., Any]) -> tuple[str, ...]:
|
||||
# Note: this code intentionally mirrors the code at the beginning of
|
||||
# getfuncargnames, to get the arguments which were excluded from its result
|
||||
# because they had default values.
|
||||
return tuple(
|
||||
p.name
|
||||
for p in signature(function).parameters.values()
|
||||
if p.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY)
|
||||
and p.default is not Parameter.empty
|
||||
)
|
||||
|
||||
|
||||
_non_printable_ascii_translate_table = {
|
||||
i: f"\\x{i:02x}" for i in range(128) if i not in range(32, 127)
|
||||
}
|
||||
_non_printable_ascii_translate_table.update(
|
||||
{ord("\t"): "\\t", ord("\r"): "\\r", ord("\n"): "\\n"}
|
||||
)
|
||||
|
||||
|
||||
def ascii_escaped(val: bytes | str) -> str:
|
||||
r"""If val is pure ASCII, return it as an str, otherwise, escape
|
||||
bytes objects into a sequence of escaped bytes:
|
||||
|
||||
b'\xc3\xb4\xc5\xd6' -> r'\xc3\xb4\xc5\xd6'
|
||||
|
||||
and escapes strings into a sequence of escaped unicode ids, e.g.:
|
||||
|
||||
r'4\nV\U00043efa\x0eMXWB\x1e\u3028\u15fd\xcd\U0007d944'
|
||||
|
||||
Note:
|
||||
The obvious "v.decode('unicode-escape')" will return
|
||||
valid UTF-8 unicode if it finds them in bytes, but we
|
||||
want to return escaped bytes for any byte, even if they match
|
||||
a UTF-8 string.
|
||||
"""
|
||||
if isinstance(val, bytes):
|
||||
ret = val.decode("ascii", "backslashreplace")
|
||||
else:
|
||||
ret = val.encode("unicode_escape").decode("ascii")
|
||||
return ret.translate(_non_printable_ascii_translate_table)
|
||||
|
||||
|
||||
def get_real_func(obj):
|
||||
"""Get the real function object of the (possibly) wrapped object by
|
||||
:func:`functools.wraps`, or :func:`functools.partial`."""
|
||||
obj = inspect.unwrap(obj)
|
||||
|
||||
if isinstance(obj, functools.partial):
|
||||
obj = obj.func
|
||||
return obj
|
||||
|
||||
|
||||
def getimfunc(func):
|
||||
try:
|
||||
return func.__func__
|
||||
except AttributeError:
|
||||
return func
|
||||
|
||||
|
||||
def safe_getattr(object: Any, name: str, default: Any) -> Any:
|
||||
"""Like getattr but return default upon any Exception or any OutcomeException.
|
||||
|
||||
Attribute access can potentially fail for 'evil' Python objects.
|
||||
See issue #214.
|
||||
It catches OutcomeException because of #2490 (issue #580), new outcomes
|
||||
are derived from BaseException instead of Exception (for more details
|
||||
check #2707).
|
||||
"""
|
||||
from _pytest.outcomes import TEST_OUTCOME
|
||||
|
||||
try:
|
||||
return getattr(object, name, default)
|
||||
except TEST_OUTCOME:
|
||||
return default
|
||||
|
||||
|
||||
def safe_isclass(obj: object) -> bool:
|
||||
"""Ignore any exception via isinstance on Python 3."""
|
||||
try:
|
||||
return inspect.isclass(obj)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def get_user_id() -> int | None:
|
||||
"""Return the current process's real user id or None if it could not be
|
||||
determined.
|
||||
|
||||
:return: The user id or None if it could not be determined.
|
||||
"""
|
||||
# mypy follows the version and platform checking expectation of PEP 484:
|
||||
# https://mypy.readthedocs.io/en/stable/common_issues.html?highlight=platform#python-version-and-system-platform-checks
|
||||
# Containment checks are too complex for mypy v1.5.0 and cause failure.
|
||||
if sys.platform == "win32" or sys.platform == "emscripten":
|
||||
# win32 does not have a getuid() function.
|
||||
# Emscripten has a return 0 stub.
|
||||
return None
|
||||
else:
|
||||
# On other platforms, a return value of -1 is assumed to indicate that
|
||||
# the current process's real user id could not be determined.
|
||||
ERROR = -1
|
||||
uid = os.getuid()
|
||||
return uid if uid != ERROR else None
|
||||
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
from typing import assert_never
|
||||
else:
|
||||
|
||||
def assert_never(value: NoReturn) -> NoReturn:
|
||||
assert False, f"Unhandled value: {value} ({type(value).__name__})"
|
||||
|
||||
|
||||
class CallableBool:
|
||||
"""
|
||||
A bool-like object that can also be called, returning its true/false value.
|
||||
|
||||
Used for backwards compatibility in cases where something was supposed to be a method
|
||||
but was implemented as a simple attribute by mistake (see `TerminalReporter.isatty`).
|
||||
|
||||
Do not use in new code.
|
||||
"""
|
||||
|
||||
def __init__(self, value: bool) -> None:
|
||||
self._value = value
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
return self._value
|
||||
|
||||
def __call__(self) -> bool:
|
||||
return self._value
|
||||
|
||||
|
||||
def running_on_ci() -> bool:
|
||||
"""Check if we're currently running on a CI system."""
|
||||
# Only enable CI mode if one of these env variables is defined and non-empty.
|
||||
# Note: review `regendoc` tox env in case this list is changed.
|
||||
env_vars = ["CI", "BUILD_NUMBER"]
|
||||
return any(os.environ.get(var) for var in env_vars)
|
||||
|
||||
|
||||
if sys.version_info >= (3, 13): # pragma: no cover
|
||||
from warnings import deprecated as deprecated
|
||||
else: # pragma: no cover
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import deprecated as deprecated
|
||||
else:
|
||||
|
||||
def deprecated(msg, /, *, category=None, stacklevel=1):
|
||||
def decorator(func):
|
||||
return func
|
||||
|
||||
return decorator
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,659 @@
|
||||
# mypy: allow-untyped-defs
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Sequence
|
||||
import dataclasses
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import textwrap
|
||||
import types
|
||||
from typing import Any
|
||||
from typing import final
|
||||
from typing import get_args
|
||||
from typing import get_origin
|
||||
from typing import Literal
|
||||
from typing import NoReturn
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TypeAlias
|
||||
from typing import Union
|
||||
|
||||
from .exceptions import UsageError
|
||||
import _pytest._io
|
||||
from _pytest.compat import NOTSET
|
||||
from _pytest.deprecated import check_ispytest
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import TypeForm
|
||||
|
||||
|
||||
FILE_OR_DIR = "file_or_dir"
|
||||
|
||||
#: The string tags accepted by :meth:`Parser.addini` for its ``type`` argument.
|
||||
_IniTypeTag: TypeAlias = Literal[
|
||||
"string", "paths", "pathlist", "args", "linelist", "bool", "int", "float"
|
||||
]
|
||||
|
||||
if TYPE_CHECKING:
|
||||
#: The forms accepted by :meth:`Parser.addini` for its ``type`` argument;
|
||||
#: ``None`` means ``"string"``.
|
||||
_IniTypeArg: TypeAlias = _IniTypeTag | TypeForm[bool | int | float | str] | None
|
||||
|
||||
|
||||
@final
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class _IniLiteral:
|
||||
"""The choices of an ini option registered with a ``Literal`` type."""
|
||||
|
||||
choices: tuple[str, ...]
|
||||
|
||||
|
||||
#: An ini option type, as stored internally after normalization: a single tag,
|
||||
#: the choices of a ``Literal`` type, or a tuple of members meaning "accept a
|
||||
#: value of any of these" (e.g. ``("int", "string")``, normalized from
|
||||
#: ``int | str``).
|
||||
IniType: TypeAlias = _IniTypeTag | _IniLiteral | tuple[_IniTypeTag | _IniLiteral, ...]
|
||||
|
||||
#: Maps each string tag or plain Python type accepted by :meth:`Parser.addini`
|
||||
#: for its ``type`` argument to the normalized string tag.
|
||||
_INI_TYPES: dict[object, _IniTypeTag] = {tag: tag for tag in get_args(_IniTypeTag)} | {
|
||||
str: "string",
|
||||
bool: "bool",
|
||||
int: "int",
|
||||
float: "float",
|
||||
}
|
||||
|
||||
|
||||
def _ini_type_to_tag(name: str, type_: object) -> _IniTypeTag:
|
||||
"""Normalize one member of an `addini(type=...)` argument to a string tag."""
|
||||
try:
|
||||
return _INI_TYPES[type_]
|
||||
except (KeyError, TypeError): # TypeError: unhashable type_
|
||||
raise ValueError(
|
||||
f"invalid type for ini option {name!r}: {type_!r} (expected one of "
|
||||
f"{', '.join(repr(tag) for tag in get_args(_IniTypeTag))}, one of "
|
||||
"the types str, bool, int, float, a union of these types such as "
|
||||
"`int | str`, or a `Literal` of strings)"
|
||||
) from None
|
||||
|
||||
|
||||
def _ini_type_to_member(name: str, type_: object) -> _IniTypeTag | _IniLiteral:
|
||||
"""Normalize one member of an `addini(type=...)` argument."""
|
||||
if get_origin(type_) is Literal:
|
||||
choices = get_args(type_)
|
||||
if not all(isinstance(choice, str) for choice in choices):
|
||||
raise ValueError(
|
||||
f"invalid type for ini option {name!r}: Literal choices "
|
||||
f"must be strings, got {choices!r}"
|
||||
)
|
||||
return _IniLiteral(choices)
|
||||
return _ini_type_to_tag(name, type_)
|
||||
|
||||
|
||||
def _ini_type_repr(type: IniType) -> str:
|
||||
"""Render an ini option type for --help output and error messages."""
|
||||
if isinstance(type, _IniLiteral):
|
||||
return " | ".join(repr(choice) for choice in type.choices)
|
||||
if isinstance(type, tuple):
|
||||
return " | ".join(_ini_type_repr(member) for member in type)
|
||||
return type
|
||||
|
||||
|
||||
def _get_argparse_dest(opts: Sequence[str]) -> str:
|
||||
long_opts = [opt for opt in opts if opt.startswith("--")]
|
||||
opt = long_opts[0] if long_opts else opts[0]
|
||||
return opt.lstrip("-").replace("-", "_")
|
||||
|
||||
|
||||
@final
|
||||
class Parser:
|
||||
"""Parser for command line arguments and config-file values.
|
||||
|
||||
:ivar extra_info: Dict of generic param -> value to display in case
|
||||
there's an error processing the command line arguments.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
usage: str | None = None,
|
||||
processopt: Callable[[Argument], None] | None = None,
|
||||
*,
|
||||
prog: str | None = None,
|
||||
_ispytest: bool = False,
|
||||
) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
|
||||
from _pytest._argcomplete import filescompleter
|
||||
|
||||
self._processopt = processopt
|
||||
self.extra_info: dict[str, Any] = {}
|
||||
self.optparser = PytestArgumentParser(usage, self.extra_info, prog=prog)
|
||||
anonymous_arggroup = self.optparser.add_argument_group("Custom options")
|
||||
self._anonymous = OptionGroup(
|
||||
anonymous_arggroup, "_anonymous", self, _ispytest=True
|
||||
)
|
||||
self._groups = [self._anonymous]
|
||||
# Maps option strings -> dest, e.g. "-V" and "--version" to "version".
|
||||
self._opt2dest: dict[str, str] = {}
|
||||
file_or_dir_arg = self.optparser.add_argument(FILE_OR_DIR, nargs="*")
|
||||
file_or_dir_arg.completer = filescompleter # type: ignore
|
||||
|
||||
self._inidict: dict[str, tuple[str, IniType, Any]] = {}
|
||||
# Maps alias -> canonical name.
|
||||
self._ini_aliases: dict[str, str] = {}
|
||||
|
||||
@property
|
||||
def prog(self) -> str:
|
||||
return self.optparser.prog
|
||||
|
||||
@prog.setter
|
||||
def prog(self, value: str) -> None:
|
||||
self.optparser.prog = value
|
||||
|
||||
def processoption(self, option: Argument) -> None:
|
||||
if self._processopt:
|
||||
if option.dest:
|
||||
self._processopt(option)
|
||||
|
||||
def getgroup(
|
||||
self, name: str, description: str = "", after: str | None = None
|
||||
) -> OptionGroup:
|
||||
"""Get (or create) a named option Group.
|
||||
|
||||
:param name: Name of the option group.
|
||||
:param description: Long description for --help output.
|
||||
:param after: Name of another group, used for ordering --help output.
|
||||
:returns: The option group.
|
||||
|
||||
The returned group object has an ``addoption`` method with the same
|
||||
signature as :func:`parser.addoption <pytest.Parser.addoption>` but
|
||||
will be shown in the respective group in the output of
|
||||
``pytest --help``.
|
||||
"""
|
||||
for group in self._groups:
|
||||
if group.name == name:
|
||||
return group
|
||||
|
||||
arggroup = self.optparser.add_argument_group(description or name)
|
||||
group = OptionGroup(arggroup, name, self, _ispytest=True)
|
||||
i = 0
|
||||
for i, grp in enumerate(self._groups):
|
||||
if grp.name == after:
|
||||
break
|
||||
self._groups.insert(i + 1, group)
|
||||
# argparse doesn't provide a way to control `--help` order, so must
|
||||
# access its internals ☹.
|
||||
self.optparser._action_groups.insert(i + 1, self.optparser._action_groups.pop())
|
||||
return group
|
||||
|
||||
def addoption(self, *opts: str, **attrs: Any) -> None:
|
||||
"""Register a command line option.
|
||||
|
||||
:param opts:
|
||||
Option names, can be short or long options.
|
||||
:param attrs:
|
||||
Same attributes as the argparse library's :meth:`add_argument()
|
||||
<argparse.ArgumentParser.add_argument>` function accepts.
|
||||
|
||||
After command line parsing, options are available on the pytest config
|
||||
object via ``config.option.NAME`` where ``NAME`` is usually set
|
||||
by passing a ``dest`` attribute, for example
|
||||
``addoption("--long", dest="NAME", ...)``.
|
||||
"""
|
||||
self._anonymous.addoption(*opts, **attrs)
|
||||
|
||||
def parse(
|
||||
self,
|
||||
args: Sequence[str | os.PathLike[str]],
|
||||
namespace: argparse.Namespace | None = None,
|
||||
) -> argparse.Namespace:
|
||||
"""Parse the arguments.
|
||||
|
||||
Unlike ``parse_known_args`` and ``parse_known_and_unknown_args``,
|
||||
raises PrintHelp on `--help` and UsageError on unknown flags
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
from _pytest._argcomplete import try_argcomplete
|
||||
|
||||
try_argcomplete(self.optparser)
|
||||
strargs = [os.fspath(x) for x in args]
|
||||
if namespace is None:
|
||||
namespace = argparse.Namespace()
|
||||
try:
|
||||
namespace._raise_print_help = True
|
||||
return self.optparser.parse_intermixed_args(strargs, namespace=namespace)
|
||||
finally:
|
||||
del namespace._raise_print_help
|
||||
|
||||
def parse_known_args(
|
||||
self,
|
||||
args: Sequence[str | os.PathLike[str]],
|
||||
namespace: argparse.Namespace | None = None,
|
||||
) -> argparse.Namespace:
|
||||
"""Parse the known arguments at this point.
|
||||
|
||||
:returns: An argparse namespace object.
|
||||
"""
|
||||
return self.parse_known_and_unknown_args(args, namespace=namespace)[0]
|
||||
|
||||
def parse_known_and_unknown_args(
|
||||
self,
|
||||
args: Sequence[str | os.PathLike[str]],
|
||||
namespace: argparse.Namespace | None = None,
|
||||
) -> tuple[argparse.Namespace, list[str]]:
|
||||
"""Parse the known arguments at this point, and also return the
|
||||
remaining unknown flag arguments.
|
||||
|
||||
:returns:
|
||||
A tuple containing an argparse namespace object for the known
|
||||
arguments, and a list of unknown flag arguments.
|
||||
"""
|
||||
strargs = [os.fspath(x) for x in args]
|
||||
if sys.version_info < (3, 12, 8) or (3, 13) <= sys.version_info < (3, 13, 1):
|
||||
# Older argparse have a bugged parse_known_intermixed_args.
|
||||
namespace, unknown = self.optparser.parse_known_args(strargs, namespace)
|
||||
assert namespace is not None
|
||||
file_or_dir = getattr(namespace, FILE_OR_DIR)
|
||||
unknown_flags: list[str] = []
|
||||
for arg in unknown:
|
||||
(unknown_flags if arg.startswith("-") else file_or_dir).append(arg)
|
||||
return namespace, unknown_flags
|
||||
else:
|
||||
return self.optparser.parse_known_intermixed_args(strargs, namespace)
|
||||
|
||||
def addini(
|
||||
self,
|
||||
name: str,
|
||||
help: str,
|
||||
type: _IniTypeArg = None,
|
||||
default: Any = NOTSET,
|
||||
*,
|
||||
aliases: Sequence[str] = (),
|
||||
) -> None:
|
||||
"""Register a configuration file option.
|
||||
|
||||
:param name:
|
||||
Name of the configuration.
|
||||
:param type:
|
||||
Type of the configuration. Can be:
|
||||
|
||||
* ``string``: a string
|
||||
* ``bool``: a boolean
|
||||
* ``args``: a list of strings, separated as in a shell
|
||||
* ``linelist``: a list of strings, separated by line breaks
|
||||
* ``paths``: a list of :class:`pathlib.Path`, separated as in a shell
|
||||
* ``pathlist``: a list of ``py.path``, separated as in a shell
|
||||
* ``int``: an integer
|
||||
* ``float``: a floating-point number
|
||||
|
||||
.. versionadded:: 8.4
|
||||
|
||||
The ``float`` and ``int`` types.
|
||||
|
||||
For the scalar types, the plain Python type may be passed instead
|
||||
of the string tag: ``str``, ``bool``, ``int`` and ``float`` (for
|
||||
example ``type=int``). A union of these types accepts a value of
|
||||
any of its members, for example ``int | str``. In TOML
|
||||
configuration files the value may then be any of the member types;
|
||||
string-based formats (INI files, ``-o`` overrides) coerce it to the
|
||||
first member that accepts it.
|
||||
|
||||
A ``Literal`` type of strings restricts the value to the given
|
||||
choices, for example ``Literal["auto", "long", "short"]``, and may
|
||||
also be a union member, for example ``int | Literal["auto"]``.
|
||||
Since the choices have no unambiguous implicit default, an
|
||||
explicit ``default`` must be passed.
|
||||
|
||||
.. versionadded:: 9.2
|
||||
|
||||
Passing a type expression such as ``int``, ``int | str``, or
|
||||
a ``Literal`` of strings.
|
||||
|
||||
For ``paths`` and ``pathlist`` types, they are considered relative to the config-file.
|
||||
In case the execution is happening without a config-file defined,
|
||||
they will be considered relative to the current working directory (for example with ``--override-ini``).
|
||||
|
||||
.. versionadded:: 7.0
|
||||
The ``paths`` variable type.
|
||||
|
||||
.. versionadded:: 8.1
|
||||
Use the current working directory to resolve ``paths`` and ``pathlist`` in the absence of a config-file.
|
||||
|
||||
Defaults to ``string`` if ``None`` or not passed.
|
||||
:param default:
|
||||
Default value if no config-file option exists but is queried.
|
||||
:param aliases:
|
||||
Additional names by which this option can be referenced.
|
||||
Aliases resolve to the canonical name.
|
||||
|
||||
.. versionadded:: 9.0
|
||||
The ``aliases`` parameter.
|
||||
|
||||
The value of configuration keys can be retrieved via a call to
|
||||
:py:func:`config.getini(name) <pytest.Config.getini>`.
|
||||
"""
|
||||
ini_type: IniType
|
||||
if type is None:
|
||||
ini_type = "string"
|
||||
elif get_origin(type) in (Union, types.UnionType):
|
||||
ini_type = tuple(
|
||||
_ini_type_to_member(name, member) for member in get_args(type)
|
||||
)
|
||||
else:
|
||||
ini_type = _ini_type_to_member(name, type)
|
||||
if default is NOTSET:
|
||||
if isinstance(ini_type, (tuple, _IniLiteral)):
|
||||
kind = "union" if isinstance(ini_type, tuple) else "Literal"
|
||||
raise ValueError(
|
||||
f"ini option {name!r} has a {kind} type, which has no "
|
||||
"implicit default; pass an explicit `default` to `addini`"
|
||||
)
|
||||
default = get_ini_default_for_type(ini_type)
|
||||
|
||||
self._inidict[name] = (help, ini_type, default)
|
||||
|
||||
for alias in aliases:
|
||||
if alias in self._inidict:
|
||||
raise ValueError(
|
||||
f"alias {alias!r} conflicts with existing configuration option"
|
||||
)
|
||||
if (already := self._ini_aliases.get(alias)) is not None:
|
||||
raise ValueError(f"{alias!r} is already an alias of {already!r}")
|
||||
self._ini_aliases[alias] = name
|
||||
|
||||
|
||||
def get_ini_default_for_type(type: _IniTypeTag) -> Any:
|
||||
"""
|
||||
Used by addini to get the default value for a given config option type, when
|
||||
default is not supplied.
|
||||
"""
|
||||
if type in ("paths", "pathlist", "args", "linelist"):
|
||||
return []
|
||||
elif type == "bool":
|
||||
return False
|
||||
elif type == "int":
|
||||
return 0
|
||||
elif type == "float":
|
||||
return 0.0
|
||||
else:
|
||||
return ""
|
||||
|
||||
|
||||
class Argument:
|
||||
"""An option defined in an OptionGroup."""
|
||||
|
||||
def __init__(self, action: argparse.Action) -> None:
|
||||
self._action = action
|
||||
|
||||
def attrs(self) -> dict[str, Any]:
|
||||
return self._action.__dict__
|
||||
|
||||
def names(self) -> Sequence[str]:
|
||||
return self._action.option_strings
|
||||
|
||||
@property
|
||||
def dest(self) -> str:
|
||||
return self._action.dest
|
||||
|
||||
@property
|
||||
def default(self) -> Any:
|
||||
return self._action.default
|
||||
|
||||
@property
|
||||
def type(self) -> Any | None:
|
||||
return self._action.type
|
||||
|
||||
def __repr__(self) -> str:
|
||||
action = getattr(self, "_action", None)
|
||||
if action is None:
|
||||
return "Argument(<uninitialized>)"
|
||||
args: list[str] = []
|
||||
args += ["opts: " + repr(self.names())]
|
||||
args += ["dest: " + repr(self.dest)]
|
||||
if action.type:
|
||||
args += ["type: " + repr(self.type)]
|
||||
args += ["default: " + repr(self.default)]
|
||||
return "Argument({})".format(", ".join(args))
|
||||
|
||||
|
||||
class OptionGroup:
|
||||
"""A group of options shown in its own section."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
arggroup: argparse._ArgumentGroup,
|
||||
name: str,
|
||||
parser: Parser | None,
|
||||
_ispytest: bool = False,
|
||||
) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
self._arggroup = arggroup
|
||||
self.name = name
|
||||
self.options: list[Argument] = []
|
||||
self.parser = parser
|
||||
|
||||
def addoption(self, *opts: str, **attrs: Any) -> None:
|
||||
"""Add an option to this group.
|
||||
|
||||
If a shortened version of a long option is specified, it will
|
||||
be suppressed in the help. ``addoption('--twowords', '--two-words')``
|
||||
results in help showing ``--two-words`` only, but ``--twowords`` gets
|
||||
accepted **and** the automatic destination is in ``args.twowords``.
|
||||
|
||||
:param opts:
|
||||
Option names, can be short or long options.
|
||||
Note that lower-case short options (e.g. `-x`) are reserved.
|
||||
:param attrs:
|
||||
Same attributes as the argparse library's :meth:`add_argument()
|
||||
<argparse.ArgumentParser.add_argument>` function accepts.
|
||||
"""
|
||||
conflict = set(opts).intersection(
|
||||
name for opt in self.options for name in opt.names()
|
||||
)
|
||||
if conflict:
|
||||
raise ValueError(f"option names {conflict} already added")
|
||||
|
||||
if self.parser and "dest" not in attrs:
|
||||
dest = _get_argparse_dest(opts)
|
||||
for group in self.parser._groups:
|
||||
for option in group.options:
|
||||
if option.dest == dest:
|
||||
raise ValueError(
|
||||
f"option dest {dest!r} already used by "
|
||||
f"{option.names()!r} (this is the option that maps to "
|
||||
f"dest {dest!r}); pass dest={dest!r} explicitly "
|
||||
"to share the destination"
|
||||
)
|
||||
self._addoption_inner(opts, attrs, allow_reserved=False)
|
||||
|
||||
def _addoption(self, *opts: str, **attrs: Any) -> None:
|
||||
"""Like addoption(), but also allows registering short lower case options (e.g. -x),
|
||||
which are reserved for pytest core."""
|
||||
self._addoption_inner(opts, attrs, allow_reserved=True)
|
||||
|
||||
def _addoption_inner(
|
||||
self, opts: tuple[str, ...], attrs: dict[str, Any], allow_reserved: bool
|
||||
) -> None:
|
||||
if not allow_reserved:
|
||||
for opt in opts:
|
||||
if len(opt) >= 2 and opt[0] == "-" and opt[1].islower():
|
||||
raise ValueError("lowercase short options are reserved")
|
||||
|
||||
action = self._arggroup.add_argument(*opts, **attrs)
|
||||
option = Argument(action)
|
||||
self.options.append(option)
|
||||
if self.parser:
|
||||
for name in option.names():
|
||||
self.parser._opt2dest[name] = option.dest
|
||||
self.parser.processoption(option)
|
||||
|
||||
|
||||
class PytestArgumentParser(argparse.ArgumentParser):
|
||||
def __init__(
|
||||
self,
|
||||
usage: str | None,
|
||||
extra_info: dict[str, str],
|
||||
*,
|
||||
prog: str | None = None,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
usage=usage,
|
||||
prog=prog,
|
||||
add_help=False,
|
||||
formatter_class=DropShorterLongHelpFormatter,
|
||||
allow_abbrev=False,
|
||||
fromfile_prefix_chars="@",
|
||||
)
|
||||
# extra_info is a dict of (param -> value) to display if there's
|
||||
# an usage error to provide more contextual information to the user.
|
||||
self.extra_info = extra_info
|
||||
|
||||
def error(self, message: str) -> NoReturn:
|
||||
"""Transform argparse error message into UsageError."""
|
||||
# TODO(py313): Replace with `exit_on_error=False`. Note that while it
|
||||
# was added in Python 3.9, it was broken until 3.13 (cpython#121018).
|
||||
msg = f"{self.prog}: error: {message}"
|
||||
if self.extra_info:
|
||||
msg += "\n" + "\n".join(
|
||||
f" {k}: {v}" for k, v in sorted(self.extra_info.items())
|
||||
)
|
||||
raise UsageError(self.format_usage() + msg)
|
||||
|
||||
|
||||
class DropShorterLongHelpFormatter(argparse.HelpFormatter):
|
||||
"""Shorten help for long options that differ only in extra hyphens.
|
||||
|
||||
- Collapse **long** options that are the same except for extra hyphens.
|
||||
- Shortcut if there are only two options and one of them is a short one.
|
||||
"""
|
||||
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
# Use more accurate terminal width.
|
||||
if "width" not in kwargs:
|
||||
kwargs["width"] = _pytest._io.get_terminal_width()
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def _format_action_invocation(self, action: argparse.Action) -> str:
|
||||
orgstr = super()._format_action_invocation(action)
|
||||
if orgstr and orgstr[0] != "-": # only optional arguments
|
||||
return orgstr
|
||||
options = orgstr.split(", ")
|
||||
if len(options) == 2 and (len(options[0]) == 2 or len(options[1]) == 2):
|
||||
# a shortcut for '-h, --help' or '--abc', '-a'
|
||||
return orgstr
|
||||
return_list = []
|
||||
short_long: dict[str, str] = {}
|
||||
for option in options:
|
||||
if len(option) == 2 or option[2] == " ":
|
||||
continue
|
||||
assert option.startswith("--"), (
|
||||
f'long optional argument without "--": [{option}]'
|
||||
)
|
||||
xxoption = option[2:]
|
||||
shortened = xxoption.replace("-", "")
|
||||
if shortened not in short_long or len(short_long[shortened]) < len(
|
||||
xxoption
|
||||
):
|
||||
short_long[shortened] = xxoption
|
||||
# now short_long has been filled out to the longest with dashes
|
||||
# **and** we keep the right option ordering from add_argument
|
||||
for option in options:
|
||||
if len(option) == 2 or option[2] == " ":
|
||||
return_list.append(option)
|
||||
if option[2:] == short_long.get(option.replace("-", "")):
|
||||
return_list.append(option.replace(" ", "=", 1))
|
||||
return ", ".join(return_list)
|
||||
|
||||
def _split_lines(self, text: str, width: int) -> list[str]:
|
||||
return _split_help_text(text, width)
|
||||
|
||||
def _format_action(self, action: argparse.Action) -> str:
|
||||
# A blank line in a help text is padded out to the help column by
|
||||
# argparse; strip the trailing whitespace that leaves behind.
|
||||
return re.sub(
|
||||
r"[ \t]+$", "", super()._format_action(action), flags=re.MULTILINE
|
||||
)
|
||||
|
||||
|
||||
# A list item marker, e.g. "- ", "* ", "1. " or "(1) ".
|
||||
_BULLET = re.compile(r"^([-*+]|\(?\d+[.)])\s+")
|
||||
|
||||
|
||||
def _split_help_text(text: str, width: int) -> list[str]:
|
||||
"""Wrap help text to ``width`` while preserving its line structure.
|
||||
|
||||
Explicit line breaks are kept, so that a help text can use paragraphs and
|
||||
lists instead of being reflowed into one blob (see #6817). Each line keeps
|
||||
its own indentation, and the continuation lines of a list item are indented
|
||||
to hang under the item's text.
|
||||
"""
|
||||
# Dedent the body, but not the first line: it usually sits inline after the
|
||||
# opening quotes of a docstring-style help text, and so contributes no
|
||||
# common indentation of its own.
|
||||
first, newline, rest = text.partition("\n")
|
||||
lines: list[str] = []
|
||||
for line in (first.strip() + newline + textwrap.dedent(rest)).splitlines():
|
||||
stripped = line.lstrip()
|
||||
if not stripped:
|
||||
lines.append("")
|
||||
continue
|
||||
indent = line[: len(line) - len(stripped)]
|
||||
bullet = _BULLET.match(stripped)
|
||||
subsequent_indent = indent + " " * len(bullet.group()) if bullet else indent
|
||||
lines.extend(
|
||||
textwrap.wrap(
|
||||
stripped,
|
||||
width,
|
||||
initial_indent=indent,
|
||||
subsequent_indent=subsequent_indent,
|
||||
break_on_hyphens=False,
|
||||
)
|
||||
)
|
||||
# Leading and trailing blank lines would only offset the text from the
|
||||
# option it documents.
|
||||
while lines and not lines[0]:
|
||||
del lines[0]
|
||||
while lines and not lines[-1]:
|
||||
del lines[-1]
|
||||
return lines
|
||||
|
||||
|
||||
class OverrideIniAction(argparse.Action):
|
||||
"""Custom argparse action that makes a CLI flag equivalent to overriding an
|
||||
option, in addition to behaving like `store_true`.
|
||||
|
||||
This can simplify things since code only needs to inspect the config option
|
||||
and not consider the CLI flag.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
option_strings: Sequence[str],
|
||||
dest: str,
|
||||
nargs: int | str | None = None,
|
||||
*args,
|
||||
ini_option: str,
|
||||
ini_value: str,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
super().__init__(option_strings, dest, 0, *args, **kwargs)
|
||||
self.ini_option = ini_option
|
||||
self.ini_value = ini_value
|
||||
|
||||
def __call__(
|
||||
self,
|
||||
parser: argparse.ArgumentParser,
|
||||
namespace: argparse.Namespace,
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
setattr(namespace, self.dest, True)
|
||||
current_overrides = getattr(namespace, "override_ini", None)
|
||||
if current_overrides is None:
|
||||
current_overrides = []
|
||||
current_overrides.append(f"{self.ini_option}={self.ini_value}")
|
||||
setattr(namespace, "override_ini", current_overrides)
|
||||
@@ -0,0 +1,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import final
|
||||
|
||||
|
||||
@final
|
||||
class UsageError(Exception):
|
||||
"""Error in pytest usage or invocation."""
|
||||
|
||||
__module__ = "pytest"
|
||||
|
||||
|
||||
class PrintHelp(Exception):
|
||||
"""Raised when pytest should print its help to skip the rest of the
|
||||
argument parsing and validation."""
|
||||
@@ -0,0 +1,501 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import KW_ONLY
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import Literal
|
||||
from typing import TypeAlias
|
||||
|
||||
import iniconfig
|
||||
|
||||
from .exceptions import UsageError
|
||||
from _pytest.nodeid import NodeId
|
||||
from _pytest.outcomes import fail
|
||||
from _pytest.pathlib import absolutepath
|
||||
from _pytest.pathlib import commonpath
|
||||
from _pytest.pathlib import safe_exists
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ConfigValue:
|
||||
"""Represents a configuration value with its origin and parsing mode.
|
||||
|
||||
This allows tracking whether a value came from a configuration file
|
||||
or from a CLI override (--override-ini), which is important for
|
||||
determining precedence when dealing with ini option aliases.
|
||||
|
||||
The mode tracks the parsing mode/data model used for the value:
|
||||
- "ini": from INI files or [tool.pytest.ini_options], where the only
|
||||
supported value types are `str` or `list[str]`.
|
||||
- "toml": from TOML files (not in INI mode), where native TOML types
|
||||
are preserved.
|
||||
"""
|
||||
|
||||
value: object
|
||||
_: KW_ONLY
|
||||
origin: Literal["file", "override"]
|
||||
mode: Literal["ini", "toml"]
|
||||
|
||||
|
||||
ConfigDict: TypeAlias = dict[str, ConfigValue]
|
||||
|
||||
|
||||
def _parse_ini_config(path: Path) -> iniconfig.IniConfig:
|
||||
"""Parse the given generic '.ini' file using legacy IniConfig parser, returning
|
||||
the parsed object.
|
||||
|
||||
Raise UsageError if the file cannot be parsed.
|
||||
"""
|
||||
try:
|
||||
return iniconfig.IniConfig(str(path))
|
||||
except iniconfig.ParseError as exc:
|
||||
raise UsageError(str(exc)) from exc
|
||||
|
||||
|
||||
def _parse_toml_file(path: Path) -> dict[str, object]:
|
||||
"""Parse the given '.toml' file, returning the decoded document.
|
||||
|
||||
Raise UsageError if the file cannot be parsed.
|
||||
"""
|
||||
if sys.version_info >= (3, 11):
|
||||
import tomllib
|
||||
else:
|
||||
import tomli as tomllib
|
||||
|
||||
toml_text = path.read_text(encoding="utf-8")
|
||||
try:
|
||||
return tomllib.loads(toml_text)
|
||||
except tomllib.TOMLDecodeError as exc:
|
||||
raise UsageError(f"{path}: {exc}") from exc
|
||||
|
||||
|
||||
def _load_pytest_ini(path: Path) -> ConfigDict | None:
|
||||
"""Load a dedicated pytest INI file (``pytest.ini``/``.pytest.ini``).
|
||||
|
||||
These files are always the source of configuration, even if they lack a
|
||||
``[pytest]`` section, in which case an empty config is returned.
|
||||
"""
|
||||
iniconfig = _parse_ini_config(path)
|
||||
|
||||
if "pytest" in iniconfig:
|
||||
return {
|
||||
k: ConfigValue(v, origin="file", mode="ini")
|
||||
for k, v in iniconfig["pytest"].items()
|
||||
}
|
||||
return {}
|
||||
|
||||
|
||||
def _load_ini_file(path: Path) -> ConfigDict | None:
|
||||
"""Load a generic '.ini' file (e.g. ``tox.ini``).
|
||||
|
||||
Only considered if it contains a ``[pytest]`` section.
|
||||
"""
|
||||
iniconfig = _parse_ini_config(path)
|
||||
|
||||
if "pytest" in iniconfig:
|
||||
return {
|
||||
k: ConfigValue(v, origin="file", mode="ini")
|
||||
for k, v in iniconfig["pytest"].items()
|
||||
}
|
||||
return None
|
||||
|
||||
|
||||
def _load_cfg_file(path: Path) -> ConfigDict | None:
|
||||
"""Load a '.cfg' file (e.g. ``setup.cfg``).
|
||||
|
||||
Only considered if it contains a ``[tool:pytest]`` section.
|
||||
"""
|
||||
iniconfig = _parse_ini_config(path)
|
||||
|
||||
if "tool:pytest" in iniconfig.sections:
|
||||
return {
|
||||
k: ConfigValue(v, origin="file", mode="ini")
|
||||
for k, v in iniconfig["tool:pytest"].items()
|
||||
}
|
||||
elif "pytest" in iniconfig.sections:
|
||||
# If a setup.cfg contains a "[pytest]" section, we raise a failure to indicate users that
|
||||
# plain "[pytest]" sections in setup.cfg files is no longer supported (#3086).
|
||||
fail(CFG_PYTEST_SECTION.format(filename="setup.cfg"), pytrace=False)
|
||||
return None
|
||||
|
||||
|
||||
def _config_from_pytest_table(
|
||||
path: Path, config: dict[str, object]
|
||||
) -> ConfigDict | None:
|
||||
"""Return the configuration in the ``[pytest]`` table of a parsed TOML
|
||||
document, or None if it has none.
|
||||
|
||||
Raise UsageError for options written outside of any table, which is the
|
||||
usual way of getting the table wrong.
|
||||
"""
|
||||
if "pytest" in config:
|
||||
# TOML mode - preserve native TOML types.
|
||||
return {
|
||||
k: ConfigValue(v, origin="file", mode="toml")
|
||||
for k, v in config["pytest"].items() # type: ignore[attr-defined]
|
||||
}
|
||||
|
||||
top_level_options = [
|
||||
key for key, value in config.items() if not isinstance(value, dict)
|
||||
]
|
||||
if top_level_options:
|
||||
raise UsageError(
|
||||
f"{path}: pytest configuration must be under a "
|
||||
f"[pytest] table (found top-level options: "
|
||||
f"{', '.join(top_level_options)})"
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def _config_from_tool_pytest(
|
||||
path: Path, config: dict[str, object]
|
||||
) -> ConfigDict | None:
|
||||
"""Return the configuration in the ``[tool.pytest]`` tables of a parsed
|
||||
TOML document, or None if it has none."""
|
||||
tool_pytest = config.get("tool", {}).get("pytest", {}) # type: ignore[attr-defined]
|
||||
|
||||
# Check for toml mode config: [tool.pytest] with content outside of ini_options.
|
||||
toml_config = {k: v for k, v in tool_pytest.items() if k != "ini_options"}
|
||||
# Check for ini mode config: [tool.pytest.ini_options].
|
||||
ini_config = tool_pytest.get("ini_options", None)
|
||||
|
||||
if toml_config and ini_config:
|
||||
raise UsageError(
|
||||
f"{path}: Cannot use both [tool.pytest] (native TOML types) and "
|
||||
"[tool.pytest.ini_options] (string-based INI format) simultaneously. "
|
||||
"Please use [tool.pytest] with native TOML types (recommended) "
|
||||
"or [tool.pytest.ini_options] for backwards compatibility."
|
||||
)
|
||||
|
||||
if toml_config:
|
||||
# TOML mode - preserve native TOML types.
|
||||
return {
|
||||
k: ConfigValue(v, origin="file", mode="toml")
|
||||
for k, v in toml_config.items()
|
||||
}
|
||||
|
||||
if ini_config is not None:
|
||||
# INI mode - TOML supports richer data types than INI files, but we need to
|
||||
# convert all scalar values to str for compatibility with the INI system.
|
||||
def make_scalar(v: object) -> str | list[str]:
|
||||
return v if isinstance(v, list) else str(v)
|
||||
|
||||
return {
|
||||
k: ConfigValue(make_scalar(v), origin="file", mode="ini")
|
||||
for k, v in ini_config.items()
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _load_pytest_toml(path: Path) -> ConfigDict | None:
|
||||
"""Load a dedicated pytest TOML file (``pytest.toml``/``.pytest.toml``).
|
||||
|
||||
Configuration is read from the ``[pytest]`` table in TOML mode. These files
|
||||
are always the source of configuration, even if empty.
|
||||
"""
|
||||
config = _config_from_pytest_table(path, _parse_toml_file(path))
|
||||
return config if config is not None else {}
|
||||
|
||||
|
||||
def _load_custom_toml(path: Path) -> ConfigDict | None:
|
||||
"""Load a TOML file with an arbitrary name, as passed via ``-c``.
|
||||
|
||||
Such a file reads its configuration from ``[pytest]``, like ``pytest.toml``
|
||||
does -- the table pytest documents for its own files (#14705). The
|
||||
``pyproject.toml`` tables ``[tool.pytest]``/``[tool.pytest.ini_options]``,
|
||||
which arbitrary TOML files used to be parsed with exclusively, keep
|
||||
working; using both styles in one file is an error.
|
||||
"""
|
||||
document = _parse_toml_file(path)
|
||||
|
||||
tool_pytest_config = _config_from_tool_pytest(path, document)
|
||||
if tool_pytest_config is None:
|
||||
return _config_from_pytest_table(path, document)
|
||||
|
||||
if "pytest" in document:
|
||||
raise UsageError(
|
||||
f"{path}: Cannot use both [pytest] and [tool.pytest]/"
|
||||
"[tool.pytest.ini_options] in the same file. Please use [pytest], "
|
||||
"which is what pytest's own configuration files use; the "
|
||||
"[tool.pytest] tables are meant for pyproject.toml."
|
||||
)
|
||||
return tool_pytest_config
|
||||
|
||||
|
||||
def _load_pyproject_toml(path: Path) -> ConfigDict | None:
|
||||
"""Load a ``pyproject.toml``-style file.
|
||||
|
||||
Configuration is read from ``[tool.pytest]`` (TOML mode) or
|
||||
``[tool.pytest.ini_options]`` (INI mode).
|
||||
"""
|
||||
return _config_from_tool_pytest(path, _parse_toml_file(path))
|
||||
|
||||
|
||||
#: Loaders for the config files pytest discovers by name, in precedence order.
|
||||
#:
|
||||
#: This mapping is the single source of truth for both *which* files are
|
||||
#: considered during rootdir discovery (see :func:`locate_config`) and *how*
|
||||
#: each of them is parsed.
|
||||
CONFIG_LOADERS_BY_NAME: dict[str, Callable[[Path], ConfigDict | None]] = {
|
||||
"pytest.toml": _load_pytest_toml,
|
||||
".pytest.toml": _load_pytest_toml,
|
||||
"pytest.ini": _load_pytest_ini,
|
||||
".pytest.ini": _load_pytest_ini,
|
||||
"pyproject.toml": _load_pyproject_toml,
|
||||
"tox.ini": _load_ini_file,
|
||||
"setup.cfg": _load_cfg_file,
|
||||
}
|
||||
|
||||
#: Fallback loaders keyed by suffix, for files that are not one of the names
|
||||
#: above.
|
||||
#:
|
||||
#: These apply to files passed explicitly via ``-c``/``--config-file``, which
|
||||
#: may have an arbitrary name. Names win over suffixes, so files with a
|
||||
#: dedicated meaning keep their semantics wherever they are passed from.
|
||||
CONFIG_LOADERS_BY_SUFFIX: dict[str, Callable[[Path], ConfigDict | None]] = {
|
||||
".ini": _load_ini_file,
|
||||
".cfg": _load_cfg_file,
|
||||
".toml": _load_custom_toml,
|
||||
}
|
||||
|
||||
|
||||
def _get_config_loader(filepath: Path) -> Callable[[Path], ConfigDict | None] | None:
|
||||
"""Return the loader responsible for the given path, if any."""
|
||||
loader = CONFIG_LOADERS_BY_NAME.get(filepath.name)
|
||||
if loader is None:
|
||||
loader = CONFIG_LOADERS_BY_SUFFIX.get(filepath.suffix)
|
||||
return loader
|
||||
|
||||
|
||||
def load_config_dict_from_file(
|
||||
filepath: Path,
|
||||
) -> ConfigDict | None:
|
||||
"""Load pytest configuration from the given file path, if supported.
|
||||
|
||||
Return None if the file does not contain valid pytest configuration.
|
||||
"""
|
||||
loader = _get_config_loader(filepath)
|
||||
if loader is None:
|
||||
return None
|
||||
return loader(filepath)
|
||||
|
||||
|
||||
def locate_config(
|
||||
invocation_dir: Path,
|
||||
args: Iterable[Path],
|
||||
) -> tuple[Path | None, Path | None, ConfigDict, Sequence[str]]:
|
||||
"""Search in the list of arguments for a valid ini-file for pytest,
|
||||
and return a tuple of (rootdir, inifile, cfg-dict, ignored-config-files), where
|
||||
ignored-config-files is a list of config basenames found that contain
|
||||
pytest configuration but were ignored."""
|
||||
config_names = list(CONFIG_LOADERS_BY_NAME)
|
||||
args = [x for x in args if not str(x).startswith("-")]
|
||||
if not args:
|
||||
args = [invocation_dir]
|
||||
found_pyproject_toml: Path | None = None
|
||||
ignored_config_files: list[str] = []
|
||||
|
||||
for arg in args:
|
||||
argpath = absolutepath(arg)
|
||||
for base in (argpath, *argpath.parents):
|
||||
for index, (config_name, loader) in enumerate(
|
||||
CONFIG_LOADERS_BY_NAME.items()
|
||||
):
|
||||
p = base / config_name
|
||||
if p.is_file():
|
||||
if p.name == "pyproject.toml" and found_pyproject_toml is None:
|
||||
found_pyproject_toml = p
|
||||
ini_config = loader(p)
|
||||
if ini_config is not None:
|
||||
for remainder in config_names[index + 1 :]:
|
||||
p2 = base / remainder
|
||||
if (
|
||||
p2.is_file()
|
||||
and CONFIG_LOADERS_BY_NAME[remainder](p2) is not None
|
||||
):
|
||||
ignored_config_files.append(remainder)
|
||||
return base, p, ini_config, ignored_config_files
|
||||
if found_pyproject_toml is not None:
|
||||
return found_pyproject_toml.parent, found_pyproject_toml, {}, []
|
||||
return None, None, {}, []
|
||||
|
||||
|
||||
def get_common_ancestor(
|
||||
invocation_dir: Path,
|
||||
paths: Iterable[Path],
|
||||
) -> Path:
|
||||
common_ancestor: Path | None = None
|
||||
for path in paths:
|
||||
if not path.exists():
|
||||
continue
|
||||
if common_ancestor is None:
|
||||
common_ancestor = path
|
||||
else:
|
||||
if common_ancestor in path.parents or path == common_ancestor:
|
||||
continue
|
||||
elif path in common_ancestor.parents:
|
||||
common_ancestor = path
|
||||
else:
|
||||
shared = commonpath(path, common_ancestor)
|
||||
if shared is not None:
|
||||
common_ancestor = shared
|
||||
if common_ancestor is None:
|
||||
common_ancestor = invocation_dir
|
||||
elif common_ancestor.is_file():
|
||||
common_ancestor = common_ancestor.parent
|
||||
return common_ancestor
|
||||
|
||||
|
||||
def get_dirs_from_args(args: Iterable[str]) -> list[Path]:
|
||||
def is_option(x: str) -> bool:
|
||||
return x.startswith("-")
|
||||
|
||||
def get_dir_from_path(path: Path) -> Path:
|
||||
return path if path.is_dir() else path.parent
|
||||
|
||||
# These look like paths but may not exist
|
||||
possible_paths = (
|
||||
absolutepath(NodeId.parse(arg).path) for arg in args if not is_option(arg)
|
||||
)
|
||||
|
||||
return [get_dir_from_path(path) for path in possible_paths if safe_exists(path)]
|
||||
|
||||
|
||||
def parse_override_ini(override_ini: Sequence[str] | None) -> ConfigDict:
|
||||
"""Parse the -o/--override-ini command line arguments and return the overrides.
|
||||
|
||||
:raises UsageError:
|
||||
If one of the values is malformed.
|
||||
"""
|
||||
overrides = {}
|
||||
# override_ini is a list of "ini=value" options.
|
||||
# Always use the last item if multiple values are set for same ini-name,
|
||||
# e.g. -o foo=bar1 -o foo=bar2 will set foo to bar2.
|
||||
for ini_config in override_ini or ():
|
||||
try:
|
||||
key, user_ini_value = ini_config.split("=", 1)
|
||||
except ValueError as e:
|
||||
raise UsageError(
|
||||
f"-o/--override-ini expects option=value style (got: {ini_config!r})."
|
||||
) from e
|
||||
else:
|
||||
overrides[key] = ConfigValue(user_ini_value, origin="override", mode="ini")
|
||||
return overrides
|
||||
|
||||
|
||||
CFG_PYTEST_SECTION = "[pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead."
|
||||
|
||||
|
||||
def determine_setup(
|
||||
*,
|
||||
inifile: str | None,
|
||||
override_ini: Sequence[str] | None,
|
||||
args: Sequence[str],
|
||||
rootdir_cmd_arg: str | None,
|
||||
invocation_dir: Path,
|
||||
) -> tuple[Path, Path | None, ConfigDict, Sequence[str]]:
|
||||
"""Determine the rootdir, inifile and ini configuration values from the
|
||||
command line arguments.
|
||||
|
||||
:param inifile:
|
||||
The `--inifile` command line argument, if given.
|
||||
:param override_ini:
|
||||
The -o/--override-ini command line arguments, if given.
|
||||
:param args:
|
||||
The free command line arguments.
|
||||
:param rootdir_cmd_arg:
|
||||
The `--rootdir` command line argument, if given.
|
||||
:param invocation_dir:
|
||||
The working directory when pytest was invoked.
|
||||
|
||||
:raises UsageError:
|
||||
"""
|
||||
rootdir = None
|
||||
dirs = get_dirs_from_args(args)
|
||||
ignored_config_files: Sequence[str] = []
|
||||
|
||||
if inifile:
|
||||
inipath_ = absolutepath(inifile)
|
||||
if not inipath_.exists():
|
||||
raise UsageError(
|
||||
f"Config file '{inipath_}' not found. "
|
||||
f"Check your '-c/--config-file' option."
|
||||
)
|
||||
if inipath_.is_dir():
|
||||
raise UsageError(
|
||||
f"Config file '{inipath_}' is a directory. "
|
||||
f"Check your '-c/--config-file' option."
|
||||
)
|
||||
inipath: Path | None = inipath_
|
||||
loader = _get_config_loader(inipath_)
|
||||
if loader is not None:
|
||||
inicfg = loader(inipath_) or {}
|
||||
elif inipath_.is_file():
|
||||
supported = ", ".join(sorted(CONFIG_LOADERS_BY_SUFFIX))
|
||||
raise UsageError(
|
||||
f"Config file '{inipath_}' has an unsupported format. "
|
||||
f"Supported extensions are: {supported}."
|
||||
)
|
||||
else:
|
||||
# A file pytest has no loader for is an error, but a path that is
|
||||
# not a regular file to begin with cannot hold configuration at
|
||||
# all: it is the way to ask for no configuration, as with
|
||||
# ``--config-file=/dev/null``.
|
||||
inicfg = {}
|
||||
if rootdir_cmd_arg is None:
|
||||
if inipath_.is_file():
|
||||
rootdir = inipath_.parent
|
||||
else:
|
||||
# Such a path also says nothing about where the project lives,
|
||||
# so the rootdir must not be derived from it -- otherwise
|
||||
# ``--config-file=/dev/null`` roots at ``/dev`` and the cache
|
||||
# plugin warns that it cannot write ``/dev/.pytest_cache``
|
||||
# (#11502).
|
||||
rootdir = get_common_ancestor(invocation_dir, dirs)
|
||||
if is_fs_root(rootdir):
|
||||
rootdir = invocation_dir
|
||||
else:
|
||||
ancestor = get_common_ancestor(invocation_dir, dirs)
|
||||
rootdir, inipath, inicfg, ignored_config_files = locate_config(
|
||||
invocation_dir, [ancestor]
|
||||
)
|
||||
if rootdir is None and rootdir_cmd_arg is None:
|
||||
for possible_rootdir in (ancestor, *ancestor.parents):
|
||||
if (possible_rootdir / "setup.py").is_file():
|
||||
rootdir = possible_rootdir
|
||||
break
|
||||
else:
|
||||
if dirs != [ancestor]:
|
||||
rootdir, inipath, inicfg, _ = locate_config(invocation_dir, dirs)
|
||||
if rootdir is None:
|
||||
rootdir = get_common_ancestor(
|
||||
invocation_dir, [invocation_dir, ancestor]
|
||||
)
|
||||
if is_fs_root(rootdir): # pragma: no cover
|
||||
rootdir = ancestor
|
||||
if rootdir_cmd_arg:
|
||||
rootdir = absolutepath(os.path.expandvars(rootdir_cmd_arg))
|
||||
if not rootdir.is_dir():
|
||||
raise UsageError(
|
||||
f"Directory '{rootdir}' not found. Check your '--rootdir' option."
|
||||
)
|
||||
|
||||
ini_overrides = parse_override_ini(override_ini)
|
||||
inicfg.update(ini_overrides)
|
||||
|
||||
assert rootdir is not None
|
||||
return rootdir, inipath, inicfg, ignored_config_files
|
||||
|
||||
|
||||
def is_fs_root(p: Path) -> bool:
|
||||
r"""
|
||||
Return True if the given path is pointing to the root of the
|
||||
file system ("/" on Unix and "C:\\" on Windows for example).
|
||||
"""
|
||||
return os.path.splitdrive(str(p))[1] == os.sep
|
||||
@@ -0,0 +1,404 @@
|
||||
# mypy: allow-untyped-defs
|
||||
# ruff: noqa: T100
|
||||
"""Interactive debugging with PDB, the Python Debugger."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Generator
|
||||
import functools
|
||||
import importlib
|
||||
import sys
|
||||
import types
|
||||
from typing import Any
|
||||
|
||||
from _pytest import outcomes
|
||||
from _pytest._code import ExceptionInfo
|
||||
from _pytest.capture import CaptureManager
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import ConftestImportFailure
|
||||
from _pytest.config import hookimpl
|
||||
from _pytest.config import PytestPluginManager
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.config.exceptions import UsageError
|
||||
from _pytest.nodes import Node
|
||||
from _pytest.reports import BaseReport
|
||||
from _pytest.runner import CallInfo
|
||||
|
||||
|
||||
def _validate_usepdb_cls(value: str) -> tuple[str, str]:
|
||||
"""Validate syntax of --pdbcls option."""
|
||||
try:
|
||||
modname, classname = value.split(":")
|
||||
except ValueError as e:
|
||||
raise argparse.ArgumentTypeError(
|
||||
f"{value!r} is not in the format 'modname:classname'"
|
||||
) from e
|
||||
return (modname, classname)
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("general")
|
||||
group.addoption(
|
||||
"--pdb",
|
||||
dest="usepdb",
|
||||
action="store_true",
|
||||
help="Start the interactive Python debugger on errors or KeyboardInterrupt",
|
||||
)
|
||||
group.addoption(
|
||||
"--pdbcls",
|
||||
dest="usepdb_cls",
|
||||
metavar="modulename:classname",
|
||||
type=_validate_usepdb_cls,
|
||||
help="Specify a custom interactive Python debugger for use with --pdb."
|
||||
"For example: --pdbcls=IPython.terminal.debugger:TerminalPdb",
|
||||
)
|
||||
group.addoption(
|
||||
"--trace",
|
||||
dest="trace",
|
||||
action="store_true",
|
||||
help="Immediately break when running each test",
|
||||
)
|
||||
|
||||
|
||||
def pytest_configure(config: Config) -> None:
|
||||
import pdb
|
||||
|
||||
if config.getvalue("trace"):
|
||||
config.pluginmanager.register(PdbTrace(), "pdbtrace")
|
||||
if config.getvalue("usepdb"):
|
||||
config.pluginmanager.register(PdbInvoke(), "pdbinvoke")
|
||||
|
||||
pytestPDB._saved.append(
|
||||
(pdb.set_trace, pytestPDB._pluginmanager, pytestPDB._config)
|
||||
)
|
||||
pdb.set_trace = pytestPDB.set_trace
|
||||
pytestPDB._pluginmanager = config.pluginmanager
|
||||
pytestPDB._config = config
|
||||
|
||||
# NOTE: not using pytest_unconfigure, since it might get called although
|
||||
# pytest_configure was not (if another plugin raises UsageError).
|
||||
def fin() -> None:
|
||||
(
|
||||
pdb.set_trace,
|
||||
pytestPDB._pluginmanager,
|
||||
pytestPDB._config,
|
||||
) = pytestPDB._saved.pop()
|
||||
|
||||
config.add_cleanup(fin)
|
||||
|
||||
|
||||
class pytestPDB:
|
||||
"""Pseudo PDB that defers to the real pdb."""
|
||||
|
||||
_pluginmanager: PytestPluginManager | None = None
|
||||
_config: Config | None = None
|
||||
_saved: list[
|
||||
tuple[Callable[..., None], PytestPluginManager | None, Config | None]
|
||||
] = []
|
||||
_recursive_debug = 0
|
||||
_wrapped_pdb_cls: tuple[type[Any], type[Any]] | None = None
|
||||
|
||||
@classmethod
|
||||
def _is_capturing(cls, capman: CaptureManager | None) -> str | bool:
|
||||
if capman:
|
||||
return capman.is_capturing()
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def _import_pdb_cls(cls, capman: CaptureManager | None):
|
||||
if not cls._config:
|
||||
import pdb
|
||||
|
||||
# Happens when using pytest.set_trace outside of a test.
|
||||
return pdb.Pdb
|
||||
|
||||
usepdb_cls = cls._config.getvalue("usepdb_cls")
|
||||
|
||||
if cls._wrapped_pdb_cls and cls._wrapped_pdb_cls[0] == usepdb_cls:
|
||||
return cls._wrapped_pdb_cls[1]
|
||||
|
||||
if usepdb_cls:
|
||||
modname, classname = usepdb_cls
|
||||
|
||||
try:
|
||||
mod = importlib.import_module(modname)
|
||||
|
||||
# Handle --pdbcls=pdb:pdb.Pdb (useful e.g. with pdbpp).
|
||||
parts = classname.split(".")
|
||||
pdb_cls = getattr(mod, parts[0])
|
||||
for part in parts[1:]:
|
||||
pdb_cls = getattr(pdb_cls, part)
|
||||
except Exception as exc:
|
||||
value = ":".join((modname, classname))
|
||||
raise UsageError(
|
||||
f"--pdbcls: could not import {value!r}: {exc}"
|
||||
) from exc
|
||||
else:
|
||||
import pdb
|
||||
|
||||
pdb_cls = pdb.Pdb
|
||||
|
||||
wrapped_cls = cls._get_pdb_wrapper_class(pdb_cls, capman)
|
||||
cls._wrapped_pdb_cls = (usepdb_cls, wrapped_cls)
|
||||
return wrapped_cls
|
||||
|
||||
@classmethod
|
||||
def _get_pdb_wrapper_class(cls, pdb_cls, capman: CaptureManager | None):
|
||||
import _pytest.config
|
||||
|
||||
class PytestPdbWrapper(pdb_cls):
|
||||
_pytest_capman = capman
|
||||
_continued = False
|
||||
|
||||
def do_debug(self, arg):
|
||||
cls._recursive_debug += 1
|
||||
ret = super().do_debug(arg)
|
||||
cls._recursive_debug -= 1
|
||||
return ret
|
||||
|
||||
if hasattr(pdb_cls, "do_debug"):
|
||||
do_debug.__doc__ = pdb_cls.do_debug.__doc__
|
||||
|
||||
def do_continue(self, arg):
|
||||
ret = super().do_continue(arg)
|
||||
if cls._recursive_debug == 0:
|
||||
assert cls._config is not None
|
||||
tw = _pytest.config.create_terminal_writer(cls._config)
|
||||
tw.line()
|
||||
|
||||
capman = self._pytest_capman
|
||||
capturing = pytestPDB._is_capturing(capman)
|
||||
if capturing:
|
||||
if capturing == "global":
|
||||
tw.sep(">", "PDB continue (IO-capturing resumed)")
|
||||
else:
|
||||
tw.sep(
|
||||
">",
|
||||
f"PDB continue (IO-capturing resumed for {capturing})",
|
||||
)
|
||||
assert capman is not None
|
||||
capman.resume()
|
||||
else:
|
||||
tw.sep(">", "PDB continue")
|
||||
assert cls._pluginmanager is not None
|
||||
cls._pluginmanager.hook.pytest_leave_pdb(config=cls._config, pdb=self)
|
||||
self._continued = True
|
||||
return ret
|
||||
|
||||
if hasattr(pdb_cls, "do_continue"):
|
||||
do_continue.__doc__ = pdb_cls.do_continue.__doc__
|
||||
|
||||
do_c = do_cont = do_continue
|
||||
|
||||
def do_quit(self, arg):
|
||||
# Raise Exit outcome when quit command is used in pdb.
|
||||
#
|
||||
# This is a bit of a hack - it would be better if BdbQuit
|
||||
# could be handled, but this would require to wrap the
|
||||
# whole pytest run, and adjust the report etc.
|
||||
ret = super().do_quit(arg)
|
||||
|
||||
if cls._recursive_debug == 0:
|
||||
outcomes.exit("Quitting debugger")
|
||||
|
||||
return ret
|
||||
|
||||
if hasattr(pdb_cls, "do_quit"):
|
||||
do_quit.__doc__ = pdb_cls.do_quit.__doc__
|
||||
|
||||
do_q = do_quit
|
||||
do_exit = do_quit
|
||||
|
||||
def setup(self, f, tb):
|
||||
"""Suspend on setup().
|
||||
|
||||
Needed after do_continue resumed, and entering another
|
||||
breakpoint again.
|
||||
"""
|
||||
ret = super().setup(f, tb)
|
||||
if not ret and self._continued:
|
||||
# pdb.setup() returns True if the command wants to exit
|
||||
# from the interaction: do not suspend capturing then.
|
||||
if self._pytest_capman:
|
||||
self._pytest_capman.suspend_global_capture(in_=True)
|
||||
return ret
|
||||
|
||||
def get_stack(self, f, t):
|
||||
stack, i = super().get_stack(f, t)
|
||||
if f is None:
|
||||
# Find last non-hidden frame.
|
||||
i = max(0, len(stack) - 1)
|
||||
while i and stack[i][0].f_locals.get("__tracebackhide__", False):
|
||||
i -= 1
|
||||
return stack, i
|
||||
|
||||
return PytestPdbWrapper
|
||||
|
||||
@classmethod
|
||||
def _init_pdb(cls, method, *args, **kwargs):
|
||||
"""Initialize PDB debugging, dropping any IO capturing."""
|
||||
import _pytest.config
|
||||
|
||||
if cls._pluginmanager is None:
|
||||
capman: CaptureManager | None = None
|
||||
else:
|
||||
capman = cls._pluginmanager.getplugin("capturemanager")
|
||||
if capman:
|
||||
capman.suspend(in_=True)
|
||||
|
||||
if cls._config:
|
||||
tw = _pytest.config.create_terminal_writer(cls._config)
|
||||
tw.line()
|
||||
|
||||
if cls._recursive_debug == 0:
|
||||
# Handle header similar to pdb.set_trace in py37+.
|
||||
header = kwargs.pop("header", None)
|
||||
if header is not None:
|
||||
tw.sep(">", header)
|
||||
else:
|
||||
capturing = cls._is_capturing(capman)
|
||||
if capturing == "global":
|
||||
tw.sep(">", f"PDB {method} (IO-capturing turned off)")
|
||||
elif capturing:
|
||||
tw.sep(
|
||||
">",
|
||||
f"PDB {method} (IO-capturing turned off for {capturing})",
|
||||
)
|
||||
else:
|
||||
tw.sep(">", f"PDB {method}")
|
||||
|
||||
_pdb = cls._import_pdb_cls(capman)(**kwargs)
|
||||
|
||||
if cls._pluginmanager:
|
||||
cls._pluginmanager.hook.pytest_enter_pdb(config=cls._config, pdb=_pdb)
|
||||
return _pdb
|
||||
|
||||
@classmethod
|
||||
def set_trace(cls, *args, **kwargs) -> None:
|
||||
"""Invoke debugging via ``Pdb.set_trace``, dropping any IO capturing."""
|
||||
frame = sys._getframe().f_back
|
||||
_pdb = cls._init_pdb("set_trace", *args, **kwargs)
|
||||
_pdb.set_trace(frame)
|
||||
|
||||
|
||||
class PdbInvoke:
|
||||
def pytest_exception_interact(
|
||||
self, node: Node, call: CallInfo[Any], report: BaseReport
|
||||
) -> None:
|
||||
capman = node.config.pluginmanager.getplugin("capturemanager")
|
||||
if capman:
|
||||
capman.suspend_global_capture(in_=True)
|
||||
out, err = capman.read_global_capture()
|
||||
sys.stdout.write(out)
|
||||
sys.stdout.write(err)
|
||||
assert call.excinfo is not None
|
||||
_enter_pdb(node, call.excinfo, report)
|
||||
|
||||
def pytest_internalerror(self, excinfo: ExceptionInfo[BaseException]) -> None:
|
||||
exc_or_tb = _postmortem_exc_or_tb(excinfo)
|
||||
post_mortem(exc_or_tb)
|
||||
|
||||
|
||||
class PdbTrace:
|
||||
@hookimpl(wrapper=True)
|
||||
def pytest_pyfunc_call(self, pyfuncitem) -> Generator[None, object, object]:
|
||||
wrap_pytest_function_for_tracing(pyfuncitem)
|
||||
return (yield)
|
||||
|
||||
|
||||
def wrap_pytest_function_for_tracing(pyfuncitem) -> None:
|
||||
"""Change the Python function object of the given Function item by a
|
||||
wrapper which actually enters pdb before calling the python function
|
||||
itself, effectively leaving the user in the pdb prompt in the first
|
||||
statement of the function."""
|
||||
_pdb = pytestPDB._init_pdb("runcall")
|
||||
testfunction = pyfuncitem.obj
|
||||
|
||||
# we can't just return `partial(pdb.runcall, testfunction)` because (on
|
||||
# python < 3.7.4) runcall's first param is `func`, which means we'd get
|
||||
# an exception if one of the kwargs to testfunction was called `func`.
|
||||
@functools.wraps(testfunction)
|
||||
def wrapper(*args, **kwargs) -> None:
|
||||
func = functools.partial(testfunction, *args, **kwargs)
|
||||
_pdb.runcall(func)
|
||||
|
||||
pyfuncitem.obj = wrapper
|
||||
|
||||
|
||||
def maybe_wrap_pytest_function_for_tracing(pyfuncitem) -> None:
|
||||
"""Wrap the given pytestfunct item for tracing support if --trace was given in
|
||||
the command line."""
|
||||
if pyfuncitem.config.getvalue("trace"):
|
||||
wrap_pytest_function_for_tracing(pyfuncitem)
|
||||
|
||||
|
||||
def _enter_pdb(
|
||||
node: Node, excinfo: ExceptionInfo[BaseException], rep: BaseReport
|
||||
) -> BaseReport:
|
||||
# XXX we reuse the TerminalReporter's terminalwriter
|
||||
# because this seems to avoid some encoding related troubles
|
||||
# for not completely clear reasons.
|
||||
tw = node.config.pluginmanager.getplugin("terminalreporter")._tw
|
||||
tw.line()
|
||||
|
||||
showcapture = node.config.option.showcapture
|
||||
|
||||
for sectionname, content in (
|
||||
("stdout", rep.capstdout),
|
||||
("stderr", rep.capstderr),
|
||||
("log", rep.caplog),
|
||||
):
|
||||
if showcapture in (sectionname, "all") and content:
|
||||
tw.sep(">", "captured " + sectionname)
|
||||
if content[-1:] == "\n":
|
||||
content = content[:-1]
|
||||
tw.line(content)
|
||||
|
||||
tw.sep(">", "traceback")
|
||||
rep.toterminal(tw)
|
||||
tw.sep(">", "entering PDB")
|
||||
tb_or_exc = _postmortem_exc_or_tb(excinfo)
|
||||
rep._pdbshown = True # type: ignore[attr-defined]
|
||||
post_mortem(tb_or_exc)
|
||||
return rep
|
||||
|
||||
|
||||
def _postmortem_exc_or_tb(
|
||||
excinfo: ExceptionInfo[BaseException],
|
||||
) -> types.TracebackType | BaseException:
|
||||
from doctest import UnexpectedException
|
||||
|
||||
get_exc = sys.version_info >= (3, 13)
|
||||
if isinstance(excinfo.value, UnexpectedException):
|
||||
# A doctest.UnexpectedException is not useful for post_mortem.
|
||||
# Use the underlying exception instead:
|
||||
underlying_exc = excinfo.value
|
||||
if get_exc:
|
||||
return underlying_exc.exc_info[1]
|
||||
|
||||
return underlying_exc.exc_info[2]
|
||||
elif isinstance(excinfo.value, ConftestImportFailure):
|
||||
# A config.ConftestImportFailure is not useful for post_mortem.
|
||||
# Use the underlying exception instead:
|
||||
cause = excinfo.value.cause
|
||||
if get_exc:
|
||||
return cause
|
||||
|
||||
assert cause.__traceback__ is not None
|
||||
return cause.__traceback__
|
||||
else:
|
||||
assert excinfo._excinfo is not None
|
||||
if get_exc:
|
||||
return excinfo._excinfo[1]
|
||||
|
||||
return excinfo._excinfo[2]
|
||||
|
||||
|
||||
def post_mortem(tb_or_exc: types.TracebackType | BaseException) -> None:
|
||||
p = pytestPDB._init_pdb("post_mortem")
|
||||
p.reset()
|
||||
p.interaction(None, tb_or_exc)
|
||||
if p.quitting:
|
||||
outcomes.exit("Quitting debugger")
|
||||
@@ -0,0 +1,176 @@
|
||||
"""Deprecation messages and bits of code used elsewhere in the codebase that
|
||||
is planned to be removed in the next pytest release.
|
||||
|
||||
Keeping it in a central location makes it easy to track what is deprecated and should
|
||||
be removed when the time comes.
|
||||
|
||||
All constants defined in this module should be instances of
|
||||
:class:`UnformattedWarning`, whose ``format`` method returns a fresh warning
|
||||
instance each time. Do not warn with shared warning instances: if a warning is
|
||||
raised as an error, CPython appends to the exception's existing ``__traceback__``,
|
||||
so a process-lifetime instance would accumulate stale frames from previous raises.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from warnings import warn
|
||||
|
||||
from _pytest.warning_types import PytestDeprecationWarning
|
||||
from _pytest.warning_types import PytestRemovedIn10Warning
|
||||
from _pytest.warning_types import UnformattedWarning
|
||||
|
||||
|
||||
# set of plugins which have been integrated into the core; we use this list to ignore
|
||||
# them during registration to avoid conflicts
|
||||
DEPRECATED_EXTERNAL_PLUGINS = {
|
||||
"pytest_catchlog",
|
||||
"pytest_capturelog",
|
||||
"pytest_faulthandler",
|
||||
"pytest_subtests",
|
||||
}
|
||||
|
||||
|
||||
# This could have been removed pytest 8, but it's harmless and common, so no rush to remove.
|
||||
YIELD_FIXTURE = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"@pytest.yield_fixture is deprecated.\n"
|
||||
"Use @pytest.fixture instead; they are the same.",
|
||||
)
|
||||
|
||||
CLASS_FIXTURE_INSTANCE_METHOD = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"{scope}-scoped fixtures defined as instance methods are deprecated.\n"
|
||||
"Instance attributes set in the {fixturename!r} fixture will NOT be visible to test methods,\n"
|
||||
"as each test gets a new instance while the fixture runs only once per class.\n"
|
||||
"Use a @classmethod decorator below @pytest.fixture and set attributes on cls instead.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#class-scoped-fixture-as-instance-method",
|
||||
)
|
||||
|
||||
# This deprecation is never really meant to be removed.
|
||||
PRIVATE = UnformattedWarning(
|
||||
PytestDeprecationWarning,
|
||||
"A private pytest class or function was used.",
|
||||
)
|
||||
|
||||
|
||||
HOOK_LEGACY_MARKING = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"The hook{type} {fullname} uses old-style configuration options (marks or attributes).\n"
|
||||
"Please use the pytest.hook{type}({hook_opts}) decorator instead\n"
|
||||
" to configure the hooks.\n"
|
||||
" See https://docs.pytest.org/en/latest/deprecations.html"
|
||||
"#configuring-hook-specs-impls-using-markers",
|
||||
)
|
||||
|
||||
MONKEYPATCH_LEGACY_NAMESPACE_PACKAGES = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"monkeypatch.syspath_prepend() called with pkg_resources legacy namespace packages detected.\n"
|
||||
"Legacy namespace packages (using pkg_resources.declare_namespace) are deprecated.\n"
|
||||
"Please use native namespace packages (PEP 420) instead.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#monkeypatch-fixup-namespace-packages",
|
||||
)
|
||||
|
||||
PARAMETRIZE_NON_COLLECTION_ITERABLE = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"Passing a non-Collection iterable to parametrize is deprecated.\n"
|
||||
"Test: {nodeid}, argvalues type: {type_name}\n"
|
||||
"Please convert to a list or tuple.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#parametrize-iterators",
|
||||
)
|
||||
|
||||
CONSOLE_MAIN = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"pytest.console_main() is deprecated and will be removed in pytest 10.\n"
|
||||
"It was never intended for programmatic use; use pytest.main() instead.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#console-main",
|
||||
)
|
||||
|
||||
CONFIG_INICFG = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"config.inicfg is deprecated, use config.getini() to access configuration values instead.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#config-inicfg",
|
||||
)
|
||||
|
||||
FIXTURE_GETFIXTUREVALUE_DURING_TEARDOWN = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
'Calling request.getfixturevalue("{argname}") during teardown is deprecated.\n'
|
||||
"Please request the fixture before teardown begins, either by declaring it in the fixture signature "
|
||||
"or by calling request.getfixturevalue() before the fixture yields.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#dynamic-fixture-request-during-teardown",
|
||||
)
|
||||
|
||||
PASTEBIN = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"The --pastebin option is deprecated. "
|
||||
"The functionality is now available in an external plugin package, pytest-pastebin.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#the-pastebin-option",
|
||||
)
|
||||
|
||||
INI_STRING_TYPE_NON_STR_VALUE = PytestRemovedIn10Warning(
|
||||
"Passing a value that is not a string to a 'string'-typed ini option is deprecated.\n"
|
||||
"In a future version this will raise a TypeError, matching the behavior of the "
|
||||
"corresponding TOML config path.\n"
|
||||
"If your plugin intentionally accepts non-string values, declare an explicit type "
|
||||
'(e.g. type="args") instead of relying on the implicit string default.'
|
||||
)
|
||||
|
||||
# You want to make some `__init__` or function "private".
|
||||
#
|
||||
# def my_private_function(some, args):
|
||||
# ...
|
||||
#
|
||||
# Do this:
|
||||
#
|
||||
# def my_private_function(some, args, *, _ispytest: bool = False):
|
||||
# check_ispytest(_ispytest)
|
||||
# ...
|
||||
#
|
||||
# Change all internal/allowed calls to
|
||||
#
|
||||
# my_private_function(some, args, _ispytest=True)
|
||||
#
|
||||
# All other calls will get the default _ispytest=False and trigger
|
||||
# the warning (possibly error in the future).
|
||||
|
||||
|
||||
FIXTURE_BASEID_DEPRECATED = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"Passing baseid to FixtureDef is deprecated. Pass node instead for fixture scoping.",
|
||||
)
|
||||
|
||||
FIXTURE_NODEID_DEPRECATED = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"Passing nodeid to _register_fixture is deprecated. "
|
||||
"Pass node instead for fixture scoping.",
|
||||
)
|
||||
|
||||
FIXTUREDEF_HAS_LOCATION_DEPRECATED = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"FixtureDef.has_location is deprecated and will be removed in pytest 10. "
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#fixturedef-has-location-deprecated",
|
||||
)
|
||||
|
||||
PARSEFACTORIES_NODEID_DEPRECATED = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"Passing nodeid string to parsefactories is deprecated. "
|
||||
"Use parsefactories(holder=obj, node=node) instead.",
|
||||
)
|
||||
|
||||
CALLSPEC2_RENAMED = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"_pytest.python.CallSpec2 has been renamed to CallSpec.\n"
|
||||
"The CallSpec2 alias will be removed in pytest 10.\n"
|
||||
"Update imports to use CallSpec instead.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#callspec2-renamed",
|
||||
)
|
||||
|
||||
TERMINAL_WRITE_LINE_BYTES = UnformattedWarning(
|
||||
PytestRemovedIn10Warning,
|
||||
"Passing bytes to TerminalReporter.write_line() is deprecated, pass a str instead.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html#terminalreporter-write-line-bytes",
|
||||
)
|
||||
|
||||
|
||||
def check_ispytest(ispytest: bool) -> None:
|
||||
if not ispytest:
|
||||
warn(PRIVATE.format(), stacklevel=3)
|
||||
@@ -0,0 +1,752 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Discover and run doctests in modules and test files."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import bdb
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Generator
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Sequence
|
||||
from contextlib import contextmanager
|
||||
import functools
|
||||
import inspect
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import re
|
||||
import sys
|
||||
import traceback
|
||||
import types
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING
|
||||
import warnings
|
||||
|
||||
from _pytest import outcomes
|
||||
from _pytest._code.code import ExceptionInfo
|
||||
from _pytest._code.code import ReprFileLocation
|
||||
from _pytest._code.code import TerminalRepr
|
||||
from _pytest._io import TerminalWriter
|
||||
from _pytest.approx import approx
|
||||
from _pytest.compat import safe_getattr
|
||||
from _pytest.config import Config
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.fixtures import fixture
|
||||
from _pytest.fixtures import FixtureFunctionDefinition
|
||||
from _pytest.fixtures import TopRequest
|
||||
from _pytest.nodes import Collector
|
||||
from _pytest.nodes import Item
|
||||
from _pytest.outcomes import OutcomeException
|
||||
from _pytest.outcomes import skip
|
||||
from _pytest.pathlib import fnmatch_ex
|
||||
from _pytest.python import Module
|
||||
from _pytest.warning_types import PytestWarning
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import doctest
|
||||
|
||||
from typing_extensions import Self
|
||||
|
||||
DOCTEST_REPORT_CHOICE_NONE = "none"
|
||||
DOCTEST_REPORT_CHOICE_CDIFF = "cdiff"
|
||||
DOCTEST_REPORT_CHOICE_NDIFF = "ndiff"
|
||||
DOCTEST_REPORT_CHOICE_UDIFF = "udiff"
|
||||
DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE = "only_first_failure"
|
||||
|
||||
DOCTEST_REPORT_CHOICES = (
|
||||
DOCTEST_REPORT_CHOICE_NONE,
|
||||
DOCTEST_REPORT_CHOICE_CDIFF,
|
||||
DOCTEST_REPORT_CHOICE_NDIFF,
|
||||
DOCTEST_REPORT_CHOICE_UDIFF,
|
||||
DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE,
|
||||
)
|
||||
|
||||
# Lazy definition of runner class
|
||||
RUNNER_CLASS = None
|
||||
# Lazy definition of output checker class
|
||||
CHECKER_CLASS: type[doctest.OutputChecker] | None = None
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
parser.addini(
|
||||
"doctest_optionflags",
|
||||
"Option flags for doctests",
|
||||
type="args",
|
||||
default=["ELLIPSIS"],
|
||||
)
|
||||
parser.addini(
|
||||
"doctest_encoding", "Encoding used for doctest files", default="utf-8"
|
||||
)
|
||||
group = parser.getgroup("collect")
|
||||
group.addoption(
|
||||
"--doctest-modules",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="Run doctests in all .py modules",
|
||||
dest="doctestmodules",
|
||||
)
|
||||
group.addoption(
|
||||
"--doctest-report",
|
||||
type=str.lower,
|
||||
default="udiff",
|
||||
help="Choose another output format for diffs on doctest failure",
|
||||
choices=DOCTEST_REPORT_CHOICES,
|
||||
dest="doctestreport",
|
||||
)
|
||||
group.addoption(
|
||||
"--doctest-glob",
|
||||
action="append",
|
||||
default=[],
|
||||
metavar="pat",
|
||||
help="Doctests file matching pattern, default: test*.txt",
|
||||
dest="doctestglob",
|
||||
)
|
||||
group.addoption(
|
||||
"--doctest-ignore-import-errors",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="Ignore doctest collection errors",
|
||||
dest="doctest_ignore_import_errors",
|
||||
)
|
||||
group.addoption(
|
||||
"--doctest-continue-on-failure",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="For a given doctest, continue to run after the first failure",
|
||||
dest="doctest_continue_on_failure",
|
||||
)
|
||||
|
||||
|
||||
def pytest_unconfigure() -> None:
|
||||
global RUNNER_CLASS
|
||||
|
||||
RUNNER_CLASS = None
|
||||
|
||||
|
||||
def pytest_collect_file(
|
||||
file_path: Path,
|
||||
parent: Collector,
|
||||
) -> DoctestModule | DoctestTextfile | None:
|
||||
config = parent.config
|
||||
if file_path.suffix == ".py":
|
||||
if config.option.doctestmodules and not any(
|
||||
(_is_setup_py(file_path), _is_main_py(file_path))
|
||||
):
|
||||
return DoctestModule.from_parent(parent, path=file_path)
|
||||
elif _is_doctest(config, file_path, parent):
|
||||
return DoctestTextfile.from_parent(parent, path=file_path)
|
||||
return None
|
||||
|
||||
|
||||
def _is_setup_py(path: Path) -> bool:
|
||||
if path.name != "setup.py":
|
||||
return False
|
||||
contents = path.read_bytes()
|
||||
return b"setuptools" in contents or b"distutils" in contents
|
||||
|
||||
|
||||
def _is_doctest(config: Config, path: Path, parent: Collector) -> bool:
|
||||
if path.suffix in (".txt", ".rst") and parent.session.isinitpath(path):
|
||||
return True
|
||||
globs = config.getoption("doctestglob") or ["test*.txt"]
|
||||
return any(fnmatch_ex(glob, path) for glob in globs)
|
||||
|
||||
|
||||
def _is_main_py(path: Path) -> bool:
|
||||
return path.name == "__main__.py"
|
||||
|
||||
|
||||
class ReprFailDoctest(TerminalRepr):
|
||||
def __init__(
|
||||
self, reprlocation_lines: Sequence[tuple[ReprFileLocation, Sequence[str]]]
|
||||
) -> None:
|
||||
self.reprlocation_lines = reprlocation_lines
|
||||
|
||||
def toterminal(self, tw: TerminalWriter) -> None:
|
||||
for reprlocation, lines in self.reprlocation_lines:
|
||||
for line in lines:
|
||||
tw.line(line)
|
||||
reprlocation.toterminal(tw)
|
||||
|
||||
|
||||
class MultipleDoctestFailures(Exception):
|
||||
def __init__(self, failures: Sequence[doctest.DocTestFailure]) -> None:
|
||||
super().__init__()
|
||||
self.failures = failures
|
||||
|
||||
|
||||
def _init_runner_class() -> type[doctest.DocTestRunner]:
|
||||
import doctest
|
||||
|
||||
class PytestDoctestRunner(doctest.DebugRunner):
|
||||
"""Runner to collect failures.
|
||||
|
||||
Note that the out variable in this case is a list instead of a
|
||||
stdout-like object.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
checker: doctest.OutputChecker | None = None,
|
||||
verbose: bool | None = None,
|
||||
optionflags: int = 0,
|
||||
continue_on_failure: bool = True,
|
||||
) -> None:
|
||||
super().__init__(checker=checker, verbose=verbose, optionflags=optionflags)
|
||||
self.continue_on_failure = continue_on_failure
|
||||
|
||||
def report_failure(
|
||||
self,
|
||||
out,
|
||||
test: doctest.DocTest,
|
||||
example: doctest.Example,
|
||||
got: str,
|
||||
) -> None:
|
||||
failure = doctest.DocTestFailure(test, example, got)
|
||||
if self.continue_on_failure:
|
||||
out.append(failure)
|
||||
else:
|
||||
raise failure
|
||||
|
||||
def report_unexpected_exception(
|
||||
self,
|
||||
out,
|
||||
test: doctest.DocTest,
|
||||
example: doctest.Example,
|
||||
exc_info: tuple[type[BaseException], BaseException, types.TracebackType],
|
||||
) -> None:
|
||||
if isinstance(exc_info[1], OutcomeException):
|
||||
raise exc_info[1]
|
||||
if isinstance(exc_info[1], bdb.BdbQuit):
|
||||
outcomes.exit("Quitting debugger")
|
||||
failure = doctest.UnexpectedException(test, example, exc_info)
|
||||
if self.continue_on_failure:
|
||||
out.append(failure)
|
||||
else:
|
||||
raise failure
|
||||
|
||||
return PytestDoctestRunner
|
||||
|
||||
|
||||
def _get_runner(
|
||||
checker: doctest.OutputChecker | None = None,
|
||||
verbose: bool | None = None,
|
||||
optionflags: int = 0,
|
||||
continue_on_failure: bool = True,
|
||||
) -> doctest.DocTestRunner:
|
||||
# We need this in order to do a lazy import on doctest
|
||||
global RUNNER_CLASS
|
||||
if RUNNER_CLASS is None:
|
||||
RUNNER_CLASS = _init_runner_class()
|
||||
# Type ignored because the continue_on_failure argument is only defined on
|
||||
# PytestDoctestRunner, which is lazily defined so can't be used as a type.
|
||||
return RUNNER_CLASS( # type: ignore
|
||||
checker=checker,
|
||||
verbose=verbose,
|
||||
optionflags=optionflags,
|
||||
continue_on_failure=continue_on_failure,
|
||||
)
|
||||
|
||||
|
||||
class DoctestItem(Item):
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
parent: DoctestTextfile | DoctestModule,
|
||||
runner: doctest.DocTestRunner,
|
||||
dtest: doctest.DocTest,
|
||||
) -> None:
|
||||
super().__init__(name, parent)
|
||||
self.runner = runner
|
||||
self.dtest = dtest
|
||||
|
||||
# Stuff needed for fixture support.
|
||||
self.obj = None
|
||||
fm = self.session._fixturemanager
|
||||
fixtureinfo = fm.getfixtureinfo(node=self, func=None, cls=None)
|
||||
self._fixtureinfo = fixtureinfo
|
||||
self.fixturenames = fixtureinfo.names_closure
|
||||
self._initrequest()
|
||||
|
||||
@classmethod
|
||||
def from_parent( # type: ignore[override]
|
||||
cls,
|
||||
parent: DoctestTextfile | DoctestModule,
|
||||
*,
|
||||
name: str,
|
||||
runner: doctest.DocTestRunner,
|
||||
dtest: doctest.DocTest,
|
||||
) -> Self:
|
||||
# incompatible signature due to imposed limits on subclass
|
||||
"""The public named constructor."""
|
||||
return super().from_parent(name=name, parent=parent, runner=runner, dtest=dtest)
|
||||
|
||||
def _initrequest(self) -> None:
|
||||
self.funcargs: dict[str, object] = {}
|
||||
self._request = TopRequest(self, _ispytest=True) # type: ignore[arg-type]
|
||||
|
||||
def setup(self) -> None:
|
||||
self._request._fillfixtures()
|
||||
globs = dict(getfixture=self._request.getfixturevalue)
|
||||
for name, value in self._request.getfixturevalue("doctest_namespace").items():
|
||||
globs[name] = value
|
||||
self.dtest.globs.update(globs)
|
||||
|
||||
def runtest(self) -> None:
|
||||
_check_all_skipped(self.dtest)
|
||||
self._disable_output_capturing_for_darwin()
|
||||
failures: list[doctest.DocTestFailure] = []
|
||||
optionflags = self.runner.optionflags
|
||||
try:
|
||||
# Type ignored because we change the type of `out` from what
|
||||
# doctest expects.
|
||||
self.runner.run(self.dtest, out=failures) # type: ignore[arg-type]
|
||||
finally:
|
||||
# Our runner can raise before doctest restores its option flags.
|
||||
self.runner.optionflags = optionflags
|
||||
if failures:
|
||||
raise MultipleDoctestFailures(failures)
|
||||
|
||||
def _disable_output_capturing_for_darwin(self) -> None:
|
||||
"""Disable output capturing. Otherwise, stdout is lost to doctest (#985)."""
|
||||
if platform.system() != "Darwin":
|
||||
return
|
||||
capman = self.config.pluginmanager.getplugin("capturemanager")
|
||||
if capman:
|
||||
capman.suspend_global_capture(in_=True)
|
||||
out, err = capman.read_global_capture()
|
||||
sys.stdout.write(out)
|
||||
sys.stderr.write(err)
|
||||
|
||||
# TODO: Type ignored -- breaks Liskov Substitution.
|
||||
def repr_failure( # type: ignore[override]
|
||||
self,
|
||||
excinfo: ExceptionInfo[BaseException],
|
||||
) -> str | TerminalRepr:
|
||||
import doctest
|
||||
|
||||
failures: (
|
||||
Sequence[doctest.DocTestFailure | doctest.UnexpectedException] | None
|
||||
) = None
|
||||
if isinstance(
|
||||
excinfo.value, doctest.DocTestFailure | doctest.UnexpectedException
|
||||
):
|
||||
failures = [excinfo.value]
|
||||
elif isinstance(excinfo.value, MultipleDoctestFailures):
|
||||
failures = excinfo.value.failures
|
||||
|
||||
if failures is None:
|
||||
return super().repr_failure(excinfo)
|
||||
|
||||
reprlocation_lines = []
|
||||
for failure in failures:
|
||||
example = failure.example
|
||||
test = failure.test
|
||||
filename = test.filename
|
||||
if test.lineno is None:
|
||||
lineno = None
|
||||
else:
|
||||
lineno = test.lineno + example.lineno + 1
|
||||
message = type(failure).__name__
|
||||
# TODO: ReprFileLocation doesn't expect a None lineno.
|
||||
reprlocation = ReprFileLocation(filename, lineno, message) # type: ignore[arg-type]
|
||||
checker = _get_checker()
|
||||
report_choice = _get_report_choice(self.config.getoption("doctestreport"))
|
||||
if lineno is not None:
|
||||
assert failure.test.docstring is not None
|
||||
lines = failure.test.docstring.splitlines(False)
|
||||
# add line numbers to the left of the error message
|
||||
assert test.lineno is not None
|
||||
lines = [
|
||||
f"{i + test.lineno + 1:03d} {x}" for (i, x) in enumerate(lines)
|
||||
]
|
||||
# trim docstring error lines to 10
|
||||
lines = lines[max(example.lineno - 9, 0) : example.lineno + 1]
|
||||
else:
|
||||
lines = [
|
||||
"EXAMPLE LOCATION UNKNOWN, not showing all tests of that example"
|
||||
]
|
||||
indent = ">>>"
|
||||
for line in example.source.splitlines():
|
||||
lines.append(f"??? {indent} {line}")
|
||||
indent = "..."
|
||||
if isinstance(failure, doctest.DocTestFailure):
|
||||
lines += checker.output_difference(
|
||||
example, failure.got, report_choice
|
||||
).split("\n")
|
||||
else:
|
||||
inner_excinfo = ExceptionInfo.from_exc_info(failure.exc_info)
|
||||
lines += [f"UNEXPECTED EXCEPTION: {inner_excinfo.value!r}"]
|
||||
lines += [
|
||||
x.strip("\n") for x in traceback.format_exception(*failure.exc_info)
|
||||
]
|
||||
reprlocation_lines.append((reprlocation, lines))
|
||||
return ReprFailDoctest(reprlocation_lines)
|
||||
|
||||
def reportinfo(self) -> tuple[os.PathLike[str] | str, int | None, str]:
|
||||
return self.path, self.dtest.lineno, f"[doctest] {self.name}"
|
||||
|
||||
|
||||
def _get_flag_lookup() -> dict[str, int]:
|
||||
import doctest
|
||||
|
||||
return dict(
|
||||
DONT_ACCEPT_TRUE_FOR_1=doctest.DONT_ACCEPT_TRUE_FOR_1,
|
||||
DONT_ACCEPT_BLANKLINE=doctest.DONT_ACCEPT_BLANKLINE,
|
||||
NORMALIZE_WHITESPACE=doctest.NORMALIZE_WHITESPACE,
|
||||
ELLIPSIS=doctest.ELLIPSIS,
|
||||
IGNORE_EXCEPTION_DETAIL=doctest.IGNORE_EXCEPTION_DETAIL,
|
||||
COMPARISON_FLAGS=doctest.COMPARISON_FLAGS,
|
||||
ALLOW_UNICODE=_get_allow_unicode_flag(),
|
||||
ALLOW_BYTES=_get_allow_bytes_flag(),
|
||||
NUMBER=_get_number_flag(),
|
||||
)
|
||||
|
||||
|
||||
def get_optionflags(config: Config) -> int:
|
||||
optionflags_str = config.getini("doctest_optionflags")
|
||||
flag_lookup_table = _get_flag_lookup()
|
||||
flag_acc = 0
|
||||
for flag in optionflags_str:
|
||||
flag_acc |= flag_lookup_table[flag]
|
||||
return flag_acc
|
||||
|
||||
|
||||
def _get_continue_on_failure(config: Config) -> bool:
|
||||
continue_on_failure: bool = config.getvalue("doctest_continue_on_failure")
|
||||
if continue_on_failure:
|
||||
# We need to turn off this if we use pdb since we should stop at
|
||||
# the first failure.
|
||||
if config.getvalue("usepdb"):
|
||||
continue_on_failure = False
|
||||
return continue_on_failure
|
||||
|
||||
|
||||
class DoctestTextfile(Module):
|
||||
obj = None
|
||||
|
||||
def collect(self) -> Iterable[DoctestItem]:
|
||||
import doctest
|
||||
|
||||
# Inspired by doctest.testfile; ideally we would use it directly,
|
||||
# but it doesn't support passing a custom checker.
|
||||
encoding = self.config.getini("doctest_encoding")
|
||||
text = self.path.read_text(encoding)
|
||||
filename = str(self.path)
|
||||
name = self.path.name
|
||||
globs = {"__name__": "__main__"}
|
||||
|
||||
optionflags = get_optionflags(self.config)
|
||||
|
||||
runner = _get_runner(
|
||||
verbose=False,
|
||||
optionflags=optionflags,
|
||||
checker=_get_checker(),
|
||||
continue_on_failure=_get_continue_on_failure(self.config),
|
||||
)
|
||||
|
||||
parser = doctest.DocTestParser()
|
||||
test = parser.get_doctest(text, globs, name, filename, 0)
|
||||
if test.examples:
|
||||
yield DoctestItem.from_parent(
|
||||
self, name=test.name, runner=runner, dtest=test
|
||||
)
|
||||
|
||||
|
||||
def _check_all_skipped(test: doctest.DocTest) -> None:
|
||||
"""Raise pytest.skip() if all examples in the given DocTest have the SKIP
|
||||
option set."""
|
||||
import doctest
|
||||
|
||||
all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples)
|
||||
if all_skipped:
|
||||
skip("all tests skipped by +SKIP option")
|
||||
|
||||
|
||||
def _is_mocked(obj: object) -> bool:
|
||||
"""Return if an object is possibly a mock object by checking the
|
||||
existence of a highly improbable attribute."""
|
||||
return (
|
||||
safe_getattr(obj, "pytest_mock_example_attribute_that_shouldnt_exist", None)
|
||||
is not None
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _patch_unwrap_mock_aware() -> Generator[None]:
|
||||
"""Context manager which replaces ``inspect.unwrap`` with a version
|
||||
that's aware of mock objects and doesn't recurse into them."""
|
||||
real_unwrap = inspect.unwrap
|
||||
|
||||
def _mock_aware_unwrap(
|
||||
func: Callable[..., Any], *, stop: Callable[[Any], Any] | None = None
|
||||
) -> Any:
|
||||
try:
|
||||
if stop is None or stop is _is_mocked:
|
||||
return real_unwrap(func, stop=_is_mocked)
|
||||
_stop = stop
|
||||
return real_unwrap(func, stop=lambda obj: _is_mocked(obj) or _stop(func))
|
||||
except Exception as e:
|
||||
warnings.warn(
|
||||
f"Got {e!r} when unwrapping {func!r}. This is usually caused "
|
||||
"by a violation of Python's object protocol; see e.g. "
|
||||
"https://github.com/pytest-dev/pytest/issues/5080",
|
||||
PytestWarning,
|
||||
)
|
||||
raise
|
||||
|
||||
inspect.unwrap = _mock_aware_unwrap
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
inspect.unwrap = real_unwrap
|
||||
|
||||
|
||||
class DoctestModule(Module):
|
||||
def collect(self) -> Iterable[DoctestItem]:
|
||||
import doctest
|
||||
|
||||
class MockAwareDocTestFinder(doctest.DocTestFinder):
|
||||
py_ver_info_minor = sys.version_info[:2]
|
||||
is_find_lineno_broken = (
|
||||
py_ver_info_minor < (3, 11)
|
||||
or (py_ver_info_minor == (3, 11) and sys.version_info.micro < 9)
|
||||
or (py_ver_info_minor == (3, 12) and sys.version_info.micro < 3)
|
||||
)
|
||||
if is_find_lineno_broken:
|
||||
|
||||
def _find_lineno(self, obj, source_lines):
|
||||
"""On older Pythons, doctest code does not take into account
|
||||
`@property`. https://github.com/python/cpython/issues/61648
|
||||
|
||||
Moreover, wrapped Doctests need to be unwrapped so the correct
|
||||
line number is returned. #8796
|
||||
"""
|
||||
if isinstance(obj, property):
|
||||
obj = getattr(obj, "fget", obj)
|
||||
|
||||
if hasattr(obj, "__wrapped__"):
|
||||
# Get the main obj in case of it being wrapped
|
||||
obj = inspect.unwrap(obj)
|
||||
|
||||
# Type ignored because this is a private function.
|
||||
return super()._find_lineno( # type:ignore[misc]
|
||||
obj,
|
||||
source_lines,
|
||||
)
|
||||
elif py_ver_info_minor == (3, 12):
|
||||
|
||||
def _find_lineno(self, obj, source_lines):
|
||||
if isinstance(obj, FixtureFunctionDefinition):
|
||||
obj = inspect.unwrap(obj)
|
||||
|
||||
# Type ignored because this is a private function.
|
||||
return super()._find_lineno( # type:ignore[misc]
|
||||
obj,
|
||||
source_lines,
|
||||
)
|
||||
|
||||
if sys.version_info < (3, 13):
|
||||
|
||||
def _from_module(self, module, object):
|
||||
"""`cached_property` objects are never considered a part
|
||||
of the 'current module'. As such they are skipped by doctest.
|
||||
Here we override `_from_module` to check the underlying
|
||||
function instead. https://github.com/python/cpython/issues/107995
|
||||
"""
|
||||
if isinstance(object, functools.cached_property):
|
||||
object = object.func
|
||||
|
||||
# Type ignored because this is a private function.
|
||||
return super()._from_module(module, object) # type: ignore[misc]
|
||||
|
||||
try:
|
||||
module = self.obj
|
||||
except Collector.CollectError:
|
||||
if self.config.getvalue("doctest_ignore_import_errors"):
|
||||
skip(f"unable to import module {self.path!r}")
|
||||
else:
|
||||
raise
|
||||
|
||||
# doctests supports fixtures via `getfixture` and autouse.
|
||||
self.session._fixturemanager.parsefactories(self)
|
||||
|
||||
# Uses internal doctest module parsing mechanism.
|
||||
finder = MockAwareDocTestFinder()
|
||||
optionflags = get_optionflags(self.config)
|
||||
runner = _get_runner(
|
||||
verbose=False,
|
||||
optionflags=optionflags,
|
||||
checker=_get_checker(),
|
||||
continue_on_failure=_get_continue_on_failure(self.config),
|
||||
)
|
||||
|
||||
for test in finder.find(module, module.__name__):
|
||||
if test.examples: # skip empty doctests
|
||||
yield DoctestItem.from_parent(
|
||||
self, name=test.name, runner=runner, dtest=test
|
||||
)
|
||||
|
||||
|
||||
def _init_checker_class() -> type[doctest.OutputChecker]:
|
||||
import doctest
|
||||
|
||||
class LiteralsOutputChecker(doctest.OutputChecker):
|
||||
# Based on doctest_nose_plugin.py from the nltk project
|
||||
# (https://github.com/nltk/nltk) and on the "numtest" doctest extension
|
||||
# by Sebastien Boisgerault (https://github.com/boisgera/numtest).
|
||||
|
||||
_unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE)
|
||||
_bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE)
|
||||
_number_re = re.compile(
|
||||
r"""
|
||||
(?P<number>
|
||||
(?P<mantissa>
|
||||
(?P<integer1> [+-]?\d*)\.(?P<fraction>\d+)
|
||||
|
|
||||
(?P<integer2> [+-]?\d+)\.
|
||||
)
|
||||
(?:
|
||||
[Ee]
|
||||
(?P<exponent1> [+-]?\d+)
|
||||
)?
|
||||
|
|
||||
(?P<integer3> [+-]?\d+)
|
||||
(?:
|
||||
[Ee]
|
||||
(?P<exponent2> [+-]?\d+)
|
||||
)
|
||||
)
|
||||
""",
|
||||
re.VERBOSE,
|
||||
)
|
||||
|
||||
def check_output(self, want: str, got: str, optionflags: int) -> bool:
|
||||
if super().check_output(want, got, optionflags):
|
||||
return True
|
||||
|
||||
allow_unicode = optionflags & _get_allow_unicode_flag()
|
||||
allow_bytes = optionflags & _get_allow_bytes_flag()
|
||||
allow_number = optionflags & _get_number_flag()
|
||||
|
||||
if not allow_unicode and not allow_bytes and not allow_number:
|
||||
return False
|
||||
|
||||
def remove_prefixes(regex: re.Pattern[str], txt: str) -> str:
|
||||
return re.sub(regex, r"\1\2", txt)
|
||||
|
||||
if allow_unicode:
|
||||
want = remove_prefixes(self._unicode_literal_re, want)
|
||||
got = remove_prefixes(self._unicode_literal_re, got)
|
||||
|
||||
if allow_bytes:
|
||||
want = remove_prefixes(self._bytes_literal_re, want)
|
||||
got = remove_prefixes(self._bytes_literal_re, got)
|
||||
|
||||
if allow_number:
|
||||
got = self._remove_unwanted_precision(want, got)
|
||||
|
||||
return super().check_output(want, got, optionflags)
|
||||
|
||||
def _remove_unwanted_precision(self, want: str, got: str) -> str:
|
||||
wants = list(self._number_re.finditer(want))
|
||||
gots = list(self._number_re.finditer(got))
|
||||
if len(wants) != len(gots):
|
||||
return got
|
||||
offset = 0
|
||||
for w, g in zip(wants, gots, strict=True):
|
||||
fraction: str | None = w.group("fraction")
|
||||
exponent: str | None = w.group("exponent1")
|
||||
if exponent is None:
|
||||
exponent = w.group("exponent2")
|
||||
precision = 0 if fraction is None else len(fraction)
|
||||
if exponent is not None:
|
||||
precision -= int(exponent)
|
||||
if float(w.group()) == approx(float(g.group()), abs=10**-precision):
|
||||
# They're close enough. Replace the text we actually
|
||||
# got with the text we want, so that it will match when we
|
||||
# check the string literally.
|
||||
got = (
|
||||
got[: g.start() + offset] + w.group() + got[g.end() + offset :]
|
||||
)
|
||||
offset += w.end() - w.start() - (g.end() - g.start())
|
||||
return got
|
||||
|
||||
return LiteralsOutputChecker
|
||||
|
||||
|
||||
def _get_checker() -> doctest.OutputChecker:
|
||||
"""Return a doctest.OutputChecker subclass that supports some
|
||||
additional options:
|
||||
|
||||
* ALLOW_UNICODE and ALLOW_BYTES options to ignore u'' and b''
|
||||
prefixes (respectively) in string literals. Useful when the same
|
||||
doctest should run in Python 2 and Python 3.
|
||||
|
||||
* NUMBER to ignore floating-point differences smaller than the
|
||||
precision of the literal number in the doctest.
|
||||
|
||||
An inner class is used to avoid importing "doctest" at the module
|
||||
level.
|
||||
"""
|
||||
global CHECKER_CLASS
|
||||
if CHECKER_CLASS is None:
|
||||
CHECKER_CLASS = _init_checker_class()
|
||||
return CHECKER_CLASS()
|
||||
|
||||
|
||||
def _get_allow_unicode_flag() -> int:
|
||||
"""Register and return the ALLOW_UNICODE flag."""
|
||||
import doctest
|
||||
|
||||
return doctest.register_optionflag("ALLOW_UNICODE")
|
||||
|
||||
|
||||
def _get_allow_bytes_flag() -> int:
|
||||
"""Register and return the ALLOW_BYTES flag."""
|
||||
import doctest
|
||||
|
||||
return doctest.register_optionflag("ALLOW_BYTES")
|
||||
|
||||
|
||||
def _get_number_flag() -> int:
|
||||
"""Register and return the NUMBER flag."""
|
||||
import doctest
|
||||
|
||||
return doctest.register_optionflag("NUMBER")
|
||||
|
||||
|
||||
def _get_report_choice(key: str) -> int:
|
||||
"""Return the actual `doctest` module flag value.
|
||||
|
||||
We want to do it as late as possible to avoid importing `doctest` and all
|
||||
its dependencies when parsing options, as it adds overhead and breaks tests.
|
||||
"""
|
||||
import doctest
|
||||
|
||||
return {
|
||||
DOCTEST_REPORT_CHOICE_UDIFF: doctest.REPORT_UDIFF,
|
||||
DOCTEST_REPORT_CHOICE_CDIFF: doctest.REPORT_CDIFF,
|
||||
DOCTEST_REPORT_CHOICE_NDIFF: doctest.REPORT_NDIFF,
|
||||
DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE: doctest.REPORT_ONLY_FIRST_FAILURE,
|
||||
DOCTEST_REPORT_CHOICE_NONE: 0,
|
||||
}[key]
|
||||
|
||||
|
||||
@fixture(scope="session")
|
||||
def doctest_namespace() -> dict[str, Any]:
|
||||
"""Fixture that returns a :py:class:`dict` that will be injected into the
|
||||
namespace of doctests.
|
||||
|
||||
Usually this fixture is used in conjunction with another ``autouse`` fixture:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def add_np(doctest_namespace):
|
||||
doctest_namespace["np"] = numpy
|
||||
|
||||
For more details: :ref:`doctest_namespace`.
|
||||
"""
|
||||
return dict()
|
||||
@@ -0,0 +1,119 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
import os
|
||||
import sys
|
||||
|
||||
from _pytest.config import Config
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.nodes import Item
|
||||
from _pytest.stash import StashKey
|
||||
import pytest
|
||||
|
||||
|
||||
fault_handler_original_stderr_fd_key = StashKey[int]()
|
||||
fault_handler_stderr_fd_key = StashKey[int]()
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
help_timeout = (
|
||||
"Dump the traceback of all threads if a test takes "
|
||||
"more than TIMEOUT seconds to finish"
|
||||
)
|
||||
help_exit_on_timeout = (
|
||||
"Exit the test process if a test takes more than "
|
||||
"faulthandler_timeout seconds to finish"
|
||||
)
|
||||
parser.addini("faulthandler_timeout", help_timeout, type=float | str, default=0.0)
|
||||
parser.addini(
|
||||
"faulthandler_exit_on_timeout", help_exit_on_timeout, type="bool", default=False
|
||||
)
|
||||
|
||||
|
||||
def pytest_configure(config: Config) -> None:
|
||||
import faulthandler
|
||||
|
||||
# at teardown we want to restore the original faulthandler fileno
|
||||
# but faulthandler has no api to return the original fileno
|
||||
# so here we stash the stderr fileno to be used at teardown
|
||||
# sys.stderr and sys.__stderr__ may be closed or patched during the session
|
||||
# so we can't rely on their values being good at that point (#11572).
|
||||
stderr_fileno = get_stderr_fileno()
|
||||
if faulthandler.is_enabled():
|
||||
config.stash[fault_handler_original_stderr_fd_key] = stderr_fileno
|
||||
config.stash[fault_handler_stderr_fd_key] = os.dup(stderr_fileno)
|
||||
faulthandler.enable(file=config.stash[fault_handler_stderr_fd_key])
|
||||
|
||||
|
||||
def pytest_unconfigure(config: Config) -> None:
|
||||
import faulthandler
|
||||
|
||||
faulthandler.disable()
|
||||
# Close the dup file installed during pytest_configure.
|
||||
if fault_handler_stderr_fd_key in config.stash:
|
||||
os.close(config.stash[fault_handler_stderr_fd_key])
|
||||
del config.stash[fault_handler_stderr_fd_key]
|
||||
# Re-enable the faulthandler if it was originally enabled.
|
||||
if fault_handler_original_stderr_fd_key in config.stash:
|
||||
faulthandler.enable(config.stash[fault_handler_original_stderr_fd_key])
|
||||
del config.stash[fault_handler_original_stderr_fd_key]
|
||||
|
||||
|
||||
def get_stderr_fileno() -> int:
|
||||
try:
|
||||
fileno = sys.stderr.fileno()
|
||||
# The Twisted Logger will return an invalid file descriptor since it is not backed
|
||||
# by an FD. So, let's also forward this to the same code path as with pytest-xdist.
|
||||
if fileno == -1:
|
||||
raise AttributeError()
|
||||
return fileno
|
||||
except (AttributeError, ValueError):
|
||||
# pytest-xdist monkeypatches sys.stderr with an object that is not an actual file.
|
||||
# https://docs.python.org/3/library/faulthandler.html#issue-with-file-descriptors
|
||||
# This is potentially dangerous, but the best we can do.
|
||||
assert sys.__stderr__ is not None
|
||||
return sys.__stderr__.fileno()
|
||||
|
||||
|
||||
def get_timeout_config_value(config: Config) -> float:
|
||||
return float(config.getini("faulthandler_timeout") or 0.0)
|
||||
|
||||
|
||||
def get_exit_on_timeout_config_value(config: Config) -> bool:
|
||||
exit_on_timeout = config.getini("faulthandler_exit_on_timeout")
|
||||
assert isinstance(exit_on_timeout, bool)
|
||||
return exit_on_timeout
|
||||
|
||||
|
||||
@pytest.hookimpl(wrapper=True, trylast=True)
|
||||
def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]:
|
||||
timeout = get_timeout_config_value(item.config)
|
||||
exit_on_timeout = get_exit_on_timeout_config_value(item.config)
|
||||
if timeout > 0:
|
||||
import faulthandler
|
||||
|
||||
stderr = item.config.stash[fault_handler_stderr_fd_key]
|
||||
faulthandler.dump_traceback_later(timeout, file=stderr, exit=exit_on_timeout)
|
||||
try:
|
||||
return (yield)
|
||||
finally:
|
||||
faulthandler.cancel_dump_traceback_later()
|
||||
else:
|
||||
return (yield)
|
||||
|
||||
|
||||
@pytest.hookimpl(tryfirst=True)
|
||||
def pytest_enter_pdb() -> None:
|
||||
"""Cancel any traceback dumping due to timeout before entering pdb."""
|
||||
import faulthandler
|
||||
|
||||
faulthandler.cancel_dump_traceback_later()
|
||||
|
||||
|
||||
@pytest.hookimpl(tryfirst=True)
|
||||
def pytest_exception_interact() -> None:
|
||||
"""Cancel any traceback dumping due to an interactive exception being
|
||||
raised."""
|
||||
import faulthandler
|
||||
|
||||
faulthandler.cancel_dump_traceback_later()
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,45 @@
|
||||
"""Provides a function to report all internal modules for using freezing
|
||||
tools."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator
|
||||
import types
|
||||
|
||||
|
||||
def freeze_includes() -> list[str]:
|
||||
"""Return a list of module names used by pytest that should be
|
||||
included by cx_freeze."""
|
||||
import _pytest
|
||||
|
||||
result = list(_iter_all_modules(_pytest))
|
||||
return result
|
||||
|
||||
|
||||
def _iter_all_modules(
|
||||
package: str | types.ModuleType,
|
||||
prefix: str = "",
|
||||
) -> Iterator[str]:
|
||||
"""Iterate over the names of all modules that can be found in the given
|
||||
package, recursively.
|
||||
|
||||
>>> import _pytest
|
||||
>>> list(_iter_all_modules(_pytest))
|
||||
['_pytest._argcomplete', '_pytest._code.code', ...]
|
||||
"""
|
||||
import os
|
||||
import pkgutil
|
||||
|
||||
if isinstance(package, str):
|
||||
path = package
|
||||
else:
|
||||
# Type ignored because typeshed doesn't define ModuleType.__path__
|
||||
# (only defined on packages).
|
||||
package_path = package.__path__
|
||||
path, prefix = package_path[0], package.__name__ + "."
|
||||
for _, name, is_package in pkgutil.iter_modules([path]):
|
||||
if is_package:
|
||||
for m in _iter_all_modules(os.path.join(path, name), prefix=name + "."):
|
||||
yield prefix + m
|
||||
else:
|
||||
yield prefix + name
|
||||
@@ -0,0 +1,284 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Version info, help messages, tracing configuration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from collections.abc import Generator
|
||||
from collections.abc import Sequence
|
||||
import os
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import ExitCode
|
||||
from _pytest.config import PrintHelp
|
||||
from _pytest.config.argparsing import _ini_type_repr
|
||||
from _pytest.config.argparsing import _split_help_text
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.terminal import TerminalReporter
|
||||
import pytest
|
||||
|
||||
|
||||
class HelpAction(argparse.Action):
|
||||
"""An argparse Action that will raise a PrintHelp exception in order to skip
|
||||
the rest of the argument parsing when --help is passed.
|
||||
|
||||
This prevents argparse from raising UsageError when `--help` is used along
|
||||
with missing required arguments when any are defined, for example by
|
||||
``pytest_addoption``. This is similar to the way that the builtin argparse
|
||||
--help option is implemented by raising SystemExit.
|
||||
|
||||
To opt in to this behavior, the parse caller must set
|
||||
`namespace._raise_print_help = True`. Otherwise it just sets the option.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, option_strings: Sequence[str], dest: str, *, help: str | None = None
|
||||
) -> None:
|
||||
super().__init__(
|
||||
option_strings=option_strings,
|
||||
dest=dest,
|
||||
nargs=0,
|
||||
const=True,
|
||||
default=False,
|
||||
help=help,
|
||||
)
|
||||
|
||||
def __call__(
|
||||
self,
|
||||
parser: argparse.ArgumentParser,
|
||||
namespace: argparse.Namespace,
|
||||
values: str | Sequence[Any] | None,
|
||||
option_string: str | None = None,
|
||||
) -> None:
|
||||
setattr(namespace, self.dest, self.const)
|
||||
|
||||
if getattr(namespace, "_raise_print_help", False):
|
||||
raise PrintHelp
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("debugconfig")
|
||||
group.addoption(
|
||||
"-V",
|
||||
"--version",
|
||||
action="count",
|
||||
default=0,
|
||||
dest="version",
|
||||
help="Display pytest version and information about plugins. "
|
||||
"When given twice, also display information about plugins.",
|
||||
)
|
||||
group._addoption( # private to use reserved lower-case short option
|
||||
"-h",
|
||||
"--help",
|
||||
action=HelpAction,
|
||||
dest="help",
|
||||
help="Show help message and configuration info",
|
||||
)
|
||||
group._addoption( # private to use reserved lower-case short option
|
||||
"-p",
|
||||
action="append",
|
||||
dest="plugins",
|
||||
default=[],
|
||||
metavar="name",
|
||||
help="Early-load given plugin module name or entry point (multi-allowed). "
|
||||
"To avoid loading of plugins, use the `no:` prefix, e.g. "
|
||||
"`no:doctest`. See also --disable-plugin-autoload.",
|
||||
)
|
||||
group.addoption(
|
||||
"--disable-plugin-autoload",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="Disable plugin auto-loading through entry point packaging metadata. "
|
||||
"Only plugins explicitly specified in -p or env var PYTEST_PLUGINS will be loaded.",
|
||||
)
|
||||
group.addoption(
|
||||
"--traceconfig",
|
||||
"--trace-config",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="Trace considerations of conftest.py files",
|
||||
)
|
||||
group.addoption(
|
||||
"--debug",
|
||||
action="store",
|
||||
nargs="?",
|
||||
const="pytestdebug.log",
|
||||
dest="debug",
|
||||
metavar="DEBUG_FILE_NAME",
|
||||
help="Store internal tracing debug information in this log file. "
|
||||
"This file is opened with 'w' and truncated as a result, care advised. "
|
||||
"Default: pytestdebug.log.",
|
||||
)
|
||||
group._addoption( # private to use reserved lower-case short option
|
||||
"-o",
|
||||
"--override-ini",
|
||||
dest="override_ini",
|
||||
action="append",
|
||||
help='Override configuration option with "option=value" style, '
|
||||
"e.g. `-o strict_xfail=True -o cache_dir=cache`.",
|
||||
)
|
||||
|
||||
|
||||
@pytest.hookimpl(wrapper=True)
|
||||
def pytest_cmdline_parse() -> Generator[None, Config, Config]:
|
||||
config = yield
|
||||
|
||||
if config.option.debug:
|
||||
# --debug | --debug <file.log> was provided.
|
||||
path = config.option.debug
|
||||
debugfile = open(path, "w", encoding="utf-8")
|
||||
debugfile.write(
|
||||
"versions pytest-{}, "
|
||||
"python-{}\ninvocation_dir={}\ncwd={}\nargs={}\n\n".format(
|
||||
pytest.__version__,
|
||||
".".join(map(str, sys.version_info)),
|
||||
config.invocation_params.dir,
|
||||
os.getcwd(),
|
||||
config.invocation_params.args,
|
||||
)
|
||||
)
|
||||
config.trace.root.setwriter(debugfile.write)
|
||||
undo_tracing = config.pluginmanager.enable_tracing()
|
||||
sys.stderr.write(f"writing pytest debug information to {path}\n")
|
||||
|
||||
def unset_tracing() -> None:
|
||||
debugfile.close()
|
||||
sys.stderr.write(f"wrote pytest debug information to {debugfile.name}\n")
|
||||
config.trace.root.setwriter(None)
|
||||
undo_tracing()
|
||||
|
||||
config.add_cleanup(unset_tracing)
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def show_version_verbose(config: Config) -> None:
|
||||
"""Show verbose pytest version installation, including plugins."""
|
||||
sys.stdout.write(
|
||||
f"This is pytest version {pytest.__version__}, imported from {pytest.__file__}\n"
|
||||
)
|
||||
plugininfo = getpluginversioninfo(config)
|
||||
if plugininfo:
|
||||
for line in plugininfo:
|
||||
sys.stdout.write(line + "\n")
|
||||
|
||||
|
||||
def pytest_cmdline_main(config: Config) -> int | ExitCode | None:
|
||||
# Note: a single `--version` argument is handled directly by `Config.main()` to avoid starting up the entire
|
||||
# pytest infrastructure just to display the version (#13574).
|
||||
if config.option.version > 1:
|
||||
show_version_verbose(config)
|
||||
return ExitCode.OK
|
||||
elif config.option.help:
|
||||
config._do_configure()
|
||||
showhelp(config)
|
||||
config._ensure_unconfigure()
|
||||
return ExitCode.OK
|
||||
return None
|
||||
|
||||
|
||||
def showhelp(config: Config) -> None:
|
||||
reporter: TerminalReporter | None = config.pluginmanager.get_plugin(
|
||||
"terminalreporter"
|
||||
)
|
||||
assert reporter is not None
|
||||
tw = reporter._tw
|
||||
tw.write(config._parser.optparser.format_help())
|
||||
tw.line()
|
||||
tw.line(
|
||||
"[pytest] configuration options in the first "
|
||||
"pytest.toml|pytest.ini|tox.ini|setup.cfg|pyproject.toml file found:"
|
||||
)
|
||||
tw.line()
|
||||
|
||||
columns = tw.fullwidth # costly call
|
||||
indent_len = 24 # based on argparse's max_help_position=24
|
||||
indent = " " * indent_len
|
||||
for name in config._parser._inidict:
|
||||
help, type, _default = config._parser._inidict[name]
|
||||
if help is None:
|
||||
raise TypeError(f"help argument cannot be None for {name}")
|
||||
spec = f"{name} ({_ini_type_repr(type)}):"
|
||||
tw.write(f" {spec}")
|
||||
spec_len = len(spec)
|
||||
wrapped = _split_help_text(help, columns - indent_len)
|
||||
if spec_len > (indent_len - 3):
|
||||
# Display help starting at a new line.
|
||||
tw.line()
|
||||
for line in wrapped:
|
||||
tw.line(indent + line if line else "")
|
||||
else:
|
||||
# Display help starting after the spec, following lines indented.
|
||||
tw.write(" " * (indent_len - spec_len - 2))
|
||||
if wrapped:
|
||||
tw.line(wrapped[0])
|
||||
for line in wrapped[1:]:
|
||||
tw.line(indent + line if line else "")
|
||||
|
||||
tw.line()
|
||||
tw.line("Environment variables:")
|
||||
vars = [
|
||||
(
|
||||
"CI",
|
||||
"When set to a non-empty value, pytest knows it is running in a "
|
||||
"CI process and does not truncate summary info",
|
||||
),
|
||||
("BUILD_NUMBER", "Equivalent to CI"),
|
||||
("PYTEST_ADDOPTS", "Extra command line options"),
|
||||
("PYTEST_PLUGINS", "Comma-separated plugins to load during startup"),
|
||||
("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "Set to disable plugin auto-loading"),
|
||||
("PYTEST_DEBUG", "Set to enable debug tracing of pytest's internals"),
|
||||
("PYTEST_DEBUG_TEMPROOT", "Override the system temporary directory"),
|
||||
("PYTEST_THEME", "The Pygments style to use for code output"),
|
||||
("PYTEST_THEME_MODE", "Set the PYTEST_THEME to be either 'dark' or 'light'"),
|
||||
]
|
||||
for name, help in vars:
|
||||
tw.line(f" {name:<24} {help}")
|
||||
tw.line()
|
||||
tw.line()
|
||||
|
||||
tw.line("to see available markers type: pytest --markers")
|
||||
tw.line("to see available fixtures type: pytest --fixtures")
|
||||
tw.line(
|
||||
"(shown according to specified file_or_dir or current dir "
|
||||
"if not specified; fixtures with leading '_' are only shown "
|
||||
"with the '-v' option"
|
||||
)
|
||||
|
||||
for warningreport in reporter.stats.get("warnings", []):
|
||||
tw.line("warning : " + warningreport.message, red=True)
|
||||
|
||||
|
||||
def getpluginversioninfo(config: Config) -> list[str]:
|
||||
lines = []
|
||||
plugininfo = config.pluginmanager.list_plugin_distinfo()
|
||||
if plugininfo:
|
||||
lines.append("registered third-party plugins:")
|
||||
for plugin, dist in plugininfo:
|
||||
loc = getattr(plugin, "__file__", repr(plugin))
|
||||
content = f"{dist.project_name}-{dist.version} at {loc}"
|
||||
lines.append(" " + content)
|
||||
return lines
|
||||
|
||||
|
||||
def pytest_report_header(config: Config) -> list[str]:
|
||||
lines = []
|
||||
if config.option.debug or config.option.traceconfig:
|
||||
lines.append(f"using: pytest-{pytest.__version__}")
|
||||
|
||||
verinfo = getpluginversioninfo(config)
|
||||
if verinfo:
|
||||
lines.extend(verinfo)
|
||||
|
||||
if config.option.traceconfig:
|
||||
lines.append("active plugins:")
|
||||
items = config.pluginmanager.list_name_plugin()
|
||||
for name, plugin in items:
|
||||
if hasattr(plugin, "__file__"):
|
||||
r = plugin.__file__
|
||||
else:
|
||||
r = repr(plugin)
|
||||
lines.append(f" {name:<20}: {r}")
|
||||
return lines
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,761 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Report test results in JUnit-XML format, for use with Jenkins and build
|
||||
integration servers.
|
||||
|
||||
Based on initial code from Ross Lawley.
|
||||
|
||||
Output conforms to
|
||||
https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
import functools
|
||||
import os
|
||||
import platform
|
||||
import re
|
||||
from typing import Literal
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
from _pytest import nodes
|
||||
from _pytest import timing
|
||||
from _pytest._code.code import ExceptionRepr
|
||||
from _pytest._code.code import ReprFileLocation
|
||||
from _pytest.compat import assert_never
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import filename_arg
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.fixtures import FixtureRequest
|
||||
from _pytest.nodeid import coerce_node_id
|
||||
from _pytest.nodeid import NodeId
|
||||
from _pytest.reports import BaseReport
|
||||
from _pytest.reports import CollectReport
|
||||
from _pytest.reports import TestReport
|
||||
from _pytest.stash import StashKey
|
||||
from _pytest.terminal import TerminalReporter
|
||||
import pytest
|
||||
|
||||
|
||||
xml_key = StashKey["LogXML"]()
|
||||
|
||||
_JunitLogging = Literal["no", "log", "system-out", "system-err", "out-err", "all"]
|
||||
_JunitDurationReport = Literal["total", "call"]
|
||||
_JunitFamily = Literal["legacy", "xunit1", "xunit2"]
|
||||
|
||||
|
||||
def bin_xml_escape(arg: object) -> str:
|
||||
r"""Visually escape invalid XML characters.
|
||||
|
||||
For example, transforms
|
||||
'hello\aworld\b'
|
||||
into
|
||||
'hello#x07world#x08'
|
||||
Note that the #xABs are *not* XML escapes - missing the ampersand «.
|
||||
The idea is to escape visually for the user rather than for XML itself.
|
||||
"""
|
||||
|
||||
def repl(matchobj: re.Match[str]) -> str:
|
||||
i = ord(matchobj.group())
|
||||
if i <= 0xFF:
|
||||
return f"#x{i:02X}"
|
||||
else:
|
||||
return f"#x{i:04X}"
|
||||
|
||||
# The spec range of valid chars is:
|
||||
# Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
||||
# For an unknown(?) reason, we disallow #x7F (DEL) as well.
|
||||
illegal_xml_re = "[^\u0009\u000a\u000d\u0020-\u007e\u0080-\ud7ff\ue000-\ufffd\U00010000-\U0010ffff]"
|
||||
return re.sub(illegal_xml_re, repl, str(arg))
|
||||
|
||||
|
||||
# Budget for the ``message`` attribute of <failure> and <error> elements.
|
||||
# The full report is always written as the element text.
|
||||
MESSAGE_ATTR_BUDGET = 500
|
||||
|
||||
|
||||
def shorten_message(msg: str, budget: int = MESSAGE_ATTR_BUDGET) -> str:
|
||||
"""Shorten ``msg`` for use in a ``message`` attribute.
|
||||
|
||||
Whole lines are kept while they fit in ``budget`` characters; a first
|
||||
line that is longer than ``budget`` on its own is cut. If anything was
|
||||
dropped, a final line points readers to the element text, which holds
|
||||
the full report.
|
||||
"""
|
||||
lines = msg.splitlines() or [""]
|
||||
kept: list[str] = []
|
||||
size = 0
|
||||
for line in lines:
|
||||
if kept and size + len(line) + 1 > budget:
|
||||
break
|
||||
kept.append(line)
|
||||
size += len(line) + 1
|
||||
shortened = "\n".join(kept)
|
||||
first_line_cut = len(shortened) > budget
|
||||
if first_line_cut:
|
||||
shortened = shortened[:budget] + "..."
|
||||
remaining = len(lines) - len(kept)
|
||||
if remaining:
|
||||
note = f"+{remaining} more line{'s' if remaining > 1 else ''}"
|
||||
elif first_line_cut:
|
||||
note = "truncated"
|
||||
else:
|
||||
return msg
|
||||
return f"{shortened}\n[{note}; full report in element text]"
|
||||
|
||||
|
||||
def merge_family(left, right) -> None:
|
||||
result = {}
|
||||
for kl, vl in left.items():
|
||||
for vr in right.values():
|
||||
if not isinstance(vl, list):
|
||||
raise TypeError(type(vl))
|
||||
result[kl] = vl + vr
|
||||
left.update(result)
|
||||
|
||||
|
||||
families = { # pylint: disable=dict-init-mutate
|
||||
"_base": {"testcase": ["classname", "name"]},
|
||||
"_base_legacy": {"testcase": ["file", "line", "url"]},
|
||||
}
|
||||
# xUnit 1.x inherits legacy attributes.
|
||||
families["xunit1"] = families["_base"].copy()
|
||||
merge_family(families["xunit1"], families["_base_legacy"])
|
||||
|
||||
# xUnit 2.x uses strict base attributes.
|
||||
families["xunit2"] = families["_base"]
|
||||
|
||||
|
||||
class _NodeReporter:
|
||||
def __init__(self, node_id: NodeId, xml: LogXML) -> None:
|
||||
self.id = node_id
|
||||
self.xml = xml
|
||||
self.add_stats = self.xml.add_stats
|
||||
self.family = self.xml.family
|
||||
self.duration = 0.0
|
||||
self.properties: list[tuple[str, str]] = []
|
||||
self.nodes: list[ET.Element] = []
|
||||
self.attrs: dict[str, str] = {}
|
||||
|
||||
def append(self, node: ET.Element) -> None:
|
||||
self.xml.add_stats(node.tag)
|
||||
self.nodes.append(node)
|
||||
|
||||
def add_property(self, name: str, value: object) -> None:
|
||||
self.properties.append((str(name), bin_xml_escape(value)))
|
||||
|
||||
def add_attribute(self, name: str, value: object) -> None:
|
||||
self.attrs[str(name)] = bin_xml_escape(value)
|
||||
|
||||
def make_properties_node(self) -> ET.Element | None:
|
||||
"""Return a Junit node containing custom properties, if any."""
|
||||
if self.properties:
|
||||
properties = ET.Element("properties")
|
||||
for name, value in self.properties:
|
||||
properties.append(ET.Element("property", name=name, value=value))
|
||||
return properties
|
||||
return None
|
||||
|
||||
def record_testreport(self, testreport: TestReport | CollectReport) -> None:
|
||||
names = mangle_test_address(testreport.nodeid)
|
||||
existing_attrs = self.attrs
|
||||
classnames = names[:-1]
|
||||
if self.xml.prefix:
|
||||
classnames.insert(0, self.xml.prefix)
|
||||
location = testreport.location
|
||||
assert location is not None
|
||||
attrs: dict[str, str] = {
|
||||
"classname": ".".join(classnames),
|
||||
"name": bin_xml_escape(names[-1]),
|
||||
"file": location[0],
|
||||
}
|
||||
if location[1] is not None:
|
||||
attrs["line"] = str(location[1])
|
||||
if hasattr(testreport, "url"):
|
||||
attrs["url"] = testreport.url
|
||||
self.attrs = attrs
|
||||
self.attrs.update(existing_attrs) # Restore any user-defined attributes.
|
||||
|
||||
# Preserve legacy testcase behavior.
|
||||
if self.family == "xunit1":
|
||||
return
|
||||
|
||||
# Filter out attributes not permitted by this test family.
|
||||
# Including custom attributes because they are not valid here.
|
||||
temp_attrs = {}
|
||||
for key in self.attrs:
|
||||
if key in families[self.family]["testcase"]:
|
||||
temp_attrs[key] = self.attrs[key]
|
||||
self.attrs = temp_attrs
|
||||
|
||||
def to_xml(self) -> ET.Element:
|
||||
testcase = ET.Element("testcase", self.attrs, time=f"{self.duration:.3f}")
|
||||
properties = self.make_properties_node()
|
||||
if properties is not None:
|
||||
testcase.append(properties)
|
||||
testcase.extend(self.nodes)
|
||||
return testcase
|
||||
|
||||
def _add_simple(self, tag: str, message: str, data: str | None = None) -> None:
|
||||
node = ET.Element(tag, message=message)
|
||||
node.text = bin_xml_escape(data)
|
||||
self.append(node)
|
||||
|
||||
def write_captured_output(self, report: TestReport) -> None:
|
||||
if not self.xml.log_passing_tests and report.passed:
|
||||
return
|
||||
|
||||
content_out = report.capstdout
|
||||
content_log = report.caplog
|
||||
content_err = report.capstderr
|
||||
if self.xml.logging == "no":
|
||||
return
|
||||
content_all = ""
|
||||
if self.xml.logging in ["log", "all"]:
|
||||
content_all = self._prepare_content(content_log, " Captured Log ")
|
||||
if self.xml.logging in ["system-out", "out-err", "all"]:
|
||||
content_all += self._prepare_content(content_out, " Captured Out ")
|
||||
self._write_content(report, content_all, "system-out")
|
||||
content_all = ""
|
||||
if self.xml.logging in ["system-err", "out-err", "all"]:
|
||||
content_all += self._prepare_content(content_err, " Captured Err ")
|
||||
self._write_content(report, content_all, "system-err")
|
||||
content_all = ""
|
||||
if content_all:
|
||||
self._write_content(report, content_all, "system-out")
|
||||
|
||||
def _prepare_content(self, content: str, header: str) -> str:
|
||||
return "\n".join([header.center(80, "-"), content, ""])
|
||||
|
||||
def _write_content(self, report: TestReport, content: str, jheader: str) -> None:
|
||||
tag = ET.Element(jheader)
|
||||
tag.text = bin_xml_escape(content)
|
||||
self.append(tag)
|
||||
|
||||
def append_pass(self, report: TestReport) -> None:
|
||||
self.add_stats("passed")
|
||||
|
||||
def append_failure(self, report: TestReport) -> None:
|
||||
# msg = str(report.longrepr.reprtraceback.extraline)
|
||||
if hasattr(report, "wasxfail"):
|
||||
self._add_simple("skipped", "xfail-marked test passes unexpectedly")
|
||||
else:
|
||||
assert report.longrepr is not None
|
||||
reprcrash: ReprFileLocation | None = getattr(
|
||||
report.longrepr, "reprcrash", None
|
||||
)
|
||||
if reprcrash is not None:
|
||||
message = reprcrash.message
|
||||
else:
|
||||
message = str(report.longrepr)
|
||||
message = bin_xml_escape(shorten_message(message))
|
||||
self._add_simple("failure", message, str(report.longrepr))
|
||||
|
||||
def append_collect_error(self, report: CollectReport) -> None:
|
||||
# msg = str(report.longrepr.reprtraceback.extraline)
|
||||
assert report.longrepr is not None
|
||||
self._add_simple("error", "collection failure", str(report.longrepr))
|
||||
|
||||
def append_collect_skipped(self, report: CollectReport) -> None:
|
||||
self._add_simple("skipped", "collection skipped", str(report.longrepr))
|
||||
|
||||
def append_error(self, report: TestReport) -> None:
|
||||
assert report.longrepr is not None
|
||||
reprcrash: ReprFileLocation | None = getattr(report.longrepr, "reprcrash", None)
|
||||
if reprcrash is not None:
|
||||
reason = reprcrash.message
|
||||
else:
|
||||
reason = str(report.longrepr)
|
||||
|
||||
if report.when == "teardown":
|
||||
msg = f'failed on teardown with "{reason}"'
|
||||
else:
|
||||
msg = f'failed on setup with "{reason}"'
|
||||
self._add_simple(
|
||||
"error", bin_xml_escape(shorten_message(msg)), str(report.longrepr)
|
||||
)
|
||||
|
||||
def append_skipped(self, report: TestReport) -> None:
|
||||
if hasattr(report, "wasxfail"):
|
||||
xfailreason = report.wasxfail
|
||||
xfailreason = xfailreason.removeprefix("reason: ")
|
||||
xfailreason = bin_xml_escape(xfailreason)
|
||||
skipped = ET.Element("skipped", type="pytest.xfail", message=xfailreason)
|
||||
self.append(skipped)
|
||||
else:
|
||||
assert isinstance(report.longrepr, tuple)
|
||||
filename, lineno, skipreason = report.longrepr
|
||||
skipreason = skipreason.removeprefix("Skipped: ")
|
||||
details = f"{filename}:{lineno}: {skipreason}"
|
||||
|
||||
skipped = ET.Element(
|
||||
"skipped", type="pytest.skip", message=bin_xml_escape(skipreason)
|
||||
)
|
||||
skipped.text = bin_xml_escape(details)
|
||||
self.append(skipped)
|
||||
self.write_captured_output(report)
|
||||
|
||||
def finalize(self) -> None:
|
||||
data = self.to_xml()
|
||||
self.__dict__.clear()
|
||||
# Type ignored because mypy doesn't like overriding a method.
|
||||
# Also the return value doesn't match...
|
||||
self.to_xml = lambda: data # type: ignore[method-assign]
|
||||
|
||||
|
||||
def _warn_incompatibility_with_xunit2(
|
||||
request: FixtureRequest, fixture_name: str
|
||||
) -> None:
|
||||
"""Emit a PytestWarning about the given fixture being incompatible with newer xunit revisions."""
|
||||
from _pytest.warning_types import PytestWarning
|
||||
|
||||
xml = request.config.stash.get(xml_key, None)
|
||||
if xml is not None and xml.family not in ("xunit1", "legacy"):
|
||||
request.node.warn(
|
||||
PytestWarning(
|
||||
f"{fixture_name} is incompatible with junit_family '{xml.family}' (use 'legacy' or 'xunit1')"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def record_property(request: FixtureRequest) -> Callable[[str, object], None]:
|
||||
"""Add extra properties to the calling test.
|
||||
|
||||
User properties become part of the test report and are available to the
|
||||
configured reporters, like JUnit XML.
|
||||
|
||||
The fixture is callable with ``name, value``. The value is automatically
|
||||
XML-encoded.
|
||||
|
||||
Example::
|
||||
|
||||
def test_function(record_property):
|
||||
record_property("example_key", 1)
|
||||
"""
|
||||
_warn_incompatibility_with_xunit2(request, "record_property")
|
||||
|
||||
def append_property(name: str, value: object) -> None:
|
||||
request.node.user_properties.append((name, value))
|
||||
|
||||
return append_property
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def record_xml_attribute(request: FixtureRequest) -> Callable[[str, object], None]:
|
||||
"""Add extra xml attributes to the tag for the calling test.
|
||||
|
||||
The fixture is callable with ``name, value``. The value is
|
||||
automatically XML-encoded.
|
||||
"""
|
||||
from _pytest.warning_types import PytestExperimentalApiWarning
|
||||
|
||||
request.node.warn(
|
||||
PytestExperimentalApiWarning("record_xml_attribute is an experimental feature")
|
||||
)
|
||||
|
||||
_warn_incompatibility_with_xunit2(request, "record_xml_attribute")
|
||||
|
||||
# Declare noop
|
||||
def add_attr_noop(name: str, value: object) -> None:
|
||||
pass
|
||||
|
||||
attr_func = add_attr_noop
|
||||
|
||||
xml = request.config.stash.get(xml_key, None)
|
||||
if xml is not None:
|
||||
node_reporter = xml.node_reporter(request.node.id)
|
||||
attr_func = node_reporter.add_attribute
|
||||
|
||||
return attr_func
|
||||
|
||||
|
||||
def _check_record_param_type(param: str, v: str) -> None:
|
||||
"""Used by record_testsuite_property to check that the given parameter name is of the proper
|
||||
type."""
|
||||
__tracebackhide__ = True
|
||||
if not isinstance(v, str):
|
||||
msg = "{param} parameter needs to be a string, but {g} given" # type: ignore[unreachable]
|
||||
raise TypeError(msg.format(param=param, g=type(v).__name__))
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def record_testsuite_property(request: FixtureRequest) -> Callable[[str, object], None]:
|
||||
"""Record a new ``<property>`` tag as child of the root ``<testsuite>``.
|
||||
|
||||
This is suitable to writing global information regarding the entire test
|
||||
suite, and is compatible with ``xunit2`` JUnit family.
|
||||
|
||||
This is a ``session``-scoped fixture which is called with ``(name, value)``. Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
def test_foo(record_testsuite_property):
|
||||
record_testsuite_property("ARCH", "PPC")
|
||||
record_testsuite_property("STORAGE_TYPE", "CEPH")
|
||||
|
||||
:param name:
|
||||
The property name.
|
||||
:param value:
|
||||
The property value. Will be converted to a string.
|
||||
|
||||
.. warning::
|
||||
|
||||
Currently this fixture **does not work** with the
|
||||
`pytest-xdist <https://github.com/pytest-dev/pytest-xdist>`__ plugin. See
|
||||
:issue:`7767` for details.
|
||||
"""
|
||||
__tracebackhide__ = True
|
||||
|
||||
def record_func(name: str, value: object) -> None:
|
||||
"""No-op function in case --junit-xml was not passed in the command-line."""
|
||||
__tracebackhide__ = True
|
||||
_check_record_param_type("name", name)
|
||||
|
||||
xml = request.config.stash.get(xml_key, None)
|
||||
if xml is not None:
|
||||
record_func = xml.add_global_property
|
||||
return record_func
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("terminal reporting")
|
||||
group.addoption(
|
||||
"--junitxml",
|
||||
"--junit-xml",
|
||||
action="store",
|
||||
dest="xmlpath",
|
||||
metavar="path",
|
||||
type=functools.partial(filename_arg, optname="--junitxml"),
|
||||
default=None,
|
||||
help="Create junit-xml style report file at given path",
|
||||
)
|
||||
group.addoption(
|
||||
"--junitprefix",
|
||||
"--junit-prefix",
|
||||
action="store",
|
||||
metavar="str",
|
||||
default=None,
|
||||
help="Prepend prefix to classnames in junit-xml output",
|
||||
)
|
||||
parser.addini(
|
||||
"junit_suite_name", "Test suite name for JUnit report", default="pytest"
|
||||
)
|
||||
parser.addini(
|
||||
"junit_logging",
|
||||
"Write captured log messages to JUnit report",
|
||||
type=_JunitLogging,
|
||||
default="no",
|
||||
)
|
||||
parser.addini(
|
||||
"junit_log_passing_tests",
|
||||
"Capture log information for passing tests to JUnit report: ",
|
||||
type="bool",
|
||||
default=True,
|
||||
)
|
||||
parser.addini(
|
||||
"junit_duration_report",
|
||||
"Duration time to report",
|
||||
type=_JunitDurationReport,
|
||||
default="total",
|
||||
)
|
||||
parser.addini(
|
||||
"junit_family",
|
||||
"Emit XML for schema",
|
||||
type=_JunitFamily,
|
||||
default="xunit2",
|
||||
)
|
||||
|
||||
|
||||
def pytest_configure(config: Config) -> None:
|
||||
xmlpath = config.option.xmlpath
|
||||
# Prevent opening xmllog on worker nodes (xdist).
|
||||
if xmlpath and not hasattr(config, "workerinput"):
|
||||
config.stash[xml_key] = LogXML(
|
||||
xmlpath,
|
||||
config.option.junitprefix,
|
||||
config.getini("junit_suite_name"),
|
||||
config.getini("junit_logging"),
|
||||
config.getini("junit_duration_report"),
|
||||
config.getini("junit_family"),
|
||||
config.getini("junit_log_passing_tests"),
|
||||
)
|
||||
config.pluginmanager.register(config.stash[xml_key])
|
||||
|
||||
|
||||
def pytest_unconfigure(config: Config) -> None:
|
||||
xml = config.stash.get(xml_key, None)
|
||||
if xml:
|
||||
del config.stash[xml_key]
|
||||
config.pluginmanager.unregister(xml)
|
||||
|
||||
|
||||
def mangle_test_address(address: str) -> list[str]:
|
||||
oid = NodeId.parse(address)
|
||||
names = [oid.path, *oid.names]
|
||||
# Convert file path to dotted path.
|
||||
names[0] = names[0].replace(nodes.SEP, ".")
|
||||
names[0] = re.sub(r"\.py$", "", names[0])
|
||||
# Put any params back as one opaque bracket (the '-'-separated
|
||||
# param-call-boundary structure inside cannot be reliably recovered).
|
||||
if oid.params is not None:
|
||||
names[-1] += f"[{oid.params}]"
|
||||
return names
|
||||
|
||||
|
||||
class LogXML:
|
||||
def __init__(
|
||||
self,
|
||||
logfile,
|
||||
prefix: str | None,
|
||||
suite_name: str = "pytest",
|
||||
logging: _JunitLogging = "no",
|
||||
report_duration: _JunitDurationReport = "total",
|
||||
family: _JunitFamily = "xunit1",
|
||||
log_passing_tests: bool = True,
|
||||
) -> None:
|
||||
logfile = os.path.expanduser(os.path.expandvars(logfile))
|
||||
self.logfile = os.path.normpath(os.path.abspath(logfile))
|
||||
self.prefix = prefix
|
||||
self.suite_name = suite_name
|
||||
self.logging = logging
|
||||
self.log_passing_tests = log_passing_tests
|
||||
self.report_duration = report_duration
|
||||
self.family = family
|
||||
self.stats: dict[str, int] = dict.fromkeys(
|
||||
["error", "passed", "failure", "skipped"], 0
|
||||
)
|
||||
self.node_reporters: dict[tuple[NodeId, object], _NodeReporter] = {}
|
||||
self.node_reporters_ordered: list[_NodeReporter] = []
|
||||
self.global_properties: list[tuple[str, str]] = []
|
||||
|
||||
# List of reports that failed on call but teardown is pending.
|
||||
self.open_reports: list[TestReport] = []
|
||||
self.cnt_double_fail_tests = 0
|
||||
|
||||
# Replaces convenience family with real family.
|
||||
if self.family == "legacy":
|
||||
self.family = "xunit1"
|
||||
|
||||
def finalize(self, report: TestReport) -> None:
|
||||
node_id = report.id
|
||||
# Local hack to handle xdist report order.
|
||||
workernode = getattr(report, "node", None)
|
||||
reporter = self.node_reporters.pop((node_id, workernode))
|
||||
|
||||
for propname, propvalue in report.user_properties:
|
||||
reporter.add_property(propname, str(propvalue))
|
||||
|
||||
if reporter is not None:
|
||||
reporter.finalize()
|
||||
|
||||
def node_reporter(self, report: BaseReport | NodeId | str) -> _NodeReporter:
|
||||
match report:
|
||||
case NodeId():
|
||||
node_id = coerce_node_id(report)
|
||||
case str():
|
||||
node_id = coerce_node_id(report)
|
||||
case BaseReport():
|
||||
node_id = report.id
|
||||
case _: # pragma: no cover
|
||||
assert_never(report)
|
||||
# Local hack to handle xdist report order.
|
||||
workernode = getattr(report, "node", None)
|
||||
|
||||
key = node_id, workernode
|
||||
|
||||
if key in self.node_reporters:
|
||||
# TODO: breaks for --dist=each
|
||||
return self.node_reporters[key]
|
||||
|
||||
reporter = _NodeReporter(node_id, self)
|
||||
|
||||
self.node_reporters[key] = reporter
|
||||
self.node_reporters_ordered.append(reporter)
|
||||
|
||||
return reporter
|
||||
|
||||
def add_stats(self, key: str) -> None:
|
||||
if key in self.stats:
|
||||
self.stats[key] += 1
|
||||
|
||||
def _opentestcase(self, report: TestReport | CollectReport) -> _NodeReporter:
|
||||
reporter = self.node_reporter(report)
|
||||
reporter.record_testreport(report)
|
||||
return reporter
|
||||
|
||||
def pytest_runtest_logreport(self, report: TestReport) -> None:
|
||||
"""Handle a setup/call/teardown report, generating the appropriate
|
||||
XML tags as necessary.
|
||||
|
||||
Note: due to plugins like xdist, this hook may be called in interlaced
|
||||
order with reports from other nodes. For example:
|
||||
|
||||
Usual call order:
|
||||
-> setup node1
|
||||
-> call node1
|
||||
-> teardown node1
|
||||
-> setup node2
|
||||
-> call node2
|
||||
-> teardown node2
|
||||
|
||||
Possible call order in xdist:
|
||||
-> setup node1
|
||||
-> call node1
|
||||
-> setup node2
|
||||
-> call node2
|
||||
-> teardown node2
|
||||
-> teardown node1
|
||||
"""
|
||||
close_report = None
|
||||
if report.passed:
|
||||
if report.when == "call": # ignore setup/teardown
|
||||
reporter = self._opentestcase(report)
|
||||
reporter.append_pass(report)
|
||||
elif report.failed:
|
||||
if report.when == "teardown":
|
||||
# The following vars are needed when xdist plugin is used.
|
||||
report_wid = getattr(report, "worker_id", None)
|
||||
report_ii = getattr(report, "item_index", None)
|
||||
close_report = next(
|
||||
(
|
||||
rep
|
||||
for rep in self.open_reports
|
||||
if (
|
||||
rep.id == report.id
|
||||
and getattr(rep, "item_index", None) == report_ii
|
||||
and getattr(rep, "worker_id", None) == report_wid
|
||||
)
|
||||
),
|
||||
None,
|
||||
)
|
||||
if close_report:
|
||||
# We need to open new testcase in case we have failure in
|
||||
# call and error in teardown in order to follow junit
|
||||
# schema.
|
||||
self.finalize(close_report)
|
||||
else:
|
||||
# A passing call with a teardown error creates separate
|
||||
# terminal reports, but JUnit XML keeps one testcase
|
||||
# element for that item (#3850).
|
||||
self.cnt_double_fail_tests += int(
|
||||
(
|
||||
report.id,
|
||||
getattr(report, "node", None),
|
||||
)
|
||||
in self.node_reporters
|
||||
)
|
||||
reporter = self._opentestcase(report)
|
||||
if report.when == "call":
|
||||
reporter.append_failure(report)
|
||||
self.open_reports.append(report)
|
||||
if not self.log_passing_tests:
|
||||
reporter.write_captured_output(report)
|
||||
else:
|
||||
reporter.append_error(report)
|
||||
elif report.skipped:
|
||||
reporter = self._opentestcase(report)
|
||||
reporter.append_skipped(report)
|
||||
self.update_testcase_duration(report)
|
||||
if report.when == "teardown":
|
||||
reporter = self._opentestcase(report)
|
||||
reporter.write_captured_output(report)
|
||||
|
||||
self.finalize(report)
|
||||
report_wid = getattr(report, "worker_id", None)
|
||||
report_ii = getattr(report, "item_index", None)
|
||||
close_report = next(
|
||||
(
|
||||
rep
|
||||
for rep in self.open_reports
|
||||
if (
|
||||
rep.id == report.id
|
||||
and getattr(rep, "item_index", None) == report_ii
|
||||
and getattr(rep, "worker_id", None) == report_wid
|
||||
)
|
||||
),
|
||||
None,
|
||||
)
|
||||
if close_report:
|
||||
self.open_reports.remove(close_report)
|
||||
|
||||
def update_testcase_duration(self, report: TestReport) -> None:
|
||||
"""Accumulate total duration for nodeid from given report and update
|
||||
the Junit.testcase with the new total if already created."""
|
||||
if self.report_duration in {"total", report.when}:
|
||||
reporter = self.node_reporter(report)
|
||||
reporter.duration += getattr(report, "duration", 0.0)
|
||||
|
||||
def pytest_collectreport(self, report: CollectReport) -> None:
|
||||
if not report.passed:
|
||||
reporter = self._opentestcase(report)
|
||||
if report.failed:
|
||||
reporter.append_collect_error(report)
|
||||
else:
|
||||
reporter.append_collect_skipped(report)
|
||||
|
||||
def pytest_internalerror(self, excrepr: ExceptionRepr) -> None:
|
||||
reporter = self.node_reporter("internal")
|
||||
reporter.attrs.update(classname="pytest", name="internal")
|
||||
reporter._add_simple("error", "internal error", str(excrepr))
|
||||
|
||||
def pytest_sessionstart(self) -> None:
|
||||
self.suite_start = timing.Instant()
|
||||
|
||||
def pytest_sessionfinish(self) -> None:
|
||||
dirname = os.path.dirname(os.path.abspath(self.logfile))
|
||||
# exist_ok avoids filesystem race conditions between checking path existence and requesting creation
|
||||
os.makedirs(dirname, exist_ok=True)
|
||||
|
||||
with open(self.logfile, "w", encoding="utf-8") as logfile:
|
||||
duration = self.suite_start.elapsed()
|
||||
|
||||
numtests = (
|
||||
self.stats["passed"]
|
||||
+ self.stats["failure"]
|
||||
+ self.stats["skipped"]
|
||||
+ self.stats["error"]
|
||||
- self.cnt_double_fail_tests
|
||||
)
|
||||
logfile.write('<?xml version="1.0" encoding="utf-8"?>')
|
||||
|
||||
suite_node = ET.Element(
|
||||
"testsuite",
|
||||
name=self.suite_name,
|
||||
errors=str(self.stats["error"]),
|
||||
failures=str(self.stats["failure"]),
|
||||
skipped=str(self.stats["skipped"]),
|
||||
tests=str(numtests),
|
||||
time=f"{duration.seconds:.3f}",
|
||||
timestamp=self.suite_start.as_utc().astimezone().isoformat(),
|
||||
hostname=platform.node(),
|
||||
)
|
||||
global_properties = self._get_global_properties_node()
|
||||
if global_properties is not None:
|
||||
suite_node.append(global_properties)
|
||||
for node_reporter in self.node_reporters_ordered:
|
||||
suite_node.append(node_reporter.to_xml())
|
||||
testsuites = ET.Element("testsuites")
|
||||
testsuites.set("name", "pytest tests")
|
||||
testsuites.append(suite_node)
|
||||
logfile.write(ET.tostring(testsuites, encoding="unicode"))
|
||||
|
||||
def pytest_terminal_summary(
|
||||
self, terminalreporter: TerminalReporter, config: pytest.Config
|
||||
) -> None:
|
||||
if config.get_verbosity() >= 0:
|
||||
terminalreporter.write_sep("-", f"generated xml file: {self.logfile}")
|
||||
|
||||
def add_global_property(self, name: str, value: object) -> None:
|
||||
__tracebackhide__ = True
|
||||
_check_record_param_type("name", name)
|
||||
self.global_properties.append((name, bin_xml_escape(value)))
|
||||
|
||||
def _get_global_properties_node(self) -> ET.Element | None:
|
||||
"""Return a Junit node containing custom properties, if any."""
|
||||
if self.global_properties:
|
||||
properties = ET.Element("properties")
|
||||
for name, value in self.global_properties:
|
||||
properties.append(ET.Element("property", name=name, value=value))
|
||||
return properties
|
||||
return None
|
||||
@@ -0,0 +1,471 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Add backward compatibility support for the legacy py path type."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
from typing import Final
|
||||
from typing import final
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from iniconfig import SectionWrapper
|
||||
|
||||
from _pytest.cacheprovider import Cache
|
||||
from _pytest.compat import LEGACY_PATH
|
||||
from _pytest.compat import legacy_path
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import hookimpl
|
||||
from _pytest.config import PytestPluginManager
|
||||
from _pytest.deprecated import check_ispytest
|
||||
from _pytest.fixtures import fixture
|
||||
from _pytest.fixtures import FixtureRequest
|
||||
from _pytest.main import Session
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
from _pytest.nodes import Collector
|
||||
from _pytest.nodes import Item
|
||||
from _pytest.nodes import Node
|
||||
from _pytest.pytester import HookRecorder
|
||||
from _pytest.pytester import Pytester
|
||||
from _pytest.pytester import RunResult
|
||||
from _pytest.terminal import TerminalReporter
|
||||
from _pytest.tmpdir import TempPathFactory
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import pexpect
|
||||
|
||||
|
||||
@final
|
||||
class Testdir:
|
||||
"""
|
||||
Similar to :class:`Pytester`, but this class works with legacy legacy_path objects instead.
|
||||
|
||||
All methods just forward to an internal :class:`Pytester` instance, converting results
|
||||
to `legacy_path` objects as necessary.
|
||||
"""
|
||||
|
||||
__test__ = False
|
||||
|
||||
CLOSE_STDIN: Final = Pytester.CLOSE_STDIN
|
||||
TimeoutExpired: Final = Pytester.TimeoutExpired
|
||||
|
||||
def __init__(self, pytester: Pytester, *, _ispytest: bool = False) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
self._pytester = pytester
|
||||
|
||||
@property
|
||||
def tmpdir(self) -> LEGACY_PATH:
|
||||
"""Temporary directory where tests are executed."""
|
||||
return legacy_path(self._pytester.path)
|
||||
|
||||
@property
|
||||
def test_tmproot(self) -> LEGACY_PATH:
|
||||
return legacy_path(self._pytester._test_tmproot)
|
||||
|
||||
@property
|
||||
def request(self):
|
||||
return self._pytester._request
|
||||
|
||||
@property
|
||||
def plugins(self):
|
||||
return self._pytester.plugins
|
||||
|
||||
@plugins.setter
|
||||
def plugins(self, plugins):
|
||||
self._pytester.plugins = plugins
|
||||
|
||||
@property
|
||||
def monkeypatch(self) -> MonkeyPatch:
|
||||
return self._pytester._monkeypatch
|
||||
|
||||
def make_hook_recorder(self, pluginmanager) -> HookRecorder:
|
||||
"""See :meth:`Pytester.make_hook_recorder`."""
|
||||
return self._pytester.make_hook_recorder(pluginmanager)
|
||||
|
||||
def chdir(self) -> None:
|
||||
"""See :meth:`Pytester.chdir`."""
|
||||
return self._pytester.chdir()
|
||||
|
||||
def finalize(self) -> None:
|
||||
return self._pytester._finalize()
|
||||
|
||||
def makefile(self, ext, *args, **kwargs) -> LEGACY_PATH:
|
||||
"""See :meth:`Pytester.makefile`."""
|
||||
if ext and not ext.startswith("."):
|
||||
# pytester.makefile is going to throw a ValueError in a way that
|
||||
# testdir.makefile did not, because
|
||||
# pathlib.Path is stricter suffixes than py.path
|
||||
# This ext arguments is likely user error, but since testdir has
|
||||
# allowed this, we will prepend "." as a workaround to avoid breaking
|
||||
# testdir usage that worked before
|
||||
ext = "." + ext
|
||||
return legacy_path(self._pytester.makefile(ext, *args, **kwargs))
|
||||
|
||||
def makeconftest(self, source) -> LEGACY_PATH:
|
||||
"""See :meth:`Pytester.makeconftest`."""
|
||||
return legacy_path(self._pytester.makeconftest(source))
|
||||
|
||||
def makeini(self, source) -> LEGACY_PATH:
|
||||
"""See :meth:`Pytester.makeini`."""
|
||||
return legacy_path(self._pytester.makeini(source))
|
||||
|
||||
def getinicfg(self, source: str) -> SectionWrapper:
|
||||
"""See :meth:`Pytester.getinicfg`."""
|
||||
return self._pytester.getinicfg(source)
|
||||
|
||||
def makepyprojecttoml(self, source) -> LEGACY_PATH:
|
||||
"""See :meth:`Pytester.makepyprojecttoml`."""
|
||||
return legacy_path(self._pytester.makepyprojecttoml(source))
|
||||
|
||||
def makepyfile(self, *args, **kwargs) -> LEGACY_PATH:
|
||||
"""See :meth:`Pytester.makepyfile`."""
|
||||
return legacy_path(self._pytester.makepyfile(*args, **kwargs))
|
||||
|
||||
def maketxtfile(self, *args, **kwargs) -> LEGACY_PATH:
|
||||
"""See :meth:`Pytester.maketxtfile`."""
|
||||
return legacy_path(self._pytester.maketxtfile(*args, **kwargs))
|
||||
|
||||
def syspathinsert(self, path=None) -> None:
|
||||
"""See :meth:`Pytester.syspathinsert`."""
|
||||
return self._pytester.syspathinsert(path)
|
||||
|
||||
def mkdir(self, name) -> LEGACY_PATH:
|
||||
"""See :meth:`Pytester.mkdir`."""
|
||||
return legacy_path(self._pytester.mkdir(name))
|
||||
|
||||
def mkpydir(self, name) -> LEGACY_PATH:
|
||||
"""See :meth:`Pytester.mkpydir`."""
|
||||
return legacy_path(self._pytester.mkpydir(name))
|
||||
|
||||
def copy_example(self, name=None) -> LEGACY_PATH:
|
||||
"""See :meth:`Pytester.copy_example`."""
|
||||
return legacy_path(self._pytester.copy_example(name))
|
||||
|
||||
def getnode(self, config: Config, arg) -> Item | Collector | None:
|
||||
"""See :meth:`Pytester.getnode`."""
|
||||
return self._pytester.getnode(config, arg)
|
||||
|
||||
def getpathnode(self, path):
|
||||
"""See :meth:`Pytester.getpathnode`."""
|
||||
return self._pytester.getpathnode(path)
|
||||
|
||||
def genitems(self, colitems: list[Item | Collector]) -> list[Item]:
|
||||
"""See :meth:`Pytester.genitems`."""
|
||||
return self._pytester.genitems(colitems)
|
||||
|
||||
def runitem(self, source):
|
||||
"""See :meth:`Pytester.runitem`."""
|
||||
return self._pytester.runitem(source)
|
||||
|
||||
def inline_runsource(self, source, *cmdlineargs):
|
||||
"""See :meth:`Pytester.inline_runsource`."""
|
||||
return self._pytester.inline_runsource(source, *cmdlineargs)
|
||||
|
||||
def inline_genitems(self, *args):
|
||||
"""See :meth:`Pytester.inline_genitems`."""
|
||||
return self._pytester.inline_genitems(*args)
|
||||
|
||||
def inline_run(self, *args, plugins=(), no_reraise_ctrlc: bool = False):
|
||||
"""See :meth:`Pytester.inline_run`."""
|
||||
return self._pytester.inline_run(
|
||||
*args, plugins=plugins, no_reraise_ctrlc=no_reraise_ctrlc
|
||||
)
|
||||
|
||||
def runpytest_inprocess(self, *args, **kwargs) -> RunResult:
|
||||
"""See :meth:`Pytester.runpytest_inprocess`."""
|
||||
return self._pytester.runpytest_inprocess(*args, **kwargs)
|
||||
|
||||
def runpytest(self, *args, **kwargs) -> RunResult:
|
||||
"""See :meth:`Pytester.runpytest`."""
|
||||
return self._pytester.runpytest(*args, **kwargs)
|
||||
|
||||
def parseconfig(self, *args) -> Config:
|
||||
"""See :meth:`Pytester.parseconfig`."""
|
||||
return self._pytester.parseconfig(*args)
|
||||
|
||||
def parseconfigure(self, *args) -> Config:
|
||||
"""See :meth:`Pytester.parseconfigure`."""
|
||||
return self._pytester.parseconfigure(*args)
|
||||
|
||||
def getitem(self, source, funcname="test_func"):
|
||||
"""See :meth:`Pytester.getitem`."""
|
||||
return self._pytester.getitem(source, funcname)
|
||||
|
||||
def getitems(self, source):
|
||||
"""See :meth:`Pytester.getitems`."""
|
||||
return self._pytester.getitems(source)
|
||||
|
||||
def getmodulecol(self, source, configargs=(), withinit=False):
|
||||
"""See :meth:`Pytester.getmodulecol`."""
|
||||
return self._pytester.getmodulecol(
|
||||
source, configargs=configargs, withinit=withinit
|
||||
)
|
||||
|
||||
def collect_by_name(self, modcol: Collector, name: str) -> Item | Collector | None:
|
||||
"""See :meth:`Pytester.collect_by_name`."""
|
||||
return self._pytester.collect_by_name(modcol, name)
|
||||
|
||||
def popen(
|
||||
self,
|
||||
cmdargs,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
stdin=CLOSE_STDIN,
|
||||
**kw,
|
||||
):
|
||||
"""See :meth:`Pytester.popen`."""
|
||||
return self._pytester.popen(cmdargs, stdout, stderr, stdin, **kw)
|
||||
|
||||
def run(self, *cmdargs, timeout=None, stdin=CLOSE_STDIN) -> RunResult:
|
||||
"""See :meth:`Pytester.run`."""
|
||||
return self._pytester.run(*cmdargs, timeout=timeout, stdin=stdin)
|
||||
|
||||
def runpython(self, script) -> RunResult:
|
||||
"""See :meth:`Pytester.runpython`."""
|
||||
return self._pytester.runpython(script)
|
||||
|
||||
def runpython_c(self, command):
|
||||
"""See :meth:`Pytester.runpython_c`."""
|
||||
return self._pytester.runpython_c(command)
|
||||
|
||||
def runpytest_subprocess(self, *args, timeout=None) -> RunResult:
|
||||
"""See :meth:`Pytester.runpytest_subprocess`."""
|
||||
return self._pytester.runpytest_subprocess(*args, timeout=timeout)
|
||||
|
||||
def spawn_pytest(self, string: str, expect_timeout: float = 10.0) -> pexpect.spawn:
|
||||
"""See :meth:`Pytester.spawn_pytest`."""
|
||||
return self._pytester.spawn_pytest(string, expect_timeout=expect_timeout)
|
||||
|
||||
def spawn(self, cmd: str, expect_timeout: float = 10.0) -> pexpect.spawn:
|
||||
"""See :meth:`Pytester.spawn`."""
|
||||
return self._pytester.spawn(cmd, expect_timeout=expect_timeout)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Testdir {self.tmpdir!r}>"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self.tmpdir)
|
||||
|
||||
|
||||
class LegacyTestdirPlugin:
|
||||
@fixture
|
||||
@staticmethod
|
||||
def testdir(pytester: Pytester) -> Testdir:
|
||||
"""
|
||||
Identical to :fixture:`pytester`, and provides an instance whose methods return
|
||||
legacy ``LEGACY_PATH`` objects instead when applicable.
|
||||
|
||||
New code should avoid using :fixture:`testdir` in favor of :fixture:`pytester`.
|
||||
"""
|
||||
return Testdir(pytester, _ispytest=True)
|
||||
|
||||
|
||||
@final
|
||||
@dataclasses.dataclass
|
||||
class TempdirFactory:
|
||||
"""Backward compatibility wrapper that implements ``py.path.local``
|
||||
for :class:`TempPathFactory`.
|
||||
|
||||
.. note::
|
||||
These days, it is preferred to use ``tmp_path_factory``.
|
||||
|
||||
:ref:`About the tmpdir and tmpdir_factory fixtures<tmpdir and tmpdir_factory>`.
|
||||
|
||||
"""
|
||||
|
||||
_tmppath_factory: TempPathFactory
|
||||
|
||||
def __init__(
|
||||
self, tmppath_factory: TempPathFactory, *, _ispytest: bool = False
|
||||
) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
self._tmppath_factory = tmppath_factory
|
||||
|
||||
def mktemp(self, basename: str, numbered: bool = True) -> LEGACY_PATH:
|
||||
"""Same as :meth:`TempPathFactory.mktemp`, but returns a ``py.path.local`` object."""
|
||||
return legacy_path(self._tmppath_factory.mktemp(basename, numbered).resolve())
|
||||
|
||||
def getbasetemp(self) -> LEGACY_PATH:
|
||||
"""Same as :meth:`TempPathFactory.getbasetemp`, but returns a ``py.path.local`` object."""
|
||||
return legacy_path(self._tmppath_factory.getbasetemp().resolve())
|
||||
|
||||
|
||||
class LegacyTmpdirPlugin:
|
||||
@fixture(scope="session")
|
||||
@staticmethod
|
||||
def tmpdir_factory(request: FixtureRequest) -> TempdirFactory:
|
||||
"""Return a :class:`pytest.TempdirFactory` instance for the test session."""
|
||||
# Set dynamically by pytest_configure().
|
||||
return request.config._tmpdirhandler # type: ignore
|
||||
|
||||
@fixture
|
||||
@staticmethod
|
||||
def tmpdir(tmp_path: Path) -> LEGACY_PATH:
|
||||
"""Return a temporary directory (as `legacy_path`_ object)
|
||||
which is unique to each test function invocation.
|
||||
The temporary directory is created as a subdirectory
|
||||
of the base temporary directory, with configurable retention,
|
||||
as discussed in :ref:`temporary directory location and retention`.
|
||||
|
||||
.. note::
|
||||
These days, it is preferred to use ``tmp_path``.
|
||||
|
||||
:ref:`About the tmpdir and tmpdir_factory fixtures<tmpdir and tmpdir_factory>`.
|
||||
|
||||
.. _legacy_path: https://py.readthedocs.io/en/latest/path.html
|
||||
"""
|
||||
return legacy_path(tmp_path)
|
||||
|
||||
|
||||
def Cache_makedir(self: Cache, name: str) -> LEGACY_PATH:
|
||||
"""Return a directory path object with the given name.
|
||||
|
||||
Same as :func:`mkdir`, but returns a legacy py path instance.
|
||||
"""
|
||||
return legacy_path(self.mkdir(name))
|
||||
|
||||
|
||||
def FixtureRequest_fspath(self: FixtureRequest) -> LEGACY_PATH:
|
||||
"""(deprecated) The file system path of the test module which collected this test."""
|
||||
return legacy_path(self.path)
|
||||
|
||||
|
||||
def TerminalReporter_startdir(self: TerminalReporter) -> LEGACY_PATH:
|
||||
"""The directory from which pytest was invoked.
|
||||
|
||||
Prefer to use ``startpath`` which is a :class:`pathlib.Path`.
|
||||
|
||||
:type: LEGACY_PATH
|
||||
"""
|
||||
return legacy_path(self.startpath)
|
||||
|
||||
|
||||
def Config_invocation_dir(self: Config) -> LEGACY_PATH:
|
||||
"""The directory from which pytest was invoked.
|
||||
|
||||
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`,
|
||||
which is a :class:`pathlib.Path`.
|
||||
|
||||
:type: LEGACY_PATH
|
||||
"""
|
||||
return legacy_path(str(self.invocation_params.dir))
|
||||
|
||||
|
||||
def Config_rootdir(self: Config) -> LEGACY_PATH:
|
||||
"""The path to the :ref:`rootdir <rootdir>`.
|
||||
|
||||
Prefer to use :attr:`rootpath`, which is a :class:`pathlib.Path`.
|
||||
|
||||
:type: LEGACY_PATH
|
||||
"""
|
||||
return legacy_path(str(self.rootpath))
|
||||
|
||||
|
||||
def Config_inifile(self: Config) -> LEGACY_PATH | None:
|
||||
"""The path to the :ref:`configfile <configfiles>`.
|
||||
|
||||
Prefer to use :attr:`inipath`, which is a :class:`pathlib.Path`.
|
||||
|
||||
:type: Optional[LEGACY_PATH]
|
||||
"""
|
||||
return legacy_path(str(self.inipath)) if self.inipath else None
|
||||
|
||||
|
||||
def Session_startdir(self: Session) -> LEGACY_PATH:
|
||||
"""The path from which pytest was invoked.
|
||||
|
||||
Prefer to use ``startpath`` which is a :class:`pathlib.Path`.
|
||||
|
||||
:type: LEGACY_PATH
|
||||
"""
|
||||
return legacy_path(self.startpath)
|
||||
|
||||
|
||||
def Config__getini_unknown_type(self, name: str, type: str, value: str | list[str]):
|
||||
if type == "pathlist":
|
||||
# TODO: This assert is probably not valid in all cases.
|
||||
assert self.inipath is not None
|
||||
dp = self.inipath.parent
|
||||
input_values = shlex.split(value) if isinstance(value, str) else value
|
||||
return [legacy_path(str(dp / x)) for x in input_values]
|
||||
else:
|
||||
raise ValueError(f"unknown configuration type: {type}", value)
|
||||
|
||||
|
||||
def Node_fspath(self: Node) -> LEGACY_PATH:
|
||||
"""(deprecated) returns a legacy_path copy of self.path"""
|
||||
return legacy_path(self.path)
|
||||
|
||||
|
||||
def Node_fspath_set(self: Node, value: LEGACY_PATH) -> None:
|
||||
self.path = Path(value)
|
||||
|
||||
|
||||
@hookimpl(tryfirst=True)
|
||||
def pytest_load_initial_conftests(early_config: Config) -> None:
|
||||
"""Monkeypatch legacy path attributes in several classes, as early as possible."""
|
||||
mp = MonkeyPatch()
|
||||
early_config.add_cleanup(mp.undo)
|
||||
|
||||
# Add Cache.makedir().
|
||||
mp.setattr(Cache, "makedir", Cache_makedir, raising=False)
|
||||
|
||||
# Add FixtureRequest.fspath property.
|
||||
mp.setattr(FixtureRequest, "fspath", property(FixtureRequest_fspath), raising=False)
|
||||
|
||||
# Add TerminalReporter.startdir property.
|
||||
mp.setattr(
|
||||
TerminalReporter, "startdir", property(TerminalReporter_startdir), raising=False
|
||||
)
|
||||
|
||||
# Add Config.{invocation_dir,rootdir,inifile} properties.
|
||||
mp.setattr(Config, "invocation_dir", property(Config_invocation_dir), raising=False)
|
||||
mp.setattr(Config, "rootdir", property(Config_rootdir), raising=False)
|
||||
mp.setattr(Config, "inifile", property(Config_inifile), raising=False)
|
||||
|
||||
# Add Session.startdir property.
|
||||
mp.setattr(Session, "startdir", property(Session_startdir), raising=False)
|
||||
|
||||
# Add pathlist configuration type.
|
||||
mp.setattr(Config, "_getini_unknown_type", Config__getini_unknown_type)
|
||||
|
||||
# Add Node.fspath property.
|
||||
mp.setattr(Node, "fspath", property(Node_fspath, Node_fspath_set), raising=False)
|
||||
|
||||
|
||||
@hookimpl
|
||||
def pytest_configure(config: Config) -> None:
|
||||
"""Installs the LegacyTmpdirPlugin if the ``tmpdir`` plugin is also installed."""
|
||||
if config.pluginmanager.has_plugin("tmpdir"):
|
||||
mp = MonkeyPatch()
|
||||
config.add_cleanup(mp.undo)
|
||||
# Create TmpdirFactory and attach it to the config object.
|
||||
#
|
||||
# This is to comply with existing plugins which expect the handler to be
|
||||
# available at pytest_configure time, but ideally should be moved entirely
|
||||
# to the tmpdir_factory session fixture.
|
||||
try:
|
||||
tmp_path_factory = config._tmp_path_factory # type: ignore[attr-defined]
|
||||
except AttributeError:
|
||||
# tmpdir plugin is blocked.
|
||||
pass
|
||||
else:
|
||||
_tmpdirhandler = TempdirFactory(tmp_path_factory, _ispytest=True)
|
||||
mp.setattr(config, "_tmpdirhandler", _tmpdirhandler, raising=False)
|
||||
|
||||
# Register an instance so @fixture above @staticmethod unwraps to a bare
|
||||
# function. Class registration would keep the staticmethod descriptor,
|
||||
# which resolve_fixture_function then binds onto test instances (#13564).
|
||||
config.pluginmanager.register(LegacyTmpdirPlugin(), "legacypath-tmpdir")
|
||||
|
||||
|
||||
@hookimpl
|
||||
def pytest_plugin_registered(plugin: object, manager: PytestPluginManager) -> None:
|
||||
# pytester is not loaded by default and is commonly loaded from a conftest,
|
||||
# so checking for it in `pytest_configure` is not enough.
|
||||
is_pytester = plugin is manager.get_plugin("pytester")
|
||||
if is_pytester and not manager.has_plugin("legacypath-pytester"):
|
||||
manager.register(LegacyTestdirPlugin(), "legacypath-pytester")
|
||||
@@ -0,0 +1,988 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Access and control log capturing."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Set as AbstractSet
|
||||
from contextlib import contextmanager
|
||||
from contextlib import nullcontext
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
from datetime import timezone
|
||||
import io
|
||||
from io import StringIO
|
||||
import logging
|
||||
from logging import LogRecord
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
from types import TracebackType
|
||||
from typing import final
|
||||
from typing import Generic
|
||||
from typing import Literal
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TypeVar
|
||||
|
||||
from _pytest import nodes
|
||||
from _pytest._io import TerminalWriter
|
||||
from _pytest.capture import CaptureManager
|
||||
from _pytest.config import _strtobool
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import create_terminal_writer
|
||||
from _pytest.config import hookimpl
|
||||
from _pytest.config import UsageError
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.deprecated import check_ispytest
|
||||
from _pytest.fixtures import fixture
|
||||
from _pytest.fixtures import FixtureRequest
|
||||
from _pytest.main import Session
|
||||
from _pytest.stash import StashKey
|
||||
from _pytest.terminal import TerminalReporter
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
logging_StreamHandler = logging.StreamHandler[StringIO]
|
||||
else:
|
||||
logging_StreamHandler = logging.StreamHandler
|
||||
|
||||
DEFAULT_LOG_FORMAT = "%(levelname)-8s %(name)s:%(filename)s:%(lineno)d %(message)s"
|
||||
DEFAULT_LOG_DATE_FORMAT = "%H:%M:%S"
|
||||
_ANSI_ESCAPE_SEQ = re.compile(r"\x1b\[[\d;]+m")
|
||||
caplog_handler_key = StashKey["LogCaptureHandler"]()
|
||||
caplog_records_key = StashKey[dict[str, list[logging.LogRecord]]]()
|
||||
|
||||
|
||||
def _remove_ansi_escape_sequences(text: str) -> str:
|
||||
return _ANSI_ESCAPE_SEQ.sub("", text)
|
||||
|
||||
|
||||
class DatetimeFormatter(logging.Formatter):
|
||||
"""A logging formatter which formats record with
|
||||
:func:`datetime.datetime.strftime` formatter instead of
|
||||
:func:`time.strftime` in case of microseconds in format string.
|
||||
"""
|
||||
|
||||
def formatTime(self, record: LogRecord, datefmt: str | None = None) -> str:
|
||||
if datefmt and "%f" in datefmt:
|
||||
ct = self.converter(record.created)
|
||||
tz = timezone(timedelta(seconds=ct.tm_gmtoff), ct.tm_zone)
|
||||
# Construct `datetime.datetime` object from `struct_time`
|
||||
# and msecs information from `record`
|
||||
# Using int() instead of round() to avoid it exceeding 1_000_000 and causing a ValueError (#11861).
|
||||
dt = datetime(*ct[0:6], microsecond=int(record.msecs * 1000), tzinfo=tz)
|
||||
return dt.strftime(datefmt)
|
||||
# Use `logging.Formatter` for non-microsecond formats
|
||||
return super().formatTime(record, datefmt)
|
||||
|
||||
|
||||
class ColoredLevelFormatter(DatetimeFormatter):
|
||||
"""A logging formatter which colorizes the %(levelname)..s part of the
|
||||
log format passed to __init__."""
|
||||
|
||||
LOGLEVEL_COLOROPTS: Mapping[int, AbstractSet[str]] = {
|
||||
logging.CRITICAL: {"red"},
|
||||
logging.ERROR: {"red", "bold"},
|
||||
logging.WARNING: {"yellow"},
|
||||
logging.INFO: {"green"},
|
||||
logging.DEBUG: {"purple"},
|
||||
logging.NOTSET: set(),
|
||||
}
|
||||
LEVELNAME_FMT_REGEX = re.compile(r"%\(levelname\)([+-.]?\d*(?:\.\d+)?s)")
|
||||
|
||||
def __init__(self, terminalwriter: TerminalWriter, *args, **kwargs) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
self._terminalwriter = terminalwriter
|
||||
self._original_fmt = self._style._fmt
|
||||
self._level_to_fmt_mapping: dict[int, str] = {}
|
||||
|
||||
for level, color_opts in self.LOGLEVEL_COLOROPTS.items():
|
||||
self.add_color_level(level, *color_opts)
|
||||
|
||||
def add_color_level(self, level: int, *color_opts: str) -> None:
|
||||
"""Add or update color opts for a log level.
|
||||
|
||||
:param level:
|
||||
Log level to apply a style to, e.g. ``logging.INFO``.
|
||||
:param color_opts:
|
||||
ANSI escape sequence color options. Capitalized colors indicates
|
||||
background color, i.e. ``'green', 'Yellow', 'bold'`` will give bold
|
||||
green text on yellow background.
|
||||
|
||||
.. warning::
|
||||
This is an experimental API.
|
||||
"""
|
||||
assert self._fmt is not None
|
||||
levelname_fmt_match = self.LEVELNAME_FMT_REGEX.search(self._fmt)
|
||||
if not levelname_fmt_match:
|
||||
return
|
||||
levelname_fmt = levelname_fmt_match.group()
|
||||
|
||||
formatted_levelname = levelname_fmt % {"levelname": logging.getLevelName(level)}
|
||||
|
||||
# add ANSI escape sequences around the formatted levelname
|
||||
color_kwargs = {name: True for name in color_opts}
|
||||
colorized_formatted_levelname = self._terminalwriter.markup(
|
||||
formatted_levelname, **color_kwargs
|
||||
)
|
||||
self._level_to_fmt_mapping[level] = self.LEVELNAME_FMT_REGEX.sub(
|
||||
colorized_formatted_levelname, self._fmt
|
||||
)
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
fmt = self._level_to_fmt_mapping.get(record.levelno, self._original_fmt)
|
||||
self._style._fmt = fmt
|
||||
return super().format(record)
|
||||
|
||||
|
||||
class PercentStyleMultiline(logging.PercentStyle):
|
||||
"""A logging style with special support for multiline messages.
|
||||
|
||||
If the message of a record consists of multiple lines, this style
|
||||
formats the message as if each line were logged separately.
|
||||
"""
|
||||
|
||||
def __init__(self, fmt: str, auto_indent: int | str | bool | None) -> None:
|
||||
super().__init__(fmt)
|
||||
self._auto_indent = self._get_auto_indent(auto_indent)
|
||||
|
||||
@staticmethod
|
||||
def _get_auto_indent(auto_indent_option: int | str | bool | None) -> int:
|
||||
"""Determine the current auto indentation setting.
|
||||
|
||||
Specify auto indent behavior (on/off/fixed) by passing in
|
||||
extra={"auto_indent": [value]} to the call to logging.log() or
|
||||
using a --log-auto-indent [value] command line or the
|
||||
log_auto_indent [value] config option.
|
||||
|
||||
Default behavior is auto-indent off.
|
||||
|
||||
Using the string "True" or "on" or the boolean True as the value
|
||||
turns auto indent on, using the string "False" or "off" or the
|
||||
boolean False or the int 0 turns it off, and specifying a
|
||||
positive integer fixes the indentation position to the value
|
||||
specified.
|
||||
|
||||
Any other values for the option are invalid, and will silently be
|
||||
converted to the default.
|
||||
|
||||
:param None|bool|int|str auto_indent_option:
|
||||
User specified option for indentation from command line, config
|
||||
or extra kwarg. Accepts int, bool or str. str option accepts the
|
||||
same range of values as boolean config options, as well as
|
||||
positive integers represented in str form.
|
||||
|
||||
:returns:
|
||||
Indentation value, which can be
|
||||
-1 (automatically determine indentation) or
|
||||
0 (auto-indent turned off) or
|
||||
>0 (explicitly set indentation position).
|
||||
"""
|
||||
match auto_indent_option:
|
||||
case None | False:
|
||||
return 0
|
||||
case True:
|
||||
return -1
|
||||
case int():
|
||||
return max(auto_indent_option, 0)
|
||||
case str():
|
||||
try:
|
||||
return max(int(auto_indent_option), 0)
|
||||
except ValueError:
|
||||
pass
|
||||
try:
|
||||
if _strtobool(auto_indent_option):
|
||||
return -1
|
||||
except ValueError:
|
||||
return 0
|
||||
return 0
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
if "\n" in record.message:
|
||||
if hasattr(record, "auto_indent"):
|
||||
# Passed in from the "extra={}" kwarg on the call to logging.log().
|
||||
auto_indent = self._get_auto_indent(record.auto_indent)
|
||||
else:
|
||||
auto_indent = self._auto_indent
|
||||
|
||||
if auto_indent:
|
||||
lines = record.message.splitlines()
|
||||
formatted = self._fmt % {**record.__dict__, "message": lines[0]}
|
||||
|
||||
if auto_indent < 0:
|
||||
indentation = _remove_ansi_escape_sequences(formatted).find(
|
||||
lines[0]
|
||||
)
|
||||
else:
|
||||
# Optimizes logging by allowing a fixed indentation.
|
||||
indentation = auto_indent
|
||||
lines[0] = formatted
|
||||
return ("\n" + " " * indentation).join(lines)
|
||||
return self._fmt % record.__dict__
|
||||
|
||||
|
||||
def get_option_ini(config: Config, *names: str):
|
||||
for name in names:
|
||||
ret = config.getoption(name) # 'default' arg won't work as expected
|
||||
if ret is None:
|
||||
ret = config.getini(name)
|
||||
if ret:
|
||||
return ret
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
"""Add options to control log capturing."""
|
||||
group = parser.getgroup("logging")
|
||||
|
||||
def add_option_ini(option, dest, default=None, type=None, **kwargs):
|
||||
parser.addini(
|
||||
dest, default=default, type=type, help="Default value for " + option
|
||||
)
|
||||
group.addoption(option, dest=dest, **kwargs)
|
||||
|
||||
add_option_ini(
|
||||
"--log-level",
|
||||
dest="log_level",
|
||||
default=None,
|
||||
type=int | str,
|
||||
metavar="LEVEL",
|
||||
help=(
|
||||
"Level of messages to catch/display."
|
||||
" Not set by default, so it depends on the root/parent log handler's"
|
||||
' effective level, where it is "WARNING" by default.'
|
||||
),
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-format",
|
||||
dest="log_format",
|
||||
default=DEFAULT_LOG_FORMAT,
|
||||
help="Log format used by the logging module",
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-date-format",
|
||||
dest="log_date_format",
|
||||
default=DEFAULT_LOG_DATE_FORMAT,
|
||||
help="Log date format used by the logging module",
|
||||
)
|
||||
parser.addini(
|
||||
"log_cli",
|
||||
default=False,
|
||||
type="bool",
|
||||
help='Enable log display during test run (also known as "live logging")',
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-cli-level",
|
||||
dest="log_cli_level",
|
||||
default=None,
|
||||
type=int | str,
|
||||
help="CLI logging level",
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-cli-format",
|
||||
dest="log_cli_format",
|
||||
default=None,
|
||||
help="Log format used by the logging module",
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-cli-date-format",
|
||||
dest="log_cli_date_format",
|
||||
default=None,
|
||||
help="Log date format used by the logging module",
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-file",
|
||||
dest="log_file",
|
||||
default=None,
|
||||
help="Path to a file when logging will be written to",
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-file-mode",
|
||||
dest="log_file_mode",
|
||||
default="w",
|
||||
type=Literal["w", "a"],
|
||||
choices=["w", "a"],
|
||||
help="Log file open mode",
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-file-level",
|
||||
dest="log_file_level",
|
||||
default=None,
|
||||
type=int | str,
|
||||
help="Log file logging level",
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-file-format",
|
||||
dest="log_file_format",
|
||||
default=None,
|
||||
help="Log format used by the logging module",
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-file-date-format",
|
||||
dest="log_file_date_format",
|
||||
default=None,
|
||||
help="Log date format used by the logging module",
|
||||
)
|
||||
add_option_ini(
|
||||
"--log-auto-indent",
|
||||
dest="log_auto_indent",
|
||||
default=None,
|
||||
type=int | bool | str,
|
||||
help="Auto-indent multiline messages passed to the logging module. Accepts true|on, false|off or an integer.",
|
||||
)
|
||||
group.addoption(
|
||||
"--log-disable",
|
||||
action="append",
|
||||
default=[],
|
||||
dest="logger_disable",
|
||||
help="Disable a logger by name. Can be passed multiple times.",
|
||||
)
|
||||
|
||||
|
||||
_HandlerType = TypeVar("_HandlerType", bound=logging.Handler)
|
||||
|
||||
|
||||
# Not using @contextmanager for performance reasons.
|
||||
class catching_logs(Generic[_HandlerType]):
|
||||
"""Context manager that prepares the whole logging machinery properly."""
|
||||
|
||||
__slots__ = ("attached_loggers", "handler", "level", "orig_level")
|
||||
|
||||
def __init__(self, handler: _HandlerType, level: int | None = None) -> None:
|
||||
self.handler = handler
|
||||
self.level = level
|
||||
self.attached_loggers: list[logging.Logger] = []
|
||||
|
||||
def __enter__(self) -> _HandlerType:
|
||||
root_logger = logging.getLogger()
|
||||
if self.level is not None:
|
||||
self.handler.setLevel(self.level)
|
||||
# Attach to root logger.
|
||||
root_logger.addHandler(self.handler)
|
||||
self.attached_loggers.append(root_logger)
|
||||
# Attach to all non-propagating loggers (won't reach root).
|
||||
# Note that will miss loggers that *become* non-propagating
|
||||
# after the `__enter__`. Not worth the trouble for now.
|
||||
for logger in root_logger.manager.loggerDict.values():
|
||||
if (
|
||||
isinstance(logger, logging.Logger)
|
||||
and not logger.propagate
|
||||
and logger is not root_logger
|
||||
):
|
||||
logger.addHandler(self.handler)
|
||||
self.attached_loggers.append(logger)
|
||||
if self.level is not None:
|
||||
# Non-propagating loggers still inherit the level (unless a logger
|
||||
# explicitly set level), so only do this on the root logger.
|
||||
self.orig_level = root_logger.level
|
||||
root_logger.setLevel(min(self.orig_level, self.level))
|
||||
return self.handler
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: TracebackType | None,
|
||||
) -> None:
|
||||
root_logger = logging.getLogger()
|
||||
if self.level is not None:
|
||||
root_logger.setLevel(self.orig_level)
|
||||
for logger in self.attached_loggers:
|
||||
logger.removeHandler(self.handler)
|
||||
self.attached_loggers.clear()
|
||||
|
||||
|
||||
class LogCaptureHandler(logging_StreamHandler):
|
||||
"""A logging handler that stores log records and the log text."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Create a new log handler."""
|
||||
super().__init__(StringIO())
|
||||
self.records: list[logging.LogRecord] = []
|
||||
|
||||
def emit(self, record: logging.LogRecord) -> None:
|
||||
"""Keep the log records in a list in addition to the log text."""
|
||||
self.records.append(record)
|
||||
super().emit(record)
|
||||
|
||||
def reset(self) -> None:
|
||||
self.records = []
|
||||
self.stream = StringIO()
|
||||
|
||||
def clear(self) -> None:
|
||||
self.records.clear()
|
||||
self.stream = StringIO()
|
||||
|
||||
def handleError(self, record: logging.LogRecord) -> None:
|
||||
if logging.raiseExceptions:
|
||||
# Fail the test if the log message is bad (emit failed).
|
||||
# The default behavior of logging is to print "Logging error"
|
||||
# to stderr with the call stack and some extra details.
|
||||
# pytest wants to make such mistakes visible during testing.
|
||||
raise # noqa: PLE0704
|
||||
|
||||
|
||||
@final
|
||||
class LogCaptureFixture:
|
||||
"""Provides access and control of log capturing."""
|
||||
|
||||
def __init__(self, item: nodes.Node, *, _ispytest: bool = False) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
self._item = item
|
||||
self._initial_handler_level: int | None = None
|
||||
# Dict of log name -> log level.
|
||||
self._initial_logger_levels: dict[str | None, int] = {}
|
||||
self._initial_disabled_logging_level: int | None = None
|
||||
|
||||
def _finalize(self) -> None:
|
||||
"""Finalize the fixture.
|
||||
|
||||
This restores the log levels and the disabled logging levels changed by :meth:`set_level`.
|
||||
"""
|
||||
# Restore log levels.
|
||||
if self._initial_handler_level is not None:
|
||||
self.handler.setLevel(self._initial_handler_level)
|
||||
for logger_name, level in self._initial_logger_levels.items():
|
||||
logger = logging.getLogger(logger_name)
|
||||
logger.setLevel(level)
|
||||
# Disable logging at the original disabled logging level.
|
||||
if self._initial_disabled_logging_level is not None:
|
||||
logging.disable(self._initial_disabled_logging_level)
|
||||
self._initial_disabled_logging_level = None
|
||||
|
||||
@property
|
||||
def handler(self) -> LogCaptureHandler:
|
||||
"""Get the logging handler used by the fixture."""
|
||||
return self._item.stash[caplog_handler_key]
|
||||
|
||||
def get_records(
|
||||
self, when: Literal["setup", "call", "teardown"]
|
||||
) -> list[logging.LogRecord]:
|
||||
"""Get the logging records for one of the possible test phases.
|
||||
|
||||
:param when:
|
||||
Which test phase to obtain the records from.
|
||||
Valid values are: "setup", "call" and "teardown".
|
||||
|
||||
:returns: The list of captured records at the given stage.
|
||||
|
||||
.. versionadded:: 3.4
|
||||
"""
|
||||
return self._item.stash[caplog_records_key].get(when, [])
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
"""The formatted log text."""
|
||||
return _remove_ansi_escape_sequences(self.handler.stream.getvalue())
|
||||
|
||||
@property
|
||||
def records(self) -> list[logging.LogRecord]:
|
||||
"""The list of log records."""
|
||||
return self.handler.records
|
||||
|
||||
@property
|
||||
def record_tuples(self) -> list[tuple[str, int, str]]:
|
||||
"""A list of a stripped down version of log records intended
|
||||
for use in assertion comparison.
|
||||
|
||||
The format of the tuple is:
|
||||
|
||||
(logger_name, log_level, message)
|
||||
"""
|
||||
return [(r.name, r.levelno, r.getMessage()) for r in self.records]
|
||||
|
||||
@property
|
||||
def messages(self) -> list[str]:
|
||||
"""A list of format-interpolated log messages.
|
||||
|
||||
Unlike 'records', which contains the format string and parameters for
|
||||
interpolation, log messages in this list are all interpolated.
|
||||
|
||||
Unlike 'text', which contains the output from the handler, log
|
||||
messages in this list are unadorned with levels, timestamps, etc,
|
||||
making exact comparisons more reliable.
|
||||
|
||||
Note that traceback or stack info (from :func:`logging.exception` or
|
||||
the `exc_info` or `stack_info` arguments to the logging functions) is
|
||||
not included, as this is added by the formatter in the handler.
|
||||
|
||||
.. versionadded:: 3.7
|
||||
"""
|
||||
return [r.getMessage() for r in self.records]
|
||||
|
||||
def clear(self) -> None:
|
||||
"""Reset the list of log records and the captured log text."""
|
||||
self.handler.clear()
|
||||
|
||||
def _force_enable_logging(
|
||||
self, level: int | str, logger_obj: logging.Logger
|
||||
) -> int:
|
||||
"""Enable the desired logging level if the global level was disabled via ``logging.disabled``.
|
||||
|
||||
Only enables logging levels greater than or equal to the requested ``level``.
|
||||
|
||||
Does nothing if the desired ``level`` wasn't disabled.
|
||||
|
||||
:param level:
|
||||
The logger level caplog should capture.
|
||||
All logging is enabled if a non-standard logging level string is supplied.
|
||||
Valid level strings are in :data:`logging._nameToLevel`.
|
||||
:param logger_obj: The logger object to check.
|
||||
|
||||
:return: The original disabled logging level.
|
||||
"""
|
||||
original_disable_level: int = logger_obj.manager.disable
|
||||
|
||||
if isinstance(level, str):
|
||||
# Try to translate the level string to an int for `logging.disable()`
|
||||
level = logging.getLevelName(level) # type: ignore[deprecated]
|
||||
|
||||
if not isinstance(level, int):
|
||||
# The level provided was not valid, so just un-disable all logging.
|
||||
logging.disable(logging.NOTSET)
|
||||
elif not logger_obj.isEnabledFor(level):
|
||||
# Each level is `10` away from other levels.
|
||||
# https://docs.python.org/3/library/logging.html#logging-levels
|
||||
disable_level = max(level - 10, logging.NOTSET)
|
||||
logging.disable(disable_level)
|
||||
|
||||
return original_disable_level
|
||||
|
||||
def set_level(self, level: int | str, logger: str | None = None) -> None:
|
||||
"""Set the threshold level of a logger for the duration of a test.
|
||||
|
||||
Logging messages which are less severe than this level will not be captured.
|
||||
|
||||
.. versionchanged:: 3.4
|
||||
The levels of the loggers changed by this function will be
|
||||
restored to their initial values at the end of the test.
|
||||
|
||||
Will enable the requested logging level if it was disabled via :func:`logging.disable`.
|
||||
|
||||
:param level: The level.
|
||||
:param logger: The logger to update. If not given, the root logger.
|
||||
"""
|
||||
logger_obj = logging.getLogger(logger)
|
||||
# Save the original log-level to restore it during teardown.
|
||||
self._initial_logger_levels.setdefault(logger, logger_obj.level)
|
||||
logger_obj.setLevel(level)
|
||||
if self._initial_handler_level is None:
|
||||
self._initial_handler_level = self.handler.level
|
||||
self.handler.setLevel(level)
|
||||
initial_disabled_logging_level = self._force_enable_logging(level, logger_obj)
|
||||
if self._initial_disabled_logging_level is None:
|
||||
self._initial_disabled_logging_level = initial_disabled_logging_level
|
||||
|
||||
@contextmanager
|
||||
def at_level(self, level: int | str, logger: str | None = None) -> Generator[None]:
|
||||
"""Context manager that sets the level for capturing of logs. After
|
||||
the end of the 'with' statement the level is restored to its original
|
||||
value.
|
||||
|
||||
Will enable the requested logging level if it was disabled via :func:`logging.disable`.
|
||||
|
||||
:param level: The level.
|
||||
:param logger: The logger to update. If not given, the root logger.
|
||||
"""
|
||||
logger_obj = logging.getLogger(logger)
|
||||
orig_level = logger_obj.level
|
||||
logger_obj.setLevel(level)
|
||||
handler_orig_level = self.handler.level
|
||||
self.handler.setLevel(level)
|
||||
original_disable_level = self._force_enable_logging(level, logger_obj)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
logger_obj.setLevel(orig_level)
|
||||
self.handler.setLevel(handler_orig_level)
|
||||
logging.disable(original_disable_level)
|
||||
|
||||
@contextmanager
|
||||
def filtering(self, filter_: logging.Filter) -> Generator[None]:
|
||||
"""Context manager that temporarily adds the given filter to the caplog's
|
||||
:meth:`handler` for the 'with' statement block, and removes that filter at the
|
||||
end of the block.
|
||||
|
||||
:param filter_: A custom :class:`logging.Filter` object.
|
||||
|
||||
.. versionadded:: 7.5
|
||||
"""
|
||||
already_present = filter_ in self.handler.filters
|
||||
if already_present:
|
||||
yield
|
||||
else:
|
||||
try:
|
||||
self.handler.addFilter(filter_)
|
||||
yield
|
||||
finally:
|
||||
self.handler.removeFilter(filter_)
|
||||
|
||||
|
||||
@fixture
|
||||
def caplog(request: FixtureRequest) -> Generator[LogCaptureFixture]:
|
||||
"""Access and control log capturing.
|
||||
|
||||
Captured logs are available through the following properties/methods::
|
||||
|
||||
* caplog.messages -> list of format-interpolated log messages
|
||||
* caplog.text -> string containing formatted log output
|
||||
* caplog.records -> list of logging.LogRecord instances
|
||||
* caplog.record_tuples -> list of (logger_name, level, message) tuples
|
||||
* caplog.clear() -> clear captured records and formatted log output string
|
||||
"""
|
||||
result = LogCaptureFixture(request.node, _ispytest=True)
|
||||
yield result
|
||||
result._finalize()
|
||||
|
||||
|
||||
def get_log_level_for_setting(config: Config, *setting_names: str) -> int | None:
|
||||
for setting_name in setting_names:
|
||||
log_level = config.getoption(setting_name)
|
||||
if log_level is None:
|
||||
log_level = config.getini(setting_name)
|
||||
# An int 0 (NOTSET) is a valid level, only None and "" mean unset.
|
||||
if log_level is not None and log_level != "":
|
||||
break
|
||||
else:
|
||||
return None
|
||||
|
||||
if isinstance(log_level, int):
|
||||
return log_level
|
||||
log_level = log_level.upper()
|
||||
try:
|
||||
return int(getattr(logging, log_level, log_level))
|
||||
except ValueError as e:
|
||||
# Python logging does not recognise this as a logging level
|
||||
raise UsageError(
|
||||
f"'{log_level}' is not recognized as a logging level name for "
|
||||
f"'{setting_name}'. Please consider passing the "
|
||||
"logging level num instead."
|
||||
) from e
|
||||
|
||||
|
||||
# run after terminalreporter/capturemanager are configured
|
||||
@hookimpl(trylast=True)
|
||||
def pytest_configure(config: Config) -> None:
|
||||
config.pluginmanager.register(LoggingPlugin(config), "logging-plugin")
|
||||
|
||||
|
||||
class LoggingPlugin:
|
||||
"""Attaches to the logging module and captures log messages for each test."""
|
||||
|
||||
def __init__(self, config: Config) -> None:
|
||||
"""Create a new plugin to capture log messages.
|
||||
|
||||
The formatter can be safely shared across all handlers so
|
||||
create a single one for the entire test session here.
|
||||
"""
|
||||
self._config = config
|
||||
|
||||
# Report logging.
|
||||
self.formatter = self._create_formatter(
|
||||
get_option_ini(config, "log_format"),
|
||||
get_option_ini(config, "log_date_format"),
|
||||
get_option_ini(config, "log_auto_indent"),
|
||||
)
|
||||
self.log_level = get_log_level_for_setting(config, "log_level")
|
||||
self.caplog_handler = LogCaptureHandler()
|
||||
self.caplog_handler.setFormatter(self.formatter)
|
||||
self.report_handler = LogCaptureHandler()
|
||||
self.report_handler.setFormatter(self.formatter)
|
||||
|
||||
# File logging.
|
||||
self.log_file_level = get_log_level_for_setting(
|
||||
config, "log_file_level", "log_level"
|
||||
)
|
||||
log_file = get_option_ini(config, "log_file") or os.devnull
|
||||
if log_file != os.devnull:
|
||||
directory = os.path.dirname(os.path.abspath(log_file))
|
||||
if not os.path.isdir(directory):
|
||||
os.makedirs(directory)
|
||||
|
||||
self.log_file_mode = get_option_ini(config, "log_file_mode") or "w"
|
||||
self.log_file_handler = _FileHandler(
|
||||
log_file, mode=self.log_file_mode, encoding="UTF-8"
|
||||
)
|
||||
log_file_format = get_option_ini(config, "log_file_format", "log_format")
|
||||
log_file_date_format = get_option_ini(
|
||||
config, "log_file_date_format", "log_date_format"
|
||||
)
|
||||
|
||||
log_file_formatter = DatetimeFormatter(
|
||||
log_file_format, datefmt=log_file_date_format
|
||||
)
|
||||
self.log_file_handler.setFormatter(log_file_formatter)
|
||||
|
||||
# CLI/live logging.
|
||||
self.log_cli_level = get_log_level_for_setting(
|
||||
config, "log_cli_level", "log_level"
|
||||
)
|
||||
if self._log_cli_enabled():
|
||||
terminal_reporter = config.pluginmanager.get_plugin("terminalreporter")
|
||||
# Guaranteed by `_log_cli_enabled()`.
|
||||
assert terminal_reporter is not None
|
||||
capture_manager = config.pluginmanager.get_plugin("capturemanager")
|
||||
# if capturemanager plugin is disabled, live logging still works.
|
||||
self.log_cli_handler: (
|
||||
_LiveLoggingStreamHandler | _LiveLoggingNullHandler
|
||||
) = _LiveLoggingStreamHandler(terminal_reporter, capture_manager)
|
||||
else:
|
||||
self.log_cli_handler = _LiveLoggingNullHandler()
|
||||
log_cli_formatter = self._create_formatter(
|
||||
get_option_ini(config, "log_cli_format", "log_format"),
|
||||
get_option_ini(config, "log_cli_date_format", "log_date_format"),
|
||||
get_option_ini(config, "log_auto_indent"),
|
||||
)
|
||||
self.log_cli_handler.setFormatter(log_cli_formatter)
|
||||
self._disable_loggers(loggers_to_disable=config.option.logger_disable)
|
||||
|
||||
def _disable_loggers(self, loggers_to_disable: list[str]) -> None:
|
||||
if not loggers_to_disable:
|
||||
return
|
||||
|
||||
for name in loggers_to_disable:
|
||||
logger = logging.getLogger(name)
|
||||
logger.disabled = True
|
||||
|
||||
def _create_formatter(self, log_format, log_date_format, auto_indent):
|
||||
# Color option doesn't exist if terminal plugin is disabled.
|
||||
color = getattr(self._config.option, "color", "no")
|
||||
if color != "no" and ColoredLevelFormatter.LEVELNAME_FMT_REGEX.search(
|
||||
log_format
|
||||
):
|
||||
formatter: logging.Formatter = ColoredLevelFormatter(
|
||||
create_terminal_writer(self._config), log_format, log_date_format
|
||||
)
|
||||
else:
|
||||
formatter = DatetimeFormatter(log_format, log_date_format)
|
||||
|
||||
formatter._style = PercentStyleMultiline(
|
||||
formatter._style._fmt, auto_indent=auto_indent
|
||||
)
|
||||
|
||||
return formatter
|
||||
|
||||
def set_log_path(self, fname: str) -> None:
|
||||
"""Set the filename parameter for Logging.FileHandler().
|
||||
|
||||
Creates parent directory if it does not exist.
|
||||
|
||||
.. warning::
|
||||
This is an experimental API.
|
||||
"""
|
||||
fpath = Path(fname)
|
||||
|
||||
if not fpath.is_absolute():
|
||||
fpath = self._config.rootpath / fpath
|
||||
|
||||
if not fpath.parent.exists():
|
||||
fpath.parent.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
# https://github.com/python/mypy/issues/11193
|
||||
stream: io.TextIOWrapper = fpath.open(mode=self.log_file_mode, encoding="UTF-8") # type: ignore[assignment]
|
||||
old_stream = self.log_file_handler.setStream(stream)
|
||||
if old_stream:
|
||||
old_stream.close()
|
||||
|
||||
def _log_cli_enabled(self) -> bool:
|
||||
"""Return whether live logging is enabled."""
|
||||
enabled = self._config.getoption(
|
||||
"--log-cli-level"
|
||||
) is not None or self._config.getini("log_cli")
|
||||
if not enabled:
|
||||
return False
|
||||
|
||||
terminal_reporter = self._config.pluginmanager.get_plugin("terminalreporter")
|
||||
if terminal_reporter is None:
|
||||
# terminal reporter is disabled e.g. by pytest-xdist.
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@hookimpl(wrapper=True, tryfirst=True)
|
||||
def pytest_sessionstart(self) -> Generator[None]:
|
||||
self.log_cli_handler.set_when("sessionstart")
|
||||
|
||||
with catching_logs(self.log_cli_handler, level=self.log_cli_level):
|
||||
with catching_logs(self.log_file_handler, level=self.log_file_level):
|
||||
return (yield)
|
||||
|
||||
@hookimpl(wrapper=True, tryfirst=True)
|
||||
def pytest_collection(self) -> Generator[None]:
|
||||
self.log_cli_handler.set_when("collection")
|
||||
|
||||
with catching_logs(self.log_cli_handler, level=self.log_cli_level):
|
||||
with catching_logs(self.log_file_handler, level=self.log_file_level):
|
||||
return (yield)
|
||||
|
||||
@hookimpl(wrapper=True)
|
||||
def pytest_runtestloop(self, session: Session) -> Generator[None, object, object]:
|
||||
if session.config.option.collectonly:
|
||||
return (yield)
|
||||
|
||||
if self._log_cli_enabled() and self._config.get_verbosity() < 1:
|
||||
# The verbose flag is needed to avoid messy test progress output.
|
||||
self._config.option.verbose = 1
|
||||
|
||||
with catching_logs(self.log_cli_handler, level=self.log_cli_level):
|
||||
with catching_logs(self.log_file_handler, level=self.log_file_level):
|
||||
return (yield) # Run all the tests.
|
||||
|
||||
@hookimpl
|
||||
def pytest_runtest_logstart(self) -> None:
|
||||
self.log_cli_handler.reset()
|
||||
self.log_cli_handler.set_when("start")
|
||||
|
||||
@hookimpl
|
||||
def pytest_runtest_logreport(self) -> None:
|
||||
self.log_cli_handler.set_when("logreport")
|
||||
|
||||
@contextmanager
|
||||
def _runtest_for(self, item: nodes.Item, when: str) -> Generator[None]:
|
||||
"""Implement the internals of the pytest_runtest_xxx() hooks."""
|
||||
with (
|
||||
catching_logs(
|
||||
self.caplog_handler,
|
||||
level=self.log_level,
|
||||
) as caplog_handler,
|
||||
catching_logs(
|
||||
self.report_handler,
|
||||
level=self.log_level,
|
||||
) as report_handler,
|
||||
):
|
||||
caplog_handler.reset()
|
||||
report_handler.reset()
|
||||
item.stash[caplog_records_key][when] = caplog_handler.records
|
||||
item.stash[caplog_handler_key] = caplog_handler
|
||||
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
log = report_handler.stream.getvalue().strip()
|
||||
item.add_report_section(when, "log", log)
|
||||
|
||||
@hookimpl(wrapper=True)
|
||||
def pytest_runtest_setup(self, item: nodes.Item) -> Generator[None]:
|
||||
self.log_cli_handler.set_when("setup")
|
||||
|
||||
empty: dict[str, list[logging.LogRecord]] = {}
|
||||
item.stash[caplog_records_key] = empty
|
||||
with self._runtest_for(item, "setup"):
|
||||
yield
|
||||
|
||||
@hookimpl(wrapper=True)
|
||||
def pytest_runtest_call(self, item: nodes.Item) -> Generator[None]:
|
||||
self.log_cli_handler.set_when("call")
|
||||
|
||||
with self._runtest_for(item, "call"):
|
||||
yield
|
||||
|
||||
@hookimpl(wrapper=True)
|
||||
def pytest_runtest_teardown(self, item: nodes.Item) -> Generator[None]:
|
||||
self.log_cli_handler.set_when("teardown")
|
||||
|
||||
try:
|
||||
with self._runtest_for(item, "teardown"):
|
||||
yield
|
||||
finally:
|
||||
del item.stash[caplog_records_key]
|
||||
del item.stash[caplog_handler_key]
|
||||
|
||||
@hookimpl
|
||||
def pytest_runtest_logfinish(self) -> None:
|
||||
self.log_cli_handler.set_when("finish")
|
||||
|
||||
@hookimpl(wrapper=True, tryfirst=True)
|
||||
def pytest_sessionfinish(self) -> Generator[None]:
|
||||
self.log_cli_handler.set_when("sessionfinish")
|
||||
|
||||
with catching_logs(self.log_cli_handler, level=self.log_cli_level):
|
||||
with catching_logs(self.log_file_handler, level=self.log_file_level):
|
||||
return (yield)
|
||||
|
||||
@hookimpl
|
||||
def pytest_unconfigure(self) -> None:
|
||||
# Close the FileHandler explicitly.
|
||||
# (logging.shutdown might have lost the weakref?!)
|
||||
self.log_file_handler.close()
|
||||
|
||||
|
||||
class _FileHandler(logging.FileHandler):
|
||||
"""A logging FileHandler with pytest tweaks."""
|
||||
|
||||
def handleError(self, record: logging.LogRecord) -> None:
|
||||
# Handled by LogCaptureHandler.
|
||||
pass
|
||||
|
||||
|
||||
class _LiveLoggingStreamHandler(logging_StreamHandler):
|
||||
"""A logging StreamHandler used by the live logging feature: it will
|
||||
write a newline before the first log message in each test.
|
||||
|
||||
During live logging we must also explicitly disable stdout/stderr
|
||||
capturing otherwise it will get captured and won't appear in the
|
||||
terminal.
|
||||
"""
|
||||
|
||||
# Officially stream needs to be a IO[str], but TerminalReporter
|
||||
# isn't. So force it.
|
||||
stream: TerminalReporter = None # type: ignore
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
terminal_reporter: TerminalReporter,
|
||||
capture_manager: CaptureManager | None,
|
||||
) -> None:
|
||||
super().__init__(stream=terminal_reporter) # type: ignore[arg-type]
|
||||
self.capture_manager = capture_manager
|
||||
self.reset()
|
||||
self.set_when(None)
|
||||
self._test_outcome_written = False
|
||||
|
||||
def reset(self) -> None:
|
||||
"""Reset the handler; should be called before the start of each test."""
|
||||
self._first_record_emitted = False
|
||||
|
||||
def set_when(self, when: str | None) -> None:
|
||||
"""Prepare for the given test phase (setup/call/teardown)."""
|
||||
self._when = when
|
||||
self._section_name_shown = False
|
||||
if when == "start":
|
||||
self._test_outcome_written = False
|
||||
|
||||
def emit(self, record: logging.LogRecord) -> None:
|
||||
ctx_manager = (
|
||||
self.capture_manager.global_and_fixture_disabled()
|
||||
if self.capture_manager
|
||||
else nullcontext()
|
||||
)
|
||||
with ctx_manager:
|
||||
if not self._first_record_emitted:
|
||||
self.stream.write("\n")
|
||||
self._first_record_emitted = True
|
||||
elif self._when in ("teardown", "finish"):
|
||||
if not self._test_outcome_written:
|
||||
self._test_outcome_written = True
|
||||
self.stream.write("\n")
|
||||
if not self._section_name_shown and self._when:
|
||||
self.stream.section("live log " + self._when, sep="-", bold=True)
|
||||
self._section_name_shown = True
|
||||
super().emit(record)
|
||||
|
||||
def handleError(self, record: logging.LogRecord) -> None:
|
||||
# Handled by LogCaptureHandler.
|
||||
pass
|
||||
|
||||
|
||||
class _LiveLoggingNullHandler(logging.NullHandler):
|
||||
"""A logging handler used when live logging is disabled."""
|
||||
|
||||
def reset(self) -> None:
|
||||
pass
|
||||
|
||||
def set_when(self, when: str) -> None:
|
||||
pass
|
||||
|
||||
def handleError(self, record: logging.LogRecord) -> None:
|
||||
# Handled by LogCaptureHandler.
|
||||
pass
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,341 @@
|
||||
"""Generic mechanism for marking and selecting python functions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import collections
|
||||
from collections.abc import Collection
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Set as AbstractSet
|
||||
import dataclasses
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from .expression import Expression
|
||||
from .structures import _EmptyParameterSetMark
|
||||
from .structures import _HiddenParam
|
||||
from .structures import EMPTY_PARAMETERSET_OPTION
|
||||
from .structures import get_empty_parameterset_mark
|
||||
from .structures import HIDDEN_PARAM
|
||||
from .structures import Mark
|
||||
from .structures import MARK_GEN
|
||||
from .structures import MarkDecorator
|
||||
from .structures import MarkGenerator
|
||||
from .structures import ParameterSet
|
||||
from _pytest.compat import NOTSET
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import ExitCode
|
||||
from _pytest.config import hookimpl
|
||||
from _pytest.config import UsageError
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.stash import StashKey
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from _pytest.nodes import Item
|
||||
|
||||
|
||||
__all__ = [
|
||||
"HIDDEN_PARAM",
|
||||
"MARK_GEN",
|
||||
"Mark",
|
||||
"MarkDecorator",
|
||||
"MarkGenerator",
|
||||
"ParameterSet",
|
||||
"get_empty_parameterset_mark",
|
||||
]
|
||||
|
||||
|
||||
old_mark_config_key = StashKey[Config | None]()
|
||||
|
||||
|
||||
def param(
|
||||
*values: object,
|
||||
marks: MarkDecorator | Collection[MarkDecorator | Mark] = (),
|
||||
id: str | _HiddenParam | None = None,
|
||||
) -> ParameterSet:
|
||||
"""Specify a parameter in `pytest.mark.parametrize`_ calls or
|
||||
:ref:`parametrized fixtures <fixture-parametrize-marks>`.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"test_input,expected",
|
||||
[
|
||||
("3+5", 8),
|
||||
pytest.param("6*9", 42, marks=pytest.mark.xfail),
|
||||
],
|
||||
)
|
||||
def test_eval(test_input, expected):
|
||||
assert eval(test_input) == expected
|
||||
|
||||
:param values: Variable args of the values of the parameter set, in order.
|
||||
|
||||
:param marks:
|
||||
A single mark or a list of marks to be applied to this parameter set.
|
||||
|
||||
:ref:`pytest.mark.usefixtures <pytest.mark.usefixtures ref>` cannot be added via this parameter.
|
||||
|
||||
:type id: str | Literal[pytest.HIDDEN_PARAM] | None
|
||||
:param id:
|
||||
The id to attribute to this parameter set.
|
||||
|
||||
.. versionadded:: 8.4
|
||||
:ref:`hidden-param` means to hide the parameter set
|
||||
from the test name. Can only be used at most 1 time, as
|
||||
test names need to be unique.
|
||||
"""
|
||||
return ParameterSet.param(*values, marks=marks, id=id)
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("general")
|
||||
group._addoption( # private to use reserved lower-case short option
|
||||
"-k",
|
||||
action="store",
|
||||
dest="keyword",
|
||||
default="",
|
||||
metavar="EXPRESSION",
|
||||
help="Only run tests matching the given keyword expression, "
|
||||
"e.g. -k 'test_method or test_other', -k 'not (slow or network)'.\n"
|
||||
"Names in the expression are matched case-insensitively as substrings "
|
||||
"of the test's keywords, which are:\n"
|
||||
"- its own name, including any parametrization id\n"
|
||||
"- the names of its class, module and directories\n"
|
||||
"- the names of the markers applied to it or to its parents\n"
|
||||
"- attributes assigned directly to the test function\n"
|
||||
"- any names in its 'extra_keyword_matches' set\n"
|
||||
"Use -m to match marker names exactly, including their arguments.",
|
||||
)
|
||||
|
||||
group._addoption( # private to use reserved lower-case short option
|
||||
"-m",
|
||||
action="store",
|
||||
dest="markexpr",
|
||||
default="",
|
||||
metavar="MARKEXPR",
|
||||
help="Only run tests matching given mark expression. "
|
||||
"For example: -m 'mark1 and not mark2'.",
|
||||
)
|
||||
|
||||
group.addoption(
|
||||
"--markers",
|
||||
action="store_true",
|
||||
help="show markers (builtin, plugin and per-project ones).",
|
||||
)
|
||||
|
||||
parser.addini("markers", "Register new markers for test functions", "linelist")
|
||||
parser.addini(
|
||||
EMPTY_PARAMETERSET_OPTION,
|
||||
"Default marker for empty parametersets",
|
||||
type=_EmptyParameterSetMark,
|
||||
default="skip",
|
||||
)
|
||||
|
||||
|
||||
@hookimpl(tryfirst=True)
|
||||
def pytest_cmdline_main(config: Config) -> int | ExitCode | None:
|
||||
import _pytest.config
|
||||
|
||||
if config.option.markers:
|
||||
config._do_configure()
|
||||
tw = _pytest.config.create_terminal_writer(config)
|
||||
for marker in config._iter_registered_markers():
|
||||
tw.write(f"@pytest.mark.{marker.signature}:", bold=True)
|
||||
tw.line(f" {marker.description}" if marker.description else "")
|
||||
tw.line()
|
||||
config._ensure_unconfigure()
|
||||
return 0
|
||||
|
||||
return None
|
||||
|
||||
|
||||
#: Attributes which are never meaningful as keywords, but do end up in the
|
||||
#: ``__dict__`` of a test function: pytest's own mark storage, and the
|
||||
#: bookkeeping decorators leave behind (``functools.wraps`` copies ``__wrapped__``
|
||||
#: and friends, ``functools.lru_cache`` adds ``cache_parameters``, ...).
|
||||
IGNORED_FUNCTION_ATTRIBUTES = frozenset({"pytestmark", "cache_parameters"})
|
||||
|
||||
|
||||
def _is_matchable_function_attribute(name: str) -> bool:
|
||||
"""Whether a test function attribute may be matched by ``-k``."""
|
||||
return not name.startswith("_") and name not in IGNORED_FUNCTION_ATTRIBUTES
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class KeywordMatcher:
|
||||
"""A matcher for keywords, as used by ``-k``.
|
||||
|
||||
Given a set of names, matches any substring of one of these names. The
|
||||
string inclusion check is case-insensitive.
|
||||
|
||||
The names are collected in :meth:`from_item` from the item and its
|
||||
parents: their node names, the names of the markers in scope, the
|
||||
attributes assigned to the test function, and the
|
||||
:attr:`~_pytest.nodes.Node.extra_keyword_matches` sets.
|
||||
|
||||
Note that these names are collected independently of
|
||||
:attr:`Node.keywords <_pytest.nodes.Node.keywords>`; writing into that
|
||||
mapping does not affect ``-k``.
|
||||
"""
|
||||
|
||||
__slots__ = ("_names",)
|
||||
|
||||
_names: AbstractSet[str]
|
||||
|
||||
@classmethod
|
||||
def from_item(cls, item: Item) -> KeywordMatcher:
|
||||
mapped_names = set()
|
||||
|
||||
# Add the names of the current item and any parent items,
|
||||
# except the Session and root Directory's which are not
|
||||
# interesting for matching.
|
||||
import pytest
|
||||
|
||||
for node in item.listchain():
|
||||
if isinstance(node, pytest.Session):
|
||||
continue
|
||||
if isinstance(node, pytest.Directory) and isinstance(
|
||||
node.parent, pytest.Session
|
||||
):
|
||||
continue
|
||||
mapped_names.add(node.name)
|
||||
|
||||
# Add the names added as extra keywords to current or parent items.
|
||||
mapped_names.update(item.listextrakeywords())
|
||||
|
||||
# Add the names attached to the current function through direct
|
||||
# assignment, ignoring the attributes that merely happen to live in the
|
||||
# function's __dict__ without anyone meaning them as keywords.
|
||||
function_obj = getattr(item, "function", None)
|
||||
if function_obj:
|
||||
mapped_names.update(
|
||||
name
|
||||
for name in function_obj.__dict__
|
||||
if _is_matchable_function_attribute(name)
|
||||
)
|
||||
|
||||
# Add the markers to the keywords as we no longer handle them correctly.
|
||||
mapped_names.update(mark.name for mark in item.iter_markers())
|
||||
|
||||
return cls(mapped_names)
|
||||
|
||||
def __call__(self, subname: str, /, **kwargs: str | int | bool | None) -> bool:
|
||||
if kwargs:
|
||||
raise UsageError("Keyword expressions do not support call parameters.")
|
||||
subname = subname.lower()
|
||||
return any(subname in name.lower() for name in self._names)
|
||||
|
||||
|
||||
def deselect_by_keyword(items: list[Item], config: Config) -> None:
|
||||
keywordexpr = config.option.keyword.lstrip()
|
||||
if not keywordexpr:
|
||||
return
|
||||
|
||||
expr = _parse_expression(keywordexpr, "Wrong expression passed to '-k'")
|
||||
|
||||
remaining = []
|
||||
deselected = []
|
||||
for colitem in items:
|
||||
if not expr.evaluate(KeywordMatcher.from_item(colitem)):
|
||||
deselected.append(colitem)
|
||||
else:
|
||||
remaining.append(colitem)
|
||||
|
||||
if deselected:
|
||||
config.hook.pytest_deselected(items=deselected)
|
||||
items[:] = remaining
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class MarkMatcher:
|
||||
"""A matcher for markers which are present.
|
||||
|
||||
Tries to match on any marker names, attached to the given colitem.
|
||||
"""
|
||||
|
||||
__slots__ = ("own_mark_name_mapping",)
|
||||
|
||||
own_mark_name_mapping: dict[str, list[Mark]]
|
||||
|
||||
@classmethod
|
||||
def from_markers(cls, markers: Iterable[Mark]) -> MarkMatcher:
|
||||
mark_name_mapping = collections.defaultdict(list)
|
||||
for mark in markers:
|
||||
mark_name_mapping[mark.name].append(mark)
|
||||
return cls(mark_name_mapping)
|
||||
|
||||
def __call__(self, name: str, /, **kwargs: str | int | bool | None) -> bool:
|
||||
if not (matches := self.own_mark_name_mapping.get(name, [])):
|
||||
return False
|
||||
|
||||
for mark in matches: # pylint: disable=consider-using-any-or-all
|
||||
if all(mark.kwargs.get(k, NOTSET) == v for k, v in kwargs.items()):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def deselect_by_mark(items: list[Item], config: Config) -> None:
|
||||
matchexpr = config.option.markexpr
|
||||
if not matchexpr:
|
||||
return
|
||||
|
||||
expr = _parse_expression(matchexpr, "Wrong expression passed to '-m'")
|
||||
_validate_marker_names(expr, config)
|
||||
|
||||
remaining: list[Item] = []
|
||||
deselected: list[Item] = []
|
||||
for item in items:
|
||||
if expr.evaluate(MarkMatcher.from_markers(item.iter_markers())):
|
||||
remaining.append(item)
|
||||
else:
|
||||
deselected.append(item)
|
||||
if deselected:
|
||||
config.hook.pytest_deselected(items=deselected)
|
||||
items[:] = remaining
|
||||
|
||||
|
||||
def _validate_marker_names(expr: Expression, config: Config) -> None:
|
||||
"""Validate that all marker names in the expression are registered.
|
||||
|
||||
Only validates when strict_markers is enabled.
|
||||
"""
|
||||
strict_markers = config.getini("strict_markers")
|
||||
if strict_markers is None:
|
||||
strict_markers = config.getini("strict")
|
||||
if not strict_markers:
|
||||
return
|
||||
|
||||
registered_markers = {m.name for m in config._iter_registered_markers()}
|
||||
|
||||
unknown_markers = expr.idents() - registered_markers
|
||||
if unknown_markers:
|
||||
unknown_str = ", ".join(sorted(unknown_markers))
|
||||
raise UsageError(
|
||||
f"Unknown marker(s) in '-m' expression: {unknown_str}. "
|
||||
"Use 'pytest --markers' to see available markers."
|
||||
)
|
||||
|
||||
|
||||
def _parse_expression(expr: str, exc_message: str) -> Expression:
|
||||
try:
|
||||
return Expression.compile(expr)
|
||||
except SyntaxError as e:
|
||||
raise UsageError(
|
||||
f"{exc_message}: {e.text}: at column {e.offset}: {e.msg}"
|
||||
) from None
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(items: list[Item], config: Config) -> None:
|
||||
deselect_by_keyword(items, config)
|
||||
deselect_by_mark(items, config)
|
||||
|
||||
|
||||
def pytest_configure(config: Config) -> None:
|
||||
config.stash[old_mark_config_key] = MARK_GEN._config
|
||||
MARK_GEN._config = config
|
||||
|
||||
# Eagerly validate the value; it is only read lazily during collection.
|
||||
config.getini(EMPTY_PARAMETERSET_OPTION)
|
||||
|
||||
|
||||
def pytest_unconfigure(config: Config) -> None:
|
||||
MARK_GEN._config = config.stash.get(old_mark_config_key, None)
|
||||
@@ -0,0 +1,363 @@
|
||||
r"""Evaluate match expressions, as used by `-k` and `-m`.
|
||||
|
||||
The grammar is:
|
||||
|
||||
expression: expr? EOF
|
||||
expr: and_expr ('or' and_expr)*
|
||||
and_expr: not_expr ('and' not_expr)*
|
||||
not_expr: 'not' not_expr | '(' expr ')' | ident kwargs?
|
||||
|
||||
ident: (\w|:|\+|-|\.|\[|\]|\\|/)+
|
||||
kwargs: ('(' name '=' value ( ', ' name '=' value )* ')')
|
||||
name: a valid ident, but not a reserved keyword
|
||||
value: (unescaped) string literal | (-)?[0-9]+ | 'False' | 'True' | 'None'
|
||||
|
||||
The semantics are:
|
||||
|
||||
- Empty expression evaluates to False.
|
||||
- ident evaluates to True or False according to a provided matcher function.
|
||||
- ident with parentheses and keyword arguments evaluates to True or False according to a provided matcher function.
|
||||
- or/and/not evaluate according to the usual boolean semantics.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Sequence
|
||||
import dataclasses
|
||||
import enum
|
||||
import keyword
|
||||
import re
|
||||
import types
|
||||
from typing import Final
|
||||
from typing import final
|
||||
from typing import Literal
|
||||
from typing import NoReturn
|
||||
from typing import overload
|
||||
from typing import Protocol
|
||||
|
||||
|
||||
__all__ = [
|
||||
"Expression",
|
||||
"ExpressionMatcher",
|
||||
]
|
||||
|
||||
|
||||
FILE_NAME: Final = "<pytest match expression>"
|
||||
|
||||
|
||||
class TokenType(enum.Enum):
|
||||
LPAREN = "left parenthesis"
|
||||
RPAREN = "right parenthesis"
|
||||
OR = "or"
|
||||
AND = "and"
|
||||
NOT = "not"
|
||||
IDENT = "identifier"
|
||||
EOF = "end of input"
|
||||
EQUAL = "="
|
||||
STRING = "string literal"
|
||||
COMMA = ","
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Token:
|
||||
__slots__ = ("pos", "type", "value")
|
||||
type: TokenType
|
||||
value: str
|
||||
pos: int
|
||||
|
||||
|
||||
class Scanner:
|
||||
__slots__ = ("current", "idents", "input", "tokens")
|
||||
|
||||
def __init__(self, input: str) -> None:
|
||||
self.input = input
|
||||
self.idents: set[str] = set()
|
||||
self.tokens = self.lex(input)
|
||||
self.current = next(self.tokens)
|
||||
|
||||
def lex(self, input: str) -> Iterator[Token]:
|
||||
pos = 0
|
||||
while pos < len(input):
|
||||
if input[pos] in (" ", "\t"):
|
||||
pos += 1
|
||||
elif input[pos] == "(":
|
||||
yield Token(TokenType.LPAREN, "(", pos)
|
||||
pos += 1
|
||||
elif input[pos] == ")":
|
||||
yield Token(TokenType.RPAREN, ")", pos)
|
||||
pos += 1
|
||||
elif input[pos] == "=":
|
||||
yield Token(TokenType.EQUAL, "=", pos)
|
||||
pos += 1
|
||||
elif input[pos] == ",":
|
||||
yield Token(TokenType.COMMA, ",", pos)
|
||||
pos += 1
|
||||
elif (quote_char := input[pos]) in ("'", '"'):
|
||||
end_quote_pos = input.find(quote_char, pos + 1)
|
||||
if end_quote_pos == -1:
|
||||
raise SyntaxError(
|
||||
f'closing quote "{quote_char}" is missing',
|
||||
(FILE_NAME, 1, pos + 1, input),
|
||||
)
|
||||
value = input[pos : end_quote_pos + 1]
|
||||
if (backslash_pos := value.find("\\")) != -1:
|
||||
raise SyntaxError(
|
||||
r'escaping with "\" not supported in marker expression',
|
||||
(FILE_NAME, 1, pos + backslash_pos + 1, input),
|
||||
)
|
||||
yield Token(TokenType.STRING, value, pos)
|
||||
pos += len(value)
|
||||
else:
|
||||
match = re.match(r"(:?\w|:|\+|-|\.|\[|\]|\\|/)+", input[pos:])
|
||||
if match:
|
||||
value = match.group(0)
|
||||
if value == "or":
|
||||
yield Token(TokenType.OR, value, pos)
|
||||
elif value == "and":
|
||||
yield Token(TokenType.AND, value, pos)
|
||||
elif value == "not":
|
||||
yield Token(TokenType.NOT, value, pos)
|
||||
else:
|
||||
yield Token(TokenType.IDENT, value, pos)
|
||||
pos += len(value)
|
||||
else:
|
||||
raise SyntaxError(
|
||||
f'unexpected character "{input[pos]}"',
|
||||
(FILE_NAME, 1, pos + 1, input),
|
||||
)
|
||||
yield Token(TokenType.EOF, "", pos)
|
||||
|
||||
@overload
|
||||
def accept(self, type: TokenType, *, reject: Literal[True]) -> Token: ...
|
||||
|
||||
@overload
|
||||
def accept(
|
||||
self, type: TokenType, *, reject: Literal[False] = False
|
||||
) -> Token | None: ...
|
||||
|
||||
def accept(self, type: TokenType, *, reject: bool = False) -> Token | None:
|
||||
if self.current.type is type:
|
||||
token = self.current
|
||||
if token.type is not TokenType.EOF:
|
||||
self.current = next(self.tokens)
|
||||
return token
|
||||
if reject:
|
||||
self.reject((type,))
|
||||
return None
|
||||
|
||||
def reject(self, expected: Sequence[TokenType]) -> NoReturn:
|
||||
raise SyntaxError(
|
||||
"expected {}; got {}".format(
|
||||
" OR ".join(type.value for type in expected),
|
||||
self.current.type.value,
|
||||
),
|
||||
(FILE_NAME, 1, self.current.pos + 1, self.input),
|
||||
)
|
||||
|
||||
|
||||
# True, False and None are legal match expression identifiers,
|
||||
# but illegal as Python identifiers. To fix this, this prefix
|
||||
# is added to identifiers in the conversion to Python AST.
|
||||
IDENT_PREFIX = "$"
|
||||
|
||||
|
||||
def expression(s: Scanner) -> tuple[ast.Expression, frozenset[str]]:
|
||||
if s.accept(TokenType.EOF):
|
||||
ret: ast.expr = ast.Constant(False)
|
||||
else:
|
||||
ret = expr(s)
|
||||
s.accept(TokenType.EOF, reject=True)
|
||||
return ast.fix_missing_locations(ast.Expression(ret)), frozenset(s.idents)
|
||||
|
||||
|
||||
def expr(s: Scanner) -> ast.expr:
|
||||
ret = and_expr(s)
|
||||
while s.accept(TokenType.OR):
|
||||
rhs = and_expr(s)
|
||||
ret = ast.BoolOp(ast.Or(), [ret, rhs])
|
||||
return ret
|
||||
|
||||
|
||||
def and_expr(s: Scanner) -> ast.expr:
|
||||
ret = not_expr(s)
|
||||
while s.accept(TokenType.AND):
|
||||
rhs = not_expr(s)
|
||||
ret = ast.BoolOp(ast.And(), [ret, rhs])
|
||||
return ret
|
||||
|
||||
|
||||
def not_expr(s: Scanner) -> ast.expr:
|
||||
if s.accept(TokenType.NOT):
|
||||
return ast.UnaryOp(ast.Not(), not_expr(s))
|
||||
if s.accept(TokenType.LPAREN):
|
||||
ret = expr(s)
|
||||
s.accept(TokenType.RPAREN, reject=True)
|
||||
return ret
|
||||
ident = s.accept(TokenType.IDENT)
|
||||
if ident:
|
||||
s.idents.add(ident.value)
|
||||
name = ast.Name(IDENT_PREFIX + ident.value, ast.Load())
|
||||
if s.accept(TokenType.LPAREN):
|
||||
ret = ast.Call(func=name, args=[], keywords=all_kwargs(s))
|
||||
s.accept(TokenType.RPAREN, reject=True)
|
||||
else:
|
||||
ret = name
|
||||
return ret
|
||||
|
||||
s.reject((TokenType.NOT, TokenType.LPAREN, TokenType.IDENT))
|
||||
|
||||
|
||||
BUILTIN_MATCHERS = {"True": True, "False": False, "None": None}
|
||||
|
||||
|
||||
def single_kwarg(s: Scanner) -> ast.keyword:
|
||||
keyword_name = s.accept(TokenType.IDENT, reject=True)
|
||||
if not keyword_name.value.isidentifier():
|
||||
raise SyntaxError(
|
||||
f"not a valid python identifier {keyword_name.value}",
|
||||
(FILE_NAME, 1, keyword_name.pos + 1, s.input),
|
||||
)
|
||||
if keyword.iskeyword(keyword_name.value):
|
||||
raise SyntaxError(
|
||||
f"unexpected reserved python keyword `{keyword_name.value}`",
|
||||
(FILE_NAME, 1, keyword_name.pos + 1, s.input),
|
||||
)
|
||||
s.accept(TokenType.EQUAL, reject=True)
|
||||
|
||||
if value_token := s.accept(TokenType.STRING):
|
||||
value: str | int | bool | None = value_token.value[1:-1] # strip quotes
|
||||
else:
|
||||
value_token = s.accept(TokenType.IDENT, reject=True)
|
||||
if (number := value_token.value).isdigit() or (
|
||||
number.startswith("-") and number[1:].isdigit()
|
||||
):
|
||||
value = int(number)
|
||||
elif value_token.value in BUILTIN_MATCHERS:
|
||||
value = BUILTIN_MATCHERS[value_token.value]
|
||||
else:
|
||||
raise SyntaxError(
|
||||
f'unexpected character/s "{value_token.value}"',
|
||||
(FILE_NAME, 1, value_token.pos + 1, s.input),
|
||||
)
|
||||
|
||||
ret = ast.keyword(keyword_name.value, ast.Constant(value))
|
||||
return ret
|
||||
|
||||
|
||||
def all_kwargs(s: Scanner) -> list[ast.keyword]:
|
||||
ret = [single_kwarg(s)]
|
||||
while s.accept(TokenType.COMMA):
|
||||
ret.append(single_kwarg(s))
|
||||
return ret
|
||||
|
||||
|
||||
class ExpressionMatcher(Protocol):
|
||||
"""A callable which, given an identifier and optional kwargs, should return
|
||||
whether it matches in an :class:`Expression` evaluation.
|
||||
|
||||
Should be prepared to handle arbitrary strings as input.
|
||||
|
||||
If no kwargs are provided, the expression of the form `foo`.
|
||||
If kwargs are provided, the expression is of the form `foo(1, b=True, "s")`.
|
||||
|
||||
If the expression is not supported (e.g. don't want to accept the kwargs
|
||||
syntax variant), should raise :class:`~pytest.UsageError`.
|
||||
|
||||
Example::
|
||||
|
||||
def matcher(name: str, /, **kwargs: str | int | bool | None) -> bool:
|
||||
# Match `cat`.
|
||||
if name == "cat" and not kwargs:
|
||||
return True
|
||||
# Match `dog(barks=True)`.
|
||||
if name == "dog" and kwargs == {"barks": False}:
|
||||
return True
|
||||
return False
|
||||
"""
|
||||
|
||||
def __call__(self, name: str, /, **kwargs: str | int | bool | None) -> bool: ...
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class MatcherNameAdapter:
|
||||
matcher: ExpressionMatcher
|
||||
name: str
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
return self.matcher(self.name)
|
||||
|
||||
def __call__(self, **kwargs: str | int | bool | None) -> bool:
|
||||
return self.matcher(self.name, **kwargs)
|
||||
|
||||
|
||||
class MatcherAdapter(Mapping[str, MatcherNameAdapter]):
|
||||
"""Adapts a matcher function to a locals mapping as required by eval()."""
|
||||
|
||||
def __init__(self, matcher: ExpressionMatcher) -> None:
|
||||
self.matcher = matcher
|
||||
|
||||
def __getitem__(self, key: str) -> MatcherNameAdapter:
|
||||
return MatcherNameAdapter(matcher=self.matcher, name=key[len(IDENT_PREFIX) :])
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
raise NotImplementedError()
|
||||
|
||||
def __len__(self) -> int:
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
@final
|
||||
class Expression:
|
||||
"""A compiled match expression as used by -k and -m.
|
||||
|
||||
The expression can be evaluated against different matchers.
|
||||
"""
|
||||
|
||||
__slots__ = ("_code", "_idents", "input")
|
||||
|
||||
def __init__(
|
||||
self, input: str, code: types.CodeType, idents: frozenset[str]
|
||||
) -> None:
|
||||
#: The original input line, as a string.
|
||||
self.input: Final = input
|
||||
self._code: Final = code
|
||||
#: All identifiers which appear in the expression.
|
||||
self._idents: Final = idents
|
||||
|
||||
@classmethod
|
||||
def compile(cls, input: str) -> Expression:
|
||||
"""Compile a match expression.
|
||||
|
||||
:param input: The input expression - one line.
|
||||
|
||||
:raises SyntaxError: If the expression is malformed.
|
||||
"""
|
||||
astexpr, idents = expression(Scanner(input))
|
||||
code = compile(
|
||||
astexpr,
|
||||
filename="<pytest match expression>",
|
||||
mode="eval",
|
||||
)
|
||||
return Expression(input, code, idents)
|
||||
|
||||
def idents(self) -> frozenset[str]:
|
||||
"""Return the set of all identifiers which appear in the expression."""
|
||||
return self._idents
|
||||
|
||||
def evaluate(self, matcher: ExpressionMatcher) -> bool:
|
||||
"""Evaluate the match expression.
|
||||
|
||||
:param matcher:
|
||||
A callback which determines whether an identifier matches or not.
|
||||
See the :class:`ExpressionMatcher` protocol for details and example.
|
||||
|
||||
:returns: Whether the expression matches or not.
|
||||
|
||||
:raises UsageError:
|
||||
If the matcher doesn't support the expression. Cannot happen if the
|
||||
matcher supports all expressions.
|
||||
"""
|
||||
return bool(eval(self._code, {"__builtins__": {}}, MatcherAdapter(matcher)))
|
||||
@@ -0,0 +1,719 @@
|
||||
# mypy: allow-untyped-defs
|
||||
from __future__ import annotations
|
||||
|
||||
import collections.abc
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Collection
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import MutableMapping
|
||||
from collections.abc import Sequence
|
||||
import dataclasses
|
||||
import enum
|
||||
import inspect
|
||||
from typing import Any
|
||||
from typing import final
|
||||
from typing import Literal
|
||||
from typing import NamedTuple
|
||||
from typing import overload
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TypeVar
|
||||
import warnings
|
||||
|
||||
from .._code import getfslineno
|
||||
from ..compat import NOTSET
|
||||
from ..compat import NotSetType
|
||||
from _pytest.compat import assert_never
|
||||
from _pytest.config import Config
|
||||
from _pytest.deprecated import check_ispytest
|
||||
from _pytest.deprecated import PARAMETRIZE_NON_COLLECTION_ITERABLE
|
||||
from _pytest.outcomes import fail
|
||||
from _pytest.raises import AbstractRaises
|
||||
from _pytest.scope import ScopeName
|
||||
from _pytest.warning_types import PytestCollectionWarning
|
||||
from _pytest.warning_types import PytestUnknownMarkWarning
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..nodes import Node
|
||||
|
||||
|
||||
EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark"
|
||||
_EmptyParameterSetMark = Literal["skip", "xfail", "fail_at_collect"]
|
||||
|
||||
|
||||
# Singleton type for HIDDEN_PARAM, as described in:
|
||||
# https://www.python.org/dev/peps/pep-0484/#support-for-singleton-types-in-unions
|
||||
class _HiddenParam(enum.Enum):
|
||||
token = 0
|
||||
|
||||
|
||||
#: Can be used as a parameter set id to hide it from the test name.
|
||||
HIDDEN_PARAM = _HiddenParam.token
|
||||
|
||||
|
||||
def istestfunc(func) -> bool:
|
||||
return callable(func) and getattr(func, "__name__", "<lambda>") != "<lambda>"
|
||||
|
||||
|
||||
def get_empty_parameterset_mark(
|
||||
config: Config, argnames: Sequence[str], func
|
||||
) -> MarkDecorator:
|
||||
from ..nodes import Collector
|
||||
|
||||
argslisting = ", ".join(argnames)
|
||||
|
||||
_fs, lineno = getfslineno(func)
|
||||
reason = f"got empty parameter set for ({argslisting})"
|
||||
requested_mark: _EmptyParameterSetMark = config.getini(EMPTY_PARAMETERSET_OPTION)
|
||||
match requested_mark:
|
||||
case "skip":
|
||||
return MARK_GEN.skip(reason=reason)
|
||||
case "xfail":
|
||||
return MARK_GEN.xfail(reason=reason, run=False)
|
||||
case "fail_at_collect":
|
||||
raise Collector.CollectError(
|
||||
f"Empty parameter set in '{func.__name__}' at line {lineno + 1}"
|
||||
)
|
||||
case unreachable:
|
||||
assert_never(unreachable)
|
||||
|
||||
|
||||
class ParameterSet(NamedTuple):
|
||||
"""The return type of :func:`pytest.param`.
|
||||
|
||||
A set of values for a set of parameters along with associated marks and
|
||||
an optional ID for the set.
|
||||
|
||||
.. versionadded:: 9.2
|
||||
"""
|
||||
|
||||
# Examples:
|
||||
#
|
||||
# pytest.param(1, 2, 3)
|
||||
# # ParameterSet(values=(1, 2, 3), marks=(), id=None)
|
||||
#
|
||||
# pytest.param("hello", id="greeting")
|
||||
# # ParameterSet(values=("hello",), marks=(), id="greeting")
|
||||
#
|
||||
# # Parameter set with marks
|
||||
# pytest.param(42, marks=pytest.mark.xfail)
|
||||
# # ParameterSet(values=(42,), marks=(MarkDecorator(...),), id=None)
|
||||
#
|
||||
# # From parametrize mark (parameter names + list of parameter sets)
|
||||
# pytest.mark.parametrize(
|
||||
# ("a", "b", "expected"),
|
||||
# [
|
||||
# (1, 2, 3),
|
||||
# pytest.param(40, 2, 42, id="everything"),
|
||||
# ],
|
||||
# )
|
||||
# # ParameterSet(values=(1, 2, 3), marks=(), id=None)
|
||||
# # ParameterSet(values=(40, 2, 42), marks=(), id="everything")
|
||||
|
||||
values: Sequence[object | NotSetType]
|
||||
marks: Collection[MarkDecorator | Mark]
|
||||
id: str | _HiddenParam | None
|
||||
|
||||
@classmethod
|
||||
def param(
|
||||
cls,
|
||||
*values: object,
|
||||
marks: MarkDecorator | Collection[MarkDecorator | Mark] = (),
|
||||
id: str | _HiddenParam | None = None,
|
||||
) -> ParameterSet:
|
||||
""":meta private:"""
|
||||
if isinstance(marks, MarkDecorator):
|
||||
marks = (marks,)
|
||||
else:
|
||||
assert isinstance(marks, collections.abc.Collection)
|
||||
if any(i.name == "usefixtures" for i in marks):
|
||||
raise ValueError(
|
||||
"pytest.param cannot add pytest.mark.usefixtures; see "
|
||||
"https://docs.pytest.org/en/stable/reference/reference.html#pytest-param"
|
||||
)
|
||||
|
||||
if id is not None:
|
||||
if not isinstance(id, str) and id is not HIDDEN_PARAM:
|
||||
raise TypeError(
|
||||
"Expected id to be a string or a `pytest.HIDDEN_PARAM` sentinel, "
|
||||
f"got {type(id)}: {id!r}",
|
||||
)
|
||||
return cls(values, marks, id)
|
||||
|
||||
@classmethod
|
||||
def extract_from(
|
||||
cls,
|
||||
parameterset: ParameterSet | Sequence[object] | object,
|
||||
force_tuple: bool = False,
|
||||
) -> ParameterSet:
|
||||
"""Extract from an object or objects.
|
||||
|
||||
:meta private:
|
||||
|
||||
:param parameterset:
|
||||
A legacy style parameterset that may or may not be a tuple,
|
||||
and may or may not be wrapped into a mess of mark objects.
|
||||
|
||||
:param force_tuple:
|
||||
Enforce tuple wrapping so single argument tuple values
|
||||
don't get decomposed and break tests.
|
||||
"""
|
||||
if isinstance(parameterset, cls):
|
||||
return parameterset
|
||||
if force_tuple:
|
||||
return cls.param(parameterset)
|
||||
else:
|
||||
# TODO: Refactor to fix this type-ignore. Currently the following
|
||||
# passes type-checking but crashes:
|
||||
#
|
||||
# @pytest.mark.parametrize(('x', 'y'), [1, 2])
|
||||
# def test_foo(x, y): pass
|
||||
return cls(parameterset, marks=[], id=None) # type: ignore[arg-type]
|
||||
|
||||
@staticmethod
|
||||
def _parse_parametrize_args(
|
||||
argnames: str | Sequence[str],
|
||||
argvalues: Iterable[ParameterSet | Sequence[object] | object],
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> tuple[Sequence[str], bool]:
|
||||
if isinstance(argnames, str):
|
||||
# A trailing comma indicates tuple-style: "arg," is equivalent to ("arg",)
|
||||
# In this case, argvalues should be a list of tuples, not wrapped values.
|
||||
# See https://github.com/pytest-dev/pytest/issues/719
|
||||
has_trailing_comma = argnames.rstrip().endswith(",")
|
||||
argnames = [x.strip() for x in argnames.split(",") if x.strip()]
|
||||
force_tuple = len(argnames) == 1 and not has_trailing_comma
|
||||
else:
|
||||
force_tuple = False
|
||||
return argnames, force_tuple
|
||||
|
||||
@staticmethod
|
||||
def _parse_parametrize_parameters(
|
||||
argvalues: Iterable[ParameterSet | Sequence[object] | object],
|
||||
force_tuple: bool,
|
||||
) -> list[ParameterSet]:
|
||||
return [
|
||||
ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def _for_parametrize(
|
||||
cls,
|
||||
argnames: str | Sequence[str],
|
||||
argvalues: Iterable[ParameterSet | Sequence[object] | object],
|
||||
func,
|
||||
config: Config,
|
||||
nodeid: str,
|
||||
) -> tuple[Sequence[str], list[ParameterSet]]:
|
||||
if not isinstance(argvalues, Collection):
|
||||
warnings.warn(
|
||||
PARAMETRIZE_NON_COLLECTION_ITERABLE.format(
|
||||
nodeid=nodeid,
|
||||
type_name=type(argvalues).__name__,
|
||||
),
|
||||
stacklevel=3,
|
||||
)
|
||||
|
||||
argnames, force_tuple = cls._parse_parametrize_args(argnames, argvalues)
|
||||
parameters = cls._parse_parametrize_parameters(argvalues, force_tuple)
|
||||
del argvalues
|
||||
|
||||
if parameters:
|
||||
# Check all parameter sets have the correct number of values.
|
||||
for param in parameters:
|
||||
try:
|
||||
values_len = len(param.values)
|
||||
except TypeError:
|
||||
values_len = None
|
||||
if values_len is None:
|
||||
fail(
|
||||
f'{nodeid}: in "parametrize" expected a sequence of values, '
|
||||
"for a single value use a one-element tuple like "
|
||||
"('value',), "
|
||||
f"got {type(param.values).__name__}:\n"
|
||||
f" {param.values}",
|
||||
pytrace=False,
|
||||
)
|
||||
if values_len != len(argnames):
|
||||
msg = (
|
||||
'{nodeid}: in "parametrize" the number of names ({names_len}):\n'
|
||||
" {names}\n"
|
||||
"must be equal to the number of values ({values_len}):\n"
|
||||
" {values}"
|
||||
)
|
||||
fail(
|
||||
msg.format(
|
||||
nodeid=nodeid,
|
||||
values=param.values,
|
||||
names=argnames,
|
||||
names_len=len(argnames),
|
||||
values_len=values_len,
|
||||
),
|
||||
pytrace=False,
|
||||
)
|
||||
else:
|
||||
# Empty parameter set (likely computed at runtime): create a single
|
||||
# parameter set with NOTSET values, with the "empty parameter set" mark applied to it.
|
||||
mark = get_empty_parameterset_mark(config, argnames, func)
|
||||
parameters.append(
|
||||
ParameterSet(
|
||||
values=(NOTSET,) * len(argnames), marks=[mark], id="NOTSET"
|
||||
)
|
||||
)
|
||||
return argnames, parameters
|
||||
|
||||
|
||||
# mypy doesn't like `__module__` defined inline in the NamedTuple.
|
||||
ParameterSet.__module__ = "pytest"
|
||||
|
||||
|
||||
@final
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Mark:
|
||||
"""A pytest mark."""
|
||||
|
||||
#: Name of the mark.
|
||||
name: str
|
||||
#: Positional arguments of the mark decorator.
|
||||
args: tuple[Any, ...]
|
||||
#: Keyword arguments of the mark decorator.
|
||||
kwargs: Mapping[str, Any]
|
||||
|
||||
#: Source Mark for ids with parametrize Marks.
|
||||
_param_ids_from: Mark | None = dataclasses.field(default=None, repr=False)
|
||||
#: Resolved/generated ids with parametrize Marks.
|
||||
_param_ids_generated: Sequence[str] | None = dataclasses.field(
|
||||
default=None, repr=False
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
args: tuple[Any, ...],
|
||||
kwargs: Mapping[str, Any],
|
||||
param_ids_from: Mark | None = None,
|
||||
param_ids_generated: Sequence[str] | None = None,
|
||||
*,
|
||||
_ispytest: bool = False,
|
||||
) -> None:
|
||||
""":meta private:"""
|
||||
check_ispytest(_ispytest)
|
||||
# Weirdness to bypass frozen=True.
|
||||
object.__setattr__(self, "name", name)
|
||||
object.__setattr__(self, "args", args)
|
||||
object.__setattr__(self, "kwargs", kwargs)
|
||||
object.__setattr__(self, "_param_ids_from", param_ids_from)
|
||||
object.__setattr__(self, "_param_ids_generated", param_ids_generated)
|
||||
|
||||
def _has_param_ids(self) -> bool:
|
||||
return "ids" in self.kwargs or len(self.args) >= 4
|
||||
|
||||
def combined_with(self, other: Mark) -> Mark:
|
||||
"""Return a new Mark which is a combination of this
|
||||
Mark and another Mark.
|
||||
|
||||
Combines by appending args and merging kwargs.
|
||||
|
||||
:param Mark other: The mark to combine with.
|
||||
:rtype: Mark
|
||||
"""
|
||||
assert self.name == other.name
|
||||
|
||||
# Remember source of ids with parametrize Marks.
|
||||
param_ids_from: Mark | None = None
|
||||
if self.name == "parametrize":
|
||||
if other._has_param_ids():
|
||||
param_ids_from = other
|
||||
elif self._has_param_ids():
|
||||
param_ids_from = self
|
||||
|
||||
return Mark(
|
||||
self.name,
|
||||
self.args + other.args,
|
||||
dict(self.kwargs, **other.kwargs),
|
||||
param_ids_from=param_ids_from,
|
||||
_ispytest=True,
|
||||
)
|
||||
|
||||
|
||||
# A generic parameter designating an object to which a Mark may
|
||||
# be applied -- a test function (callable) or class.
|
||||
# Note: a lambda is not allowed, but this can't be represented.
|
||||
Markable = TypeVar("Markable", bound=Callable[..., object] | type)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class MarkDecorator:
|
||||
"""A decorator for applying a mark on test functions and classes.
|
||||
|
||||
``MarkDecorators`` are created with ``pytest.mark``::
|
||||
|
||||
mark1 = pytest.mark.NAME # Simple MarkDecorator
|
||||
mark2 = pytest.mark.NAME(name1=value) # Parametrized MarkDecorator
|
||||
|
||||
and can then be applied as decorators to test functions::
|
||||
|
||||
@mark2
|
||||
def test_function():
|
||||
pass
|
||||
|
||||
When a ``MarkDecorator`` is called, it does the following:
|
||||
|
||||
1. If called with a single class as its only positional argument and no
|
||||
additional keyword arguments, it attaches the mark to the class so it
|
||||
gets applied automatically to all test cases found in that class.
|
||||
|
||||
2. If called with a single function as its only positional argument and
|
||||
no additional keyword arguments, it attaches the mark to the function,
|
||||
containing all the arguments already stored internally in the
|
||||
``MarkDecorator``.
|
||||
|
||||
3. When called in any other case, it returns a new ``MarkDecorator``
|
||||
instance with the original ``MarkDecorator``'s content updated with
|
||||
the arguments passed to this call.
|
||||
|
||||
Note: The rules above prevent a ``MarkDecorator`` from storing only a
|
||||
single function or class reference as its positional argument with no
|
||||
additional keyword or positional arguments. You can work around this by
|
||||
using `with_args()`.
|
||||
"""
|
||||
|
||||
mark: Mark
|
||||
|
||||
def __init__(self, mark: Mark, *, _ispytest: bool = False) -> None:
|
||||
""":meta private:"""
|
||||
check_ispytest(_ispytest)
|
||||
self.mark = mark
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Alias for mark.name."""
|
||||
return self.mark.name
|
||||
|
||||
@property
|
||||
def args(self) -> tuple[Any, ...]:
|
||||
"""Alias for mark.args."""
|
||||
return self.mark.args
|
||||
|
||||
@property
|
||||
def kwargs(self) -> Mapping[str, Any]:
|
||||
"""Alias for mark.kwargs."""
|
||||
return self.mark.kwargs
|
||||
|
||||
@property
|
||||
def markname(self) -> str:
|
||||
""":meta private:"""
|
||||
return self.name # for backward-compat (2.4.1 had this attr)
|
||||
|
||||
def with_args(self, *args: object, **kwargs: object) -> MarkDecorator:
|
||||
"""Return a MarkDecorator with extra arguments added.
|
||||
|
||||
Unlike calling the MarkDecorator, with_args() can be used even
|
||||
if the sole argument is a callable/class.
|
||||
"""
|
||||
mark = Mark(self.name, args, kwargs, _ispytest=True)
|
||||
return MarkDecorator(self.mark.combined_with(mark), _ispytest=True)
|
||||
|
||||
# Type ignored because the overloads overlap with an incompatible
|
||||
# return type. Not much we can do about that. Thankfully mypy picks
|
||||
# the first match so it works out even if we break the rules.
|
||||
@overload
|
||||
def __call__(self, arg: Markable) -> Markable: # type: ignore[overload-overlap]
|
||||
pass
|
||||
|
||||
@overload
|
||||
def __call__(self, *args: object, **kwargs: object) -> MarkDecorator:
|
||||
pass
|
||||
|
||||
def __call__(self, *args: object, **kwargs: object):
|
||||
"""Call the MarkDecorator."""
|
||||
if args and not kwargs:
|
||||
func = args[0]
|
||||
is_class = inspect.isclass(func)
|
||||
# For staticmethods/classmethods, the marks are eventually fetched from the
|
||||
# function object, not the descriptor, so unwrap.
|
||||
unwrapped_func = func
|
||||
if isinstance(func, staticmethod | classmethod):
|
||||
unwrapped_func = func.__func__
|
||||
if len(args) == 1 and (istestfunc(unwrapped_func) or is_class):
|
||||
store_mark(unwrapped_func, self.mark)
|
||||
return func
|
||||
return self.with_args(*args, **kwargs)
|
||||
|
||||
|
||||
def get_unpacked_marks(
|
||||
obj: object | type,
|
||||
*,
|
||||
consider_mro: bool = True,
|
||||
) -> list[Mark]:
|
||||
"""Obtain the unpacked marks that are stored on an object.
|
||||
|
||||
If obj is a class and consider_mro is true, return marks applied to
|
||||
this class and all of its super-classes in MRO order. If consider_mro
|
||||
is false, only return marks applied directly to this class.
|
||||
"""
|
||||
if isinstance(obj, type):
|
||||
if not consider_mro:
|
||||
mark_lists = [obj.__dict__.get("pytestmark", [])]
|
||||
else:
|
||||
mark_lists = [
|
||||
x.__dict__.get("pytestmark", []) for x in reversed(obj.__mro__)
|
||||
]
|
||||
mark_list = []
|
||||
for item in mark_lists:
|
||||
if isinstance(item, list):
|
||||
mark_list.extend(item)
|
||||
else:
|
||||
mark_list.append(item)
|
||||
else:
|
||||
mark_attribute = getattr(obj, "pytestmark", [])
|
||||
if mark_attribute is None:
|
||||
warnings.warn(
|
||||
"Module defines a `__getattr__` which returns None for "
|
||||
"'pytestmark' instead of raising AttributeError. "
|
||||
"Make sure `__getattr__` raises AttributeError for "
|
||||
"attributes it does not provide. "
|
||||
"See https://github.com/pytest-dev/pytest/issues/8265",
|
||||
PytestCollectionWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
mark_list = []
|
||||
elif isinstance(mark_attribute, list):
|
||||
mark_list = mark_attribute
|
||||
else:
|
||||
mark_list = [mark_attribute]
|
||||
return list(normalize_mark_list(mark_list))
|
||||
|
||||
|
||||
def normalize_mark_list(
|
||||
mark_list: Iterable[Mark | MarkDecorator],
|
||||
) -> Iterable[Mark]:
|
||||
"""
|
||||
Normalize an iterable of Mark or MarkDecorator objects into a list of marks
|
||||
by retrieving the `mark` attribute on MarkDecorator instances.
|
||||
|
||||
:param mark_list: marks to normalize
|
||||
:returns: A new list of the extracted Mark objects
|
||||
"""
|
||||
for mark in mark_list:
|
||||
mark_obj = getattr(mark, "mark", mark)
|
||||
if not isinstance(mark_obj, Mark):
|
||||
raise TypeError(f"got {mark_obj!r} instead of Mark")
|
||||
yield mark_obj
|
||||
|
||||
|
||||
def store_mark(obj, mark: Mark) -> None:
|
||||
"""Store a Mark on an object.
|
||||
|
||||
This is used to implement the Mark declarations/decorators correctly.
|
||||
"""
|
||||
assert isinstance(mark, Mark), mark
|
||||
|
||||
from ..fixtures import getfixturemarker
|
||||
|
||||
if getfixturemarker(obj) is not None:
|
||||
fail(
|
||||
"Marks cannot be applied to fixtures.\n"
|
||||
"See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function"
|
||||
)
|
||||
|
||||
# Always reassign name to avoid updating pytestmark in a reference that
|
||||
# was only borrowed.
|
||||
obj.pytestmark = [*get_unpacked_marks(obj, consider_mro=False), mark]
|
||||
|
||||
|
||||
# Typing for builtin pytest marks. This is cheating; it gives builtin marks
|
||||
# special privilege, and breaks modularity. But practicality beats purity...
|
||||
if TYPE_CHECKING:
|
||||
|
||||
class _SkipMarkDecorator(MarkDecorator):
|
||||
@overload # type: ignore[override,no-overload-impl]
|
||||
def __call__(self, arg: Markable) -> Markable: ...
|
||||
|
||||
@overload
|
||||
def __call__(self, reason: str = ...) -> MarkDecorator: ...
|
||||
|
||||
class _SkipifMarkDecorator(MarkDecorator):
|
||||
def __call__( # type: ignore[override]
|
||||
self,
|
||||
condition: str | bool = ...,
|
||||
*conditions: str | bool,
|
||||
reason: str = ...,
|
||||
) -> MarkDecorator: ...
|
||||
|
||||
class _XfailMarkDecorator(MarkDecorator):
|
||||
@overload # type: ignore[override,no-overload-impl]
|
||||
def __call__(self, arg: Markable) -> Markable: ...
|
||||
|
||||
@overload
|
||||
def __call__(
|
||||
self,
|
||||
condition: str | bool = True,
|
||||
*conditions: str | bool,
|
||||
reason: str = ...,
|
||||
run: bool = ...,
|
||||
raises: type[BaseException]
|
||||
| tuple[type[BaseException], ...]
|
||||
| AbstractRaises[BaseException]
|
||||
| None = ...,
|
||||
strict: bool = ...,
|
||||
) -> MarkDecorator: ...
|
||||
|
||||
class _ParametrizeMarkDecorator(MarkDecorator):
|
||||
def __call__( # type: ignore[override]
|
||||
self,
|
||||
argnames: str | Sequence[str],
|
||||
argvalues: Iterable[ParameterSet | Sequence[object] | object],
|
||||
# TODO(pytest10): Change to below after PARAMETRIZE_NON_COLLECTION_ITERABLE deprecation.
|
||||
# Overload doesn't work, see #14606.
|
||||
# argvalues: Collection[ParameterSet | Sequence[object] | object],
|
||||
*,
|
||||
indirect: bool | Sequence[str] = ...,
|
||||
ids: Iterable[str | float | int | bool | _HiddenParam | None]
|
||||
| Callable[[Any], object | None]
|
||||
| None = ...,
|
||||
scope: ScopeName | None = ...,
|
||||
) -> MarkDecorator: ...
|
||||
|
||||
class _UsefixturesMarkDecorator(MarkDecorator):
|
||||
def __call__(self, *fixtures: str) -> MarkDecorator: # type: ignore[override]
|
||||
...
|
||||
|
||||
class _FilterwarningsMarkDecorator(MarkDecorator):
|
||||
def __call__(self, *filters: str) -> MarkDecorator: # type: ignore[override]
|
||||
...
|
||||
|
||||
|
||||
@final
|
||||
class MarkGenerator:
|
||||
"""Factory for :class:`MarkDecorator` objects - exposed as
|
||||
a ``pytest.mark`` singleton instance.
|
||||
|
||||
Example::
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.slowtest
|
||||
def test_function():
|
||||
pass
|
||||
|
||||
applies a 'slowtest' :class:`Mark` on ``test_function``.
|
||||
"""
|
||||
|
||||
# See TYPE_CHECKING above.
|
||||
if TYPE_CHECKING:
|
||||
skip: _SkipMarkDecorator
|
||||
skipif: _SkipifMarkDecorator
|
||||
xfail: _XfailMarkDecorator
|
||||
parametrize: _ParametrizeMarkDecorator
|
||||
usefixtures: _UsefixturesMarkDecorator
|
||||
filterwarnings: _FilterwarningsMarkDecorator
|
||||
|
||||
def __init__(self, *, _ispytest: bool = False) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
self._config: Config | None = None
|
||||
self._markers: set[str] = set()
|
||||
|
||||
def __getattr__(self, name: str) -> MarkDecorator:
|
||||
"""Generate a new :class:`MarkDecorator` with the given name."""
|
||||
if name[0] == "_":
|
||||
raise AttributeError("Marker name must NOT start with underscore")
|
||||
|
||||
if self._config is not None:
|
||||
# We store a set of markers as a performance optimisation - if a mark
|
||||
# name is in the set we definitely know it, but a mark may be known and
|
||||
# not in the set. We therefore start by updating the set!
|
||||
if name not in self._markers:
|
||||
self._markers.update(
|
||||
m.name for m in self._config._iter_registered_markers()
|
||||
)
|
||||
|
||||
# If the name is not in the set of known marks after updating,
|
||||
# then it really is time to issue a warning or an error.
|
||||
if name not in self._markers:
|
||||
# Raise a specific error for common misspellings of "parametrize".
|
||||
if name in ["parameterize", "parametrise", "parameterise"]:
|
||||
__tracebackhide__ = True
|
||||
fail(f"Unknown '{name}' mark, did you mean 'parametrize'?")
|
||||
|
||||
strict_markers = self._config.getini("strict_markers")
|
||||
if strict_markers is None:
|
||||
strict_markers = self._config.getini("strict")
|
||||
if strict_markers:
|
||||
fail(
|
||||
f"{name!r} not found in `markers` configuration option",
|
||||
pytrace=False,
|
||||
)
|
||||
|
||||
warnings.warn(
|
||||
f"Unknown pytest.mark.{name} - is this a typo? You can register "
|
||||
"custom marks to avoid this warning - for details, see "
|
||||
"https://docs.pytest.org/en/stable/how-to/mark.html",
|
||||
PytestUnknownMarkWarning,
|
||||
2,
|
||||
)
|
||||
|
||||
return MarkDecorator(Mark(name, (), {}, _ispytest=True), _ispytest=True)
|
||||
|
||||
|
||||
MARK_GEN = MarkGenerator(_ispytest=True)
|
||||
|
||||
|
||||
@final
|
||||
class NodeKeywords(MutableMapping[str, Any]):
|
||||
__slots__ = ("_markers", "node", "parent")
|
||||
|
||||
def __init__(self, node: Node) -> None:
|
||||
self.node = node
|
||||
self.parent = node.parent
|
||||
self._markers = {node.name: True}
|
||||
|
||||
def __getitem__(self, key: str) -> Any:
|
||||
try:
|
||||
return self._markers[key]
|
||||
except KeyError:
|
||||
if self.parent is None:
|
||||
raise
|
||||
return self.parent.keywords[key]
|
||||
|
||||
def __setitem__(self, key: str, value: Any) -> None:
|
||||
self._markers[key] = value
|
||||
|
||||
# Note: we could've avoided explicitly implementing some of the methods
|
||||
# below and use the collections.abc fallback, but that would be slow.
|
||||
|
||||
def __contains__(self, key: object) -> bool:
|
||||
return key in self._markers or (
|
||||
self.parent is not None and key in self.parent.keywords
|
||||
)
|
||||
|
||||
def update( # type: ignore[override]
|
||||
self,
|
||||
other: Mapping[str, Any] | Iterable[tuple[str, Any]] = (),
|
||||
**kwds: Any,
|
||||
) -> None:
|
||||
self._markers.update(other)
|
||||
self._markers.update(kwds)
|
||||
|
||||
def __delitem__(self, key: str) -> None:
|
||||
raise ValueError("cannot delete key in keywords dict")
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
# Doesn't need to be fast.
|
||||
yield from self._markers
|
||||
if self.parent is not None:
|
||||
for keyword in self.parent.keywords:
|
||||
# self._marks and self.parent.keywords can have duplicates.
|
||||
if keyword not in self._markers:
|
||||
yield keyword
|
||||
|
||||
def __len__(self) -> int:
|
||||
# Doesn't need to be fast.
|
||||
return sum(1 for keyword in self)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<NodeKeywords for node {self.node}>"
|
||||
@@ -0,0 +1,456 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Monkeypatching and mocking functionality."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import MutableMapping
|
||||
from contextlib import contextmanager
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing import final
|
||||
from typing import overload
|
||||
from typing import TypeVar
|
||||
import warnings
|
||||
|
||||
from _pytest.compat import NOTSET
|
||||
from _pytest.compat import NotSetType
|
||||
from _pytest.deprecated import MONKEYPATCH_LEGACY_NAMESPACE_PACKAGES
|
||||
from _pytest.fixtures import fixture
|
||||
from _pytest.warning_types import PytestWarning
|
||||
|
||||
|
||||
RE_IMPORT_ERROR_NAME = re.compile(r"^No module named (.*)$")
|
||||
|
||||
|
||||
K = TypeVar("K")
|
||||
V = TypeVar("V")
|
||||
|
||||
|
||||
@fixture
|
||||
def monkeypatch() -> Generator[MonkeyPatch]:
|
||||
"""A convenient fixture for monkey-patching.
|
||||
|
||||
The fixture provides these methods to modify objects, dictionaries, or
|
||||
:data:`os.environ`:
|
||||
|
||||
* :meth:`monkeypatch.setattr(obj, name, value, raising=True) <pytest.MonkeyPatch.setattr>`
|
||||
* :meth:`monkeypatch.delattr(obj, name, raising=True) <pytest.MonkeyPatch.delattr>`
|
||||
* :meth:`monkeypatch.setitem(mapping, name, value) <pytest.MonkeyPatch.setitem>`
|
||||
* :meth:`monkeypatch.delitem(obj, name, raising=True) <pytest.MonkeyPatch.delitem>`
|
||||
* :meth:`monkeypatch.setenv(name, value, prepend=None) <pytest.MonkeyPatch.setenv>`
|
||||
* :meth:`monkeypatch.delenv(name, raising=True) <pytest.MonkeyPatch.delenv>`
|
||||
* :meth:`monkeypatch.syspath_prepend(path) <pytest.MonkeyPatch.syspath_prepend>`
|
||||
* :meth:`monkeypatch.chdir(path) <pytest.MonkeyPatch.chdir>`
|
||||
* :meth:`monkeypatch.context() <pytest.MonkeyPatch.context>`
|
||||
|
||||
All modifications will be undone after the requesting test function or
|
||||
fixture has finished. The ``raising`` parameter determines if a :class:`KeyError`
|
||||
or :class:`AttributeError` will be raised if the set/deletion operation does not have the
|
||||
specified target.
|
||||
|
||||
To undo modifications done by the fixture in a contained scope,
|
||||
use :meth:`context() <pytest.MonkeyPatch.context>`.
|
||||
"""
|
||||
mpatch = MonkeyPatch()
|
||||
yield mpatch
|
||||
mpatch.undo()
|
||||
|
||||
|
||||
def resolve(name: str) -> object:
|
||||
# Simplified from zope.dottedname.
|
||||
parts = name.split(".")
|
||||
|
||||
used = parts.pop(0)
|
||||
found: object = importlib.import_module(used)
|
||||
for part in parts:
|
||||
used += "." + part
|
||||
try:
|
||||
found = getattr(found, part)
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
continue
|
||||
# We use explicit un-nesting of the handling block in order
|
||||
# to avoid nested exceptions.
|
||||
try:
|
||||
importlib.import_module(used)
|
||||
except ImportError as ex:
|
||||
expected = str(ex).split()[-1]
|
||||
if expected == used:
|
||||
raise
|
||||
else:
|
||||
raise ImportError(f"import error in {used}: {ex}") from ex
|
||||
found = annotated_getattr(found, part, used)
|
||||
return found
|
||||
|
||||
|
||||
def annotated_getattr(obj: object, name: str, ann: str) -> object:
|
||||
try:
|
||||
obj = getattr(obj, name)
|
||||
except AttributeError as e:
|
||||
raise AttributeError(
|
||||
f"{type(obj).__name__!r} object at {ann} has no attribute {name!r}"
|
||||
) from e
|
||||
return obj
|
||||
|
||||
|
||||
def derive_importpath(import_path: str, raising: bool) -> tuple[str, object]:
|
||||
if not isinstance(import_path, str) or "." not in import_path:
|
||||
raise TypeError(f"must be absolute import path string, not {import_path!r}")
|
||||
module, attr = import_path.rsplit(".", 1)
|
||||
target = resolve(module)
|
||||
if raising:
|
||||
annotated_getattr(target, attr, ann=module)
|
||||
return attr, target
|
||||
|
||||
|
||||
def _is_data_descriptor(cls: type, name: str) -> bool:
|
||||
"""Return True if looking up ``name`` on ``cls`` finds a data descriptor.
|
||||
|
||||
Data descriptors take precedence over the instance ``__dict__``, so
|
||||
``setattr()`` on an instance is routed through the descriptor instead of
|
||||
writing an entry into the instance ``__dict__``.
|
||||
"""
|
||||
for klass in cls.__mro__:
|
||||
if name in klass.__dict__:
|
||||
descr_type = type(klass.__dict__[name])
|
||||
return hasattr(descr_type, "__set__") or hasattr(descr_type, "__delete__")
|
||||
return False
|
||||
|
||||
|
||||
@final
|
||||
class MonkeyPatch:
|
||||
"""Helper to conveniently monkeypatch attributes/items/environment
|
||||
variables/syspath.
|
||||
|
||||
Returned by the :fixture:`monkeypatch` fixture.
|
||||
|
||||
.. versionchanged:: 6.2
|
||||
Can now also be used directly as `pytest.MonkeyPatch()`, for when
|
||||
the fixture is not available. In this case, use
|
||||
:meth:`with MonkeyPatch.context() as mp: <context>` or remember to call
|
||||
:meth:`undo` explicitly.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._setattr: list[tuple[object, str, object]] = []
|
||||
self._setitem: list[tuple[Mapping[Any, Any], object, object]] = []
|
||||
self._cwd: str | None = None
|
||||
self._savesyspath: list[str] | None = None
|
||||
|
||||
@classmethod
|
||||
@contextmanager
|
||||
def context(cls) -> Generator[MonkeyPatch]:
|
||||
"""Context manager that returns a new :class:`MonkeyPatch` object
|
||||
which undoes any patching done inside the ``with`` block upon exit.
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import functools
|
||||
|
||||
|
||||
def test_partial(monkeypatch):
|
||||
with monkeypatch.context() as m:
|
||||
m.setattr(functools, "partial", 3)
|
||||
|
||||
Useful in situations where it is desired to undo some patches before the test ends,
|
||||
such as mocking ``stdlib`` functions that might break pytest itself if mocked (for examples
|
||||
of this see :issue:`3290`).
|
||||
"""
|
||||
m = cls()
|
||||
try:
|
||||
yield m
|
||||
finally:
|
||||
m.undo()
|
||||
|
||||
@overload
|
||||
def setattr(
|
||||
self,
|
||||
target: str,
|
||||
name: object,
|
||||
value: NotSetType = ...,
|
||||
raising: bool = ...,
|
||||
) -> None: ...
|
||||
|
||||
@overload
|
||||
def setattr(
|
||||
self,
|
||||
target: object,
|
||||
name: str,
|
||||
value: object,
|
||||
raising: bool = ...,
|
||||
) -> None: ...
|
||||
|
||||
def setattr(
|
||||
self,
|
||||
target: str | object,
|
||||
name: object | str,
|
||||
value: object = NOTSET,
|
||||
raising: bool = True,
|
||||
) -> None:
|
||||
"""
|
||||
Set attribute value on target, memorizing the old value.
|
||||
|
||||
For example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import os
|
||||
|
||||
monkeypatch.setattr(os, "getcwd", lambda: "/")
|
||||
|
||||
The code above replaces the :func:`os.getcwd` function by a ``lambda`` which
|
||||
always returns ``"/"``.
|
||||
|
||||
For convenience, you can specify a string as ``target`` which
|
||||
will be interpreted as a dotted import path, with the last part
|
||||
being the attribute name:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
monkeypatch.setattr("os.getcwd", lambda: "/")
|
||||
|
||||
Raises :class:`AttributeError` if the attribute does not exist, unless
|
||||
``raising`` is set to False.
|
||||
|
||||
**Where to patch**
|
||||
|
||||
``monkeypatch.setattr`` works by (temporarily) changing the object that a name points to with another one.
|
||||
There can be many names pointing to any individual object, so for patching to work you must ensure
|
||||
that you patch the name used by the system under test.
|
||||
|
||||
See the section :ref:`Where to patch <python:where-to-patch>` in the :mod:`unittest.mock`
|
||||
docs for a complete explanation, which is meant for :func:`unittest.mock.patch` but
|
||||
applies to ``monkeypatch.setattr`` as well.
|
||||
"""
|
||||
__tracebackhide__ = True
|
||||
import inspect
|
||||
|
||||
if value is NOTSET:
|
||||
if not isinstance(target, str):
|
||||
raise TypeError(
|
||||
"use setattr(target, name, value) or "
|
||||
"setattr(target, value) with target being a dotted "
|
||||
"import string"
|
||||
)
|
||||
value = name
|
||||
name, target = derive_importpath(target, raising)
|
||||
else:
|
||||
if not isinstance(name, str):
|
||||
raise TypeError(
|
||||
"use setattr(target, name, value) with name being a string or "
|
||||
"setattr(target, value) with target being a dotted "
|
||||
"import string"
|
||||
)
|
||||
|
||||
oldval = getattr(target, name, NOTSET)
|
||||
if raising and oldval is NOTSET:
|
||||
raise AttributeError(f"{target!r} has no attribute {name!r}")
|
||||
|
||||
# avoid class descriptors like staticmethod/classmethod
|
||||
if inspect.isclass(target):
|
||||
oldval = target.__dict__.get(name, NOTSET)
|
||||
elif not _is_data_descriptor(type(target), name):
|
||||
# With no data descriptor in the way, the `setattr()` below writes
|
||||
# into the instance `__dict__`, so `undo()` has to restore that
|
||||
# `__dict__` entry. Assigning an inherited `oldval` back onto the
|
||||
# instance would instead leave behind a new entry shadowing the
|
||||
# class attribute, which permanently freezes descriptors that
|
||||
# resolve dynamically (#10644).
|
||||
target_dict = getattr(target, "__dict__", None)
|
||||
if isinstance(target_dict, Mapping):
|
||||
oldval = target_dict.get(name, NOTSET)
|
||||
setattr(target, name, value)
|
||||
self._setattr.append((target, name, oldval))
|
||||
|
||||
def delattr(
|
||||
self,
|
||||
target: object | str,
|
||||
name: str | NotSetType = NOTSET,
|
||||
raising: bool = True,
|
||||
) -> None:
|
||||
"""Delete attribute ``name`` from ``target``.
|
||||
|
||||
If no ``name`` is specified and ``target`` is a string
|
||||
it will be interpreted as a dotted import path with the
|
||||
last part being the attribute name.
|
||||
|
||||
Raises AttributeError it the attribute does not exist, unless
|
||||
``raising`` is set to False.
|
||||
"""
|
||||
__tracebackhide__ = True
|
||||
import inspect
|
||||
|
||||
if name is NOTSET:
|
||||
if not isinstance(target, str):
|
||||
raise TypeError(
|
||||
"use delattr(target, name) or "
|
||||
"delattr(target) with target being a dotted "
|
||||
"import string"
|
||||
)
|
||||
name, target = derive_importpath(target, raising)
|
||||
|
||||
if not hasattr(target, name):
|
||||
if raising:
|
||||
raise AttributeError(name)
|
||||
else:
|
||||
oldval = getattr(target, name, NOTSET)
|
||||
# Avoid class descriptors like staticmethod/classmethod.
|
||||
if inspect.isclass(target):
|
||||
oldval = target.__dict__.get(name, NOTSET)
|
||||
delattr(target, name)
|
||||
self._setattr.append((target, name, oldval))
|
||||
|
||||
def setitem(self, dic: Mapping[K, V], name: K, value: V) -> None:
|
||||
"""Set dictionary entry ``name`` to value."""
|
||||
oldval = dic.get(name, NOTSET)
|
||||
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
|
||||
dic[name] = value # type: ignore[index]
|
||||
self._setitem.append((dic, name, oldval))
|
||||
|
||||
def delitem(self, dic: Mapping[K, V], name: K, raising: bool = True) -> None:
|
||||
"""Delete ``name`` from dict.
|
||||
|
||||
Raises ``KeyError`` if it doesn't exist, unless ``raising`` is set to
|
||||
False.
|
||||
"""
|
||||
if name not in dic:
|
||||
if raising:
|
||||
raise KeyError(name)
|
||||
else:
|
||||
oldval = dic.get(name, NOTSET)
|
||||
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
|
||||
del dic[name] # type: ignore[attr-defined]
|
||||
self._setitem.append((dic, name, oldval))
|
||||
|
||||
def setenv(self, name: str, value: str, prepend: str | None = None) -> None:
|
||||
"""Set environment variable ``name`` to ``value``.
|
||||
|
||||
If ``prepend`` is a character, read the current environment variable
|
||||
value and prepend the ``value`` adjoined with the ``prepend``
|
||||
character.
|
||||
"""
|
||||
if not isinstance(value, str):
|
||||
warnings.warn( # type: ignore[unreachable]
|
||||
PytestWarning(
|
||||
f"Value of environment variable {name} type should be str, but got "
|
||||
f"{value!r} (type: {type(value).__name__}); converted to str implicitly"
|
||||
),
|
||||
stacklevel=2,
|
||||
)
|
||||
value = str(value)
|
||||
if prepend and name in os.environ:
|
||||
value = value + prepend + os.environ[name]
|
||||
self.setitem(os.environ, name, value)
|
||||
|
||||
def delenv(self, name: str, raising: bool = True) -> None:
|
||||
"""Delete ``name`` from the environment.
|
||||
|
||||
Raises ``KeyError`` if it does not exist, unless ``raising`` is set to
|
||||
False.
|
||||
"""
|
||||
environ: MutableMapping[str, str] = os.environ
|
||||
self.delitem(environ, name, raising=raising)
|
||||
|
||||
def syspath_prepend(self, path) -> None:
|
||||
"""Prepend ``path`` to ``sys.path`` list of import locations."""
|
||||
if self._savesyspath is None:
|
||||
self._savesyspath = sys.path[:]
|
||||
sys.path.insert(0, str(path))
|
||||
|
||||
# https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
|
||||
# this is only needed when pkg_resources was already loaded by the namespace package
|
||||
if "pkg_resources" in sys.modules:
|
||||
import pkg_resources
|
||||
from pkg_resources import fixup_namespace_packages
|
||||
|
||||
# Only issue deprecation warning if this call would actually have an
|
||||
# effect for this specific path.
|
||||
if (
|
||||
hasattr(pkg_resources, "_namespace_packages")
|
||||
and pkg_resources._namespace_packages
|
||||
):
|
||||
path_obj = Path(str(path))
|
||||
for ns_pkg in pkg_resources._namespace_packages:
|
||||
if ns_pkg is None:
|
||||
continue
|
||||
ns_pkg_path = path_obj / ns_pkg.replace(".", os.sep)
|
||||
if ns_pkg_path.is_dir():
|
||||
warnings.warn(
|
||||
MONKEYPATCH_LEGACY_NAMESPACE_PACKAGES.format(), stacklevel=2
|
||||
)
|
||||
break
|
||||
|
||||
fixup_namespace_packages(str(path))
|
||||
|
||||
# A call to syspathinsert() usually means that the caller wants to
|
||||
# import some dynamically created files, thus with python3 we
|
||||
# invalidate its import caches.
|
||||
# This is especially important when any namespace package is in use,
|
||||
# since then the mtime based FileFinder cache (that gets created in
|
||||
# this case already) gets not invalidated when writing the new files
|
||||
# quickly afterwards.
|
||||
from importlib import invalidate_caches
|
||||
|
||||
invalidate_caches()
|
||||
|
||||
def chdir(self, path: str | os.PathLike[str]) -> None:
|
||||
"""Change the current working directory to the specified path.
|
||||
|
||||
:param path:
|
||||
The path to change into.
|
||||
"""
|
||||
if self._cwd is None:
|
||||
self._cwd = os.getcwd()
|
||||
os.chdir(path)
|
||||
|
||||
def undo(self) -> None:
|
||||
"""Undo previous changes.
|
||||
|
||||
This call consumes the undo stack. Calling it a second time has no
|
||||
effect unless you do more monkeypatching after the undo call.
|
||||
|
||||
There is generally no need to call `undo()`, since it is
|
||||
called automatically during tear-down.
|
||||
|
||||
.. note::
|
||||
The same `monkeypatch` fixture is used across a
|
||||
single test function invocation. If `monkeypatch` is used both by
|
||||
the test function itself and one of the test fixtures,
|
||||
calling `undo()` will undo all of the changes made in
|
||||
both functions.
|
||||
|
||||
Prefer to use :meth:`context() <pytest.MonkeyPatch.context>` instead.
|
||||
"""
|
||||
for obj, name, value in reversed(self._setattr):
|
||||
if value is not NOTSET:
|
||||
setattr(obj, name, value)
|
||||
else:
|
||||
delattr(obj, name)
|
||||
self._setattr[:] = []
|
||||
for dictionary, key, value in reversed(self._setitem):
|
||||
if value is NOTSET:
|
||||
try:
|
||||
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
|
||||
del dictionary[key]
|
||||
except KeyError:
|
||||
pass # Was already deleted, so we have the desired state.
|
||||
else:
|
||||
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
|
||||
dictionary[key] = value
|
||||
self._setitem[:] = []
|
||||
if self._savesyspath is not None:
|
||||
sys.path[:] = self._savesyspath
|
||||
self._savesyspath = None
|
||||
|
||||
if self._cwd is not None:
|
||||
os.chdir(self._cwd)
|
||||
self._cwd = None
|
||||
@@ -0,0 +1,113 @@
|
||||
"""Structured representation of a pytest "nodeid".
|
||||
|
||||
A nodeid is represented as a ``::``-separated string, identifying a node in the collection
|
||||
tree, e.g. ``path/to/test_file.py::TestClass::test_method[param]``.
|
||||
|
||||
The :class:`NodeId` class represents that information in a proper dataclass with the relevant
|
||||
parts readily available, avoiding reparsing that information when needed and also making for
|
||||
a better type than `str`.
|
||||
|
||||
The traditional ``::``-joined string form remains available via ``str(node_id)``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True, slots=True, kw_only=True)
|
||||
class NodeId:
|
||||
"""Structured identifier of a node in the collection tree.
|
||||
|
||||
:param path:
|
||||
``/``-normalized, rootpath-relative filesystem path. Empty string
|
||||
for the session root.
|
||||
:param names:
|
||||
Ordered ``::``-segment names after the path.
|
||||
:param params:
|
||||
Raw contents inside the outermost ``[...]`` bracket, kept as one
|
||||
opaque string -- traditional string-node ids are often recovered from the outside (for example
|
||||
files), so we cannot recover the actual params and values used to build the parametrization.
|
||||
"""
|
||||
|
||||
path: str
|
||||
names: tuple[str, ...] = ()
|
||||
params: str | None = None
|
||||
_str_cache: str | None = dataclasses.field(
|
||||
default=None, init=False, repr=False, compare=False
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def parse(cls, nodeid: str) -> NodeId:
|
||||
"""Parse a nodeid string into its path, names, and raw params."""
|
||||
path, sep, tail = nodeid.partition("::")
|
||||
if not sep:
|
||||
# No "::" -- everything is the path. Never interpret a bracket
|
||||
# here: file paths can legitimately contain "[" (e.g. test[1].py).
|
||||
self = cls(path=path)
|
||||
else:
|
||||
# Peel the params bracket off the tail FIRST.
|
||||
name_part, bracket, param_part = tail.partition("[")
|
||||
names = tuple(name_part.split("::"))
|
||||
params = param_part.removesuffix("]") if bracket else None
|
||||
self = cls(path=path, names=names, params=params)
|
||||
# We already have the original string in hand -- cache it directly
|
||||
# as _str_cache instead of letting __str__ reconstruct it later.
|
||||
object.__setattr__(self, "_str_cache", nodeid)
|
||||
return self
|
||||
|
||||
def __str__(self) -> str:
|
||||
if self._str_cache is not None:
|
||||
return self._str_cache
|
||||
s = "::".join((self.path, *self.names))
|
||||
if self.params is not None:
|
||||
s += f"[{self.params}]"
|
||||
object.__setattr__(self, "_str_cache", s)
|
||||
return s
|
||||
|
||||
@property
|
||||
def rest(self) -> str | None:
|
||||
"""Everything after the first ``"::"`` as a single string, or
|
||||
``None`` when there is no ``"::"`` (i.e. ``names`` is empty).
|
||||
"""
|
||||
if not self.names:
|
||||
return None
|
||||
s = "::".join(self.names)
|
||||
if self.params is not None:
|
||||
s += f"[{self.params}]"
|
||||
return s
|
||||
|
||||
def child(self, name: str) -> NodeId:
|
||||
"""Return a new :class:`NodeId` for a child collector node.
|
||||
|
||||
:raises ValueError: if called on a node that already has params
|
||||
(i.e., a leaf item) -- only collector ids (``params=None``) can
|
||||
have children.
|
||||
"""
|
||||
if self.params is not None:
|
||||
raise ValueError(
|
||||
f"cannot call .child() on a parameterised id {self!r}; "
|
||||
"only collector ids (params=None) can have children"
|
||||
)
|
||||
return NodeId(path=self.path, names=(*self.names, name))
|
||||
|
||||
def with_params(self, params: str | None) -> NodeId:
|
||||
"""Return a new :class:`NodeId` with ``params`` set.
|
||||
|
||||
:raises ValueError: if ``self`` is already parameterised (i.e. an
|
||||
item id with ``params is not None``).
|
||||
"""
|
||||
if self.params is not None:
|
||||
raise ValueError(
|
||||
f"cannot call .with_params() on a parameterised id {self!r}; "
|
||||
"only collector ids (params=None) can be parameterised"
|
||||
)
|
||||
return NodeId(path=self.path, names=self.names, params=params)
|
||||
|
||||
|
||||
def coerce_node_id(nodeid: str | NodeId) -> NodeId:
|
||||
"""Return ``nodeid`` unchanged if already a :class:`NodeId`; otherwise
|
||||
parse it via :meth:`NodeId.parse`."""
|
||||
if isinstance(nodeid, NodeId):
|
||||
return nodeid
|
||||
return NodeId.parse(nodeid)
|
||||
@@ -0,0 +1,797 @@
|
||||
# mypy: allow-untyped-defs
|
||||
from __future__ import annotations
|
||||
|
||||
import abc
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import MutableMapping
|
||||
from functools import cached_property
|
||||
from functools import lru_cache
|
||||
import os
|
||||
import pathlib
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from typing import cast
|
||||
from typing import NoReturn
|
||||
from typing import overload
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TypeVar
|
||||
import warnings
|
||||
|
||||
import pluggy
|
||||
|
||||
import _pytest._code
|
||||
from _pytest._code import getfslineno
|
||||
from _pytest._code.code import ExceptionInfo
|
||||
from _pytest._code.code import TerminalRepr
|
||||
from _pytest._code.code import Traceback
|
||||
from _pytest._code.code import TracebackStyle
|
||||
from _pytest.compat import LEGACY_PATH
|
||||
from _pytest.compat import signature
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import ConftestImportFailure
|
||||
from _pytest.mark.structures import Mark
|
||||
from _pytest.mark.structures import MarkDecorator
|
||||
from _pytest.mark.structures import NodeKeywords
|
||||
from _pytest.nodeid import coerce_node_id
|
||||
from _pytest.nodeid import NodeId
|
||||
from _pytest.outcomes import fail
|
||||
from _pytest.pathlib import absolutepath
|
||||
from _pytest.stash import Stash
|
||||
from _pytest.warning_types import PytestWarning
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import Self
|
||||
|
||||
# Imported here due to circular import.
|
||||
from _pytest.main import Session
|
||||
|
||||
|
||||
SEP = "/"
|
||||
|
||||
|
||||
def norm_sep(path: str | os.PathLike[str]) -> str:
|
||||
"""Normalize path separators to forward slashes for nodeid compatibility.
|
||||
|
||||
Replaces backslashes with forward slashes. This handles both Windows native
|
||||
paths and cross-platform data (e.g., Windows paths in serialized test reports
|
||||
when running on Linux).
|
||||
|
||||
:param path: A path string or PathLike object.
|
||||
:returns: String with all backslashes replaced by forward slashes.
|
||||
"""
|
||||
return os.fspath(path).replace("\\", SEP)
|
||||
|
||||
|
||||
tracebackcutdir = Path(_pytest.__file__).parent
|
||||
|
||||
|
||||
_T = TypeVar("_T")
|
||||
|
||||
|
||||
_NodeType = TypeVar("_NodeType", bound="Node")
|
||||
|
||||
|
||||
class NodeMeta(abc.ABCMeta):
|
||||
"""Metaclass used by :class:`Node` to enforce that direct construction raises
|
||||
:class:`Failed`.
|
||||
|
||||
This behaviour supports the indirection introduced with :meth:`Node.from_parent`,
|
||||
the named constructor to be used instead of direct construction. The design
|
||||
decision to enforce indirection with :class:`NodeMeta` was made as a
|
||||
temporary aid for refactoring the collection tree, which was diagnosed to
|
||||
have :class:`Node` objects whose creational patterns were overly entangled.
|
||||
Once the refactoring is complete, this metaclass can be removed.
|
||||
|
||||
See https://github.com/pytest-dev/pytest/projects/3 for an overview of the
|
||||
progress on detangling the :class:`Node` classes.
|
||||
"""
|
||||
|
||||
def __call__(cls, *k, **kw) -> NoReturn:
|
||||
msg = (
|
||||
"Direct construction of {name} has been deprecated, please use {name}.from_parent.\n"
|
||||
"See "
|
||||
"https://docs.pytest.org/en/stable/deprecations.html#node-construction-changed-to-node-from-parent"
|
||||
" for more details."
|
||||
).format(name=f"{cls.__module__}.{cls.__name__}")
|
||||
fail(msg, pytrace=False)
|
||||
|
||||
def _create(cls: type[_T], *k, **kw) -> _T:
|
||||
try:
|
||||
return super().__call__(*k, **kw) # type: ignore[no-any-return,misc]
|
||||
except TypeError:
|
||||
sig = signature(getattr(cls, "__init__"))
|
||||
known_kw = {k: v for k, v in kw.items() if k in sig.parameters}
|
||||
from .warning_types import PytestDeprecationWarning
|
||||
|
||||
warnings.warn(
|
||||
PytestDeprecationWarning(
|
||||
f"{cls} is not using a cooperative constructor and only takes {set(known_kw)}.\n"
|
||||
"See https://docs.pytest.org/en/stable/deprecations.html"
|
||||
"#constructors-of-custom-pytest-node-subclasses-should-take-kwargs "
|
||||
"for more details."
|
||||
)
|
||||
)
|
||||
|
||||
return super().__call__(*k, **known_kw) # type: ignore[no-any-return,misc]
|
||||
|
||||
|
||||
class Node(abc.ABC, metaclass=NodeMeta):
|
||||
r"""Base class of :class:`Collector` and :class:`Item`, the components of
|
||||
the test collection tree.
|
||||
|
||||
``Collector``\'s are the internal nodes of the tree, and ``Item``\'s are the
|
||||
leaf nodes.
|
||||
"""
|
||||
|
||||
# Implemented in the legacypath plugin.
|
||||
#: A ``LEGACY_PATH`` copy of the :attr:`path` attribute. Intended for usage
|
||||
#: for methods not migrated to ``pathlib.Path`` yet, such as
|
||||
#: :meth:`Item.reportinfo <pytest.Item.reportinfo>`. Will be deprecated in
|
||||
#: a future release, prefer using :attr:`path` instead.
|
||||
fspath: LEGACY_PATH
|
||||
|
||||
# Use __slots__ to make attribute access faster.
|
||||
# Note that __dict__ is still available.
|
||||
__slots__ = (
|
||||
"__dict__",
|
||||
"_id",
|
||||
"_store",
|
||||
"config",
|
||||
"name",
|
||||
"parent",
|
||||
"path",
|
||||
"session",
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
parent: Collector | None = None,
|
||||
config: Config | None = None,
|
||||
session: Session | None = None,
|
||||
fspath: None = None,
|
||||
path: Path | None = None,
|
||||
nodeid: NodeId | str | None = None,
|
||||
) -> None:
|
||||
#: A unique name within the scope of the parent node.
|
||||
self.name: str = name
|
||||
|
||||
#: The parent collector node.
|
||||
self.parent = parent
|
||||
|
||||
if config:
|
||||
#: The pytest config object.
|
||||
self.config: Config = config
|
||||
else:
|
||||
if not parent:
|
||||
raise TypeError("config or parent must be provided")
|
||||
self.config = parent.config
|
||||
|
||||
if session:
|
||||
#: The pytest session this node is part of.
|
||||
self.session: Session = session
|
||||
else:
|
||||
if not parent:
|
||||
raise TypeError("session or parent must be provided")
|
||||
self.session = parent.session
|
||||
|
||||
if path is None:
|
||||
assert parent is not None
|
||||
path = parent.path
|
||||
#: Filesystem path where this node was collected from.
|
||||
self.path: pathlib.Path = path
|
||||
|
||||
# The explicit annotation is to avoid publicly exposing NodeKeywords.
|
||||
#: Mapping of the names collected for this node and its parents: the
|
||||
#: node names themselves, the names of the markers applied to them
|
||||
#: (mapping to the :class:`~pytest.Mark`), and, for a test function,
|
||||
#: its attributes and parametrization id.
|
||||
#:
|
||||
#: Mostly useful for ``"markname" in item.keywords`` checks. Note that
|
||||
#: this mapping is not what ``-k`` matches against, so writing to it
|
||||
#: does not affect test selection, and that it is unrelated to
|
||||
#: :attr:`extra_keyword_matches`.
|
||||
self.keywords: MutableMapping[str, Any] = NodeKeywords(self)
|
||||
|
||||
#: The marker objects belonging to this node.
|
||||
self.own_markers: list[Mark] = []
|
||||
|
||||
#: Extra names for ``-k`` to match this node and its children on.
|
||||
self.extra_keyword_matches: set[str] = set()
|
||||
|
||||
if nodeid is not None:
|
||||
if not isinstance(nodeid, (NodeId, str)): # pragma: no cover
|
||||
raise ValueError(
|
||||
f"nodeid must be a NodeId, str, "
|
||||
f"or None, got {nodeid!r} (name={name}). Do not pass nodeid explicitly -- use "
|
||||
f"Node.from_parent() and let pytest compute it automatically."
|
||||
)
|
||||
# Accept nodeid as string for now because some downstream consumers
|
||||
# (plugins defining custom Node subclasses) still pass nodeid as a plain string.
|
||||
self._id = coerce_node_id(nodeid)
|
||||
else:
|
||||
if not self.parent: # pragma: no cover
|
||||
raise TypeError("nodeid or parent must be provided")
|
||||
self._id = self.parent.id.child(self.name)
|
||||
|
||||
#: A place where plugins can store information on the node for their
|
||||
#: own use.
|
||||
self.stash: Stash = Stash()
|
||||
# Deprecated alias. Was never public. Can be removed in a few releases.
|
||||
self._store = self.stash
|
||||
|
||||
@classmethod
|
||||
def from_parent(cls, parent: Collector, **kw) -> Self:
|
||||
"""Public constructor for Nodes.
|
||||
|
||||
This indirection got introduced in order to enable removing
|
||||
the fragile logic from the node constructors.
|
||||
|
||||
Subclasses can use ``super().from_parent(...)`` when overriding the
|
||||
construction.
|
||||
|
||||
:param parent: The parent node of this Node.
|
||||
"""
|
||||
if "config" in kw:
|
||||
raise TypeError("config is not a valid argument for from_parent")
|
||||
if "session" in kw:
|
||||
raise TypeError("session is not a valid argument for from_parent")
|
||||
return cls._create(parent=parent, **kw)
|
||||
|
||||
@property
|
||||
def ihook(self) -> pluggy.HookRelay:
|
||||
"""Path-sensitive hook proxy used to call pytest hooks."""
|
||||
return self.session.gethookproxy(self.path)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "<{} {}>".format(self.__class__.__name__, getattr(self, "name", None))
|
||||
|
||||
def warn(self, warning: Warning) -> None:
|
||||
"""Issue a warning for this Node.
|
||||
|
||||
Warnings will be displayed after the test session, unless explicitly suppressed.
|
||||
|
||||
:param Warning warning:
|
||||
The warning instance to issue.
|
||||
|
||||
:raises ValueError: If ``warning`` instance is not a subclass of Warning.
|
||||
|
||||
Example usage:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
node.warn(PytestWarning("some message"))
|
||||
node.warn(UserWarning("some message"))
|
||||
|
||||
.. versionchanged:: 6.2
|
||||
Any subclass of :class:`Warning` is now accepted, rather than only
|
||||
:class:`PytestWarning <pytest.PytestWarning>` subclasses.
|
||||
"""
|
||||
# enforce type checks here to avoid getting a generic type error later otherwise.
|
||||
if not isinstance(warning, Warning):
|
||||
raise ValueError(
|
||||
f"warning must be an instance of Warning or subclass, got {warning!r}"
|
||||
)
|
||||
path, lineno = get_fslocation_from_item(self)
|
||||
assert lineno is not None
|
||||
warnings.warn_explicit(
|
||||
warning,
|
||||
category=None,
|
||||
filename=str(path),
|
||||
lineno=lineno + 1,
|
||||
)
|
||||
|
||||
# Methods for ordering nodes.
|
||||
|
||||
@property
|
||||
def nodeid(self) -> str:
|
||||
"""A ::-separated string denoting its collection tree address."""
|
||||
return str(self._id)
|
||||
|
||||
@property
|
||||
def id(self) -> NodeId: # pragma: no cover
|
||||
"""The structured (non-string) form of :attr:`nodeid`.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return self._id
|
||||
|
||||
def setup(self) -> None:
|
||||
pass
|
||||
|
||||
def teardown(self) -> None:
|
||||
pass
|
||||
|
||||
def iter_parents(self) -> Iterator[Node]:
|
||||
"""Iterate over all parent collectors starting from and including self
|
||||
up to the root of the collection tree.
|
||||
|
||||
.. versionadded:: 8.1
|
||||
"""
|
||||
parent: Node | None = self
|
||||
while parent is not None:
|
||||
yield parent
|
||||
parent = parent.parent
|
||||
|
||||
def listchain(self) -> list[Node]:
|
||||
"""Return a list of all parent collectors starting from the root of the
|
||||
collection tree down to and including self."""
|
||||
chain = []
|
||||
item: Node | None = self
|
||||
while item is not None:
|
||||
chain.append(item)
|
||||
item = item.parent
|
||||
chain.reverse()
|
||||
return chain
|
||||
|
||||
def add_marker(self, marker: str | MarkDecorator, append: bool = True) -> None:
|
||||
"""Dynamically add a marker object to the node.
|
||||
|
||||
:param marker:
|
||||
The marker.
|
||||
:param append:
|
||||
Whether to append the marker, or prepend it.
|
||||
"""
|
||||
from _pytest.mark import MARK_GEN
|
||||
|
||||
if isinstance(marker, MarkDecorator):
|
||||
marker_ = marker
|
||||
elif isinstance(marker, str):
|
||||
marker_ = getattr(MARK_GEN, marker)
|
||||
else:
|
||||
raise ValueError("is not a string or pytest.mark.* Marker")
|
||||
self.keywords[marker_.name] = marker_.mark
|
||||
if append:
|
||||
self.own_markers.append(marker_.mark)
|
||||
else:
|
||||
self.own_markers.insert(0, marker_.mark)
|
||||
|
||||
def iter_markers(self, name: str | None = None) -> Iterator[Mark]:
|
||||
"""Iterate over all markers of the node.
|
||||
|
||||
:param name: If given, filter the results by the name attribute.
|
||||
:returns: An iterator of the markers of the node.
|
||||
"""
|
||||
return (x[1] for x in self.iter_markers_with_node(name=name))
|
||||
|
||||
def iter_markers_with_node(
|
||||
self, name: str | None = None
|
||||
) -> Iterator[tuple[Node, Mark]]:
|
||||
"""Iterate over all markers of the node.
|
||||
|
||||
:param name: If given, filter the results by the name attribute.
|
||||
:returns: An iterator of (node, mark) tuples.
|
||||
"""
|
||||
for node in self.iter_parents():
|
||||
for mark in node.own_markers:
|
||||
if name is None or getattr(mark, "name", None) == name:
|
||||
yield node, mark
|
||||
|
||||
@overload
|
||||
def get_closest_marker(self, name: str) -> Mark | None: ...
|
||||
|
||||
@overload
|
||||
def get_closest_marker(self, name: str, default: Mark | MarkDecorator) -> Mark: ...
|
||||
|
||||
def get_closest_marker(
|
||||
self, name: str, default: Mark | MarkDecorator | None = None
|
||||
) -> Mark | None:
|
||||
"""Return the first marker matching the name, from closest (for
|
||||
example function) to farther level (for example module level).
|
||||
|
||||
:param default:
|
||||
Fallback return value if no marker was found. A
|
||||
:class:`~pytest.MarkDecorator` such as ``pytest.mark.foo(1)`` is
|
||||
also accepted, in which case its :class:`~pytest.Mark` is returned.
|
||||
:param name: Name to filter by.
|
||||
"""
|
||||
if isinstance(default, MarkDecorator):
|
||||
default = default.mark
|
||||
return next(self.iter_markers(name=name), default)
|
||||
|
||||
def listextrakeywords(self) -> set[str]:
|
||||
"""Return a set of all extra keywords in self and any parents."""
|
||||
extra_keywords: set[str] = set()
|
||||
for item in self.listchain():
|
||||
extra_keywords.update(item.extra_keyword_matches)
|
||||
return extra_keywords
|
||||
|
||||
def listnames(self) -> list[str]:
|
||||
return [x.name for x in self.listchain()]
|
||||
|
||||
def addfinalizer(self, fin: Callable[[], object]) -> None:
|
||||
"""Register a function to be called without arguments when this node is
|
||||
finalized.
|
||||
|
||||
This method can only be called when this node is active
|
||||
in a setup chain, for example during self.setup().
|
||||
"""
|
||||
self.session._setupstate.addfinalizer(fin, self)
|
||||
|
||||
def getparent(self, cls: type[_NodeType]) -> _NodeType | None:
|
||||
"""Get the closest parent node (including self) which is an instance of
|
||||
the given class.
|
||||
|
||||
:param cls: The node class to search for.
|
||||
:returns: The node, if found.
|
||||
"""
|
||||
for node in self.iter_parents():
|
||||
if isinstance(node, cls):
|
||||
return node
|
||||
return None
|
||||
|
||||
def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
|
||||
return excinfo.traceback
|
||||
|
||||
def _repr_failure_py(
|
||||
self,
|
||||
excinfo: ExceptionInfo[BaseException],
|
||||
style: TracebackStyle | None = None,
|
||||
) -> TerminalRepr:
|
||||
from _pytest.fixtures import FixtureLookupError
|
||||
|
||||
if isinstance(excinfo.value, ConftestImportFailure):
|
||||
excinfo = ExceptionInfo.from_exception(excinfo.value.cause)
|
||||
if isinstance(excinfo.value, fail.Exception):
|
||||
if not excinfo.value.pytrace:
|
||||
style = "value"
|
||||
if isinstance(excinfo.value, FixtureLookupError):
|
||||
return excinfo.value.formatrepr()
|
||||
|
||||
tbfilter: bool | Callable[[ExceptionInfo[BaseException]], Traceback]
|
||||
if self.config.getoption("fulltrace", False):
|
||||
style = "long"
|
||||
tbfilter = False
|
||||
else:
|
||||
tbfilter = self._traceback_filter
|
||||
if style == "auto":
|
||||
style = "long"
|
||||
# XXX should excinfo.getrepr record all data and toterminal() process it?
|
||||
if style is None:
|
||||
if self.config.getoption("tbstyle", "auto") == "short":
|
||||
style = "short"
|
||||
else:
|
||||
style = "long"
|
||||
|
||||
if self.config.get_verbosity() > 1:
|
||||
truncate_locals = False
|
||||
else:
|
||||
truncate_locals = True
|
||||
|
||||
truncate_args = False if self.config.get_verbosity() > 2 else True
|
||||
|
||||
# excinfo.getrepr() formats paths relative to the CWD if `abspath` is False.
|
||||
# It is possible for a fixture/test to change the CWD while this code runs, which
|
||||
# would then result in the user seeing confusing paths in the failure message.
|
||||
# To fix this, if the CWD changed, always display the full absolute path.
|
||||
# It will be better to just always display paths relative to invocation_dir, but
|
||||
# this requires a lot of plumbing (#6428).
|
||||
try:
|
||||
abspath = Path(os.getcwd()) != self.config.invocation_params.dir
|
||||
except OSError:
|
||||
abspath = True
|
||||
|
||||
return excinfo.getrepr(
|
||||
funcargs=True,
|
||||
abspath=abspath,
|
||||
showlocals=self.config.getoption("showlocals", False),
|
||||
style=style,
|
||||
tbfilter=tbfilter,
|
||||
truncate_locals=truncate_locals,
|
||||
truncate_args=truncate_args,
|
||||
)
|
||||
|
||||
def repr_failure(
|
||||
self,
|
||||
excinfo: ExceptionInfo[BaseException],
|
||||
style: TracebackStyle | None = None,
|
||||
) -> str | TerminalRepr:
|
||||
"""Return a representation of a collection or test failure.
|
||||
|
||||
.. seealso:: :ref:`non-python tests`
|
||||
|
||||
:param excinfo: Exception information for the failure.
|
||||
"""
|
||||
return self._repr_failure_py(excinfo, style)
|
||||
|
||||
|
||||
def get_fslocation_from_item(node: Node) -> tuple[str | Path, int | None]:
|
||||
"""Try to extract the actual location from a node, depending on available attributes:
|
||||
|
||||
* "location": a pair (path, lineno)
|
||||
* "obj": a Python object that the node wraps.
|
||||
* "path": just a path
|
||||
|
||||
:rtype: A tuple of (str|Path, int) with filename and 0-based line number.
|
||||
"""
|
||||
# See Item.location.
|
||||
location: tuple[str, int | None, str] | None = getattr(node, "location", None)
|
||||
if location is not None:
|
||||
return location[:2]
|
||||
obj = getattr(node, "obj", None)
|
||||
if obj is not None:
|
||||
return getfslineno(obj)
|
||||
return getattr(node, "path", "unknown location"), -1
|
||||
|
||||
|
||||
class Collector(Node, abc.ABC):
|
||||
"""Base class of all collectors.
|
||||
|
||||
Collector create children through `collect()` and thus iteratively build
|
||||
the collection tree.
|
||||
"""
|
||||
|
||||
class CollectError(Exception):
|
||||
"""An error during collection, contains a custom message."""
|
||||
|
||||
@abc.abstractmethod
|
||||
def collect(self) -> Iterable[Item | Collector]:
|
||||
"""Collect children (items and collectors) for this collector."""
|
||||
raise NotImplementedError("abstract")
|
||||
|
||||
# TODO: This omits the style= parameter which breaks Liskov Substitution.
|
||||
def repr_failure( # type: ignore[override]
|
||||
self, excinfo: ExceptionInfo[BaseException]
|
||||
) -> str | TerminalRepr:
|
||||
"""Return a representation of a collection failure.
|
||||
|
||||
:param excinfo: Exception information for the failure.
|
||||
"""
|
||||
if isinstance(excinfo.value, self.CollectError) and not self.config.getoption(
|
||||
"fulltrace", False
|
||||
):
|
||||
exc = excinfo.value
|
||||
return str(exc.args[0])
|
||||
|
||||
# Respect explicit tbstyle option, but default to "short"
|
||||
# (_repr_failure_py uses "long" with "fulltrace" option always).
|
||||
tbstyle = self.config.getoption("tbstyle", "auto")
|
||||
if tbstyle == "auto":
|
||||
tbstyle = "short"
|
||||
|
||||
return self._repr_failure_py(excinfo, style=tbstyle)
|
||||
|
||||
def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
|
||||
if hasattr(self, "path"):
|
||||
traceback = excinfo.traceback
|
||||
ntraceback = traceback.cut(path=self.path)
|
||||
if ntraceback == traceback:
|
||||
ntraceback = ntraceback.cut(excludepath=tracebackcutdir)
|
||||
return ntraceback.filter(excinfo)
|
||||
return excinfo.traceback
|
||||
|
||||
|
||||
@lru_cache(maxsize=1000)
|
||||
def _check_initialpaths_for_relpath(
|
||||
initial_paths: frozenset[Path], path: Path
|
||||
) -> str | None:
|
||||
if path in initial_paths:
|
||||
return ""
|
||||
|
||||
for parent in path.parents:
|
||||
if parent in initial_paths:
|
||||
return str(path.relative_to(parent))
|
||||
|
||||
return None
|
||||
|
||||
|
||||
class FSCollector(Collector, abc.ABC):
|
||||
"""Base class for filesystem collectors."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
fspath: None = None,
|
||||
path_or_parent: Path | Node | None = None,
|
||||
path: Path | None = None,
|
||||
name: str | None = None,
|
||||
parent: Collector | None = None,
|
||||
config: Config | None = None,
|
||||
session: Session | None = None,
|
||||
nodeid: NodeId | None = None,
|
||||
) -> None:
|
||||
if path_or_parent:
|
||||
if isinstance(path_or_parent, Node):
|
||||
assert parent is None
|
||||
parent = cast(FSCollector, path_or_parent)
|
||||
elif isinstance(path_or_parent, Path):
|
||||
assert path is None
|
||||
path = path_or_parent
|
||||
assert path is not None
|
||||
|
||||
if name is None:
|
||||
name = path.name
|
||||
if parent is not None and parent.path != path:
|
||||
try:
|
||||
rel = path.relative_to(parent.path)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
name = str(rel)
|
||||
name = norm_sep(name)
|
||||
self.path = path
|
||||
|
||||
if session is None:
|
||||
assert parent is not None
|
||||
session = parent.session
|
||||
|
||||
if nodeid is None:
|
||||
try:
|
||||
path_str: str | None = str(
|
||||
self.path.relative_to(session.config.rootpath)
|
||||
)
|
||||
except ValueError:
|
||||
path_str = _check_initialpaths_for_relpath(session._initialpaths, path)
|
||||
|
||||
if path_str:
|
||||
path_str = norm_sep(path_str)
|
||||
if path_str is not None:
|
||||
nodeid = NodeId(path=path_str)
|
||||
|
||||
super().__init__(
|
||||
name=name,
|
||||
parent=parent,
|
||||
config=config,
|
||||
session=session,
|
||||
nodeid=nodeid,
|
||||
path=path,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_parent(
|
||||
cls,
|
||||
parent,
|
||||
*,
|
||||
fspath: None = None,
|
||||
path: Path | None = None,
|
||||
**kw,
|
||||
) -> Self:
|
||||
"""The public constructor."""
|
||||
return super().from_parent(parent=parent, fspath=fspath, path=path, **kw)
|
||||
|
||||
|
||||
class File(FSCollector, abc.ABC):
|
||||
"""Base class for collecting tests from a file.
|
||||
|
||||
:ref:`non-python tests`.
|
||||
"""
|
||||
|
||||
|
||||
class Directory(FSCollector, abc.ABC):
|
||||
"""Base class for collecting files from a directory.
|
||||
|
||||
A basic directory collector does the following: goes over the files and
|
||||
sub-directories in the directory and creates collectors for them by calling
|
||||
the hooks :hook:`pytest_collect_directory` and :hook:`pytest_collect_file`,
|
||||
after checking that they are not ignored using
|
||||
:hook:`pytest_ignore_collect`.
|
||||
|
||||
The default directory collectors are :class:`~pytest.Dir` and
|
||||
:class:`~pytest.Package`.
|
||||
|
||||
.. versionadded:: 8.0
|
||||
|
||||
:ref:`custom directory collectors`.
|
||||
"""
|
||||
|
||||
|
||||
class Item(Node, abc.ABC):
|
||||
"""Base class of all test invocation items.
|
||||
|
||||
Note that for a single function there might be multiple test invocation items.
|
||||
"""
|
||||
|
||||
nextitem = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name,
|
||||
parent=None,
|
||||
config: Config | None = None,
|
||||
session: Session | None = None,
|
||||
nodeid: NodeId | str | None = None,
|
||||
**kw,
|
||||
) -> None:
|
||||
# The first two arguments are intentionally passed positionally,
|
||||
# to keep plugins who define a node type which inherits from
|
||||
# (pytest.Item, pytest.File) working (see issue #8435).
|
||||
# They can be made kwargs when the deprecation above is done.
|
||||
super().__init__(
|
||||
name,
|
||||
parent,
|
||||
config=config,
|
||||
session=session,
|
||||
nodeid=nodeid,
|
||||
**kw,
|
||||
)
|
||||
self._report_sections: list[tuple[str, str, str]] = []
|
||||
|
||||
#: A list of tuples (name, value) that holds user defined properties
|
||||
#: for this test.
|
||||
self.user_properties: list[tuple[str, object]] = []
|
||||
|
||||
self._check_item_and_collector_diamond_inheritance()
|
||||
|
||||
def _check_item_and_collector_diamond_inheritance(self) -> None:
|
||||
"""
|
||||
Check if the current type inherits from both File and Collector
|
||||
at the same time, emitting a warning accordingly (#8447).
|
||||
"""
|
||||
cls = type(self)
|
||||
|
||||
# We inject an attribute in the type to avoid issuing this warning
|
||||
# for the same class more than once, which is not helpful.
|
||||
# It is a hack, but was deemed acceptable in order to avoid
|
||||
# flooding the user in the common case.
|
||||
attr_name = "_pytest_diamond_inheritance_warning_shown"
|
||||
if getattr(cls, attr_name, False):
|
||||
return
|
||||
setattr(cls, attr_name, True)
|
||||
|
||||
problems = ", ".join(
|
||||
base.__name__ for base in cls.__bases__ if issubclass(base, Collector)
|
||||
)
|
||||
if problems:
|
||||
warnings.warn(
|
||||
f"{cls.__name__} is an Item subclass and should not be a collector, "
|
||||
f"however its bases {problems} are collectors.\n"
|
||||
"Please split the Collectors and the Item into separate node types.\n"
|
||||
"Pytest Doc example: https://docs.pytest.org/en/latest/example/nonpython.html\n"
|
||||
"example pull request on a plugin: https://github.com/asmeurer/pytest-flakes/pull/40/",
|
||||
PytestWarning,
|
||||
)
|
||||
|
||||
@abc.abstractmethod
|
||||
def runtest(self) -> None:
|
||||
"""Run the test case for this item.
|
||||
|
||||
Must be implemented by subclasses.
|
||||
|
||||
.. seealso:: :ref:`non-python tests`
|
||||
"""
|
||||
raise NotImplementedError("runtest must be implemented by Item subclass")
|
||||
|
||||
def add_report_section(self, when: str, key: str, content: str) -> None:
|
||||
"""Add a new report section, similar to what's done internally to add
|
||||
stdout and stderr captured output::
|
||||
|
||||
item.add_report_section("call", "stdout", "report section contents")
|
||||
|
||||
:param str when:
|
||||
One of the possible capture states, ``"setup"``, ``"call"``, ``"teardown"``.
|
||||
:param str key:
|
||||
Name of the section, can be customized at will. Pytest uses ``"stdout"`` and
|
||||
``"stderr"`` internally.
|
||||
:param str content:
|
||||
The full contents as a string.
|
||||
"""
|
||||
if content:
|
||||
self._report_sections.append((when, key, content))
|
||||
|
||||
def reportinfo(self) -> tuple[os.PathLike[str] | str, int | None, str]:
|
||||
"""Get location information for this item for test reports.
|
||||
|
||||
Returns a tuple with three elements:
|
||||
|
||||
- The path of the test (default ``self.path``)
|
||||
- The 0-based line number of the test (default ``None``)
|
||||
- A name of the test to be shown (default ``""``)
|
||||
|
||||
.. seealso:: :ref:`non-python tests`
|
||||
"""
|
||||
return self.path, None, ""
|
||||
|
||||
@cached_property
|
||||
def location(self) -> tuple[str, int | None, str]:
|
||||
"""
|
||||
Returns a tuple of ``(relfspath, lineno, testname)`` for this item
|
||||
where ``relfspath`` is file path relative to ``config.rootpath``
|
||||
and lineno is a 0-based line number.
|
||||
"""
|
||||
location = self.reportinfo()
|
||||
path = absolutepath(location[0])
|
||||
relfspath = self.session._node_location_to_relpath(path)
|
||||
assert type(location[2]) is str
|
||||
return (relfspath, location[1], location[2])
|
||||
@@ -0,0 +1,286 @@
|
||||
"""Exception classes and constants handling test outcomes as well as
|
||||
functions creating them."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing import ClassVar
|
||||
from typing import NoReturn
|
||||
|
||||
|
||||
class OutcomeException(BaseException):
|
||||
"""OutcomeException and its subclass instances indicate and contain info
|
||||
about test and collection outcomes."""
|
||||
|
||||
def __init__(self, msg: str | None = None, pytrace: bool = True) -> None:
|
||||
if msg is not None and not isinstance(msg, str):
|
||||
error_msg = ( # type: ignore[unreachable]
|
||||
"{} expected string as 'msg' parameter, got '{}' instead.\n"
|
||||
"Perhaps you meant to use a mark?"
|
||||
)
|
||||
raise TypeError(error_msg.format(type(self).__name__, type(msg).__name__))
|
||||
super().__init__(msg)
|
||||
self.msg = msg
|
||||
self.pytrace = pytrace
|
||||
|
||||
def __repr__(self) -> str:
|
||||
if self.msg is not None:
|
||||
return self.msg
|
||||
return f"<{self.__class__.__name__} instance>"
|
||||
|
||||
__str__ = __repr__
|
||||
|
||||
|
||||
TEST_OUTCOME = (OutcomeException, Exception)
|
||||
|
||||
|
||||
class Skipped(OutcomeException):
|
||||
# XXX hackish: on 3k we fake to live in the builtins
|
||||
# in order to have Skipped exception printing shorter/nicer
|
||||
__module__ = "builtins"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
msg: str | None = None,
|
||||
pytrace: bool = True,
|
||||
allow_module_level: bool = False,
|
||||
*,
|
||||
_use_item_location: bool = False,
|
||||
) -> None:
|
||||
super().__init__(msg=msg, pytrace=pytrace)
|
||||
self.allow_module_level = allow_module_level
|
||||
# If true, the skip location is reported as the item's location,
|
||||
# instead of the place that raises the exception/calls skip().
|
||||
self._use_item_location = _use_item_location
|
||||
|
||||
|
||||
class Failed(OutcomeException):
|
||||
"""Raised from an explicit call to pytest.fail()."""
|
||||
|
||||
__module__ = "builtins"
|
||||
|
||||
|
||||
class Exit(Exception):
|
||||
"""Raised for immediate program exits (no tracebacks/summaries)."""
|
||||
|
||||
def __init__(
|
||||
self, msg: str = "unknown reason", returncode: int | None = None
|
||||
) -> None:
|
||||
self.msg = msg
|
||||
self.returncode = returncode
|
||||
super().__init__(msg)
|
||||
|
||||
|
||||
class XFailed(Failed):
|
||||
"""Raised from an explicit call to pytest.xfail()."""
|
||||
|
||||
|
||||
class _Exit:
|
||||
"""Exit testing process.
|
||||
|
||||
:param reason:
|
||||
The message to show as the reason for exiting pytest. reason has a default value
|
||||
only because `msg` is deprecated.
|
||||
|
||||
:param returncode:
|
||||
Return code to be used when exiting pytest. None means the same as ``0`` (no error),
|
||||
same as :func:`sys.exit`.
|
||||
|
||||
:raises pytest.exit.Exception:
|
||||
The exception that is raised.
|
||||
"""
|
||||
|
||||
Exception: ClassVar[type[Exit]] = Exit
|
||||
|
||||
def __call__(self, reason: str = "", returncode: int | None = None) -> NoReturn:
|
||||
__tracebackhide__ = True
|
||||
raise Exit(msg=reason, returncode=returncode)
|
||||
|
||||
|
||||
exit: _Exit = _Exit()
|
||||
|
||||
|
||||
class _Skip:
|
||||
"""Skip an executing test with the given message.
|
||||
|
||||
This function should be called only during testing (setup, call or teardown) or
|
||||
during collection by using the ``allow_module_level`` flag. This function can
|
||||
be called in doctests as well.
|
||||
|
||||
:param reason:
|
||||
The message to show the user as reason for the skip.
|
||||
|
||||
:param allow_module_level:
|
||||
Allows this function to be called at module level.
|
||||
Raising the skip exception at module level will stop
|
||||
the execution of the module and prevent the collection of all tests in the module,
|
||||
even those defined before the `skip` call.
|
||||
|
||||
Defaults to False.
|
||||
|
||||
:raises pytest.skip.Exception:
|
||||
The exception that is raised.
|
||||
|
||||
.. note::
|
||||
It is better to use the :ref:`pytest.mark.skipif ref` marker when
|
||||
possible to declare a test to be skipped under certain conditions
|
||||
like mismatching platforms or dependencies.
|
||||
Similarly, use the ``# doctest: +SKIP`` directive (see :py:data:`doctest.SKIP`)
|
||||
to skip a doctest statically.
|
||||
"""
|
||||
|
||||
Exception: ClassVar[type[Skipped]] = Skipped
|
||||
|
||||
def __call__(self, reason: str = "", allow_module_level: bool = False) -> NoReturn:
|
||||
__tracebackhide__ = True
|
||||
raise Skipped(msg=reason, allow_module_level=allow_module_level)
|
||||
|
||||
|
||||
skip: _Skip = _Skip()
|
||||
|
||||
|
||||
class _Fail:
|
||||
"""Explicitly fail an executing test with the given message.
|
||||
|
||||
:param reason:
|
||||
The message to show the user as reason for the failure.
|
||||
|
||||
:param pytrace:
|
||||
If False, msg represents the full failure information and no
|
||||
python traceback will be reported.
|
||||
|
||||
:raises pytest.fail.Exception:
|
||||
The exception that is raised.
|
||||
"""
|
||||
|
||||
Exception: ClassVar[type[Failed]] = Failed
|
||||
|
||||
def __call__(self, reason: str = "", pytrace: bool = True) -> NoReturn:
|
||||
__tracebackhide__ = True
|
||||
raise Failed(msg=reason, pytrace=pytrace)
|
||||
|
||||
|
||||
fail: _Fail = _Fail()
|
||||
|
||||
|
||||
class _XFail:
|
||||
"""Imperatively xfail an executing test or setup function with the given reason.
|
||||
|
||||
This function should be called only during testing (setup, call or teardown).
|
||||
|
||||
No other code is executed after using ``xfail()`` (it is implemented
|
||||
internally by raising an exception).
|
||||
|
||||
:param reason:
|
||||
The message to show the user as reason for the xfail.
|
||||
|
||||
.. note::
|
||||
It is better to use the :ref:`pytest.mark.xfail ref` marker when
|
||||
possible to declare a test to be xfailed under certain conditions
|
||||
like known bugs or missing features.
|
||||
|
||||
:raises pytest.xfail.Exception:
|
||||
The exception that is raised.
|
||||
"""
|
||||
|
||||
Exception: ClassVar[type[XFailed]] = XFailed
|
||||
|
||||
def __call__(self, reason: str = "") -> NoReturn:
|
||||
__tracebackhide__ = True
|
||||
raise XFailed(msg=reason)
|
||||
|
||||
|
||||
xfail: _XFail = _XFail()
|
||||
|
||||
|
||||
def importorskip(
|
||||
modname: str,
|
||||
minversion: str | None = None,
|
||||
reason: str | None = None,
|
||||
*,
|
||||
exc_type: type[ImportError] | None = None,
|
||||
) -> Any:
|
||||
"""Import and return the requested module ``modname``, or skip the
|
||||
current test if the module cannot be imported.
|
||||
|
||||
:param modname:
|
||||
The name of the module to import.
|
||||
:param minversion:
|
||||
If given, the imported module's ``__version__`` attribute must be at
|
||||
least this minimal version, otherwise the test is still skipped.
|
||||
:param reason:
|
||||
If given, this reason is shown as the message when the module cannot
|
||||
be imported.
|
||||
:param exc_type:
|
||||
The exception that should be captured in order to skip modules.
|
||||
Must be :py:class:`ImportError` or a subclass.
|
||||
|
||||
Defaults to :class:`ModuleNotFoundError` when not given, which means
|
||||
the module must be missing for the test to be skipped.
|
||||
Pass ``exc_type=ImportError`` to also skip modules that raise
|
||||
:class:`ImportError` during import.
|
||||
|
||||
See :ref:`import-or-skip-import-error` for details.
|
||||
|
||||
|
||||
:returns:
|
||||
The imported module. This should be assigned to its canonical name.
|
||||
|
||||
:raises pytest.skip.Exception:
|
||||
If the module cannot be imported.
|
||||
|
||||
Example::
|
||||
|
||||
docutils = pytest.importorskip("docutils")
|
||||
|
||||
.. versionadded:: 8.2
|
||||
|
||||
The ``exc_type`` parameter.
|
||||
|
||||
.. versionchanged:: 9.1
|
||||
|
||||
The default for ``exc_type`` is now :class:`ModuleNotFoundError`.
|
||||
"""
|
||||
import warnings
|
||||
|
||||
__tracebackhide__ = True
|
||||
compile(modname, "", "eval") # to catch syntaxerrors
|
||||
|
||||
# Keep the public signature compatible while using the pytest 9.1 default behavior.
|
||||
if exc_type is None:
|
||||
exc_type = ModuleNotFoundError
|
||||
|
||||
skipped: Skipped | None = None
|
||||
|
||||
with warnings.catch_warnings():
|
||||
# Make sure to ignore ImportWarnings that might happen because
|
||||
# of existing directories with the same name we're trying to
|
||||
# import but without a __init__.py file.
|
||||
warnings.simplefilter("ignore")
|
||||
|
||||
try:
|
||||
importlib.import_module(modname)
|
||||
except exc_type as exc:
|
||||
# Do not raise or issue warnings inside the catch_warnings() block.
|
||||
if reason is None:
|
||||
reason = f"could not import {modname!r}: {exc}"
|
||||
skipped = Skipped(reason, allow_module_level=True)
|
||||
if skipped:
|
||||
raise skipped
|
||||
|
||||
mod = sys.modules[modname]
|
||||
if minversion is None:
|
||||
return mod
|
||||
verattr = getattr(mod, "__version__", None)
|
||||
if minversion is not None:
|
||||
# Imported lazily to improve start-up time.
|
||||
from packaging.version import Version
|
||||
|
||||
if verattr is None or Version(verattr) < Version(minversion):
|
||||
raise Skipped(
|
||||
f"module {modname!r} has __version__ {verattr!r}, required is: {minversion!r}",
|
||||
allow_module_level=True,
|
||||
)
|
||||
return mod
|
||||
@@ -0,0 +1,121 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Submit failure or test session information to a pastebin service."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from io import StringIO
|
||||
import tempfile
|
||||
from typing import IO
|
||||
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import create_terminal_writer
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.deprecated import PASTEBIN
|
||||
from _pytest.stash import StashKey
|
||||
from _pytest.terminal import TerminalReporter
|
||||
import pytest
|
||||
|
||||
|
||||
pastebinfile_key = StashKey[IO[bytes]]()
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("terminal reporting")
|
||||
group.addoption(
|
||||
"--pastebin",
|
||||
metavar="mode",
|
||||
action="store",
|
||||
dest="pastebin",
|
||||
default=None,
|
||||
choices=["failed", "all"],
|
||||
help="Send failed|all info to bpaste.net pastebin service",
|
||||
)
|
||||
|
||||
|
||||
@pytest.hookimpl(trylast=True)
|
||||
def pytest_configure(config: Config) -> None:
|
||||
if config.option.pastebin:
|
||||
config.issue_config_time_warning(PASTEBIN.format(), 2)
|
||||
|
||||
if config.option.pastebin == "all":
|
||||
tr = config.pluginmanager.getplugin("terminalreporter")
|
||||
# If no terminal reporter plugin is present, nothing we can do here;
|
||||
# this can happen when this function executes in a worker node
|
||||
# when using pytest-xdist, for example.
|
||||
if tr is not None:
|
||||
# pastebin file will be UTF-8 encoded binary file.
|
||||
config.stash[pastebinfile_key] = tempfile.TemporaryFile("w+b")
|
||||
oldwrite = tr._tw.write
|
||||
|
||||
def tee_write(s, **kwargs):
|
||||
oldwrite(s, **kwargs)
|
||||
if isinstance(s, str):
|
||||
s = s.encode("utf-8")
|
||||
config.stash[pastebinfile_key].write(s)
|
||||
|
||||
tr._tw.write = tee_write
|
||||
|
||||
|
||||
def pytest_unconfigure(config: Config) -> None:
|
||||
if pastebinfile_key in config.stash:
|
||||
pastebinfile = config.stash[pastebinfile_key]
|
||||
# Get terminal contents and delete file.
|
||||
pastebinfile.seek(0)
|
||||
sessionlog = pastebinfile.read()
|
||||
pastebinfile.close()
|
||||
del config.stash[pastebinfile_key]
|
||||
# Undo our patching in the terminal reporter.
|
||||
tr = config.pluginmanager.getplugin("terminalreporter")
|
||||
del tr._tw.__dict__["write"]
|
||||
# Write summary.
|
||||
tr.write_sep("=", "Sending information to Paste Service")
|
||||
pastebinurl = create_new_paste(sessionlog)
|
||||
tr.write_line(f"pastebin session-log: {pastebinurl}\n")
|
||||
|
||||
|
||||
def create_new_paste(contents: str | bytes) -> str:
|
||||
"""Create a new paste using the bpaste.net service.
|
||||
|
||||
:contents: Paste contents string.
|
||||
:returns: URL to the pasted contents, or an error message.
|
||||
"""
|
||||
import re
|
||||
from urllib.error import HTTPError
|
||||
from urllib.parse import urlencode
|
||||
from urllib.request import urlopen
|
||||
|
||||
params = {"code": contents, "lexer": "text", "expiry": "1week"}
|
||||
url = "https://bpa.st"
|
||||
try:
|
||||
response: str = (
|
||||
urlopen(url, data=urlencode(params).encode("ascii")).read().decode("utf-8")
|
||||
)
|
||||
except HTTPError as e:
|
||||
with e: # HTTPErrors are also http responses that must be closed!
|
||||
return f"bad response: {e}"
|
||||
except OSError as e: # eg urllib.error.URLError
|
||||
return f"bad response: {e}"
|
||||
m = re.search(r'href="/raw/(\w+)"', response)
|
||||
if m:
|
||||
return f"{url}/show/{m.group(1)}"
|
||||
else:
|
||||
return "bad response: invalid format ('" + response + "')"
|
||||
|
||||
|
||||
def pytest_terminal_summary(terminalreporter: TerminalReporter) -> None:
|
||||
if terminalreporter.config.option.pastebin != "failed":
|
||||
return
|
||||
if "failed" in terminalreporter.stats:
|
||||
terminalreporter.write_sep("=", "Sending information to Paste Service")
|
||||
for rep in terminalreporter.stats["failed"]:
|
||||
try:
|
||||
msg = rep.longrepr.reprtraceback.reprentries[-1].reprfileloc
|
||||
except AttributeError:
|
||||
msg = terminalreporter._getfailureheadline(rep)
|
||||
file = StringIO()
|
||||
tw = create_terminal_writer(terminalreporter.config, file)
|
||||
rep.toterminal(tw)
|
||||
s = file.getvalue()
|
||||
assert len(s)
|
||||
pastebinurl = create_new_paste(s)
|
||||
terminalreporter.write_line(f"{msg} --> {pastebinurl}")
|
||||
File diff suppressed because it is too large.
Load diff
Whitespace-only changes.
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,74 @@
|
||||
"""Helper plugin for pytester; should not be loaded on its own."""
|
||||
|
||||
# This plugin contains assertions used by pytester. pytester cannot
|
||||
# contain them itself, since it is imported by the `pytest` module,
|
||||
# hence cannot be subject to assertion rewriting, which requires a
|
||||
# module to not be already imported.
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
from _pytest.reports import CollectReport
|
||||
from _pytest.reports import TestReport
|
||||
|
||||
|
||||
def assertoutcome(
|
||||
outcomes: tuple[
|
||||
Sequence[TestReport],
|
||||
Sequence[CollectReport | TestReport],
|
||||
Sequence[CollectReport | TestReport],
|
||||
],
|
||||
passed: int = 0,
|
||||
skipped: int = 0,
|
||||
failed: int = 0,
|
||||
) -> None:
|
||||
__tracebackhide__ = True
|
||||
|
||||
realpassed, realskipped, realfailed = outcomes
|
||||
obtained = {
|
||||
"failed": len(realfailed),
|
||||
"passed": len(realpassed),
|
||||
"skipped": len(realskipped),
|
||||
}
|
||||
expected = {"failed": failed, "passed": passed, "skipped": skipped}
|
||||
assert obtained == expected, outcomes
|
||||
|
||||
|
||||
def assert_outcomes(
|
||||
outcomes: dict[str, int],
|
||||
passed: int = 0,
|
||||
skipped: int = 0,
|
||||
failed: int = 0,
|
||||
errors: int = 0,
|
||||
xpassed: int = 0,
|
||||
xfailed: int = 0,
|
||||
warnings: int | None = None,
|
||||
deselected: int | None = None,
|
||||
) -> None:
|
||||
"""Assert that the specified outcomes appear with the respective
|
||||
numbers (0 means it didn't occur) in the text output from a test run."""
|
||||
__tracebackhide__ = True
|
||||
|
||||
obtained = {
|
||||
"passed": outcomes.get("passed", 0),
|
||||
"skipped": outcomes.get("skipped", 0),
|
||||
"failed": outcomes.get("failed", 0),
|
||||
"errors": outcomes.get("errors", 0),
|
||||
"xpassed": outcomes.get("xpassed", 0),
|
||||
"xfailed": outcomes.get("xfailed", 0),
|
||||
}
|
||||
expected = {
|
||||
"passed": passed,
|
||||
"skipped": skipped,
|
||||
"failed": failed,
|
||||
"errors": errors,
|
||||
"xpassed": xpassed,
|
||||
"xfailed": xfailed,
|
||||
}
|
||||
if warnings is not None:
|
||||
obtained["warnings"] = outcomes.get("warnings", 0)
|
||||
expected["warnings"] = warnings
|
||||
if deselected is not None:
|
||||
obtained["deselected"] = outcomes.get("deselected", 0)
|
||||
expected["deselected"] = deselected
|
||||
assert obtained == expected
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,378 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Record warnings during test function execution."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Generator
|
||||
from collections.abc import Iterator
|
||||
from pprint import pformat
|
||||
import re
|
||||
from types import TracebackType
|
||||
from typing import Any
|
||||
from typing import final
|
||||
from typing import overload
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TypeVar
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import ParamSpec
|
||||
from typing_extensions import Self
|
||||
|
||||
P = ParamSpec("P")
|
||||
|
||||
import warnings
|
||||
|
||||
from _pytest.deprecated import check_ispytest
|
||||
from _pytest.fixtures import fixture
|
||||
from _pytest.outcomes import Exit
|
||||
from _pytest.outcomes import fail
|
||||
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
@fixture
|
||||
def recwarn() -> Generator[WarningsRecorder]:
|
||||
"""Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions.
|
||||
|
||||
See :ref:`warnings` for information on warning categories.
|
||||
"""
|
||||
wrec = WarningsRecorder(_ispytest=True)
|
||||
with wrec:
|
||||
warnings.simplefilter("default")
|
||||
yield wrec
|
||||
|
||||
|
||||
@overload
|
||||
def deprecated_call(
|
||||
*, match: str | re.Pattern[str] | None = ...
|
||||
) -> WarningsRecorder: ...
|
||||
|
||||
|
||||
@overload
|
||||
def deprecated_call(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: ...
|
||||
|
||||
|
||||
def deprecated_call(
|
||||
func: Callable[..., Any] | None = None, *args: Any, **kwargs: Any
|
||||
) -> WarningsRecorder | Any:
|
||||
"""Assert that code produces a ``DeprecationWarning`` or ``PendingDeprecationWarning`` or ``FutureWarning``.
|
||||
|
||||
This function can be used as a context manager::
|
||||
|
||||
>>> import warnings
|
||||
>>> def api_call_v2():
|
||||
... warnings.warn('use v3 of this api', DeprecationWarning)
|
||||
... return 200
|
||||
|
||||
>>> import pytest
|
||||
>>> with pytest.deprecated_call():
|
||||
... assert api_call_v2() == 200
|
||||
>>> with pytest.deprecated_call(match="^use v3 of this api$") as warning_messages:
|
||||
... assert api_call_v2() == 200
|
||||
|
||||
You may use the keyword argument ``match`` to assert
|
||||
that the warning matches a text or regex.
|
||||
|
||||
The return value is a list of :class:`warnings.WarningMessage` objects,
|
||||
one for each warning emitted
|
||||
(regardless of whether it is an ``expected_warning`` or not).
|
||||
"""
|
||||
__tracebackhide__ = True
|
||||
dep_warnings = (DeprecationWarning, PendingDeprecationWarning, FutureWarning)
|
||||
if func is None:
|
||||
return warns(dep_warnings, *args, **kwargs)
|
||||
|
||||
with warns(dep_warnings):
|
||||
return func(*args, **kwargs)
|
||||
|
||||
|
||||
@overload
|
||||
def warns(
|
||||
expected_warning: type[Warning] | tuple[type[Warning], ...] = ...,
|
||||
*,
|
||||
match: str | re.Pattern[str] | None = ...,
|
||||
) -> WarningsChecker: ...
|
||||
|
||||
|
||||
@overload
|
||||
def warns(
|
||||
expected_warning: type[Warning] | tuple[type[Warning], ...],
|
||||
func: Callable[P, T],
|
||||
*args: P.args,
|
||||
**kwargs: P.kwargs,
|
||||
) -> T: ...
|
||||
|
||||
|
||||
def warns(
|
||||
expected_warning: type[Warning] | tuple[type[Warning], ...] = Warning,
|
||||
func: Callable[..., object] | None = None,
|
||||
*args: Any,
|
||||
**kwargs: Any,
|
||||
) -> WarningsChecker | Any:
|
||||
r"""Assert that code raises a particular class of warning.
|
||||
|
||||
Specifically, the parameter ``expected_warning`` can be a warning class or tuple
|
||||
of warning classes, and the code inside the ``with`` block must issue at least one
|
||||
warning of that class or classes.
|
||||
|
||||
This helper produces a list of :class:`warnings.WarningMessage` objects, one for
|
||||
each warning emitted (regardless of whether it is an ``expected_warning`` or not).
|
||||
Since pytest 8.0, unmatched warnings are also re-emitted when the context closes.
|
||||
|
||||
This function should be used as a context manager::
|
||||
|
||||
>>> import pytest
|
||||
>>> with pytest.warns(RuntimeWarning):
|
||||
... warnings.warn("my warning", RuntimeWarning)
|
||||
|
||||
The ``match`` keyword argument can be used to assert
|
||||
that the warning matches a text or regex::
|
||||
|
||||
>>> with pytest.warns(UserWarning, match='must be 0 or None'):
|
||||
... warnings.warn("value must be 0 or None", UserWarning)
|
||||
|
||||
>>> with pytest.warns(UserWarning, match=r'must be \d+$'):
|
||||
... warnings.warn("value must be 42", UserWarning)
|
||||
|
||||
>>> with pytest.warns(UserWarning): # catch re-emitted warning
|
||||
... with pytest.warns(UserWarning, match=r'must be \d+$'):
|
||||
... warnings.warn("this is not here", UserWarning)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
Failed: Regex pattern did not match any of the 1 warnings emitted.
|
||||
Regex: ...
|
||||
Emitted warnings: ...UserWarning...
|
||||
|
||||
**Using with** ``pytest.mark.parametrize``
|
||||
|
||||
When using :ref:`pytest.mark.parametrize ref` it is possible to parametrize tests
|
||||
such that some runs raise a warning and others do not.
|
||||
|
||||
This could be achieved in the same way as with exceptions, see
|
||||
:ref:`parametrizing_conditional_raising` for an example.
|
||||
|
||||
"""
|
||||
__tracebackhide__ = True
|
||||
if func is None and not args:
|
||||
match: str | re.Pattern[str] | None = kwargs.pop("match", None)
|
||||
if kwargs:
|
||||
argnames = ", ".join(sorted(kwargs))
|
||||
raise TypeError(
|
||||
f"Unexpected keyword arguments passed to pytest.warns: {argnames}"
|
||||
"\nUse context-manager form instead?"
|
||||
)
|
||||
return WarningsChecker(expected_warning, match_expr=match, _ispytest=True)
|
||||
else:
|
||||
if not callable(func):
|
||||
raise TypeError(f"{func!r} object (type: {type(func)}) must be callable")
|
||||
with WarningsChecker(expected_warning, _ispytest=True):
|
||||
return func(*args, **kwargs)
|
||||
|
||||
|
||||
class WarningsRecorder(warnings.catch_warnings):
|
||||
"""A context manager to record raised warnings.
|
||||
|
||||
Each recorded warning is an instance of :class:`warnings.WarningMessage`.
|
||||
|
||||
Adapted from `warnings.catch_warnings`.
|
||||
|
||||
.. note::
|
||||
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
|
||||
differently; see :ref:`ensuring_function_triggers`.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, *, _ispytest: bool = False) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
super().__init__(record=True)
|
||||
self._entered = False
|
||||
self._list: list[warnings.WarningMessage] = []
|
||||
|
||||
@property
|
||||
def list(self) -> list[warnings.WarningMessage]:
|
||||
"""The list of recorded warnings."""
|
||||
return self._list
|
||||
|
||||
def __getitem__(self, i: int) -> warnings.WarningMessage:
|
||||
"""Get a recorded warning by index."""
|
||||
return self._list[i]
|
||||
|
||||
def __iter__(self) -> Iterator[warnings.WarningMessage]:
|
||||
"""Iterate through the recorded warnings."""
|
||||
return iter(self._list)
|
||||
|
||||
def __len__(self) -> int:
|
||||
"""The number of recorded warnings."""
|
||||
return len(self._list)
|
||||
|
||||
def pop(self, cls: type[Warning] = Warning) -> warnings.WarningMessage:
|
||||
"""Pop the first recorded warning which is an instance of ``cls``,
|
||||
but not an instance of a child class of any other match.
|
||||
Raises ``AssertionError`` if there is no match.
|
||||
"""
|
||||
best_idx: int | None = None
|
||||
for i, w in enumerate(self._list):
|
||||
if w.category == cls:
|
||||
return self._list.pop(i) # exact match, stop looking
|
||||
if issubclass(w.category, cls) and (
|
||||
best_idx is None
|
||||
or not issubclass(w.category, self._list[best_idx].category)
|
||||
):
|
||||
best_idx = i
|
||||
if best_idx is not None:
|
||||
return self._list.pop(best_idx)
|
||||
__tracebackhide__ = True
|
||||
raise AssertionError(f"{cls!r} not found in warning list")
|
||||
|
||||
def clear(self) -> None:
|
||||
"""Clear the list of recorded warnings."""
|
||||
self._list[:] = []
|
||||
|
||||
# Type ignored because we basically want the `catch_warnings` generic type
|
||||
# parameter to be ourselves but that is not possible(?).
|
||||
def __enter__(self) -> Self: # type: ignore[override]
|
||||
if self._entered:
|
||||
__tracebackhide__ = True
|
||||
raise RuntimeError(f"Cannot enter {self!r} twice")
|
||||
_list = super().__enter__()
|
||||
# record=True means it's None.
|
||||
assert _list is not None
|
||||
self._list = _list
|
||||
warnings.simplefilter("always")
|
||||
return self
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: TracebackType | None,
|
||||
) -> None:
|
||||
if not self._entered:
|
||||
__tracebackhide__ = True
|
||||
raise RuntimeError(f"Cannot exit {self!r} without entering first")
|
||||
|
||||
super().__exit__(exc_type, exc_val, exc_tb)
|
||||
|
||||
# Built-in catch_warnings does not reset entered state so we do it
|
||||
# manually here for this context manager to become reusable.
|
||||
self._entered = False
|
||||
|
||||
|
||||
@final
|
||||
class WarningsChecker(WarningsRecorder):
|
||||
def __init__(
|
||||
self,
|
||||
expected_warning: type[Warning] | tuple[type[Warning], ...] = Warning,
|
||||
match_expr: str | re.Pattern[str] | None = None,
|
||||
*,
|
||||
_ispytest: bool = False,
|
||||
) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
super().__init__(_ispytest=True)
|
||||
|
||||
msg = "exceptions must be derived from Warning, not %s"
|
||||
if isinstance(expected_warning, tuple):
|
||||
for exc in expected_warning:
|
||||
if not issubclass(exc, Warning):
|
||||
raise TypeError(msg % type(exc))
|
||||
expected_warning_tup = expected_warning
|
||||
elif isinstance(expected_warning, type) and issubclass(
|
||||
expected_warning, Warning
|
||||
):
|
||||
expected_warning_tup = (expected_warning,)
|
||||
else:
|
||||
raise TypeError(msg % type(expected_warning))
|
||||
|
||||
self.expected_warning = expected_warning_tup
|
||||
self.match_expr = match_expr
|
||||
|
||||
def matches(self, warning: warnings.WarningMessage) -> bool:
|
||||
assert self.expected_warning is not None
|
||||
return issubclass(warning.category, self.expected_warning) and bool(
|
||||
self.match_expr is None or re.search(self.match_expr, str(warning.message))
|
||||
)
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: TracebackType | None,
|
||||
) -> None:
|
||||
super().__exit__(exc_type, exc_val, exc_tb)
|
||||
|
||||
__tracebackhide__ = True
|
||||
|
||||
# BaseExceptions like pytest.{skip,fail,xfail,exit} or Ctrl-C within
|
||||
# pytest.warns should *not* trigger "DID NOT WARN" and get suppressed
|
||||
# when the warning doesn't happen. Control-flow exceptions should always
|
||||
# propagate.
|
||||
if exc_val is not None and (
|
||||
not isinstance(exc_val, Exception)
|
||||
# Exit is an Exception, not a BaseException, for some reason.
|
||||
or isinstance(exc_val, Exit)
|
||||
):
|
||||
return
|
||||
|
||||
def found_str() -> str:
|
||||
return pformat([record.message for record in self], indent=2)
|
||||
|
||||
try:
|
||||
if not any(issubclass(w.category, self.expected_warning) for w in self):
|
||||
fail(
|
||||
f"DID NOT WARN. No warnings of type {self.expected_warning} were emitted.\n"
|
||||
f" Emitted warnings: {found_str()}."
|
||||
)
|
||||
elif not any(self.matches(w) for w in self):
|
||||
escape_hint = ""
|
||||
if isinstance(self.match_expr, str) and any(
|
||||
self.match_expr == str(w.message)
|
||||
for w in self
|
||||
if issubclass(w.category, self.expected_warning)
|
||||
):
|
||||
escape_hint = "\n Did you mean to `re.escape()` the regex?"
|
||||
fail(
|
||||
f"Regex pattern did not match any of the {len(self)} warnings emitted.\n"
|
||||
f" Regex: {self.match_expr!r}\n"
|
||||
f" Emitted warnings: {found_str()}.{escape_hint}"
|
||||
)
|
||||
finally:
|
||||
# Whether or not any warnings matched, we want to re-emit all unmatched warnings.
|
||||
for w in self:
|
||||
if not self.matches(w):
|
||||
warnings.warn_explicit(
|
||||
message=w.message,
|
||||
category=w.category,
|
||||
filename=w.filename,
|
||||
lineno=w.lineno,
|
||||
source=w.source,
|
||||
)
|
||||
|
||||
# Currently in Python it is possible to pass other types than an
|
||||
# `str` message when creating `Warning` instances, however this
|
||||
# causes an exception when :func:`warnings.filterwarnings` is used
|
||||
# to filter those warnings. See
|
||||
# https://github.com/python/cpython/issues/103577 for a discussion.
|
||||
# While this can be considered a bug in CPython, we put guards in
|
||||
# pytest as the error message produced without this check in place
|
||||
# is confusing (#10865).
|
||||
for w in self:
|
||||
if type(w.message) is not UserWarning:
|
||||
# If the warning was of an incorrect type then `warnings.warn()`
|
||||
# creates a UserWarning. Any other warning must have been specified
|
||||
# explicitly.
|
||||
continue
|
||||
if not w.message.args:
|
||||
# UserWarning() without arguments must have been specified explicitly.
|
||||
continue
|
||||
msg = w.message.args[0]
|
||||
if isinstance(msg, str):
|
||||
continue
|
||||
# It's possible that UserWarning was explicitly specified, and
|
||||
# its first argument was not a string. But that case can't be
|
||||
# distinguished from an invalid type.
|
||||
raise TypeError(
|
||||
f"Warning must be str or Warning, got {msg!r} (type {type(msg).__name__})"
|
||||
)
|
||||
@@ -0,0 +1,734 @@
|
||||
# mypy: allow-untyped-defs
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Sequence
|
||||
import dataclasses
|
||||
from io import StringIO
|
||||
import os
|
||||
from pprint import pprint
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing import cast
|
||||
from typing import final
|
||||
from typing import Literal
|
||||
from typing import NoReturn
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from _pytest._code.code import ExceptionChainRepr
|
||||
from _pytest._code.code import ExceptionInfo
|
||||
from _pytest._code.code import ExceptionRepr
|
||||
from _pytest._code.code import ReprEntry
|
||||
from _pytest._code.code import ReprEntryNative
|
||||
from _pytest._code.code import ReprExceptionInfo
|
||||
from _pytest._code.code import ReprFileLocation
|
||||
from _pytest._code.code import ReprFuncArgs
|
||||
from _pytest._code.code import ReprLocals
|
||||
from _pytest._code.code import ReprTraceback
|
||||
from _pytest._code.code import TerminalRepr
|
||||
from _pytest._io import TerminalWriter
|
||||
from _pytest.config import Config
|
||||
from _pytest.nodeid import coerce_node_id
|
||||
from _pytest.nodeid import NodeId
|
||||
from _pytest.nodes import Collector
|
||||
from _pytest.nodes import Item
|
||||
from _pytest.outcomes import fail
|
||||
from _pytest.outcomes import skip
|
||||
|
||||
|
||||
if sys.version_info < (3, 11):
|
||||
from exceptiongroup import BaseExceptionGroup
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import Self
|
||||
|
||||
from _pytest.runner import CallInfo
|
||||
|
||||
|
||||
def getworkerinfoline(node):
|
||||
try:
|
||||
return node._workerinfocache
|
||||
except AttributeError:
|
||||
d = node.workerinfo
|
||||
ver = "{}.{}.{}".format(*d["version_info"][:3])
|
||||
node._workerinfocache = s = "[{}] {} -- Python {} {}".format(
|
||||
d["id"], d["sysplatform"], ver, d["executable"]
|
||||
)
|
||||
return s
|
||||
|
||||
|
||||
class BaseReport:
|
||||
when: str | None
|
||||
location: tuple[str, int | None, str] | None
|
||||
longrepr: (
|
||||
ExceptionInfo[BaseException] | tuple[str, int, str] | str | TerminalRepr | None
|
||||
)
|
||||
sections: list[tuple[str, str]]
|
||||
outcome: Literal["passed", "failed", "skipped"]
|
||||
|
||||
_id: NodeId
|
||||
|
||||
def __init__(self, **kw: Any) -> None:
|
||||
self.__dict__.update(kw)
|
||||
|
||||
@property
|
||||
def nodeid(self) -> str:
|
||||
return str(self._id)
|
||||
|
||||
@nodeid.setter
|
||||
def nodeid(self, value: str) -> None:
|
||||
self._id = NodeId.parse(value)
|
||||
|
||||
@property
|
||||
def id(self) -> NodeId:
|
||||
"""The structured (non-string) form of ``nodeid``.
|
||||
|
||||
:meta private:
|
||||
"""
|
||||
return self._id
|
||||
|
||||
if TYPE_CHECKING:
|
||||
# Can have arbitrary fields given to __init__().
|
||||
def __getattr__(self, key: str) -> Any: ...
|
||||
|
||||
def toterminal(self, out: TerminalWriter) -> None:
|
||||
if hasattr(self, "node"):
|
||||
worker_info = getworkerinfoline(self.node)
|
||||
if worker_info:
|
||||
out.line(worker_info)
|
||||
|
||||
longrepr = self.longrepr
|
||||
if longrepr is None:
|
||||
return
|
||||
|
||||
if hasattr(longrepr, "toterminal"):
|
||||
longrepr_terminal = cast(TerminalRepr, longrepr)
|
||||
longrepr_terminal.toterminal(out)
|
||||
else:
|
||||
try:
|
||||
s = str(longrepr)
|
||||
except UnicodeEncodeError:
|
||||
s = "<unprintable longrepr>"
|
||||
out.line(s)
|
||||
|
||||
def get_sections(self, prefix: str) -> Iterator[tuple[str, str]]:
|
||||
for name, content in self.sections:
|
||||
if name.startswith(prefix):
|
||||
yield prefix, content
|
||||
|
||||
@property
|
||||
def longreprtext(self) -> str:
|
||||
"""Read-only property that returns the full string representation of
|
||||
``longrepr``.
|
||||
|
||||
.. versionadded:: 3.0
|
||||
"""
|
||||
file = StringIO()
|
||||
tw = TerminalWriter(file)
|
||||
tw.hasmarkup = False
|
||||
self.toterminal(tw)
|
||||
exc = file.getvalue()
|
||||
return exc.strip()
|
||||
|
||||
@property
|
||||
def caplog(self) -> str:
|
||||
"""Return captured log lines, if log capturing is enabled.
|
||||
|
||||
.. versionadded:: 3.5
|
||||
"""
|
||||
return "\n".join(
|
||||
content for (prefix, content) in self.get_sections("Captured log")
|
||||
)
|
||||
|
||||
@property
|
||||
def capstdout(self) -> str:
|
||||
"""Return captured text from stdout, if capturing is enabled.
|
||||
|
||||
.. versionadded:: 3.0
|
||||
"""
|
||||
return "".join(
|
||||
content for (prefix, content) in self.get_sections("Captured stdout")
|
||||
)
|
||||
|
||||
@property
|
||||
def capstderr(self) -> str:
|
||||
"""Return captured text from stderr, if capturing is enabled.
|
||||
|
||||
.. versionadded:: 3.0
|
||||
"""
|
||||
return "".join(
|
||||
content for (prefix, content) in self.get_sections("Captured stderr")
|
||||
)
|
||||
|
||||
@property
|
||||
def passed(self) -> bool:
|
||||
"""Whether the outcome is passed."""
|
||||
return self.outcome == "passed"
|
||||
|
||||
@property
|
||||
def failed(self) -> bool:
|
||||
"""Whether the outcome is failed."""
|
||||
return self.outcome == "failed"
|
||||
|
||||
@property
|
||||
def skipped(self) -> bool:
|
||||
"""Whether the outcome is skipped."""
|
||||
return self.outcome == "skipped"
|
||||
|
||||
@property
|
||||
def fspath(self) -> str:
|
||||
"""The path portion of the reported node, as a string."""
|
||||
return self._id.path
|
||||
|
||||
@property
|
||||
def count_towards_summary(self) -> bool:
|
||||
"""**Experimental** Whether this report should be counted towards the
|
||||
totals shown at the end of the test session: "1 passed, 1 failure, etc".
|
||||
|
||||
.. note::
|
||||
|
||||
This function is considered **experimental**, so beware that it is subject to changes
|
||||
even in patch releases.
|
||||
"""
|
||||
return True
|
||||
|
||||
@property
|
||||
def head_line(self) -> str | None:
|
||||
"""**Experimental** The head line shown with longrepr output for this
|
||||
report, more commonly during traceback representation during
|
||||
failures::
|
||||
|
||||
________ Test.foo ________
|
||||
|
||||
|
||||
In the example above, the head_line is "Test.foo".
|
||||
|
||||
.. note::
|
||||
|
||||
This function is considered **experimental**, so beware that it is subject to changes
|
||||
even in patch releases.
|
||||
"""
|
||||
if self.location is not None:
|
||||
_fspath, _lineno, domain = self.location
|
||||
return domain
|
||||
return None
|
||||
|
||||
def _get_verbose_word_with_markup(
|
||||
self, config: Config, default_markup: Mapping[str, bool]
|
||||
) -> tuple[str, Mapping[str, bool]]:
|
||||
_category, _short, verbose = config.hook.pytest_report_teststatus(
|
||||
report=self, config=config
|
||||
)
|
||||
|
||||
if isinstance(verbose, str):
|
||||
return verbose, default_markup
|
||||
|
||||
if isinstance(verbose, Sequence) and len(verbose) == 2:
|
||||
word, markup = verbose
|
||||
if isinstance(word, str) and isinstance(markup, Mapping):
|
||||
return word, markup
|
||||
|
||||
fail( # pragma: no cover
|
||||
"pytest_report_teststatus() hook (from a plugin) returned "
|
||||
f"an invalid verbose value: {verbose!r}.\nExpected either a string "
|
||||
"or a tuple of (word, markup)."
|
||||
)
|
||||
|
||||
def _to_json(self) -> dict[str, Any]:
|
||||
"""Return the contents of this report as a dict of builtin entries,
|
||||
suitable for serialization.
|
||||
|
||||
This was originally the serialize_report() function from xdist (ca03269).
|
||||
|
||||
Experimental method.
|
||||
"""
|
||||
return _report_to_json(self)
|
||||
|
||||
@classmethod
|
||||
def _from_json(cls, reportdict: dict[str, object]) -> Self:
|
||||
"""Create either a TestReport or CollectReport, depending on the calling class.
|
||||
|
||||
It is the callers responsibility to know which class to pass here.
|
||||
|
||||
This was originally the serialize_report() function from xdist (ca03269).
|
||||
|
||||
Experimental method.
|
||||
"""
|
||||
kwargs = _report_kwargs_from_json(reportdict)
|
||||
return cls(**kwargs)
|
||||
|
||||
|
||||
def _report_unserialization_failure(
|
||||
type_name: str, report_class: type[BaseReport], reportdict
|
||||
) -> NoReturn:
|
||||
url = "https://github.com/pytest-dev/pytest/issues"
|
||||
stream = StringIO()
|
||||
pprint("-" * 100, stream=stream)
|
||||
pprint(f"INTERNALERROR: Unknown entry type returned: {type_name}", stream=stream)
|
||||
pprint(f"report_name: {report_class}", stream=stream)
|
||||
pprint(reportdict, stream=stream)
|
||||
pprint(f"Please report this bug at {url}", stream=stream)
|
||||
pprint("-" * 100, stream=stream)
|
||||
raise RuntimeError(stream.getvalue())
|
||||
|
||||
|
||||
def _format_failed_longrepr(
|
||||
item: Item, call: CallInfo[None], excinfo: ExceptionInfo[BaseException]
|
||||
):
|
||||
if call.when == "call":
|
||||
longrepr = item.repr_failure(excinfo)
|
||||
else:
|
||||
# Exception in setup or teardown.
|
||||
longrepr = item._repr_failure_py(
|
||||
excinfo, style=item.config.getoption("tbstyle", "auto")
|
||||
)
|
||||
return longrepr
|
||||
|
||||
|
||||
def _format_exception_group_all_skipped_longrepr(
|
||||
item: Item,
|
||||
excinfo: ExceptionInfo[BaseExceptionGroup[BaseException | BaseExceptionGroup]],
|
||||
) -> tuple[str, int, str]:
|
||||
r = excinfo._getreprcrash()
|
||||
assert r is not None, (
|
||||
"There should always be a traceback entry for skipping a test."
|
||||
)
|
||||
if all(
|
||||
getattr(skip, "_use_item_location", False) for skip in excinfo.value.exceptions
|
||||
):
|
||||
path, line = item.reportinfo()[:2]
|
||||
assert line is not None
|
||||
loc = (os.fspath(path), line + 1)
|
||||
default_msg = "skipped"
|
||||
else:
|
||||
loc = (str(r.path), r.lineno)
|
||||
default_msg = r.message
|
||||
|
||||
# Get all unique skip messages.
|
||||
msgs: list[str] = []
|
||||
for exception in excinfo.value.exceptions:
|
||||
m = getattr(exception, "msg", None) or (
|
||||
exception.args[0] if exception.args else None
|
||||
)
|
||||
if m and m not in msgs:
|
||||
msgs.append(m)
|
||||
|
||||
reason = "; ".join(msgs) if msgs else default_msg
|
||||
longrepr = (*loc, reason)
|
||||
return longrepr
|
||||
|
||||
|
||||
class TestReport(BaseReport):
|
||||
"""Basic test report object (also used for setup and teardown calls if
|
||||
they fail).
|
||||
|
||||
Reports can contain arbitrary extra attributes.
|
||||
"""
|
||||
|
||||
__test__ = False
|
||||
|
||||
# Defined by skipping plugin.
|
||||
# xfail reason if xfailed, otherwise not defined. Use hasattr to distinguish.
|
||||
wasxfail: str
|
||||
|
||||
_id: NodeId
|
||||
|
||||
@property
|
||||
def id(self) -> NodeId:
|
||||
"""The structured (non-string) form of ``nodeid``."""
|
||||
return self._id
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
nodeid: str | NodeId,
|
||||
location: tuple[str, int | None, str],
|
||||
keywords: Mapping[str, Literal[1]],
|
||||
outcome: Literal["passed", "failed", "skipped"],
|
||||
longrepr: ExceptionInfo[BaseException]
|
||||
| tuple[str, int, str]
|
||||
| str
|
||||
| TerminalRepr
|
||||
| None,
|
||||
when: Literal["setup", "call", "teardown"],
|
||||
sections: Iterable[tuple[str, str]] = (),
|
||||
duration: float = 0,
|
||||
start: float = 0,
|
||||
stop: float = 0,
|
||||
user_properties: Iterable[tuple[str, object]] | None = None,
|
||||
**extra,
|
||||
) -> None:
|
||||
#: Normalized collection nodeid.
|
||||
self._id = coerce_node_id(nodeid)
|
||||
|
||||
#: A (filesystempath, lineno, domaininfo) tuple indicating the
|
||||
#: actual location of a test item - it might be different from the
|
||||
#: collected one e.g. if a method is inherited from a different module.
|
||||
#: The filesystempath may be relative to ``config.rootdir``.
|
||||
#: The line number is 0-based.
|
||||
self.location: tuple[str, int | None, str] = location
|
||||
|
||||
#: The names in :attr:`Node.keywords <_pytest.nodes.Node.keywords>`
|
||||
#: of the item, each mapping to ``1``: only the names survive into the
|
||||
#: report, the values of the node keywords are not carried over.
|
||||
self.keywords: Mapping[str, Literal[1]] = keywords
|
||||
|
||||
#: Test outcome, always one of "passed", "failed", "skipped".
|
||||
self.outcome = outcome
|
||||
|
||||
#: None or a failure representation.
|
||||
self.longrepr = longrepr
|
||||
|
||||
#: One of 'setup', 'call', 'teardown' to indicate runtest phase.
|
||||
self.when: Literal["setup", "call", "teardown"] = when
|
||||
|
||||
#: User properties is a list of tuples (name, value) that holds user
|
||||
#: defined properties of the test.
|
||||
self.user_properties = list(user_properties or [])
|
||||
|
||||
#: Tuples of str ``(heading, content)`` with extra information
|
||||
#: for the test report. Used by pytest to add text captured
|
||||
#: from ``stdout``, ``stderr``, and intercepted logging events. May
|
||||
#: be used by other plugins to add arbitrary information to reports.
|
||||
self.sections = list(sections)
|
||||
|
||||
#: Time it took to run just the test.
|
||||
self.duration: float = duration
|
||||
|
||||
#: The system time when the call started, in seconds since the epoch.
|
||||
self.start: float = start
|
||||
#: The system time when the call ended, in seconds since the epoch.
|
||||
self.stop: float = stop
|
||||
|
||||
self.__dict__.update(extra)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<{self.__class__.__name__} {self.nodeid!r} when={self.when!r} outcome={self.outcome!r}>"
|
||||
|
||||
@classmethod
|
||||
def from_item_and_call(cls, item: Item, call: CallInfo[None]) -> TestReport:
|
||||
"""Create and fill a TestReport with standard item and call info.
|
||||
|
||||
:param item: The item.
|
||||
:param call: The call info.
|
||||
"""
|
||||
when = call.when
|
||||
# Remove "collect" from the Literal type -- only for collection calls.
|
||||
assert when != "collect"
|
||||
duration = call.duration
|
||||
start = call.start
|
||||
stop = call.stop
|
||||
keywords: Mapping[str, Literal[1]] = dict.fromkeys(item.keywords, 1)
|
||||
excinfo = call.excinfo
|
||||
sections = []
|
||||
if not call.excinfo:
|
||||
outcome: Literal["passed", "failed", "skipped"] = "passed"
|
||||
longrepr: (
|
||||
ExceptionInfo[BaseException]
|
||||
| tuple[str, int, str]
|
||||
| str
|
||||
| TerminalRepr
|
||||
| None
|
||||
) = None
|
||||
else:
|
||||
if not isinstance(excinfo, ExceptionInfo):
|
||||
outcome = "failed"
|
||||
longrepr = excinfo
|
||||
elif isinstance(excinfo.value, skip.Exception):
|
||||
outcome = "skipped"
|
||||
r = excinfo._getreprcrash()
|
||||
assert r is not None, (
|
||||
"There should always be a traceback entry for skipping a test."
|
||||
)
|
||||
if excinfo.value._use_item_location:
|
||||
path, line = item.reportinfo()[:2]
|
||||
assert line is not None
|
||||
longrepr = (os.fspath(path), line + 1, r.message)
|
||||
else:
|
||||
longrepr = (str(r.path), r.lineno, r.message)
|
||||
elif isinstance(excinfo.value, BaseExceptionGroup) and (
|
||||
excinfo.value.split(skip.Exception)[1] is None
|
||||
):
|
||||
# All exceptions in the group are skip exceptions.
|
||||
outcome = "skipped"
|
||||
excinfo = cast(
|
||||
ExceptionInfo[
|
||||
BaseExceptionGroup[BaseException | BaseExceptionGroup]
|
||||
],
|
||||
excinfo,
|
||||
)
|
||||
longrepr = _format_exception_group_all_skipped_longrepr(item, excinfo)
|
||||
else:
|
||||
outcome = "failed"
|
||||
longrepr = _format_failed_longrepr(item, call, excinfo)
|
||||
for rwhen, key, content in item._report_sections:
|
||||
sections.append((f"Captured {key} {rwhen}", content))
|
||||
return cls(
|
||||
item.id,
|
||||
item.location,
|
||||
keywords,
|
||||
outcome,
|
||||
longrepr,
|
||||
when,
|
||||
sections,
|
||||
duration,
|
||||
start,
|
||||
stop,
|
||||
user_properties=item.user_properties,
|
||||
)
|
||||
|
||||
|
||||
@final
|
||||
class CollectReport(BaseReport):
|
||||
"""Collection report object.
|
||||
|
||||
Reports can contain arbitrary extra attributes.
|
||||
"""
|
||||
|
||||
when = "collect"
|
||||
|
||||
_id: NodeId
|
||||
|
||||
@property
|
||||
def id(self) -> NodeId:
|
||||
"""The structured (non-string) form of ``nodeid``."""
|
||||
return self._id
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
nodeid: str | NodeId,
|
||||
outcome: Literal["passed", "failed", "skipped"],
|
||||
longrepr: ExceptionInfo[BaseException]
|
||||
| tuple[str, int, str]
|
||||
| str
|
||||
| TerminalRepr
|
||||
| None,
|
||||
result: list[Item | Collector] | None,
|
||||
sections: Iterable[tuple[str, str]] = (),
|
||||
**extra,
|
||||
) -> None:
|
||||
#: Normalized collection nodeid.
|
||||
self._id = nodeid if isinstance(nodeid, NodeId) else NodeId.parse(nodeid)
|
||||
|
||||
#: Test outcome, always one of "passed", "failed", "skipped".
|
||||
self.outcome = outcome
|
||||
|
||||
#: None or a failure representation.
|
||||
self.longrepr = longrepr
|
||||
|
||||
#: The collected items and collection nodes.
|
||||
self.result = result or []
|
||||
|
||||
#: Tuples of str ``(heading, content)`` with extra information
|
||||
#: for the test report. Used by pytest to add text captured
|
||||
#: from ``stdout``, ``stderr``, and intercepted logging events. May
|
||||
#: be used by other plugins to add arbitrary information to reports.
|
||||
self.sections = list(sections)
|
||||
|
||||
self.__dict__.update(extra)
|
||||
|
||||
@property
|
||||
def location( # type:ignore[override]
|
||||
self,
|
||||
) -> tuple[str, int | None, str] | None:
|
||||
return (self.fspath, None, self.fspath)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<CollectReport {self.nodeid!r} lenresult={len(self.result)} outcome={self.outcome!r}>"
|
||||
|
||||
|
||||
class CollectErrorRepr(TerminalRepr):
|
||||
def __init__(self, msg: str) -> None:
|
||||
self.longrepr = msg
|
||||
|
||||
def toterminal(self, out: TerminalWriter) -> None:
|
||||
out.line(self.longrepr, red=True)
|
||||
|
||||
|
||||
def pytest_report_to_serializable(
|
||||
report: CollectReport | TestReport,
|
||||
) -> dict[str, Any] | None:
|
||||
if isinstance(report, TestReport | CollectReport):
|
||||
data = report._to_json()
|
||||
data["$report_type"] = report.__class__.__name__
|
||||
return data
|
||||
# TODO: Check if this is actually reachable.
|
||||
return None # type: ignore[unreachable]
|
||||
|
||||
|
||||
def pytest_report_from_serializable(
|
||||
data: dict[str, Any],
|
||||
) -> CollectReport | TestReport | None:
|
||||
if "$report_type" in data:
|
||||
if data["$report_type"] == "TestReport":
|
||||
return TestReport._from_json(data)
|
||||
elif data["$report_type"] == "CollectReport":
|
||||
return CollectReport._from_json(data)
|
||||
assert False, "Unknown report_type unserialize data: {}".format(
|
||||
data["$report_type"]
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def _report_to_json(report: BaseReport) -> dict[str, Any]:
|
||||
"""Return the contents of this report as a dict of builtin entries,
|
||||
suitable for serialization.
|
||||
|
||||
This was originally the serialize_report() function from xdist (ca03269).
|
||||
"""
|
||||
|
||||
def serialize_repr_entry(
|
||||
entry: ReprEntry | ReprEntryNative,
|
||||
) -> dict[str, Any]:
|
||||
data = dataclasses.asdict(entry)
|
||||
for key, value in data.items():
|
||||
if hasattr(value, "__dict__"):
|
||||
data[key] = dataclasses.asdict(value)
|
||||
entry_data = {"type": type(entry).__name__, "data": data}
|
||||
return entry_data
|
||||
|
||||
def serialize_repr_traceback(reprtraceback: ReprTraceback) -> dict[str, Any]:
|
||||
result = dataclasses.asdict(reprtraceback)
|
||||
result["reprentries"] = [
|
||||
serialize_repr_entry(x) for x in reprtraceback.reprentries
|
||||
]
|
||||
return result
|
||||
|
||||
def serialize_repr_crash(
|
||||
reprcrash: ReprFileLocation | None,
|
||||
) -> dict[str, Any] | None:
|
||||
if reprcrash is not None:
|
||||
return dataclasses.asdict(reprcrash)
|
||||
else:
|
||||
return None
|
||||
|
||||
def serialize_exception_longrepr(rep: BaseReport) -> dict[str, Any]:
|
||||
assert rep.longrepr is not None
|
||||
# TODO: Investigate whether the duck typing is really necessary here.
|
||||
longrepr = cast(ExceptionRepr, rep.longrepr)
|
||||
result: dict[str, Any] = {
|
||||
"reprcrash": serialize_repr_crash(longrepr.reprcrash),
|
||||
"reprtraceback": serialize_repr_traceback(longrepr.reprtraceback),
|
||||
"sections": longrepr.sections,
|
||||
}
|
||||
if isinstance(longrepr, ExceptionChainRepr):
|
||||
result["chain"] = []
|
||||
for repr_traceback, repr_crash, description in longrepr.chain:
|
||||
result["chain"].append(
|
||||
(
|
||||
serialize_repr_traceback(repr_traceback),
|
||||
serialize_repr_crash(repr_crash),
|
||||
description,
|
||||
)
|
||||
)
|
||||
else:
|
||||
result["chain"] = None
|
||||
return result
|
||||
|
||||
d = report.__dict__.copy()
|
||||
if "_id" in d:
|
||||
# nodeid is a property (backed by self._id) on TestReport/CollectReport,
|
||||
# so it's absent from __dict__ -- emit the wire-format "nodeid" string
|
||||
# key that xdist and other consumers expect, and never expose the
|
||||
# internal structured id object on the wire.
|
||||
d["nodeid"] = str(d.pop("_id"))
|
||||
if hasattr(report.longrepr, "toterminal"):
|
||||
if hasattr(report.longrepr, "reprtraceback") and hasattr(
|
||||
report.longrepr, "reprcrash"
|
||||
):
|
||||
d["longrepr"] = serialize_exception_longrepr(report)
|
||||
else:
|
||||
d["longrepr"] = str(report.longrepr)
|
||||
else:
|
||||
d["longrepr"] = report.longrepr
|
||||
for name in d:
|
||||
if isinstance(d[name], os.PathLike):
|
||||
d[name] = os.fspath(d[name])
|
||||
elif name == "result":
|
||||
d[name] = None # for now
|
||||
return d
|
||||
|
||||
|
||||
def _report_kwargs_from_json(reportdict: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Return **kwargs that can be used to construct a TestReport or
|
||||
CollectReport instance.
|
||||
|
||||
This was originally the serialize_report() function from xdist (ca03269).
|
||||
"""
|
||||
|
||||
def deserialize_repr_entry(entry_data):
|
||||
data = entry_data["data"]
|
||||
entry_type = entry_data["type"]
|
||||
if entry_type == "ReprEntry":
|
||||
reprfuncargs = None
|
||||
reprfileloc = None
|
||||
reprlocals = None
|
||||
if data["reprfuncargs"]:
|
||||
reprfuncargs = ReprFuncArgs(**data["reprfuncargs"])
|
||||
if data["reprfileloc"]:
|
||||
reprfileloc = ReprFileLocation(**data["reprfileloc"])
|
||||
if data["reprlocals"]:
|
||||
reprlocals = ReprLocals(data["reprlocals"]["lines"])
|
||||
|
||||
reprentry: ReprEntry | ReprEntryNative = ReprEntry(
|
||||
lines=data["lines"],
|
||||
reprfuncargs=reprfuncargs,
|
||||
reprlocals=reprlocals,
|
||||
reprfileloc=reprfileloc,
|
||||
style=data["style"],
|
||||
)
|
||||
elif entry_type == "ReprEntryNative":
|
||||
reprentry = ReprEntryNative(data["lines"])
|
||||
else:
|
||||
_report_unserialization_failure(entry_type, TestReport, reportdict)
|
||||
return reprentry
|
||||
|
||||
def deserialize_repr_traceback(repr_traceback_dict):
|
||||
repr_traceback_dict["reprentries"] = [
|
||||
deserialize_repr_entry(x) for x in repr_traceback_dict["reprentries"]
|
||||
]
|
||||
return ReprTraceback(**repr_traceback_dict)
|
||||
|
||||
def deserialize_repr_crash(repr_crash_dict: dict[str, Any] | None):
|
||||
if repr_crash_dict is not None:
|
||||
return ReprFileLocation(**repr_crash_dict)
|
||||
else:
|
||||
return None
|
||||
|
||||
if (
|
||||
reportdict["longrepr"]
|
||||
and "reprcrash" in reportdict["longrepr"]
|
||||
and "reprtraceback" in reportdict["longrepr"]
|
||||
):
|
||||
reprtraceback = deserialize_repr_traceback(
|
||||
reportdict["longrepr"]["reprtraceback"]
|
||||
)
|
||||
reprcrash = deserialize_repr_crash(reportdict["longrepr"]["reprcrash"])
|
||||
if reportdict["longrepr"]["chain"]:
|
||||
chain = []
|
||||
for repr_traceback_data, repr_crash_data, description in reportdict[
|
||||
"longrepr"
|
||||
]["chain"]:
|
||||
chain.append(
|
||||
(
|
||||
deserialize_repr_traceback(repr_traceback_data),
|
||||
deserialize_repr_crash(repr_crash_data),
|
||||
description,
|
||||
)
|
||||
)
|
||||
exception_info: ExceptionChainRepr | ReprExceptionInfo = ExceptionChainRepr(
|
||||
chain
|
||||
)
|
||||
else:
|
||||
exception_info = ReprExceptionInfo(
|
||||
reprtraceback=reprtraceback,
|
||||
reprcrash=reprcrash,
|
||||
)
|
||||
|
||||
for section in reportdict["longrepr"]["sections"]:
|
||||
exception_info.addsection(*section)
|
||||
reportdict["longrepr"] = exception_info
|
||||
|
||||
return reportdict
|
||||
@@ -0,0 +1,593 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Basic collect and runtest protocol implementations."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import bdb
|
||||
from collections.abc import Callable
|
||||
import dataclasses
|
||||
import os
|
||||
import sys
|
||||
import types
|
||||
from typing import cast
|
||||
from typing import final
|
||||
from typing import Generic
|
||||
from typing import Literal
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TypeVar
|
||||
|
||||
from .config import Config
|
||||
from .reports import BaseReport
|
||||
from .reports import CollectErrorRepr
|
||||
from .reports import CollectReport
|
||||
from .reports import TestReport
|
||||
from _pytest import timing
|
||||
from _pytest._code.code import ExceptionChainRepr
|
||||
from _pytest._code.code import ExceptionInfo
|
||||
from _pytest._code.code import TerminalRepr
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.deprecated import check_ispytest
|
||||
from _pytest.nodes import Collector
|
||||
from _pytest.nodes import Directory
|
||||
from _pytest.nodes import Item
|
||||
from _pytest.nodes import Node
|
||||
from _pytest.outcomes import Exit
|
||||
from _pytest.outcomes import OutcomeException
|
||||
from _pytest.outcomes import Skipped
|
||||
from _pytest.outcomes import TEST_OUTCOME
|
||||
|
||||
|
||||
if sys.version_info < (3, 11):
|
||||
from exceptiongroup import BaseExceptionGroup
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from _pytest.main import Session
|
||||
from _pytest.terminal import TerminalReporter
|
||||
|
||||
#
|
||||
# pytest plugin hooks.
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("terminal reporting", "Reporting", after="general")
|
||||
group.addoption(
|
||||
"--durations",
|
||||
action="store",
|
||||
type=int,
|
||||
default=None,
|
||||
metavar="N",
|
||||
help="Show N slowest setup/test durations (N=0 for all)",
|
||||
)
|
||||
group.addoption(
|
||||
"--durations-min",
|
||||
action="store",
|
||||
type=float,
|
||||
default=None,
|
||||
metavar="N",
|
||||
help="Minimal duration in seconds for inclusion in slowest list. "
|
||||
"Default: 0.005 (or 0.0 if -vv is given).",
|
||||
)
|
||||
|
||||
|
||||
def pytest_terminal_summary(terminalreporter: TerminalReporter) -> None:
|
||||
durations = terminalreporter.config.option.durations
|
||||
durations_min = terminalreporter.config.option.durations_min
|
||||
verbose = terminalreporter.config.get_verbosity()
|
||||
if durations is None:
|
||||
return
|
||||
if durations_min is None:
|
||||
durations_min = 0.005 if verbose < 2 else 0.0
|
||||
tr = terminalreporter
|
||||
dlist = []
|
||||
for replist in tr.stats.values():
|
||||
for rep in replist:
|
||||
if hasattr(rep, "duration"):
|
||||
dlist.append(rep)
|
||||
if not dlist:
|
||||
return
|
||||
dlist.sort(key=lambda x: x.duration, reverse=True)
|
||||
if not durations:
|
||||
tr.write_sep("=", "slowest durations")
|
||||
else:
|
||||
tr.write_sep("=", f"slowest {durations} durations")
|
||||
dlist = dlist[:durations]
|
||||
|
||||
for i, rep in enumerate(dlist):
|
||||
if rep.duration < durations_min:
|
||||
tr.write_line("")
|
||||
message = f"({len(dlist) - i} durations < {durations_min:g}s hidden."
|
||||
if terminalreporter.config.option.durations_min is None:
|
||||
message += " Use -vv to show these durations."
|
||||
message += ")"
|
||||
tr.write_line(message)
|
||||
break
|
||||
tr.write_line(f"{rep.duration:02.2f}s {rep.when:<8} {rep.nodeid}")
|
||||
|
||||
|
||||
def pytest_sessionstart(session: Session) -> None:
|
||||
session._setupstate = SetupState()
|
||||
|
||||
|
||||
def pytest_sessionfinish(session: Session) -> None:
|
||||
session._setupstate.teardown_exact(None)
|
||||
|
||||
|
||||
def pytest_runtest_protocol(item: Item, nextitem: Item | None) -> bool:
|
||||
ihook = item.ihook
|
||||
ihook.pytest_runtest_logstart(nodeid=item.nodeid, location=item.location)
|
||||
runtestprotocol(item, nextitem=nextitem)
|
||||
ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location)
|
||||
return True
|
||||
|
||||
|
||||
def runtestprotocol(
|
||||
item: Item, log: bool = True, nextitem: Item | None = None
|
||||
) -> list[TestReport]:
|
||||
hasrequest = hasattr(item, "_request")
|
||||
if hasrequest and not item._request: # type: ignore[attr-defined]
|
||||
# This only happens if the item is re-run, as is done by
|
||||
# pytest-rerunfailures.
|
||||
item._initrequest() # type: ignore[attr-defined]
|
||||
try:
|
||||
rep = call_and_report(item, "setup", log)
|
||||
reports = [rep]
|
||||
if rep.passed:
|
||||
setup_only = item.config.getoption("setuponly", False)
|
||||
if item.config.getoption("setupshow", False):
|
||||
show_test_item(item, add_space=not setup_only)
|
||||
if not setup_only:
|
||||
reports.append(call_and_report(item, "call", log))
|
||||
# If the session is about to fail or stop, teardown everything - this is
|
||||
# necessary to correctly report fixture teardown errors (see #11706)
|
||||
if item.session.shouldfail or item.session.shouldstop:
|
||||
nextitem = None
|
||||
reports.append(call_and_report(item, "teardown", log, nextitem=nextitem))
|
||||
finally:
|
||||
# After all teardown hooks have been called (or an exception was reraised)
|
||||
# want funcargs and request info to go away.
|
||||
if hasrequest:
|
||||
item._request = False # type: ignore[attr-defined]
|
||||
item.funcargs = None # type: ignore[attr-defined]
|
||||
return reports
|
||||
|
||||
|
||||
def show_test_item(item: Item, *, add_space: bool) -> None:
|
||||
"""Show test function, parameters and the fixtures of the test item."""
|
||||
tw = item.config.get_terminal_writer()
|
||||
tw.line()
|
||||
tw.write(" " * 8)
|
||||
tw.write(item.nodeid)
|
||||
used_fixtures = sorted(getattr(item, "fixturenames", []))
|
||||
if used_fixtures:
|
||||
tw.write(f" (fixtures used: {', '.join(used_fixtures)})")
|
||||
if add_space:
|
||||
tw.write(" ")
|
||||
tw.flush()
|
||||
|
||||
|
||||
def pytest_runtest_setup(item: Item) -> None:
|
||||
_update_current_test_var(item, "setup")
|
||||
item.session._setupstate.setup(item)
|
||||
|
||||
|
||||
def pytest_runtest_call(item: Item) -> None:
|
||||
_update_current_test_var(item, "call")
|
||||
try:
|
||||
del sys.last_type
|
||||
del sys.last_value
|
||||
del sys.last_traceback
|
||||
if sys.version_info >= (3, 12, 0):
|
||||
del sys.last_exc # type:ignore[attr-defined]
|
||||
except AttributeError:
|
||||
pass
|
||||
try:
|
||||
item.runtest()
|
||||
except Exception as e:
|
||||
# Store trace info to allow postmortem debugging
|
||||
sys.last_type = type(e)
|
||||
sys.last_value = e
|
||||
if sys.version_info >= (3, 12, 0):
|
||||
sys.last_exc = e # type:ignore[attr-defined]
|
||||
assert e.__traceback__ is not None
|
||||
# Skip *this* frame
|
||||
sys.last_traceback = e.__traceback__.tb_next
|
||||
raise
|
||||
|
||||
|
||||
def pytest_runtest_teardown(item: Item, nextitem: Item | None) -> None:
|
||||
_update_current_test_var(item, "teardown")
|
||||
item.session._setupstate.teardown_exact(nextitem)
|
||||
_update_current_test_var(item, None)
|
||||
|
||||
|
||||
def _update_current_test_var(
|
||||
item: Item, when: Literal["setup", "call", "teardown"] | None
|
||||
) -> None:
|
||||
"""Update :envvar:`PYTEST_CURRENT_TEST` to reflect the current item and stage.
|
||||
|
||||
If ``when`` is None, delete ``PYTEST_CURRENT_TEST`` from the environment.
|
||||
"""
|
||||
var_name = "PYTEST_CURRENT_TEST"
|
||||
if when:
|
||||
value = f"{item.nodeid} ({when})"
|
||||
# don't allow null bytes on environment variables (see #2644, #2957)
|
||||
value = value.replace("\x00", "(null)")
|
||||
os.environ[var_name] = value
|
||||
else:
|
||||
os.environ.pop(var_name)
|
||||
|
||||
|
||||
def pytest_report_teststatus(report: BaseReport) -> tuple[str, str, str] | None:
|
||||
if report.when in ("setup", "teardown"):
|
||||
if report.failed:
|
||||
# category, shortletter, verbose-word
|
||||
return "error", "E", "ERROR"
|
||||
elif report.skipped:
|
||||
return "skipped", "s", "SKIPPED"
|
||||
else:
|
||||
return "", "", ""
|
||||
return None
|
||||
|
||||
|
||||
#
|
||||
# Implementation
|
||||
|
||||
|
||||
def call_and_report(
|
||||
item: Item, when: Literal["setup", "call", "teardown"], log: bool = True, **kwds
|
||||
) -> TestReport:
|
||||
ihook = item.ihook
|
||||
if when == "setup":
|
||||
runtest_hook: Callable[..., None] = ihook.pytest_runtest_setup
|
||||
elif when == "call":
|
||||
runtest_hook = ihook.pytest_runtest_call
|
||||
elif when == "teardown":
|
||||
runtest_hook = ihook.pytest_runtest_teardown
|
||||
else:
|
||||
assert False, f"Unhandled runtest hook case: {when}"
|
||||
|
||||
call = CallInfo.from_call(
|
||||
lambda: runtest_hook(item=item, **kwds),
|
||||
when=when,
|
||||
reraise=get_reraise_exceptions(item.config),
|
||||
)
|
||||
report: TestReport = ihook.pytest_runtest_makereport(item=item, call=call)
|
||||
if log:
|
||||
ihook.pytest_runtest_logreport(report=report)
|
||||
if check_interactive_exception(call, report):
|
||||
ihook.pytest_exception_interact(node=item, call=call, report=report)
|
||||
return report
|
||||
|
||||
|
||||
def get_reraise_exceptions(config: Config) -> tuple[type[BaseException], ...]:
|
||||
"""Return exception types that should not be suppressed in general."""
|
||||
reraise: tuple[type[BaseException], ...] = (Exit,)
|
||||
if not config.getoption("usepdb", False):
|
||||
reraise += (KeyboardInterrupt,)
|
||||
return reraise
|
||||
|
||||
|
||||
def check_interactive_exception(call: CallInfo[object], report: BaseReport) -> bool:
|
||||
"""Check whether the call raised an exception that should be reported as
|
||||
interactive."""
|
||||
if call.excinfo is None:
|
||||
# Didn't raise.
|
||||
return False
|
||||
if hasattr(report, "wasxfail"):
|
||||
# Exception was expected.
|
||||
return False
|
||||
unittest = sys.modules.get("unittest")
|
||||
if isinstance(call.excinfo.value, Skipped | bdb.BdbQuit) or (
|
||||
unittest is not None and isinstance(call.excinfo.value, unittest.SkipTest)
|
||||
):
|
||||
# Special control flow exception.
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
TResult = TypeVar("TResult", covariant=True)
|
||||
|
||||
|
||||
@final
|
||||
@dataclasses.dataclass
|
||||
class CallInfo(Generic[TResult]):
|
||||
"""Result/Exception info of a function invocation."""
|
||||
|
||||
_result: TResult | None
|
||||
#: The captured exception of the call, if it raised.
|
||||
excinfo: ExceptionInfo[BaseException] | None
|
||||
#: The system time when the call started, in seconds since the epoch.
|
||||
start: float
|
||||
#: The system time when the call ended, in seconds since the epoch.
|
||||
stop: float
|
||||
#: The call duration, in seconds.
|
||||
duration: float
|
||||
#: The context of invocation: "collect", "setup", "call" or "teardown".
|
||||
when: Literal["collect", "setup", "call", "teardown"]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
result: TResult | None,
|
||||
excinfo: ExceptionInfo[BaseException] | None,
|
||||
start: float,
|
||||
stop: float,
|
||||
duration: float,
|
||||
when: Literal["collect", "setup", "call", "teardown"],
|
||||
*,
|
||||
_ispytest: bool = False,
|
||||
) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
self._result = result
|
||||
self.excinfo = excinfo
|
||||
self.start = start
|
||||
self.stop = stop
|
||||
self.duration = duration
|
||||
self.when = when
|
||||
|
||||
@property
|
||||
def result(self) -> TResult:
|
||||
"""The return value of the call, if it didn't raise.
|
||||
|
||||
Can only be accessed if excinfo is None.
|
||||
"""
|
||||
if self.excinfo is not None:
|
||||
raise AttributeError(f"{self!r} has no valid result")
|
||||
# The cast is safe because an exception wasn't raised, hence
|
||||
# _result has the expected function return type (which may be
|
||||
# None, that's why a cast and not an assert).
|
||||
return cast(TResult, self._result)
|
||||
|
||||
@classmethod
|
||||
def from_call(
|
||||
cls,
|
||||
func: Callable[[], TResult],
|
||||
when: Literal["collect", "setup", "call", "teardown"],
|
||||
reraise: type[BaseException] | tuple[type[BaseException], ...] | None = None,
|
||||
) -> CallInfo[TResult]:
|
||||
"""Call func, wrapping the result in a CallInfo.
|
||||
|
||||
:param func:
|
||||
The function to call. Called without arguments.
|
||||
:type func: Callable[[], _pytest.runner.TResult]
|
||||
:param when:
|
||||
The phase in which the function is called.
|
||||
:param reraise:
|
||||
Exception or exceptions that shall propagate if raised by the
|
||||
function, instead of being wrapped in the CallInfo.
|
||||
"""
|
||||
excinfo = None
|
||||
instant = timing.Instant()
|
||||
try:
|
||||
result: TResult | None = func()
|
||||
except BaseException:
|
||||
excinfo = ExceptionInfo.from_current()
|
||||
if reraise is not None and isinstance(excinfo.value, reraise):
|
||||
raise
|
||||
result = None
|
||||
duration = instant.elapsed()
|
||||
return cls(
|
||||
start=duration.start.time,
|
||||
stop=duration.stop.time,
|
||||
duration=duration.seconds,
|
||||
when=when,
|
||||
result=result,
|
||||
excinfo=excinfo,
|
||||
_ispytest=True,
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
if self.excinfo is None:
|
||||
return f"<CallInfo when={self.when!r} result: {self._result!r}>"
|
||||
return f"<CallInfo when={self.when!r} excinfo={self.excinfo!r}>"
|
||||
|
||||
|
||||
def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> TestReport:
|
||||
return TestReport.from_item_and_call(item, call)
|
||||
|
||||
|
||||
def pytest_make_collect_report(collector: Collector) -> CollectReport:
|
||||
def collect() -> list[Item | Collector]:
|
||||
# Before collecting, if this is a Directory, load the conftests.
|
||||
# If a conftest import fails to load, it is considered a collection
|
||||
# error of the Directory collector. This is why it's done inside of the
|
||||
# CallInfo wrapper.
|
||||
#
|
||||
# Note: initial conftests are loaded early, not here.
|
||||
if isinstance(collector, Directory):
|
||||
collector.config.pluginmanager._loadconftestmodules(
|
||||
collector.path,
|
||||
collector.config.getoption("importmode"),
|
||||
rootpath=collector.config.rootpath,
|
||||
consider_namespace_packages=collector.config.getini(
|
||||
"consider_namespace_packages"
|
||||
),
|
||||
)
|
||||
|
||||
return list(collector.collect())
|
||||
|
||||
call = CallInfo.from_call(
|
||||
collect, "collect", reraise=(KeyboardInterrupt, SystemExit)
|
||||
)
|
||||
longrepr: tuple[str, int, str] | str | TerminalRepr | None = None
|
||||
if not call.excinfo:
|
||||
outcome: Literal["passed", "skipped", "failed"] = "passed"
|
||||
else:
|
||||
skip_exceptions = [Skipped]
|
||||
unittest = sys.modules.get("unittest")
|
||||
if unittest is not None:
|
||||
skip_exceptions.append(unittest.SkipTest)
|
||||
if isinstance(call.excinfo.value, tuple(skip_exceptions)):
|
||||
outcome = "skipped"
|
||||
r_ = collector._repr_failure_py(call.excinfo, "line")
|
||||
assert isinstance(r_, ExceptionChainRepr), repr(r_)
|
||||
r = r_.reprcrash
|
||||
assert r
|
||||
longrepr = (str(r.path), r.lineno, r.message)
|
||||
else:
|
||||
outcome = "failed"
|
||||
errorinfo = collector.repr_failure(call.excinfo)
|
||||
if not hasattr(errorinfo, "toterminal"):
|
||||
assert isinstance(errorinfo, str)
|
||||
errorinfo = CollectErrorRepr(errorinfo)
|
||||
longrepr = errorinfo
|
||||
result = call.result if not call.excinfo else None
|
||||
rep = CollectReport(collector.id, outcome, longrepr, result)
|
||||
rep.call = call # type: ignore # see collect_one_node
|
||||
return rep
|
||||
|
||||
|
||||
class SetupState:
|
||||
"""Shared state for setting up/tearing down test items or collectors
|
||||
in a session.
|
||||
|
||||
Suppose we have a collection tree as follows:
|
||||
|
||||
<Session session>
|
||||
<Module mod1>
|
||||
<Function item1>
|
||||
<Module mod2>
|
||||
<Function item2>
|
||||
|
||||
The SetupState maintains a stack. The stack starts out empty:
|
||||
|
||||
[]
|
||||
|
||||
During the setup phase of item1, setup(item1) is called. What it does
|
||||
is:
|
||||
|
||||
push session to stack, run session.setup()
|
||||
push mod1 to stack, run mod1.setup()
|
||||
push item1 to stack, run item1.setup()
|
||||
|
||||
The stack is:
|
||||
|
||||
[session, mod1, item1]
|
||||
|
||||
While the stack is in this shape, it is allowed to add finalizers to
|
||||
each of session, mod1, item1 using addfinalizer().
|
||||
|
||||
During the teardown phase of item1, teardown_exact(item2) is called,
|
||||
where item2 is the next item to item1. What it does is:
|
||||
|
||||
pop item1 from stack, run its teardowns
|
||||
pop mod1 from stack, run its teardowns
|
||||
|
||||
mod1 was popped because it ended its purpose with item1. The stack is:
|
||||
|
||||
[session]
|
||||
|
||||
During the setup phase of item2, setup(item2) is called. What it does
|
||||
is:
|
||||
|
||||
push mod2 to stack, run mod2.setup()
|
||||
push item2 to stack, run item2.setup()
|
||||
|
||||
Stack:
|
||||
|
||||
[session, mod2, item2]
|
||||
|
||||
During the teardown phase of item2, teardown_exact(None) is called,
|
||||
because item2 is the last item. What it does is:
|
||||
|
||||
pop item2 from stack, run its teardowns
|
||||
pop mod2 from stack, run its teardowns
|
||||
pop session from stack, run its teardowns
|
||||
|
||||
Stack:
|
||||
|
||||
[]
|
||||
|
||||
The end!
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
# The stack is in the dict insertion order.
|
||||
self.stack: dict[
|
||||
Node,
|
||||
tuple[
|
||||
# Node's finalizers.
|
||||
list[Callable[[], object]],
|
||||
# Node's exception and original traceback, if its setup raised.
|
||||
tuple[OutcomeException | Exception, types.TracebackType | None] | None,
|
||||
],
|
||||
] = {}
|
||||
|
||||
def is_node_active(self, node: Node) -> bool:
|
||||
"""Check if a node is currently active in the stack -- set up and not
|
||||
torn down yet."""
|
||||
return node in self.stack
|
||||
|
||||
def setup(self, item: Item) -> None:
|
||||
"""Setup objects along the collector chain to the item."""
|
||||
needed_collectors = item.listchain()
|
||||
|
||||
# If a collector fails its setup, fail its entire subtree of items.
|
||||
# The setup is not retried for each item - the same exception is used.
|
||||
for col, (finalizers, exc) in self.stack.items():
|
||||
assert col in needed_collectors, "previous item was not torn down properly"
|
||||
if exc:
|
||||
raise exc[0].with_traceback(exc[1])
|
||||
|
||||
for col in needed_collectors[len(self.stack) :]:
|
||||
assert col not in self.stack
|
||||
# Push onto the stack.
|
||||
self.stack[col] = ([col.teardown], None)
|
||||
try:
|
||||
col.setup()
|
||||
except TEST_OUTCOME as exc:
|
||||
self.stack[col] = (self.stack[col][0], (exc, exc.__traceback__))
|
||||
raise
|
||||
|
||||
def addfinalizer(self, finalizer: Callable[[], object], node: Node) -> None:
|
||||
"""Attach a finalizer to the given node.
|
||||
|
||||
The node must be currently active in the stack.
|
||||
"""
|
||||
assert node and not isinstance(node, tuple)
|
||||
assert callable(finalizer)
|
||||
assert node in self.stack, (node, self.stack)
|
||||
self.stack[node][0].append(finalizer)
|
||||
|
||||
def teardown_exact(self, nextitem: Item | None) -> None:
|
||||
"""Teardown the current stack up until reaching nodes that nextitem
|
||||
also descends from.
|
||||
|
||||
When nextitem is None (meaning we're at the last item), the entire
|
||||
stack is torn down.
|
||||
"""
|
||||
needed_collectors = (nextitem and nextitem.listchain()) or []
|
||||
exceptions: list[BaseException] = []
|
||||
while self.stack:
|
||||
if list(self.stack.keys()) == needed_collectors[: len(self.stack)]:
|
||||
break
|
||||
node, (finalizers, _) = self.stack.popitem()
|
||||
these_exceptions = []
|
||||
while finalizers:
|
||||
fin = finalizers.pop()
|
||||
try:
|
||||
fin()
|
||||
except TEST_OUTCOME as e:
|
||||
these_exceptions.append(e)
|
||||
|
||||
if len(these_exceptions) == 1:
|
||||
exceptions.extend(these_exceptions)
|
||||
elif these_exceptions:
|
||||
msg = f"errors while tearing down {node!r}"
|
||||
exceptions.append(BaseExceptionGroup(msg, these_exceptions[::-1]))
|
||||
|
||||
if len(exceptions) == 1:
|
||||
raise exceptions[0]
|
||||
elif exceptions:
|
||||
raise BaseExceptionGroup("errors during test teardown", exceptions[::-1])
|
||||
if nextitem is None:
|
||||
assert not self.stack
|
||||
|
||||
|
||||
def collect_one_node(collector: Collector) -> CollectReport:
|
||||
ihook = collector.ihook
|
||||
ihook.pytest_collectstart(collector=collector)
|
||||
rep: CollectReport = ihook.pytest_make_collect_report(collector=collector)
|
||||
call = rep.__dict__.pop("call", None)
|
||||
if call and check_interactive_exception(call, rep):
|
||||
ihook.pytest_exception_interact(node=collector, call=call, report=rep)
|
||||
return rep
|
||||
@@ -0,0 +1,91 @@
|
||||
"""
|
||||
Scope definition and related utilities.
|
||||
|
||||
Those are defined here, instead of in the 'fixtures' module because
|
||||
their use is spread across many other pytest modules, and centralizing it in 'fixtures'
|
||||
would cause circular references.
|
||||
|
||||
Also this makes the module light to import, as it should.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
from functools import total_ordering
|
||||
from typing import Literal
|
||||
|
||||
|
||||
ScopeName = Literal["session", "package", "module", "class", "function"]
|
||||
|
||||
|
||||
@total_ordering
|
||||
class Scope(Enum):
|
||||
"""
|
||||
Represents one of the possible fixture scopes in pytest.
|
||||
|
||||
Scopes are ordered from lower to higher, that is:
|
||||
|
||||
->>> higher ->>>
|
||||
|
||||
Function < Class < Module < Package < Session
|
||||
|
||||
<<<- lower <<<-
|
||||
"""
|
||||
|
||||
# Scopes need to be listed from lower to higher.
|
||||
Function = "function"
|
||||
Class = "class"
|
||||
Module = "module"
|
||||
Package = "package"
|
||||
Session = "session"
|
||||
|
||||
def next_lower(self) -> Scope:
|
||||
"""Return the next lower scope."""
|
||||
index = _SCOPE_INDICES[self]
|
||||
if index == 0:
|
||||
raise ValueError(f"{self} is the lower-most scope")
|
||||
return _ALL_SCOPES[index - 1]
|
||||
|
||||
def next_higher(self) -> Scope:
|
||||
"""Return the next higher scope."""
|
||||
index = _SCOPE_INDICES[self]
|
||||
if index == len(_SCOPE_INDICES) - 1:
|
||||
raise ValueError(f"{self} is the upper-most scope")
|
||||
return _ALL_SCOPES[index + 1]
|
||||
|
||||
def __lt__(self, other: Scope) -> bool:
|
||||
self_index = _SCOPE_INDICES[self]
|
||||
other_index = _SCOPE_INDICES[other]
|
||||
return self_index < other_index
|
||||
|
||||
@classmethod
|
||||
def from_user(
|
||||
cls, scope_name: ScopeName, descr: str, where: str | None = None
|
||||
) -> Scope:
|
||||
"""
|
||||
Given a scope name from the user, return the equivalent Scope enum. Should be used
|
||||
whenever we want to convert a user provided scope name to its enum object.
|
||||
|
||||
If the scope name is invalid, construct a user friendly message and call pytest.fail.
|
||||
"""
|
||||
from _pytest.outcomes import fail
|
||||
|
||||
try:
|
||||
# Holding this reference is necessary for mypy at the moment.
|
||||
scope = Scope(scope_name)
|
||||
except ValueError:
|
||||
fail(
|
||||
"{} {}got an unexpected scope value '{}'".format(
|
||||
descr, f"from {where} " if where else "", scope_name
|
||||
),
|
||||
pytrace=False,
|
||||
)
|
||||
return scope
|
||||
|
||||
|
||||
_ALL_SCOPES = list(Scope)
|
||||
_SCOPE_INDICES = {scope: index for index, scope in enumerate(_ALL_SCOPES)}
|
||||
|
||||
|
||||
# Ordered list of scopes which can contain many tests (in practice all except Function).
|
||||
HIGH_SCOPES = [x for x in Scope if x is not Scope.Function]
|
||||
@@ -0,0 +1,98 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
|
||||
from _pytest._io.saferepr import saferepr
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import ExitCode
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.fixtures import FixtureDef
|
||||
from _pytest.fixtures import SubRequest
|
||||
from _pytest.scope import Scope
|
||||
import pytest
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("debugconfig")
|
||||
group.addoption(
|
||||
"--setuponly",
|
||||
"--setup-only",
|
||||
action="store_true",
|
||||
help="Only setup fixtures, do not execute tests",
|
||||
)
|
||||
group.addoption(
|
||||
"--setupshow",
|
||||
"--setup-show",
|
||||
action="store_true",
|
||||
help="Show setup of fixtures while executing tests",
|
||||
)
|
||||
|
||||
|
||||
@pytest.hookimpl(wrapper=True)
|
||||
def pytest_fixture_setup(
|
||||
fixturedef: FixtureDef[object], request: SubRequest
|
||||
) -> Generator[None, object, object]:
|
||||
try:
|
||||
return (yield)
|
||||
finally:
|
||||
if request.config.option.setupshow:
|
||||
if hasattr(request, "param"):
|
||||
# Save the fixture parameter so ._show_fixture_action() can
|
||||
# display it now and during the teardown (in .finish()).
|
||||
if fixturedef.ids:
|
||||
if callable(fixturedef.ids):
|
||||
param = fixturedef.ids(request.param)
|
||||
else:
|
||||
param = fixturedef.ids[request.param_index]
|
||||
else:
|
||||
param = request.param
|
||||
fixturedef.cached_param = param # type: ignore[attr-defined]
|
||||
_show_fixture_action(fixturedef, request.config, "SETUP")
|
||||
|
||||
|
||||
def pytest_fixture_post_finalizer(
|
||||
fixturedef: FixtureDef[object], request: SubRequest
|
||||
) -> None:
|
||||
if fixturedef.cached_result is not None:
|
||||
config = request.config
|
||||
if config.option.setupshow:
|
||||
_show_fixture_action(fixturedef, request.config, "TEARDOWN")
|
||||
if hasattr(fixturedef, "cached_param"):
|
||||
del fixturedef.cached_param
|
||||
|
||||
|
||||
def _show_fixture_action(
|
||||
fixturedef: FixtureDef[object], config: Config, msg: str
|
||||
) -> None:
|
||||
capman = config.pluginmanager.getplugin("capturemanager")
|
||||
if capman:
|
||||
capman.suspend_global_capture()
|
||||
|
||||
tw = config.get_terminal_writer()
|
||||
tw.line()
|
||||
# Use smaller indentation the higher the scope: Session = 0, Package = 1, etc.
|
||||
scope_indent = list(reversed(Scope)).index(fixturedef._scope)
|
||||
tw.write(" " * 2 * scope_indent)
|
||||
|
||||
scopename = fixturedef.scope[0].upper()
|
||||
tw.write(f"{msg:<8} {scopename} {fixturedef.argname}")
|
||||
|
||||
if msg == "SETUP":
|
||||
deps = sorted(arg for arg in fixturedef.argnames if arg != "request")
|
||||
if deps:
|
||||
tw.write(" (fixtures used: {})".format(", ".join(deps)))
|
||||
|
||||
if hasattr(fixturedef, "cached_param"):
|
||||
tw.write(f"[{saferepr(fixturedef.cached_param, maxsize=42)}]")
|
||||
|
||||
tw.flush()
|
||||
|
||||
if capman:
|
||||
capman.resume_global_capture()
|
||||
|
||||
|
||||
@pytest.hookimpl(tryfirst=True)
|
||||
def pytest_cmdline_main(config: Config) -> int | ExitCode | None:
|
||||
if config.option.setuponly:
|
||||
config.option.setupshow = True
|
||||
return None
|
||||
@@ -0,0 +1,39 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import ExitCode
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.fixtures import FixtureDef
|
||||
from _pytest.fixtures import SubRequest
|
||||
import pytest
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("debugconfig")
|
||||
group.addoption(
|
||||
"--setupplan",
|
||||
"--setup-plan",
|
||||
action="store_true",
|
||||
help="Show what fixtures and tests would be executed but "
|
||||
"don't execute anything",
|
||||
)
|
||||
|
||||
|
||||
@pytest.hookimpl(tryfirst=True)
|
||||
def pytest_fixture_setup(
|
||||
fixturedef: FixtureDef[object], request: SubRequest
|
||||
) -> object | None:
|
||||
# Will return a dummy fixture if the setuponly option is provided.
|
||||
if request.config.option.setupplan:
|
||||
my_cache_key = fixturedef.cache_key(request)
|
||||
fixturedef.cached_result = (None, my_cache_key, None)
|
||||
return fixturedef.cached_result
|
||||
return None
|
||||
|
||||
|
||||
@pytest.hookimpl(tryfirst=True)
|
||||
def pytest_cmdline_main(config: Config) -> int | ExitCode | None:
|
||||
if config.option.setupplan:
|
||||
config.option.setuponly = True
|
||||
config.option.setupshow = True
|
||||
return None
|
||||
@@ -0,0 +1,321 @@
|
||||
# mypy: allow-untyped-defs
|
||||
"""Support for skip/xfail functions and markers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Generator
|
||||
from collections.abc import Mapping
|
||||
import dataclasses
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import hookimpl
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.mark.structures import Mark
|
||||
from _pytest.nodes import Item
|
||||
from _pytest.outcomes import fail
|
||||
from _pytest.outcomes import skip
|
||||
from _pytest.outcomes import xfail
|
||||
from _pytest.raises import AbstractRaises
|
||||
from _pytest.reports import BaseReport
|
||||
from _pytest.reports import TestReport
|
||||
from _pytest.runner import CallInfo
|
||||
from _pytest.stash import StashKey
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("general")
|
||||
group.addoption(
|
||||
"--runxfail",
|
||||
action="store_true",
|
||||
dest="runxfail",
|
||||
default=False,
|
||||
help="Report the results of xfail tests as if they were not marked",
|
||||
)
|
||||
|
||||
parser.addini(
|
||||
"strict_xfail",
|
||||
"Default for the strict parameter of xfail "
|
||||
"markers when not given explicitly (default: False) (alias: xfail_strict)",
|
||||
type="bool",
|
||||
# None => fallback to `strict`.
|
||||
default=None,
|
||||
aliases=["xfail_strict"],
|
||||
)
|
||||
|
||||
|
||||
def pytest_configure(config: Config) -> None:
|
||||
if config.option.runxfail:
|
||||
# yay a hack
|
||||
import pytest
|
||||
|
||||
old = pytest.xfail
|
||||
config.add_cleanup(lambda: setattr(pytest, "xfail", old))
|
||||
|
||||
def nop(*args, **kwargs):
|
||||
pass
|
||||
|
||||
nop.Exception = xfail.Exception # type: ignore[attr-defined]
|
||||
setattr(pytest, "xfail", nop)
|
||||
|
||||
config.addinivalue_line(
|
||||
"markers",
|
||||
"skip(reason=None): skip the given test function with an optional reason. "
|
||||
'Example: skip(reason="no way of currently testing this") skips the '
|
||||
"test.",
|
||||
)
|
||||
config.addinivalue_line(
|
||||
"markers",
|
||||
"skipif(condition, ..., *, reason=...): "
|
||||
"skip the given test function if any of the conditions evaluate to True. "
|
||||
"Example: skipif(sys.platform == 'win32') skips the test if we are on the win32 platform. "
|
||||
"See https://docs.pytest.org/en/stable/reference/reference.html#pytest-mark-skipif",
|
||||
)
|
||||
config.addinivalue_line(
|
||||
"markers",
|
||||
"xfail(condition, ..., *, reason=..., run=True, raises=None, strict=strict_xfail): "
|
||||
"mark the test function as an expected failure if any of the conditions "
|
||||
"evaluate to True. Optionally specify a reason for better reporting "
|
||||
"and run=False if you don't even want to execute the test function. "
|
||||
"If only specific exception(s) are expected, you can list them in "
|
||||
"raises, and if the test fails in other ways, it will be reported as "
|
||||
"a true failure. See https://docs.pytest.org/en/stable/reference/reference.html#pytest-mark-xfail",
|
||||
)
|
||||
|
||||
|
||||
def evaluate_condition(item: Item, mark: Mark, condition: object) -> tuple[bool, str]:
|
||||
"""Evaluate a single skipif/xfail condition.
|
||||
|
||||
If an old-style string condition is given, it is eval()'d, otherwise the
|
||||
condition is bool()'d. If this fails, an appropriately formatted pytest.fail
|
||||
is raised.
|
||||
|
||||
Returns (result, reason). The reason is only relevant if the result is True.
|
||||
"""
|
||||
# String condition.
|
||||
if isinstance(condition, str):
|
||||
globals_ = {
|
||||
"os": os,
|
||||
"sys": sys,
|
||||
"platform": platform,
|
||||
"config": item.config,
|
||||
}
|
||||
for dictionary in reversed(
|
||||
item.ihook.pytest_markeval_namespace(config=item.config)
|
||||
):
|
||||
if not isinstance(dictionary, Mapping):
|
||||
raise ValueError(
|
||||
f"pytest_markeval_namespace() needs to return a dict, got {dictionary!r}"
|
||||
)
|
||||
globals_.update(dictionary)
|
||||
if hasattr(item, "obj"):
|
||||
globals_.update(item.obj.__globals__)
|
||||
try:
|
||||
filename = f"<{mark.name} condition>"
|
||||
condition_code = compile(condition, filename, "eval")
|
||||
result = eval(condition_code, globals_)
|
||||
except SyntaxError as exc:
|
||||
msglines = [
|
||||
f"Error evaluating {mark.name!r} condition",
|
||||
" " + condition,
|
||||
" " + " " * (exc.offset or 0) + "^",
|
||||
"SyntaxError: invalid syntax",
|
||||
]
|
||||
fail("\n".join(msglines), pytrace=False)
|
||||
except Exception as exc:
|
||||
msglines = [
|
||||
f"Error evaluating {mark.name!r} condition",
|
||||
" " + condition,
|
||||
*traceback.format_exception_only(exc),
|
||||
]
|
||||
fail("\n".join(msglines), pytrace=False)
|
||||
|
||||
# Boolean condition.
|
||||
else:
|
||||
try:
|
||||
result = bool(condition)
|
||||
except Exception as exc:
|
||||
msglines = [
|
||||
f"Error evaluating {mark.name!r} condition as a boolean",
|
||||
*traceback.format_exception_only(exc),
|
||||
]
|
||||
fail("\n".join(msglines), pytrace=False)
|
||||
|
||||
reason = mark.kwargs.get("reason", None)
|
||||
if reason is None:
|
||||
if isinstance(condition, str):
|
||||
reason = "condition: " + condition
|
||||
else:
|
||||
# XXX better be checked at collection time
|
||||
msg = (
|
||||
f"Error evaluating {mark.name!r}: "
|
||||
+ "you need to specify reason=STRING when using booleans as conditions."
|
||||
)
|
||||
fail(msg, pytrace=False)
|
||||
|
||||
return result, reason
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Skip:
|
||||
"""The result of evaluate_skip_marks()."""
|
||||
|
||||
reason: str = "unconditional skip"
|
||||
|
||||
|
||||
def evaluate_skip_marks(item: Item) -> Skip | None:
|
||||
"""Evaluate skip and skipif marks on item, returning Skip if triggered."""
|
||||
for mark in item.iter_markers(name="skipif"):
|
||||
if "condition" not in mark.kwargs:
|
||||
conditions = mark.args
|
||||
else:
|
||||
conditions = (mark.kwargs["condition"],)
|
||||
|
||||
# Unconditional.
|
||||
if not conditions:
|
||||
reason = mark.kwargs.get("reason", "")
|
||||
return Skip(reason)
|
||||
|
||||
# If any of the conditions are true.
|
||||
for condition in conditions:
|
||||
result, reason = evaluate_condition(item, mark, condition)
|
||||
if result:
|
||||
return Skip(reason)
|
||||
|
||||
for mark in item.iter_markers(name="skip"):
|
||||
try:
|
||||
return Skip(*mark.args, **mark.kwargs)
|
||||
except TypeError as e:
|
||||
raise TypeError(str(e) + " - maybe you meant pytest.mark.skipif?") from None
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Xfail:
|
||||
"""The result of evaluate_xfail_marks()."""
|
||||
|
||||
__slots__ = ("raises", "reason", "run", "strict")
|
||||
|
||||
reason: str
|
||||
run: bool
|
||||
strict: bool
|
||||
raises: (
|
||||
type[BaseException]
|
||||
| tuple[type[BaseException], ...]
|
||||
| AbstractRaises[BaseException]
|
||||
| None
|
||||
)
|
||||
|
||||
|
||||
def evaluate_xfail_marks(item: Item) -> Xfail | None:
|
||||
"""Evaluate xfail marks on item, returning Xfail if triggered."""
|
||||
for mark in item.iter_markers(name="xfail"):
|
||||
run = mark.kwargs.get("run", True)
|
||||
strict = mark.kwargs.get("strict")
|
||||
if strict is None:
|
||||
strict = item.config.getini("strict_xfail")
|
||||
if strict is None:
|
||||
strict = item.config.getini("strict")
|
||||
raises = mark.kwargs.get("raises", None)
|
||||
if "condition" not in mark.kwargs:
|
||||
conditions = mark.args
|
||||
else:
|
||||
conditions = (mark.kwargs["condition"],)
|
||||
|
||||
# Unconditional.
|
||||
if not conditions:
|
||||
reason = mark.kwargs.get("reason", "")
|
||||
return Xfail(reason, run, strict, raises)
|
||||
|
||||
# If any of the conditions are true.
|
||||
for condition in conditions:
|
||||
result, reason = evaluate_condition(item, mark, condition)
|
||||
if result:
|
||||
return Xfail(reason, run, strict, raises)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
# Saves the xfail mark evaluation. Can be refreshed during call if None.
|
||||
xfailed_key = StashKey[Xfail | None]()
|
||||
|
||||
|
||||
@hookimpl(tryfirst=True)
|
||||
def pytest_runtest_setup(item: Item) -> None:
|
||||
skipped = evaluate_skip_marks(item)
|
||||
if skipped:
|
||||
raise skip.Exception(skipped.reason, _use_item_location=True)
|
||||
|
||||
item.stash[xfailed_key] = xfailed = evaluate_xfail_marks(item)
|
||||
if xfailed and not item.config.option.runxfail and not xfailed.run:
|
||||
raise xfail.Exception("[NOTRUN] " + xfailed.reason, pytrace=False)
|
||||
|
||||
|
||||
@hookimpl(wrapper=True)
|
||||
def pytest_runtest_call(item: Item) -> Generator[None]:
|
||||
xfailed = item.stash.get(xfailed_key, None)
|
||||
if xfailed is None:
|
||||
item.stash[xfailed_key] = xfailed = evaluate_xfail_marks(item)
|
||||
|
||||
if xfailed and not item.config.option.runxfail and not xfailed.run:
|
||||
raise xfail.Exception("[NOTRUN] " + xfailed.reason, pytrace=False)
|
||||
|
||||
try:
|
||||
return (yield)
|
||||
finally:
|
||||
# The test run may have added an xfail mark dynamically.
|
||||
xfailed = item.stash.get(xfailed_key, None)
|
||||
if xfailed is None:
|
||||
item.stash[xfailed_key] = xfailed = evaluate_xfail_marks(item)
|
||||
|
||||
|
||||
@hookimpl(wrapper=True)
|
||||
def pytest_runtest_makereport(
|
||||
item: Item, call: CallInfo[None]
|
||||
) -> Generator[None, TestReport, TestReport]:
|
||||
rep = yield
|
||||
xfailed = item.stash.get(xfailed_key, None)
|
||||
if item.config.option.runxfail:
|
||||
pass # don't interfere
|
||||
elif call.excinfo and isinstance(call.excinfo.value, xfail.Exception):
|
||||
assert call.excinfo.value.msg is not None
|
||||
rep.wasxfail = call.excinfo.value.msg
|
||||
rep.outcome = "skipped"
|
||||
elif not rep.skipped and xfailed:
|
||||
if call.excinfo:
|
||||
raises = xfailed.raises
|
||||
if raises is None or (
|
||||
(
|
||||
isinstance(raises, type | tuple)
|
||||
and isinstance(call.excinfo.value, raises)
|
||||
)
|
||||
or (
|
||||
isinstance(raises, AbstractRaises)
|
||||
and raises.matches(call.excinfo.value)
|
||||
)
|
||||
):
|
||||
rep.outcome = "skipped"
|
||||
rep.wasxfail = xfailed.reason
|
||||
else:
|
||||
rep.outcome = "failed"
|
||||
elif call.when == "call":
|
||||
if xfailed.strict:
|
||||
rep.outcome = "failed"
|
||||
rep.longrepr = "[XPASS(strict)] " + xfailed.reason
|
||||
else:
|
||||
rep.outcome = "passed"
|
||||
rep.wasxfail = xfailed.reason
|
||||
return rep
|
||||
|
||||
|
||||
def pytest_report_teststatus(report: BaseReport) -> tuple[str, str, str] | None:
|
||||
if hasattr(report, "wasxfail"):
|
||||
if report.skipped:
|
||||
return "xfailed", "x", "XFAIL"
|
||||
elif report.passed:
|
||||
return "xpassed", "X", "XPASS"
|
||||
return None
|
||||
@@ -0,0 +1,116 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from typing import cast
|
||||
from typing import Generic
|
||||
from typing import TypeVar
|
||||
|
||||
|
||||
__all__ = ["Stash", "StashKey"]
|
||||
|
||||
|
||||
T = TypeVar("T")
|
||||
D = TypeVar("D")
|
||||
|
||||
|
||||
class StashKey(Generic[T]):
|
||||
"""``StashKey`` is an object used as a key to a :class:`Stash`.
|
||||
|
||||
A ``StashKey`` is associated with the type ``T`` of the value of the key.
|
||||
|
||||
A ``StashKey`` is unique and cannot conflict with another key.
|
||||
|
||||
.. versionadded:: 7.0
|
||||
"""
|
||||
|
||||
__slots__ = ()
|
||||
|
||||
|
||||
class Stash:
|
||||
r"""``Stash`` is a type-safe heterogeneous mutable mapping that
|
||||
allows keys and value types to be defined separately from
|
||||
where it (the ``Stash``) is created.
|
||||
|
||||
Usually you will be given an object which has a ``Stash``, for example
|
||||
:class:`~pytest.Config` or a :class:`~_pytest.nodes.Node`:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
stash: Stash = some_object.stash
|
||||
|
||||
If a module or plugin wants to store data in this ``Stash``, it creates
|
||||
:class:`StashKey`\s for its keys (at the module level):
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# At the top-level of the module
|
||||
some_str_key = StashKey[str]()
|
||||
some_bool_key = StashKey[bool]()
|
||||
|
||||
To store information:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# Value type must match the key.
|
||||
stash[some_str_key] = "value"
|
||||
stash[some_bool_key] = True
|
||||
|
||||
To retrieve the information:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# The static type of some_str is str.
|
||||
some_str = stash[some_str_key]
|
||||
# The static type of some_bool is bool.
|
||||
some_bool = stash[some_bool_key]
|
||||
|
||||
.. versionadded:: 7.0
|
||||
"""
|
||||
|
||||
__slots__ = ("_storage",)
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._storage: dict[StashKey[Any], object] = {}
|
||||
|
||||
def __setitem__(self, key: StashKey[T], value: T) -> None:
|
||||
"""Set a value for key."""
|
||||
self._storage[key] = value
|
||||
|
||||
def __getitem__(self, key: StashKey[T]) -> T:
|
||||
"""Get the value for key.
|
||||
|
||||
Raises ``KeyError`` if the key wasn't set before.
|
||||
"""
|
||||
return cast(T, self._storage[key])
|
||||
|
||||
def get(self, key: StashKey[T], default: D) -> T | D:
|
||||
"""Get the value for key, or return default if the key wasn't set
|
||||
before."""
|
||||
try:
|
||||
return self[key]
|
||||
except KeyError:
|
||||
return default
|
||||
|
||||
def setdefault(self, key: StashKey[T], default: T) -> T:
|
||||
"""Return the value of key if already set, otherwise set the value
|
||||
of key to default and return default."""
|
||||
try:
|
||||
return self[key]
|
||||
except KeyError:
|
||||
self[key] = default
|
||||
return default
|
||||
|
||||
def __delitem__(self, key: StashKey[T]) -> None:
|
||||
"""Delete the value for key.
|
||||
|
||||
Raises ``KeyError`` if the key wasn't set before.
|
||||
"""
|
||||
del self._storage[key]
|
||||
|
||||
def __contains__(self, key: StashKey[T]) -> bool:
|
||||
"""Return whether key was set."""
|
||||
return key in self._storage
|
||||
|
||||
def __len__(self) -> int:
|
||||
"""Return how many items exist in the stash."""
|
||||
return len(self._storage)
|
||||
@@ -0,0 +1,219 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from _pytest import nodes
|
||||
from _pytest.cacheprovider import Cache
|
||||
from _pytest.config import Config
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.main import Session
|
||||
from _pytest.nodeid import NodeId
|
||||
from _pytest.reports import TestReport
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import Self
|
||||
|
||||
STEPWISE_CACHE_DIR = "cache/stepwise"
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("general")
|
||||
group.addoption(
|
||||
"--sw",
|
||||
"--stepwise",
|
||||
action="store_true",
|
||||
default=False,
|
||||
dest="stepwise",
|
||||
help="Exit on test failure and continue from last failing test next time",
|
||||
)
|
||||
group.addoption(
|
||||
"--sw-skip",
|
||||
"--stepwise-skip",
|
||||
action="store_true",
|
||||
default=False,
|
||||
dest="stepwise_skip",
|
||||
help="Ignore the first failing test but stop on the next failing test. "
|
||||
"Implicitly enables --stepwise.",
|
||||
)
|
||||
group.addoption(
|
||||
"--sw-reset",
|
||||
"--stepwise-reset",
|
||||
action="store_true",
|
||||
default=False,
|
||||
dest="stepwise_reset",
|
||||
help="Resets stepwise state, restarting the stepwise workflow. "
|
||||
"Implicitly enables --stepwise.",
|
||||
)
|
||||
|
||||
|
||||
def pytest_configure(config: Config) -> None:
|
||||
# --stepwise-skip/--stepwise-reset implies stepwise.
|
||||
if config.option.stepwise_skip or config.option.stepwise_reset:
|
||||
config.option.stepwise = True
|
||||
if config.getoption("stepwise"):
|
||||
config.pluginmanager.register(StepwisePlugin(config), "stepwiseplugin")
|
||||
|
||||
|
||||
def pytest_sessionfinish(session: Session) -> None:
|
||||
if not session.config.getoption("stepwise"):
|
||||
assert session.config.cache is not None
|
||||
if hasattr(session.config, "workerinput"):
|
||||
# Do not update cache if this process is a xdist worker to prevent
|
||||
# race conditions (#10641).
|
||||
return
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class StepwiseCacheInfo:
|
||||
# The nodeid of the last failed test.
|
||||
last_failed: NodeId | None
|
||||
|
||||
# The number of tests in the last time --stepwise was run.
|
||||
# We use this information as a simple way to invalidate the cache information, avoiding
|
||||
# confusing behavior in case the cache is stale.
|
||||
last_test_count: int | None
|
||||
|
||||
# The date when the cache was last updated, for information purposes only.
|
||||
last_cache_date_str: str
|
||||
|
||||
@property
|
||||
def last_cache_date(self) -> datetime:
|
||||
return datetime.fromisoformat(self.last_cache_date_str)
|
||||
|
||||
@classmethod
|
||||
def empty(cls) -> Self:
|
||||
return cls(
|
||||
last_failed=None,
|
||||
last_test_count=None,
|
||||
last_cache_date_str=datetime.now().isoformat(),
|
||||
)
|
||||
|
||||
def update_date_to_now(self) -> None:
|
||||
self.last_cache_date_str = datetime.now().isoformat()
|
||||
|
||||
|
||||
class StepwisePlugin:
|
||||
def __init__(self, config: Config) -> None:
|
||||
self.config = config
|
||||
self.session: Session | None = None
|
||||
self.report_status: list[str] = []
|
||||
assert config.cache is not None
|
||||
self.cache: Cache = config.cache
|
||||
self.skip: bool = config.getoption("stepwise_skip")
|
||||
self.reset: bool = config.getoption("stepwise_reset")
|
||||
self.cached_info = self._load_cached_info()
|
||||
|
||||
def _load_cached_info(self) -> StepwiseCacheInfo:
|
||||
cached_dict: dict[str, Any] | None = self.cache.get(STEPWISE_CACHE_DIR, None)
|
||||
if cached_dict:
|
||||
try:
|
||||
last_failed: str | None = cached_dict["last_failed"]
|
||||
return StepwiseCacheInfo(
|
||||
NodeId.parse(last_failed) if last_failed is not None else None,
|
||||
cached_dict["last_test_count"],
|
||||
cached_dict["last_cache_date_str"],
|
||||
)
|
||||
except (KeyError, TypeError) as e:
|
||||
error = f"{type(e).__name__}: {e}"
|
||||
self.report_status.append(f"error reading cache, discarding ({error})")
|
||||
|
||||
# Cache not found or error during load, return a new cache.
|
||||
return StepwiseCacheInfo.empty()
|
||||
|
||||
def pytest_sessionstart(self, session: Session) -> None:
|
||||
self.session = session
|
||||
|
||||
def pytest_collection_modifyitems(
|
||||
self, config: Config, items: list[nodes.Item]
|
||||
) -> None:
|
||||
last_test_count = self.cached_info.last_test_count
|
||||
self.cached_info.last_test_count = len(items)
|
||||
|
||||
if self.reset:
|
||||
self.report_status.append("resetting state, not skipping.")
|
||||
self.cached_info.last_failed = None
|
||||
return
|
||||
|
||||
if not self.cached_info.last_failed:
|
||||
self.report_status.append("no previously failed tests, not skipping.")
|
||||
return
|
||||
|
||||
if last_test_count is not None and last_test_count != len(items):
|
||||
self.report_status.append(
|
||||
f"test count changed, not skipping (now {len(items)} tests, previously {last_test_count})."
|
||||
)
|
||||
self.cached_info.last_failed = None
|
||||
return
|
||||
|
||||
# Check all item nodes until we find a match on last failed.
|
||||
failed_index = None
|
||||
for index, item in enumerate(items):
|
||||
if item.id == self.cached_info.last_failed:
|
||||
failed_index = index
|
||||
break
|
||||
|
||||
# If the previously failed test was not found among the test items,
|
||||
# do not skip any tests.
|
||||
if failed_index is None:
|
||||
self.report_status.append("previously failed test not found, not skipping.")
|
||||
else:
|
||||
cache_age = datetime.now() - self.cached_info.last_cache_date
|
||||
# Round up to avoid showing microseconds.
|
||||
cache_age = timedelta(seconds=int(cache_age.total_seconds()))
|
||||
self.report_status.append(
|
||||
f"skipping {failed_index} already passed items (cache from {cache_age} ago,"
|
||||
f" use --sw-reset to discard)."
|
||||
)
|
||||
deselected = items[:failed_index]
|
||||
del items[:failed_index]
|
||||
config.hook.pytest_deselected(items=deselected)
|
||||
|
||||
def pytest_runtest_logreport(self, report: TestReport) -> None:
|
||||
if report.failed:
|
||||
if self.skip:
|
||||
# Remove test from the failed ones (if it exists) and unset the skip option
|
||||
# to make sure the following tests will not be skipped.
|
||||
if report.id == self.cached_info.last_failed:
|
||||
self.cached_info.last_failed = None
|
||||
|
||||
self.skip = False
|
||||
else:
|
||||
# Mark test as the last failing and interrupt the test session.
|
||||
self.cached_info.last_failed = report.id
|
||||
assert self.session is not None
|
||||
self.session.shouldstop = (
|
||||
"Test failed, continuing from this test next run."
|
||||
)
|
||||
|
||||
else:
|
||||
# If the test was actually run and did pass.
|
||||
if report.when == "call":
|
||||
# Remove test from the failed ones, if exists.
|
||||
if report.id == self.cached_info.last_failed:
|
||||
self.cached_info.last_failed = None
|
||||
|
||||
def pytest_report_collectionfinish(self) -> list[str] | None:
|
||||
if self.config.get_verbosity() >= 0 and self.report_status:
|
||||
return [f"stepwise: {x}" for x in self.report_status]
|
||||
return None
|
||||
|
||||
def pytest_sessionfinish(self) -> None:
|
||||
if hasattr(self.config, "workerinput"):
|
||||
# Do not update cache if this process is a xdist worker to prevent
|
||||
# race conditions (#10641).
|
||||
return
|
||||
self.cached_info.update_date_to_now()
|
||||
last_failed = self.cached_info.last_failed
|
||||
self.cache.set(
|
||||
STEPWISE_CACHE_DIR,
|
||||
{
|
||||
"last_failed": str(last_failed) if last_failed is not None else None,
|
||||
"last_test_count": self.cached_info.last_test_count,
|
||||
"last_cache_date_str": self.cached_info.last_cache_date_str,
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,412 @@
|
||||
"""Builtin plugin that adds subtests support."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import Mapping
|
||||
from contextlib import AbstractContextManager
|
||||
from contextlib import contextmanager
|
||||
from contextlib import ExitStack
|
||||
from contextlib import nullcontext
|
||||
import dataclasses
|
||||
import time
|
||||
from types import TracebackType
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import pluggy
|
||||
|
||||
from _pytest._code import ExceptionInfo
|
||||
from _pytest._io.saferepr import saferepr
|
||||
from _pytest.capture import CaptureFixture
|
||||
from _pytest.capture import FDCapture
|
||||
from _pytest.capture import SysCapture
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import hookimpl
|
||||
from _pytest.config.argparsing import Parser
|
||||
from _pytest.deprecated import check_ispytest
|
||||
from _pytest.fixtures import fixture
|
||||
from _pytest.fixtures import SubRequest
|
||||
from _pytest.logging import catching_logs
|
||||
from _pytest.logging import LogCaptureHandler
|
||||
from _pytest.logging import LoggingPlugin
|
||||
from _pytest.nodeid import NodeId
|
||||
from _pytest.reports import TestReport
|
||||
from _pytest.runner import CallInfo
|
||||
from _pytest.runner import check_interactive_exception
|
||||
from _pytest.runner import get_reraise_exceptions
|
||||
from _pytest.stash import StashKey
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import Self
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
Config._add_verbosity_ini(
|
||||
parser,
|
||||
Config.VERBOSITY_SUBTESTS,
|
||||
help=(
|
||||
"Specify verbosity level for subtests. "
|
||||
"Higher levels will generate output for passed subtests. Failed subtests are always reported."
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True, slots=True, kw_only=True)
|
||||
class SubtestContext:
|
||||
"""The values passed to Subtests.test() that are included in the test report."""
|
||||
|
||||
msg: str | None
|
||||
kwargs: Mapping[str, Any]
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
# Brute-force the returned kwargs dict to be JSON serializable (pytest-dev/pytest-xdist#1273).
|
||||
object.__setattr__(
|
||||
self, "kwargs", {k: saferepr(v) for (k, v) in self.kwargs.items()}
|
||||
)
|
||||
|
||||
def _to_json(self) -> dict[str, Any]:
|
||||
result = dataclasses.asdict(self)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def _from_json(cls, d: dict[str, Any]) -> Self:
|
||||
return cls(msg=d["msg"], kwargs=d["kwargs"])
|
||||
|
||||
|
||||
@dataclasses.dataclass(init=False)
|
||||
class SubtestReport(TestReport):
|
||||
context: SubtestContext
|
||||
|
||||
@property
|
||||
def head_line(self) -> str:
|
||||
_, _, domain = self.location
|
||||
return f"{domain} {self._sub_test_description()}"
|
||||
|
||||
def _sub_test_description(self) -> str:
|
||||
parts = []
|
||||
if self.context.msg is not None:
|
||||
parts.append(f"[{self.context.msg}]")
|
||||
if self.context.kwargs:
|
||||
params_desc = ", ".join(
|
||||
f"{k}={v}" for (k, v) in self.context.kwargs.items()
|
||||
)
|
||||
parts.append(f"({params_desc})")
|
||||
return " ".join(parts) or "(<subtest>)"
|
||||
|
||||
def _to_json(self) -> dict[str, Any]:
|
||||
data = super()._to_json()
|
||||
del data["context"]
|
||||
data["_report_type"] = "SubTestReport"
|
||||
data["_subtest.context"] = self.context._to_json()
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def _from_json(cls, reportdict: dict[str, Any]) -> SubtestReport:
|
||||
report = super()._from_json(reportdict)
|
||||
report.context = SubtestContext._from_json(reportdict["_subtest.context"])
|
||||
return report
|
||||
|
||||
@classmethod
|
||||
def _new(
|
||||
cls,
|
||||
test_report: TestReport,
|
||||
context: SubtestContext,
|
||||
captured_output: Captured | None,
|
||||
captured_logs: CapturedLogs | None,
|
||||
) -> Self:
|
||||
result = super()._from_json(test_report._to_json())
|
||||
result.context = context
|
||||
|
||||
if captured_output:
|
||||
if captured_output.out:
|
||||
result.sections.append(("Captured stdout call", captured_output.out))
|
||||
if captured_output.err:
|
||||
result.sections.append(("Captured stderr call", captured_output.err))
|
||||
|
||||
if captured_logs and (log := captured_logs.handler.stream.getvalue()):
|
||||
result.sections.append(("Captured log call", log))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@fixture
|
||||
def subtests(request: SubRequest) -> Subtests:
|
||||
"""Provides subtests functionality."""
|
||||
capmam = request.node.config.pluginmanager.get_plugin("capturemanager")
|
||||
suspend_capture_ctx = (
|
||||
capmam.global_and_fixture_disabled if capmam is not None else nullcontext
|
||||
)
|
||||
return Subtests(request.node.ihook, suspend_capture_ctx, request, _ispytest=True)
|
||||
|
||||
|
||||
class Subtests:
|
||||
"""Subtests fixture, enables declaring subtests inside test functions via the :meth:`test` method."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
ihook: pluggy.HookRelay,
|
||||
suspend_capture_ctx: Callable[[], AbstractContextManager[None]],
|
||||
request: SubRequest,
|
||||
*,
|
||||
_ispytest: bool = False,
|
||||
) -> None:
|
||||
check_ispytest(_ispytest)
|
||||
self._ihook = ihook
|
||||
self._suspend_capture_ctx = suspend_capture_ctx
|
||||
self._request = request
|
||||
|
||||
def test(
|
||||
self,
|
||||
msg: str | None = None,
|
||||
**kwargs: Any,
|
||||
) -> _SubTestContextManager:
|
||||
"""
|
||||
Context manager for subtests, capturing exceptions raised inside the subtest scope and
|
||||
reporting assertion failures and errors individually.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
def test(subtests):
|
||||
for i in range(5):
|
||||
with subtests.test("custom message", i=i):
|
||||
assert i % 2 == 0
|
||||
|
||||
:param msg:
|
||||
If given, the message will be shown in the test report in case of subtest failure.
|
||||
|
||||
:param kwargs:
|
||||
Arbitrary values that are also added to the subtest report.
|
||||
"""
|
||||
return _SubTestContextManager(
|
||||
self._ihook,
|
||||
msg,
|
||||
kwargs,
|
||||
request=self._request,
|
||||
suspend_capture_ctx=self._suspend_capture_ctx,
|
||||
config=self._request.config,
|
||||
)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class _SubTestContextManager:
|
||||
"""
|
||||
Context manager for subtests, capturing exceptions raised inside the subtest scope and handling
|
||||
them through the pytest machinery.
|
||||
"""
|
||||
|
||||
# Note: initially the logic for this context manager was implemented directly
|
||||
# in Subtests.test() as a @contextmanager, however, it is not possible to control the output fully when
|
||||
# exiting from it due to an exception when in `--exitfirst` mode, so this was refactored into an
|
||||
# explicit context manager class (pytest-dev/pytest-subtests#134).
|
||||
|
||||
ihook: pluggy.HookRelay
|
||||
msg: str | None
|
||||
kwargs: dict[str, Any]
|
||||
suspend_capture_ctx: Callable[[], AbstractContextManager[None]]
|
||||
request: SubRequest
|
||||
config: Config
|
||||
|
||||
def __enter__(self) -> None:
|
||||
__tracebackhide__ = True
|
||||
|
||||
self._start = time.time()
|
||||
self._precise_start = time.perf_counter()
|
||||
self._exc_info = None
|
||||
|
||||
self._exit_stack = ExitStack()
|
||||
self._captured_output = self._exit_stack.enter_context(
|
||||
capturing_output(self.request)
|
||||
)
|
||||
self._captured_logs = self._exit_stack.enter_context(
|
||||
capturing_logs(self.request)
|
||||
)
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: TracebackType | None,
|
||||
) -> bool:
|
||||
__tracebackhide__ = True
|
||||
if exc_val is not None:
|
||||
exc_info = ExceptionInfo.from_exception(exc_val)
|
||||
else:
|
||||
exc_info = None
|
||||
|
||||
self._exit_stack.close()
|
||||
|
||||
precise_stop = time.perf_counter()
|
||||
duration = precise_stop - self._precise_start
|
||||
stop = time.time()
|
||||
|
||||
call_info = CallInfo[None](
|
||||
None,
|
||||
exc_info,
|
||||
start=self._start,
|
||||
stop=stop,
|
||||
duration=duration,
|
||||
when="call",
|
||||
_ispytest=True,
|
||||
)
|
||||
report = self.ihook.pytest_runtest_makereport(
|
||||
item=self.request.node, call=call_info
|
||||
)
|
||||
sub_report = SubtestReport._new(
|
||||
report,
|
||||
SubtestContext(msg=self.msg, kwargs=self.kwargs),
|
||||
captured_output=self._captured_output,
|
||||
captured_logs=self._captured_logs,
|
||||
)
|
||||
|
||||
if sub_report.failed:
|
||||
failed_subtests = self.config.stash[failed_subtests_key]
|
||||
failed_subtests[self.request.node.id] += 1
|
||||
|
||||
with self.suspend_capture_ctx():
|
||||
self.ihook.pytest_runtest_logreport(report=sub_report)
|
||||
|
||||
if check_interactive_exception(call_info, sub_report):
|
||||
self.ihook.pytest_exception_interact(
|
||||
node=self.request.node, call=call_info, report=sub_report
|
||||
)
|
||||
|
||||
if exc_val is not None:
|
||||
if isinstance(exc_val, get_reraise_exceptions(self.config)):
|
||||
return False
|
||||
if self.request.session.shouldfail:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@contextmanager
|
||||
def capturing_output(request: SubRequest) -> Iterator[Captured]:
|
||||
option = request.config.getoption("capture", None)
|
||||
|
||||
capman = request.config.pluginmanager.getplugin("capturemanager")
|
||||
if getattr(capman, "_capture_fixture", None):
|
||||
# capsys or capfd are active, subtest should not capture.
|
||||
fixture = None
|
||||
elif option == "sys":
|
||||
fixture = CaptureFixture(SysCapture, request, _ispytest=True)
|
||||
elif option == "fd":
|
||||
fixture = CaptureFixture(FDCapture, request, _ispytest=True)
|
||||
else:
|
||||
fixture = None
|
||||
|
||||
if fixture is not None:
|
||||
fixture._start()
|
||||
|
||||
captured = Captured()
|
||||
try:
|
||||
yield captured
|
||||
finally:
|
||||
if fixture is not None:
|
||||
out, err = fixture.readouterr()
|
||||
fixture.close()
|
||||
captured.out = out
|
||||
captured.err = err
|
||||
|
||||
|
||||
@contextmanager
|
||||
def capturing_logs(
|
||||
request: SubRequest,
|
||||
) -> Iterator[CapturedLogs | None]:
|
||||
logging_plugin: LoggingPlugin | None = request.config.pluginmanager.getplugin(
|
||||
"logging-plugin"
|
||||
)
|
||||
if logging_plugin is None:
|
||||
yield None
|
||||
else:
|
||||
handler = LogCaptureHandler()
|
||||
handler.setFormatter(logging_plugin.formatter)
|
||||
|
||||
captured_logs = CapturedLogs(handler)
|
||||
with catching_logs(handler, level=logging_plugin.log_level):
|
||||
yield captured_logs
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Captured:
|
||||
out: str = ""
|
||||
err: str = ""
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class CapturedLogs:
|
||||
handler: LogCaptureHandler
|
||||
|
||||
|
||||
def pytest_report_to_serializable(report: TestReport) -> dict[str, Any] | None:
|
||||
if isinstance(report, SubtestReport):
|
||||
return report._to_json()
|
||||
return None
|
||||
|
||||
|
||||
def pytest_report_from_serializable(data: dict[str, Any]) -> SubtestReport | None:
|
||||
if data.get("_report_type") == "SubTestReport":
|
||||
return SubtestReport._from_json(data)
|
||||
return None
|
||||
|
||||
|
||||
# Dict of NodeId -> number of failed subtests.
|
||||
# Used to fail top-level tests that passed but contain failed subtests.
|
||||
failed_subtests_key = StashKey[defaultdict[NodeId, int]]()
|
||||
|
||||
|
||||
def pytest_configure(config: Config) -> None:
|
||||
config.stash[failed_subtests_key] = defaultdict(int)
|
||||
|
||||
|
||||
@hookimpl(tryfirst=True)
|
||||
def pytest_report_teststatus(
|
||||
report: TestReport,
|
||||
config: Config,
|
||||
) -> tuple[str, str, str | Mapping[str, bool]] | None:
|
||||
if report.when != "call":
|
||||
return None
|
||||
|
||||
quiet = config.get_verbosity(Config.VERBOSITY_SUBTESTS) == 0
|
||||
if isinstance(report, SubtestReport):
|
||||
outcome = report.outcome
|
||||
description = report._sub_test_description()
|
||||
|
||||
if hasattr(report, "wasxfail"):
|
||||
if quiet:
|
||||
return "", "", ""
|
||||
elif outcome == "skipped":
|
||||
# x letter is used for regular xfail, y for subtest xfail
|
||||
return "xfailed", "y", f"SUBXFAIL{description}"
|
||||
# outcome == "passed" in an xfail is only possible via a @pytest.mark.xfail mark, which
|
||||
# is not applicable to a subtest, which only handles pytest.xfail().
|
||||
else: # pragma: no cover
|
||||
# This should not normally happen, unless some plugin is setting wasxfail without
|
||||
# the correct outcome. Pytest expects the call outcome to be either skipped or
|
||||
# passed in case of xfail.
|
||||
# Let's pass this report to the next hook.
|
||||
return None
|
||||
|
||||
if report.failed:
|
||||
return outcome, "u", f"SUBFAILED{description}"
|
||||
elif quiet:
|
||||
return "", "", ""
|
||||
elif report.passed:
|
||||
return f"subtests {outcome}", "u", f"SUBPASSED{description}"
|
||||
elif report.skipped:
|
||||
return outcome, "-", f"SUBSKIPPED{description}"
|
||||
|
||||
else:
|
||||
failed_subtests_count = config.stash[failed_subtests_key][report.id]
|
||||
# Top-level test, fail if it contains failed subtests and it has passed.
|
||||
if report.passed and failed_subtests_count > 0:
|
||||
report.outcome = "failed"
|
||||
suffix = "s" if failed_subtests_count > 1 else ""
|
||||
report.longrepr = f"contains {failed_subtests_count} failed subtest{suffix}"
|
||||
|
||||
return None
|
||||
File diff suppressed because it is too large.
Load diff
Loaded 100 of 1641 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user