mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 20:44:18 +00:00
detect_gcp_credentials hook
This commit is contained in:
parent
b73acb198e
commit
e0c61d89d0
929 changed files with 311695 additions and 0 deletions
|
|
@ -40,3 +40,12 @@ repos:
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
additional_dependencies: [types-all]
|
additional_dependencies: [types-all]
|
||||||
|
|
||||||
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
rev: v2.4.0 # Use the latest released version of pre-commit-hooks
|
||||||
|
hooks:
|
||||||
|
- id: detect-gcp-credentials
|
||||||
|
name: Detect GCP Credentials
|
||||||
|
entry: hooks/detect_gcp_credentials.py
|
||||||
|
language: python
|
||||||
|
types: [text]
|
||||||
|
|
|
||||||
|
|
@ -199,3 +199,9 @@
|
||||||
language: python
|
language: python
|
||||||
types: [text]
|
types: [text]
|
||||||
stages: [commit, push, manual]
|
stages: [commit, push, manual]
|
||||||
|
- id: detect-gcp-credentials
|
||||||
|
name: detect gcp credentials
|
||||||
|
language: python
|
||||||
|
description: detect gcp hardcoded gcp credentials
|
||||||
|
entry: detect-gcp-credentials
|
||||||
|
types: [text]
|
||||||
|
|
|
||||||
247
.venv/bin/Activate.ps1
Normal file
247
.venv/bin/Activate.ps1
Normal file
|
|
@ -0,0 +1,247 @@
|
||||||
|
<#
|
||||||
|
.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 declarations --------------------------------------------------- #>
|
||||||
|
|
||||||
|
<#
|
||||||
|
.Synopsis
|
||||||
|
Remove all shell session elements added by the Activate script, including the
|
||||||
|
addition of the virtual environment's Python executable from the beginning of
|
||||||
|
the PATH variable.
|
||||||
|
|
||||||
|
.Parameter NonDestructive
|
||||||
|
If present, do not remove this function from the global namespace for the
|
||||||
|
session.
|
||||||
|
|
||||||
|
#>
|
||||||
|
function global:deactivate ([switch]$NonDestructive) {
|
||||||
|
# Revert to original values
|
||||||
|
|
||||||
|
# The prior prompt:
|
||||||
|
if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) {
|
||||||
|
Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt
|
||||||
|
Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT
|
||||||
|
}
|
||||||
|
|
||||||
|
# The prior PYTHONHOME:
|
||||||
|
if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) {
|
||||||
|
Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME
|
||||||
|
Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME
|
||||||
|
}
|
||||||
|
|
||||||
|
# The prior PATH:
|
||||||
|
if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) {
|
||||||
|
Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH
|
||||||
|
Remove-Item -Path Env:_OLD_VIRTUAL_PATH
|
||||||
|
}
|
||||||
|
|
||||||
|
# Just remove the VIRTUAL_ENV altogether:
|
||||||
|
if (Test-Path -Path Env:VIRTUAL_ENV) {
|
||||||
|
Remove-Item -Path env:VIRTUAL_ENV
|
||||||
|
}
|
||||||
|
|
||||||
|
# Just remove VIRTUAL_ENV_PROMPT altogether.
|
||||||
|
if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) {
|
||||||
|
Remove-Item -Path env:VIRTUAL_ENV_PROMPT
|
||||||
|
}
|
||||||
|
|
||||||
|
# Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether:
|
||||||
|
if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
|
||||||
|
Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
|
||||||
|
}
|
||||||
|
|
||||||
|
# Leave deactivate function in the global namespace if requested:
|
||||||
|
if (-not $NonDestructive) {
|
||||||
|
Remove-Item -Path function:deactivate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
<#
|
||||||
|
.Description
|
||||||
|
Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the
|
||||||
|
given folder, and returns them in a map.
|
||||||
|
|
||||||
|
For each line in the pyvenv.cfg file, if that line can be parsed into exactly
|
||||||
|
two strings separated by `=` (with any amount of whitespace surrounding the =)
|
||||||
|
then it is considered a `key = value` line. The left hand string is the key,
|
||||||
|
the right hand is the value.
|
||||||
|
|
||||||
|
If the value starts with a `'` or a `"` then the first and last character is
|
||||||
|
stripped from the value before being captured.
|
||||||
|
|
||||||
|
.Parameter ConfigDir
|
||||||
|
Path to the directory that contains the `pyvenv.cfg` file.
|
||||||
|
#>
|
||||||
|
function Get-PyVenvConfig(
|
||||||
|
[String]
|
||||||
|
$ConfigDir
|
||||||
|
) {
|
||||||
|
Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"
|
||||||
|
|
||||||
|
# Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue).
|
||||||
|
$pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
|
||||||
|
|
||||||
|
# An empty map will be returned if no config file is found.
|
||||||
|
$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]
|
||||||
|
|
||||||
|
# Remove extraneous quotations around a string value.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
<# Begin Activate script --------------------------------------------------- #>
|
||||||
|
|
||||||
|
# Determine the containing directory of this script
|
||||||
|
$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
||||||
|
$VenvExecDir = Get-Item -Path $VenvExecPath
|
||||||
|
|
||||||
|
Write-Verbose "Activation script is located in path: '$VenvExecPath'"
|
||||||
|
Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)"
|
||||||
|
Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)"
|
||||||
|
|
||||||
|
# Set values required in priority: CmdLine, ConfigFile, Default
|
||||||
|
# First, get the location of the virtual environment, it might not be
|
||||||
|
# VenvExecDir if specified on the command line.
|
||||||
|
if ($VenvDir) {
|
||||||
|
Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir."
|
||||||
|
$VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/")
|
||||||
|
Write-Verbose "VenvDir=$VenvDir"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Next, read the `pyvenv.cfg` file to determine any required value such
|
||||||
|
# as `prompt`.
|
||||||
|
$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir
|
||||||
|
|
||||||
|
# Next, set the prompt from the command line, or the config file, or
|
||||||
|
# just use the name of the virtual environment folder.
|
||||||
|
if ($Prompt) {
|
||||||
|
Write-Verbose "Prompt specified as argument, using '$Prompt'"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value"
|
||||||
|
if ($pyvenvCfg -and $pyvenvCfg['prompt']) {
|
||||||
|
Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'"
|
||||||
|
$Prompt = $pyvenvCfg['prompt'];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)"
|
||||||
|
Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'"
|
||||||
|
$Prompt = Split-Path -Path $venvDir -Leaf
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Verbose "Prompt = '$Prompt'"
|
||||||
|
Write-Verbose "VenvDir='$VenvDir'"
|
||||||
|
|
||||||
|
# Deactivate any currently active virtual environment, but leave the
|
||||||
|
# deactivate function in place.
|
||||||
|
deactivate -nondestructive
|
||||||
|
|
||||||
|
# Now set the environment variable VIRTUAL_ENV, used by many tools to determine
|
||||||
|
# that there is an activated venv.
|
||||||
|
$env:VIRTUAL_ENV = $VenvDir
|
||||||
|
|
||||||
|
if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) {
|
||||||
|
|
||||||
|
Write-Verbose "Setting prompt to '$Prompt'"
|
||||||
|
|
||||||
|
# Set the prompt to include the env name
|
||||||
|
# Make sure _OLD_VIRTUAL_PROMPT is global
|
||||||
|
function global:_OLD_VIRTUAL_PROMPT { "" }
|
||||||
|
Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_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
|
||||||
|
}
|
||||||
|
$env:VIRTUAL_ENV_PROMPT = $Prompt
|
||||||
|
}
|
||||||
|
|
||||||
|
# Clear PYTHONHOME
|
||||||
|
if (Test-Path -Path Env:PYTHONHOME) {
|
||||||
|
Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME
|
||||||
|
Remove-Item -Path Env:PYTHONHOME
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add the venv to the PATH
|
||||||
|
Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH
|
||||||
|
$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH"
|
||||||
69
.venv/bin/activate
Normal file
69
.venv/bin/activate
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
# This file must be used with "source bin/activate" *from bash*
|
||||||
|
# you cannot run it directly
|
||||||
|
|
||||||
|
deactivate () {
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# This should detect bash and zsh, which have a hash command that must
|
||||||
|
# be called to get it to forget past commands. Without forgetting
|
||||||
|
# past commands the $PATH changes we made may not be respected
|
||||||
|
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
|
||||||
|
hash -r 2> /dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
VIRTUAL_ENV="/Users/admin/Git_repos/pre-commit-hooks/.venv"
|
||||||
|
export VIRTUAL_ENV
|
||||||
|
|
||||||
|
_OLD_VIRTUAL_PATH="$PATH"
|
||||||
|
PATH="$VIRTUAL_ENV/bin:$PATH"
|
||||||
|
export PATH
|
||||||
|
|
||||||
|
# unset PYTHONHOME if set
|
||||||
|
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
|
||||||
|
# could use `if (set -u; : $PYTHONHOME) ;` in bash
|
||||||
|
if [ -n "${PYTHONHOME:-}" ] ; then
|
||||||
|
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
|
||||||
|
unset PYTHONHOME
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
|
||||||
|
_OLD_VIRTUAL_PS1="${PS1:-}"
|
||||||
|
PS1="(.venv) ${PS1:-}"
|
||||||
|
export PS1
|
||||||
|
VIRTUAL_ENV_PROMPT="(.venv) "
|
||||||
|
export VIRTUAL_ENV_PROMPT
|
||||||
|
fi
|
||||||
|
|
||||||
|
# This should detect bash and zsh, which have a hash command that must
|
||||||
|
# be called to get it to forget past commands. Without forgetting
|
||||||
|
# past commands the $PATH changes we made may not be respected
|
||||||
|
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
|
||||||
|
hash -r 2> /dev/null
|
||||||
|
fi
|
||||||
26
.venv/bin/activate.csh
Normal file
26
.venv/bin/activate.csh
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
# 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>.
|
||||||
|
# Ported to Python 3.3 venv by Andrew Svetlov <andrew.svetlov@gmail.com>
|
||||||
|
|
||||||
|
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate'
|
||||||
|
|
||||||
|
# Unset irrelevant variables.
|
||||||
|
deactivate nondestructive
|
||||||
|
|
||||||
|
setenv VIRTUAL_ENV "/Users/admin/Git_repos/pre-commit-hooks/.venv"
|
||||||
|
|
||||||
|
set _OLD_VIRTUAL_PATH="$PATH"
|
||||||
|
setenv PATH "$VIRTUAL_ENV/bin:$PATH"
|
||||||
|
|
||||||
|
|
||||||
|
set _OLD_VIRTUAL_PROMPT="$prompt"
|
||||||
|
|
||||||
|
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
|
||||||
|
set prompt = "(.venv) $prompt"
|
||||||
|
setenv VIRTUAL_ENV_PROMPT "(.venv) "
|
||||||
|
endif
|
||||||
|
|
||||||
|
alias pydoc python -m pydoc
|
||||||
|
|
||||||
|
rehash
|
||||||
66
.venv/bin/activate.fish
Normal file
66
.venv/bin/activate.fish
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
# This file must be used with "source <venv>/bin/activate.fish" *from fish*
|
||||||
|
# (https://fishshell.com/); you cannot run it directly.
|
||||||
|
|
||||||
|
function deactivate -d "Exit virtual environment and return to normal shell 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 "$_OLD_VIRTUAL_PYTHONHOME"
|
||||||
|
set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME
|
||||||
|
set -e _OLD_VIRTUAL_PYTHONHOME
|
||||||
|
end
|
||||||
|
|
||||||
|
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
|
||||||
|
functions -e fish_prompt
|
||||||
|
set -e _OLD_FISH_PROMPT_OVERRIDE
|
||||||
|
functions -c _old_fish_prompt fish_prompt
|
||||||
|
functions -e _old_fish_prompt
|
||||||
|
end
|
||||||
|
|
||||||
|
set -e VIRTUAL_ENV
|
||||||
|
set -e VIRTUAL_ENV_PROMPT
|
||||||
|
if test "$argv[1]" != "nondestructive"
|
||||||
|
# Self-destruct!
|
||||||
|
functions -e deactivate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Unset irrelevant variables.
|
||||||
|
deactivate nondestructive
|
||||||
|
|
||||||
|
set -gx VIRTUAL_ENV "/Users/admin/Git_repos/pre-commit-hooks/.venv"
|
||||||
|
|
||||||
|
set -gx _OLD_VIRTUAL_PATH $PATH
|
||||||
|
set -gx PATH "$VIRTUAL_ENV/bin" $PATH
|
||||||
|
|
||||||
|
# Unset PYTHONHOME if set.
|
||||||
|
if set -q PYTHONHOME
|
||||||
|
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
|
||||||
|
set -e PYTHONHOME
|
||||||
|
end
|
||||||
|
|
||||||
|
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
|
||||||
|
# fish uses a function instead of an env var to generate the prompt.
|
||||||
|
|
||||||
|
# Save the current fish_prompt function as the function _old_fish_prompt.
|
||||||
|
functions -c fish_prompt _old_fish_prompt
|
||||||
|
|
||||||
|
# With the original prompt function renamed, we can override with our own.
|
||||||
|
function fish_prompt
|
||||||
|
# Save the return status of the last command.
|
||||||
|
set -l old_status $status
|
||||||
|
|
||||||
|
# Output the venv prompt; color taken from the blue of the Python logo.
|
||||||
|
printf "%s%s%s" (set_color 4B8BBE) "(.venv) " (set_color normal)
|
||||||
|
|
||||||
|
# Restore the return status of the previous command.
|
||||||
|
echo "exit $old_status" | .
|
||||||
|
# Output the original/"old" prompt.
|
||||||
|
_old_fish_prompt
|
||||||
|
end
|
||||||
|
|
||||||
|
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
|
||||||
|
set -gx VIRTUAL_ENV_PROMPT "(.venv) "
|
||||||
|
end
|
||||||
8
.venv/bin/coverage
Executable file
8
.venv/bin/coverage
Executable file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/Users/admin/Git_repos/pre-commit-hooks/.venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from coverage.cmdline import main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(main())
|
||||||
8
.venv/bin/coverage-3.10
Executable file
8
.venv/bin/coverage-3.10
Executable file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/Users/admin/Git_repos/pre-commit-hooks/.venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from coverage.cmdline import main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(main())
|
||||||
8
.venv/bin/coverage3
Executable file
8
.venv/bin/coverage3
Executable file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/Users/admin/Git_repos/pre-commit-hooks/.venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from coverage.cmdline import main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(main())
|
||||||
8
.venv/bin/pip
Executable file
8
.venv/bin/pip
Executable file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/Users/admin/Git_repos/pre-commit-hooks/.venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pip._internal.cli.main import main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(main())
|
||||||
8
.venv/bin/pip3
Executable file
8
.venv/bin/pip3
Executable file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/Users/admin/Git_repos/pre-commit-hooks/.venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pip._internal.cli.main import main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(main())
|
||||||
8
.venv/bin/pip3.10
Executable file
8
.venv/bin/pip3.10
Executable file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/Users/admin/Git_repos/pre-commit-hooks/.venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pip._internal.cli.main import main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(main())
|
||||||
8
.venv/bin/py.test
Executable file
8
.venv/bin/py.test
Executable file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/Users/admin/Git_repos/pre-commit-hooks/.venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pytest import console_main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(console_main())
|
||||||
8
.venv/bin/pytest
Executable file
8
.venv/bin/pytest
Executable file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/Users/admin/Git_repos/pre-commit-hooks/.venv/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from pytest import console_main
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||||
|
sys.exit(console_main())
|
||||||
1
.venv/bin/python
Symbolic link
1
.venv/bin/python
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
python3
|
||||||
1
.venv/bin/python3
Symbolic link
1
.venv/bin/python3
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
/usr/local/bin/python3
|
||||||
1
.venv/bin/python3.10
Symbolic link
1
.venv/bin/python3.10
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
python3
|
||||||
128
.venv/lib/python3.10/site-packages/_distutils_hack/__init__.py
Normal file
128
.venv/lib/python3.10/site-packages/_distutils_hack/__init__.py
Normal file
|
|
@ -0,0 +1,128 @@
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import importlib
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
|
is_pypy = '__pypy__' in sys.builtin_module_names
|
||||||
|
|
||||||
|
|
||||||
|
warnings.filterwarnings('ignore',
|
||||||
|
r'.+ distutils\b.+ deprecated',
|
||||||
|
DeprecationWarning)
|
||||||
|
|
||||||
|
|
||||||
|
def warn_distutils_present():
|
||||||
|
if 'distutils' not in sys.modules:
|
||||||
|
return
|
||||||
|
if is_pypy and sys.version_info < (3, 7):
|
||||||
|
# PyPy for 3.6 unconditionally imports distutils, so bypass the warning
|
||||||
|
# https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
|
||||||
|
return
|
||||||
|
warnings.warn(
|
||||||
|
"Distutils was imported before Setuptools, but importing Setuptools "
|
||||||
|
"also replaces the `distutils` module in `sys.modules`. This may lead "
|
||||||
|
"to undesirable behaviors or errors. To avoid these issues, avoid "
|
||||||
|
"using distutils directly, ensure that setuptools is installed in the "
|
||||||
|
"traditional way (e.g. not an editable install), and/or make sure "
|
||||||
|
"that setuptools is always imported before distutils.")
|
||||||
|
|
||||||
|
|
||||||
|
def clear_distutils():
|
||||||
|
if 'distutils' not in sys.modules:
|
||||||
|
return
|
||||||
|
warnings.warn("Setuptools is replacing distutils.")
|
||||||
|
mods = [name for name in sys.modules if re.match(r'distutils\b', name)]
|
||||||
|
for name in mods:
|
||||||
|
del sys.modules[name]
|
||||||
|
|
||||||
|
|
||||||
|
def enabled():
|
||||||
|
"""
|
||||||
|
Allow selection of distutils by environment variable.
|
||||||
|
"""
|
||||||
|
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
|
||||||
|
return which == 'local'
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_local_distutils():
|
||||||
|
clear_distutils()
|
||||||
|
distutils = importlib.import_module('setuptools._distutils')
|
||||||
|
distutils.__name__ = 'distutils'
|
||||||
|
sys.modules['distutils'] = distutils
|
||||||
|
|
||||||
|
# sanity check that submodules load as expected
|
||||||
|
core = importlib.import_module('distutils.core')
|
||||||
|
assert '_distutils' in core.__file__, core.__file__
|
||||||
|
|
||||||
|
|
||||||
|
def do_override():
|
||||||
|
"""
|
||||||
|
Ensure that the local copy of distutils is preferred over stdlib.
|
||||||
|
|
||||||
|
See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
|
||||||
|
for more motivation.
|
||||||
|
"""
|
||||||
|
if enabled():
|
||||||
|
warn_distutils_present()
|
||||||
|
ensure_local_distutils()
|
||||||
|
|
||||||
|
|
||||||
|
class DistutilsMetaFinder:
|
||||||
|
def find_spec(self, fullname, path, target=None):
|
||||||
|
if path is not None:
|
||||||
|
return
|
||||||
|
|
||||||
|
method_name = 'spec_for_{fullname}'.format(**locals())
|
||||||
|
method = getattr(self, method_name, lambda: None)
|
||||||
|
return method()
|
||||||
|
|
||||||
|
def spec_for_distutils(self):
|
||||||
|
import importlib.abc
|
||||||
|
import importlib.util
|
||||||
|
|
||||||
|
class DistutilsLoader(importlib.abc.Loader):
|
||||||
|
|
||||||
|
def create_module(self, spec):
|
||||||
|
return importlib.import_module('setuptools._distutils')
|
||||||
|
|
||||||
|
def exec_module(self, module):
|
||||||
|
pass
|
||||||
|
|
||||||
|
return importlib.util.spec_from_loader('distutils', DistutilsLoader())
|
||||||
|
|
||||||
|
def spec_for_pip(self):
|
||||||
|
"""
|
||||||
|
Ensure stdlib distutils when running under pip.
|
||||||
|
See pypa/pip#8761 for rationale.
|
||||||
|
"""
|
||||||
|
if self.pip_imported_during_build():
|
||||||
|
return
|
||||||
|
clear_distutils()
|
||||||
|
self.spec_for_distutils = lambda: None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def pip_imported_during_build():
|
||||||
|
"""
|
||||||
|
Detect if pip is being imported in a build script. Ref #2355.
|
||||||
|
"""
|
||||||
|
import traceback
|
||||||
|
return any(
|
||||||
|
frame.f_globals['__file__'].endswith('setup.py')
|
||||||
|
for frame, line in traceback.walk_stack(None)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
DISTUTILS_FINDER = DistutilsMetaFinder()
|
||||||
|
|
||||||
|
|
||||||
|
def add_shim():
|
||||||
|
sys.meta_path.insert(0, DISTUTILS_FINDER)
|
||||||
|
|
||||||
|
|
||||||
|
def remove_shim():
|
||||||
|
try:
|
||||||
|
sys.meta_path.remove(DISTUTILS_FINDER)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
__import__('_distutils_hack').do_override()
|
||||||
10
.venv/lib/python3.10/site-packages/_pytest/__init__.py
Normal file
10
.venv/lib/python3.10/site-packages/_pytest/__init__.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
__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") # type:ignore[assignment]
|
||||||
117
.venv/lib/python3.10/site-packages/_pytest/_argcomplete.py
Normal file
117
.venv/lib/python3.10/site-packages/_pytest/_argcomplete.py
Normal file
|
|
@ -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).
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
from glob import glob
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import Any
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
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: Optional[FastFilesCompleter] = 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
|
||||||
24
.venv/lib/python3.10/site-packages/_pytest/_code/__init__.py
Normal file
24
.venv/lib/python3.10/site-packages/_pytest/_code/__init__.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
"""Python inspection/code generation API."""
|
||||||
|
|
||||||
|
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",
|
||||||
|
"filter_traceback",
|
||||||
|
"Frame",
|
||||||
|
"getfslineno",
|
||||||
|
"getrawcode",
|
||||||
|
"Traceback",
|
||||||
|
"TracebackEntry",
|
||||||
|
"Source",
|
||||||
|
]
|
||||||
1411
.venv/lib/python3.10/site-packages/_pytest/_code/code.py
Normal file
1411
.venv/lib/python3.10/site-packages/_pytest/_code/code.py
Normal file
File diff suppressed because it is too large
Load diff
219
.venv/lib/python3.10/site-packages/_pytest/_code/source.py
Normal file
219
.venv/lib/python3.10/site-packages/_pytest/_code/source.py
Normal file
|
|
@ -0,0 +1,219 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
import ast
|
||||||
|
from bisect import bisect_right
|
||||||
|
import inspect
|
||||||
|
import textwrap
|
||||||
|
import tokenize
|
||||||
|
import types
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import Iterator
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import overload
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Union
|
||||||
|
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] = []
|
||||||
|
elif isinstance(obj, Source):
|
||||||
|
self.lines = obj.lines
|
||||||
|
elif isinstance(obj, (tuple, list)):
|
||||||
|
self.lines = deindent(x.rstrip("\n") for x in obj)
|
||||||
|
elif isinstance(obj, str):
|
||||||
|
self.lines = deindent(obj.split("\n"))
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
rawcode = getrawcode(obj)
|
||||||
|
src = inspect.getsource(rawcode)
|
||||||
|
except TypeError:
|
||||||
|
src = inspect.getsource(obj) # type: ignore[arg-type]
|
||||||
|
self.lines = deindent(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: Union[int, slice]) -> Union[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]
|
||||||
|
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.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.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)
|
||||||
|
return newsource
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return "\n".join(self.lines)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# helper functions
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
def findsource(obj) -> Tuple[Optional[Source], int]:
|
||||||
|
try:
|
||||||
|
sourcelines, lineno = inspect.findsource(obj)
|
||||||
|
except Exception:
|
||||||
|
return None, -1
|
||||||
|
source = Source()
|
||||||
|
source.lines = [line.rstrip() for line in 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 get_statement_startend2(lineno: int, node: ast.AST) -> Tuple[int, Optional[int]]:
|
||||||
|
# Flatten all statements and except handlers into one lineno-list.
|
||||||
|
# AST's line numbers start indexing at 1.
|
||||||
|
values: List[int] = []
|
||||||
|
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: Optional[List[ast.stmt]] = getattr(x, name, None)
|
||||||
|
if val:
|
||||||
|
# Treat the finally/orelse part as its own statement.
|
||||||
|
values.append(val[0].lineno - 1 - 1)
|
||||||
|
values.sort()
|
||||||
|
insert_index = bisect_right(values, lineno)
|
||||||
|
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: Optional[ast.AST] = 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.
|
||||||
|
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,8 @@
|
||||||
|
from .terminalwriter import get_terminal_width
|
||||||
|
from .terminalwriter import TerminalWriter
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"TerminalWriter",
|
||||||
|
"get_terminal_width",
|
||||||
|
]
|
||||||
676
.venv/lib/python3.10/site-packages/_pytest/_io/pprint.py
Normal file
676
.venv/lib/python3.10/site-packages/_pytest/_io/pprint.py
Normal file
|
|
@ -0,0 +1,676 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
# This module was imported from the cpython standard library
|
||||||
|
# (https://github.com/python/cpython/) at commit
|
||||||
|
# c5140945c723ae6c4b7ee81ff720ac8ea4b52cfd (python3.12).
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
import collections as _collections
|
||||||
|
import dataclasses as _dataclasses
|
||||||
|
from io import StringIO as _StringIO
|
||||||
|
import re
|
||||||
|
import types as _types
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Dict
|
||||||
|
from typing import IO
|
||||||
|
from typing import Iterator
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Set
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
|
||||||
|
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: Optional[int] = 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:
|
||||||
|
sio = _StringIO()
|
||||||
|
self._format(object, sio, 0, 0, set(), 0)
|
||||||
|
return sio.getvalue()
|
||||||
|
|
||||||
|
def _format(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
objid = id(object)
|
||||||
|
if objid in context:
|
||||||
|
stream.write(_recursion(object))
|
||||||
|
return
|
||||||
|
|
||||||
|
p = self._dispatch.get(type(object).__repr__, None)
|
||||||
|
if p is not None:
|
||||||
|
context.add(objid)
|
||||||
|
p(self, object, stream, indent, allowance, context, level + 1)
|
||||||
|
context.remove(objid)
|
||||||
|
elif (
|
||||||
|
_dataclasses.is_dataclass(object)
|
||||||
|
and not isinstance(object, type)
|
||||||
|
and object.__dataclass_params__.repr
|
||||||
|
and
|
||||||
|
# Check dataclass has generated repr method.
|
||||||
|
hasattr(object.__repr__, "__wrapped__")
|
||||||
|
and "__create_fn__" in object.__repr__.__wrapped__.__qualname__
|
||||||
|
):
|
||||||
|
context.add(objid)
|
||||||
|
self._pprint_dataclass(
|
||||||
|
object, stream, indent, allowance, context, level + 1
|
||||||
|
)
|
||||||
|
context.remove(objid)
|
||||||
|
else:
|
||||||
|
stream.write(self._repr(object, context, level))
|
||||||
|
|
||||||
|
def _pprint_dataclass(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
cls_name = object.__class__.__name__
|
||||||
|
items = [
|
||||||
|
(f.name, getattr(object, f.name))
|
||||||
|
for f in _dataclasses.fields(object)
|
||||||
|
if f.repr
|
||||||
|
]
|
||||||
|
stream.write(cls_name + "(")
|
||||||
|
self._format_namespace_items(items, stream, indent, allowance, context, level)
|
||||||
|
stream.write(")")
|
||||||
|
|
||||||
|
_dispatch: Dict[
|
||||||
|
Callable[..., str],
|
||||||
|
Callable[["PrettyPrinter", Any, IO[str], int, int, Set[int], int], None],
|
||||||
|
] = {}
|
||||||
|
|
||||||
|
def _pprint_dict(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
write = stream.write
|
||||||
|
write("{")
|
||||||
|
items = sorted(object.items(), key=_safe_tuple)
|
||||||
|
self._format_dict_items(items, stream, indent, allowance, context, level)
|
||||||
|
write("}")
|
||||||
|
|
||||||
|
_dispatch[dict.__repr__] = _pprint_dict
|
||||||
|
|
||||||
|
def _pprint_ordered_dict(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
if not len(object):
|
||||||
|
stream.write(repr(object))
|
||||||
|
return
|
||||||
|
cls = object.__class__
|
||||||
|
stream.write(cls.__name__ + "(")
|
||||||
|
self._pprint_dict(object, stream, indent, allowance, context, level)
|
||||||
|
stream.write(")")
|
||||||
|
|
||||||
|
_dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict
|
||||||
|
|
||||||
|
def _pprint_list(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
stream.write("[")
|
||||||
|
self._format_items(object, stream, indent, allowance, context, level)
|
||||||
|
stream.write("]")
|
||||||
|
|
||||||
|
_dispatch[list.__repr__] = _pprint_list
|
||||||
|
|
||||||
|
def _pprint_tuple(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
stream.write("(")
|
||||||
|
self._format_items(object, stream, indent, allowance, context, level)
|
||||||
|
stream.write(")")
|
||||||
|
|
||||||
|
_dispatch[tuple.__repr__] = _pprint_tuple
|
||||||
|
|
||||||
|
def _pprint_set(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
if not len(object):
|
||||||
|
stream.write(repr(object))
|
||||||
|
return
|
||||||
|
typ = object.__class__
|
||||||
|
if typ is set:
|
||||||
|
stream.write("{")
|
||||||
|
endchar = "}"
|
||||||
|
else:
|
||||||
|
stream.write(typ.__name__ + "({")
|
||||||
|
endchar = "})"
|
||||||
|
object = sorted(object, key=_safe_key)
|
||||||
|
self._format_items(object, stream, indent, allowance, context, level)
|
||||||
|
stream.write(endchar)
|
||||||
|
|
||||||
|
_dispatch[set.__repr__] = _pprint_set
|
||||||
|
_dispatch[frozenset.__repr__] = _pprint_set
|
||||||
|
|
||||||
|
def _pprint_str(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
write = stream.write
|
||||||
|
if not len(object):
|
||||||
|
write(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:
|
||||||
|
chunks.append(repr(current))
|
||||||
|
if len(chunks) == 1:
|
||||||
|
write(rep)
|
||||||
|
return
|
||||||
|
if level == 1:
|
||||||
|
write("(")
|
||||||
|
for i, rep in enumerate(chunks):
|
||||||
|
if i > 0:
|
||||||
|
write("\n" + " " * indent)
|
||||||
|
write(rep)
|
||||||
|
if level == 1:
|
||||||
|
write(")")
|
||||||
|
|
||||||
|
_dispatch[str.__repr__] = _pprint_str
|
||||||
|
|
||||||
|
def _pprint_bytes(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
write = stream.write
|
||||||
|
if len(object) <= 4:
|
||||||
|
write(repr(object))
|
||||||
|
return
|
||||||
|
parens = level == 1
|
||||||
|
if parens:
|
||||||
|
indent += 1
|
||||||
|
allowance += 1
|
||||||
|
write("(")
|
||||||
|
delim = ""
|
||||||
|
for rep in _wrap_bytes_repr(object, self._width - indent, allowance):
|
||||||
|
write(delim)
|
||||||
|
write(rep)
|
||||||
|
if not delim:
|
||||||
|
delim = "\n" + " " * indent
|
||||||
|
if parens:
|
||||||
|
write(")")
|
||||||
|
|
||||||
|
_dispatch[bytes.__repr__] = _pprint_bytes
|
||||||
|
|
||||||
|
def _pprint_bytearray(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
write = stream.write
|
||||||
|
write("bytearray(")
|
||||||
|
self._pprint_bytes(
|
||||||
|
bytes(object), stream, indent + 10, allowance + 1, context, level + 1
|
||||||
|
)
|
||||||
|
write(")")
|
||||||
|
|
||||||
|
_dispatch[bytearray.__repr__] = _pprint_bytearray
|
||||||
|
|
||||||
|
def _pprint_mappingproxy(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
stream.write("mappingproxy(")
|
||||||
|
self._format(object.copy(), stream, indent, allowance, context, level)
|
||||||
|
stream.write(")")
|
||||||
|
|
||||||
|
_dispatch[_types.MappingProxyType.__repr__] = _pprint_mappingproxy
|
||||||
|
|
||||||
|
def _pprint_simplenamespace(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
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()
|
||||||
|
stream.write(cls_name + "(")
|
||||||
|
self._format_namespace_items(items, stream, indent, allowance, context, level)
|
||||||
|
stream.write(")")
|
||||||
|
|
||||||
|
_dispatch[_types.SimpleNamespace.__repr__] = _pprint_simplenamespace
|
||||||
|
|
||||||
|
def _format_dict_items(
|
||||||
|
self,
|
||||||
|
items: List[Tuple[Any, Any]],
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
if not items:
|
||||||
|
return
|
||||||
|
|
||||||
|
write = stream.write
|
||||||
|
item_indent = indent + self._indent_per_level
|
||||||
|
delimnl = "\n" + " " * item_indent
|
||||||
|
for key, ent in items:
|
||||||
|
write(delimnl)
|
||||||
|
write(self._repr(key, context, level))
|
||||||
|
write(": ")
|
||||||
|
self._format(ent, stream, item_indent, 1, context, level)
|
||||||
|
write(",")
|
||||||
|
|
||||||
|
write("\n" + " " * indent)
|
||||||
|
|
||||||
|
def _format_namespace_items(
|
||||||
|
self,
|
||||||
|
items: List[Tuple[Any, Any]],
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
if not items:
|
||||||
|
return
|
||||||
|
|
||||||
|
write = stream.write
|
||||||
|
item_indent = indent + self._indent_per_level
|
||||||
|
delimnl = "\n" + " " * item_indent
|
||||||
|
for key, ent in items:
|
||||||
|
write(delimnl)
|
||||||
|
write(key)
|
||||||
|
write("=")
|
||||||
|
if id(ent) in context:
|
||||||
|
# Special-case representation of recursion to match standard
|
||||||
|
# recursive dataclass repr.
|
||||||
|
write("...")
|
||||||
|
else:
|
||||||
|
self._format(
|
||||||
|
ent,
|
||||||
|
stream,
|
||||||
|
item_indent + len(key) + 1,
|
||||||
|
1,
|
||||||
|
context,
|
||||||
|
level,
|
||||||
|
)
|
||||||
|
|
||||||
|
write(",")
|
||||||
|
|
||||||
|
write("\n" + " " * indent)
|
||||||
|
|
||||||
|
def _format_items(
|
||||||
|
self,
|
||||||
|
items: List[Any],
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
if not items:
|
||||||
|
return
|
||||||
|
|
||||||
|
write = stream.write
|
||||||
|
item_indent = indent + self._indent_per_level
|
||||||
|
delimnl = "\n" + " " * item_indent
|
||||||
|
|
||||||
|
for item in items:
|
||||||
|
write(delimnl)
|
||||||
|
self._format(item, stream, item_indent, 1, context, level)
|
||||||
|
write(",")
|
||||||
|
|
||||||
|
write("\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,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
rdf = self._repr(object.default_factory, context, level)
|
||||||
|
stream.write(f"{object.__class__.__name__}({rdf}, ")
|
||||||
|
self._pprint_dict(object, stream, indent, allowance, context, level)
|
||||||
|
stream.write(")")
|
||||||
|
|
||||||
|
_dispatch[_collections.defaultdict.__repr__] = _pprint_default_dict
|
||||||
|
|
||||||
|
def _pprint_counter(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
stream.write(object.__class__.__name__ + "(")
|
||||||
|
|
||||||
|
if object:
|
||||||
|
stream.write("{")
|
||||||
|
items = object.most_common()
|
||||||
|
self._format_dict_items(items, stream, indent, allowance, context, level)
|
||||||
|
stream.write("}")
|
||||||
|
|
||||||
|
stream.write(")")
|
||||||
|
|
||||||
|
_dispatch[_collections.Counter.__repr__] = _pprint_counter
|
||||||
|
|
||||||
|
def _pprint_chain_map(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
if not len(object.maps) or (len(object.maps) == 1 and not len(object.maps[0])):
|
||||||
|
stream.write(repr(object))
|
||||||
|
return
|
||||||
|
|
||||||
|
stream.write(object.__class__.__name__ + "(")
|
||||||
|
self._format_items(object.maps, stream, indent, allowance, context, level)
|
||||||
|
stream.write(")")
|
||||||
|
|
||||||
|
_dispatch[_collections.ChainMap.__repr__] = _pprint_chain_map
|
||||||
|
|
||||||
|
def _pprint_deque(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
stream.write(object.__class__.__name__ + "(")
|
||||||
|
if object.maxlen is not None:
|
||||||
|
stream.write("maxlen=%d, " % object.maxlen)
|
||||||
|
stream.write("[")
|
||||||
|
|
||||||
|
self._format_items(object, stream, indent, allowance + 1, context, level)
|
||||||
|
stream.write("])")
|
||||||
|
|
||||||
|
_dispatch[_collections.deque.__repr__] = _pprint_deque
|
||||||
|
|
||||||
|
def _pprint_user_dict(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
self._format(object.data, stream, indent, allowance, context, level - 1)
|
||||||
|
|
||||||
|
_dispatch[_collections.UserDict.__repr__] = _pprint_user_dict
|
||||||
|
|
||||||
|
def _pprint_user_list(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
self._format(object.data, stream, indent, allowance, context, level - 1)
|
||||||
|
|
||||||
|
_dispatch[_collections.UserList.__repr__] = _pprint_user_list
|
||||||
|
|
||||||
|
def _pprint_user_string(
|
||||||
|
self,
|
||||||
|
object: Any,
|
||||||
|
stream: IO[str],
|
||||||
|
indent: int,
|
||||||
|
allowance: int,
|
||||||
|
context: Set[int],
|
||||||
|
level: int,
|
||||||
|
) -> None:
|
||||||
|
self._format(object.data, stream, indent, allowance, context, level - 1)
|
||||||
|
|
||||||
|
_dispatch[_collections.UserString.__repr__] = _pprint_user_string
|
||||||
|
|
||||||
|
def _safe_repr(
|
||||||
|
self, object: Any, context: Set[int], maxlevels: Optional[int], level: int
|
||||||
|
) -> str:
|
||||||
|
typ = type(object)
|
||||||
|
if typ in _builtin_scalars:
|
||||||
|
return repr(object)
|
||||||
|
|
||||||
|
r = getattr(typ, "__repr__", None)
|
||||||
|
|
||||||
|
if issubclass(typ, dict) and r is dict.__repr__:
|
||||||
|
if not object:
|
||||||
|
return "{}"
|
||||||
|
objid = id(object)
|
||||||
|
if maxlevels and level >= maxlevels:
|
||||||
|
return "{...}"
|
||||||
|
if objid in context:
|
||||||
|
return _recursion(object)
|
||||||
|
context.add(objid)
|
||||||
|
components: List[str] = []
|
||||||
|
append = components.append
|
||||||
|
level += 1
|
||||||
|
for k, v in sorted(object.items(), key=_safe_tuple):
|
||||||
|
krepr = self._safe_repr(k, context, maxlevels, level)
|
||||||
|
vrepr = self._safe_repr(v, context, maxlevels, level)
|
||||||
|
append(f"{krepr}: {vrepr}")
|
||||||
|
context.remove(objid)
|
||||||
|
return "{%s}" % ", ".join(components)
|
||||||
|
|
||||||
|
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)
|
||||||
130
.venv/lib/python3.10/site-packages/_pytest/_io/saferepr.py
Normal file
130
.venv/lib/python3.10/site-packages/_pytest/_io/saferepr.py
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
import pprint
|
||||||
|
import reprlib
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
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 exc:
|
||||||
|
exc_info = f"unpresentable exception ({_try_repr_or_str(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: Optional[int], 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 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: Optional[int] = 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)
|
||||||
248
.venv/lib/python3.10/site-packages/_pytest/_io/terminalwriter.py
Normal file
248
.venv/lib/python3.10/site-packages/_pytest/_io/terminalwriter.py
Normal file
|
|
@ -0,0 +1,248 @@
|
||||||
|
"""Helper functions for writing to terminals and files."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
from typing import final
|
||||||
|
from typing import Literal
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Sequence
|
||||||
|
from typing import TextIO
|
||||||
|
|
||||||
|
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: Optional[TextIO] = 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: Optional[int] = 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("\x1b[%sm" % cod for cod in esc) + text + "\x1b[0m"
|
||||||
|
return text
|
||||||
|
|
||||||
|
def sep(
|
||||||
|
self,
|
||||||
|
sepchar: str,
|
||||||
|
title: Optional[str] = None,
|
||||||
|
fullwidth: Optional[int] = 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)
|
||||||
|
|
||||||
|
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()
|
||||||
|
for indent, new_line in zip(indents, new_lines):
|
||||||
|
self.line(indent + new_line)
|
||||||
|
|
||||||
|
def _highlight(
|
||||||
|
self, source: str, lexer: Literal["diff", "python"] = "python"
|
||||||
|
) -> str:
|
||||||
|
"""Highlight the given source if we have markup support."""
|
||||||
|
from _pytest.config.exceptions import UsageError
|
||||||
|
|
||||||
|
if not source or not self.hasmarkup or not self.code_highlight:
|
||||||
|
return source
|
||||||
|
|
||||||
|
try:
|
||||||
|
from pygments.formatters.terminal import TerminalFormatter
|
||||||
|
|
||||||
|
if lexer == "python":
|
||||||
|
from pygments.lexers.python import PythonLexer as Lexer
|
||||||
|
elif lexer == "diff":
|
||||||
|
from pygments.lexers.diff import DiffLexer as Lexer
|
||||||
|
from pygments import highlight
|
||||||
|
import pygments.util
|
||||||
|
except ImportError:
|
||||||
|
return source
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
highlighted: str = highlight(
|
||||||
|
source,
|
||||||
|
Lexer(),
|
||||||
|
TerminalFormatter(
|
||||||
|
bg=os.getenv("PYTEST_THEME_MODE", "dark"),
|
||||||
|
style=os.getenv("PYTEST_THEME"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
# 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.
|
||||||
|
return "\x1b[0m" + highlighted
|
||||||
|
except pygments.util.ClassNotFound as e:
|
||||||
|
raise UsageError(
|
||||||
|
"PYTEST_THEME environment variable had an invalid value: '{}'. "
|
||||||
|
"Only valid pygment styles are allowed.".format(
|
||||||
|
os.getenv("PYTEST_THEME")
|
||||||
|
)
|
||||||
|
) from e
|
||||||
|
except pygments.util.OptionError as e:
|
||||||
|
raise UsageError(
|
||||||
|
"PYTEST_THEME_MODE environment variable had an invalid value: '{}'. "
|
||||||
|
"The only allowed values are 'dark' and 'light'.".format(
|
||||||
|
os.getenv("PYTEST_THEME_MODE")
|
||||||
|
)
|
||||||
|
) from e
|
||||||
55
.venv/lib/python3.10/site-packages/_pytest/_io/wcwidth.py
Normal file
55
.venv/lib/python3.10/site-packages/_pytest/_io/wcwidth.py
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
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
|
||||||
111
.venv/lib/python3.10/site-packages/_pytest/_py/error.py
Normal file
111
.venv/lib/python3.10/site-packages/_pytest/_py/error.py
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
"""create errno-specific classes for IO or os calls."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import errno
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import Callable
|
||||||
|
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 unavailiable
|
||||||
|
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, "UnknownErrno%d" % (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
|
||||||
|
errno = value.errno
|
||||||
|
if sys.platform == "win32":
|
||||||
|
try:
|
||||||
|
cls = self._geterrnoclass(_winerrnomap[errno])
|
||||||
|
except KeyError:
|
||||||
|
raise value
|
||||||
|
else:
|
||||||
|
# we are not on Windows, or we got a proper OSError
|
||||||
|
cls = self._geterrnoclass(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]
|
||||||
1490
.venv/lib/python3.10/site-packages/_pytest/_py/path.py
Normal file
1490
.venv/lib/python3.10/site-packages/_pytest/_py/path.py
Normal file
File diff suppressed because it is too large
Load diff
16
.venv/lib/python3.10/site-packages/_pytest/_version.py
Normal file
16
.venv/lib/python3.10/site-packages/_pytest/_version.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
# file generated by setuptools_scm
|
||||||
|
# don't change, don't track in version control
|
||||||
|
TYPE_CHECKING = False
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from typing import Tuple, Union
|
||||||
|
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
||||||
|
else:
|
||||||
|
VERSION_TUPLE = object
|
||||||
|
|
||||||
|
version: str
|
||||||
|
__version__: str
|
||||||
|
__version_tuple__: VERSION_TUPLE
|
||||||
|
version_tuple: VERSION_TUPLE
|
||||||
|
|
||||||
|
__version__ = version = '8.1.1'
|
||||||
|
__version_tuple__ = version_tuple = (8, 1, 1)
|
||||||
191
.venv/lib/python3.10/site-packages/_pytest/assertion/__init__.py
Normal file
191
.venv/lib/python3.10/site-packages/_pytest/assertion/__init__.py
Normal file
|
|
@ -0,0 +1,191 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Support for presenting detailed information in failing assertions."""
|
||||||
|
import sys
|
||||||
|
from typing import Any
|
||||||
|
from typing import Generator
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from _pytest.assertion import rewrite
|
||||||
|
from _pytest.assertion import truncate
|
||||||
|
from _pytest.assertion import util
|
||||||
|
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.",
|
||||||
|
)
|
||||||
|
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 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)))
|
||||||
|
for hook in sys.meta_path:
|
||||||
|
if isinstance(hook, rewrite.AssertionRewritingHook):
|
||||||
|
importhook = hook
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# TODO(typing): Add a protocol for mark_rewrite() and use it
|
||||||
|
# for importhook and for PytestPluginManager.rewrite_hook.
|
||||||
|
importhook = DummyRewriteHook() # type: ignore
|
||||||
|
importhook.mark_rewrite(*names)
|
||||||
|
|
||||||
|
|
||||||
|
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: Optional[rewrite.AssertionRewritingHook] = 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) -> Optional[str]:
|
||||||
|
"""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.truncate_if_required(new_expl, item)
|
||||||
|
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
|
||||||
|
) -> Optional[List[str]]:
|
||||||
|
return util.assertrepr_compare(config=config, op=op, left=left, right=right)
|
||||||
1193
.venv/lib/python3.10/site-packages/_pytest/assertion/rewrite.py
Normal file
1193
.venv/lib/python3.10/site-packages/_pytest/assertion/rewrite.py
Normal file
File diff suppressed because it is too large
Load diff
118
.venv/lib/python3.10/site-packages/_pytest/assertion/truncate.py
Normal file
118
.venv/lib/python3.10/site-packages/_pytest/assertion/truncate.py
Normal file
|
|
@ -0,0 +1,118 @@
|
||||||
|
"""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 typing import List
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from _pytest.assertion import util
|
||||||
|
from _pytest.config import Config
|
||||||
|
from _pytest.nodes import Item
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_MAX_LINES = 8
|
||||||
|
DEFAULT_MAX_CHARS = 8 * 80
|
||||||
|
USAGE_MSG = "use '-vv' to show"
|
||||||
|
|
||||||
|
|
||||||
|
def truncate_if_required(
|
||||||
|
explanation: List[str], item: Item, max_length: Optional[int] = None
|
||||||
|
) -> List[str]:
|
||||||
|
"""Truncate this assertion explanation if the given test item is eligible."""
|
||||||
|
if _should_truncate_item(item):
|
||||||
|
return _truncate_explanation(explanation)
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _should_truncate_item(item: Item) -> bool:
|
||||||
|
"""Whether or not this test item is eligible for truncation."""
|
||||||
|
verbose = item.config.get_verbosity(Config.VERBOSITY_ASSERTIONS)
|
||||||
|
return verbose < 2 and not util.running_on_ci()
|
||||||
|
|
||||||
|
|
||||||
|
def _truncate_explanation(
|
||||||
|
input_lines: List[str],
|
||||||
|
max_lines: Optional[int] = None,
|
||||||
|
max_chars: Optional[int] = None,
|
||||||
|
) -> List[str]:
|
||||||
|
"""Truncate given list of strings that makes up the assertion explanation.
|
||||||
|
|
||||||
|
Truncates to either 8 lines, or 640 characters - whichever the input reaches
|
||||||
|
first, taking the truncation explanation into account. The remaining lines
|
||||||
|
will be replaced by a usage message.
|
||||||
|
"""
|
||||||
|
if max_lines is None:
|
||||||
|
max_lines = DEFAULT_MAX_LINES
|
||||||
|
if max_chars is None:
|
||||||
|
max_chars = DEFAULT_MAX_CHARS
|
||||||
|
|
||||||
|
# Check if truncation required
|
||||||
|
input_char_count = len("".join(input_lines))
|
||||||
|
# The length of the truncation explanation depends on the number of lines
|
||||||
|
# removed but is at least 68 characters:
|
||||||
|
# The real value is
|
||||||
|
# 64 (for the base message:
|
||||||
|
# '...\n...Full output truncated (1 line hidden), use '-vv' to show")'
|
||||||
|
# )
|
||||||
|
# + 1 (for plural)
|
||||||
|
# + int(math.log10(len(input_lines) - max_lines)) (number of hidden line, at least 1)
|
||||||
|
# + 3 for the '...' added to the truncated line
|
||||||
|
# But if there's more than 100 lines it's very likely that we're going to
|
||||||
|
# truncate, so we don't need the exact value using log10.
|
||||||
|
tolerable_max_chars = (
|
||||||
|
max_chars + 70 # 64 + 1 (for plural) + 2 (for '99') + 3 for '...'
|
||||||
|
)
|
||||||
|
# The truncation explanation add two lines to the output
|
||||||
|
tolerable_max_lines = max_lines + 2
|
||||||
|
if (
|
||||||
|
len(input_lines) <= tolerable_max_lines
|
||||||
|
and input_char_count <= tolerable_max_chars
|
||||||
|
):
|
||||||
|
return input_lines
|
||||||
|
# Truncate first to max_lines, and then truncate to max_chars if necessary
|
||||||
|
truncated_explanation = input_lines[:max_lines]
|
||||||
|
truncated_char = True
|
||||||
|
# We reevaluate the need to truncate chars following removal of some lines
|
||||||
|
if len("".join(truncated_explanation)) > tolerable_max_chars:
|
||||||
|
truncated_explanation = _truncate_by_char_count(
|
||||||
|
truncated_explanation, max_chars
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
truncated_char = False
|
||||||
|
|
||||||
|
truncated_line_count = len(input_lines) - len(truncated_explanation)
|
||||||
|
if truncated_explanation[-1]:
|
||||||
|
# Add ellipsis and take into account part-truncated final line
|
||||||
|
truncated_explanation[-1] = truncated_explanation[-1] + "..."
|
||||||
|
if truncated_char:
|
||||||
|
# It's possible that we did not remove any char from this line
|
||||||
|
truncated_line_count += 1
|
||||||
|
else:
|
||||||
|
# Add proper ellipsis when we were able to fit a full line exactly
|
||||||
|
truncated_explanation[-1] = "..."
|
||||||
|
return [
|
||||||
|
*truncated_explanation,
|
||||||
|
"",
|
||||||
|
f"...Full output truncated ({truncated_line_count} line"
|
||||||
|
f"{'' if truncated_line_count == 1 else 's'} hidden), {USAGE_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
|
||||||
609
.venv/lib/python3.10/site-packages/_pytest/assertion/util.py
Normal file
609
.venv/lib/python3.10/site-packages/_pytest/assertion/util.py
Normal file
|
|
@ -0,0 +1,609 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Utilities for assertion debugging."""
|
||||||
|
import collections.abc
|
||||||
|
import os
|
||||||
|
import pprint
|
||||||
|
from typing import AbstractSet
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import List
|
||||||
|
from typing import Literal
|
||||||
|
from typing import Mapping
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Protocol
|
||||||
|
from typing import Sequence
|
||||||
|
from unicodedata import normalize
|
||||||
|
|
||||||
|
from _pytest import outcomes
|
||||||
|
import _pytest._code
|
||||||
|
from _pytest._io.pprint import PrettyPrinter
|
||||||
|
from _pytest._io.saferepr import saferepr
|
||||||
|
from _pytest._io.saferepr import saferepr_unlimited
|
||||||
|
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: Optional[Callable[[str, object, object], Optional[str]]] = None
|
||||||
|
|
||||||
|
# Works similarly as _reprcompare attribute. Is populated with the hook call
|
||||||
|
# when pytest_runtest_setup is called.
|
||||||
|
_assertion_pass: Optional[Callable[[int, str, str], None]] = None
|
||||||
|
|
||||||
|
# Config object which is assigned during pytest_runtest_protocol.
|
||||||
|
_config: Optional[Config] = None
|
||||||
|
|
||||||
|
|
||||||
|
class _HighlightFunc(Protocol):
|
||||||
|
def __call__(self, source: str, lexer: Literal["diff", "python"] = "python") -> str:
|
||||||
|
"""Apply highlighting to the given source."""
|
||||||
|
|
||||||
|
|
||||||
|
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 issequence(x: Any) -> bool:
|
||||||
|
return isinstance(x, collections.abc.Sequence) and not isinstance(x, str)
|
||||||
|
|
||||||
|
|
||||||
|
def istext(x: Any) -> bool:
|
||||||
|
return isinstance(x, str)
|
||||||
|
|
||||||
|
|
||||||
|
def isdict(x: Any) -> bool:
|
||||||
|
return isinstance(x, dict)
|
||||||
|
|
||||||
|
|
||||||
|
def isset(x: Any) -> bool:
|
||||||
|
return isinstance(x, (set, frozenset))
|
||||||
|
|
||||||
|
|
||||||
|
def isnamedtuple(obj: Any) -> bool:
|
||||||
|
return isinstance(obj, tuple) and getattr(obj, "_fields", None) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def isdatacls(obj: Any) -> bool:
|
||||||
|
return getattr(obj, "__dataclass_fields__", None) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def isattrs(obj: Any) -> bool:
|
||||||
|
return getattr(obj, "__attrs_attrs__", None) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def isiterable(obj: Any) -> bool:
|
||||||
|
try:
|
||||||
|
iter(obj)
|
||||||
|
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 eq" in code_filename
|
||||||
|
|
||||||
|
return code_filename == "<string>" # data class
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def assertrepr_compare(
|
||||||
|
config, op: str, left: Any, right: Any, use_ascii: bool = False
|
||||||
|
) -> Optional[List[str]]:
|
||||||
|
"""Return specialised explanations for some operators/operands."""
|
||||||
|
verbose = config.get_verbosity(Config.VERBOSITY_ASSERTIONS)
|
||||||
|
|
||||||
|
# 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}"
|
||||||
|
highlighter = config.get_terminal_writer()._highlight
|
||||||
|
|
||||||
|
explanation = None
|
||||||
|
try:
|
||||||
|
if op == "==":
|
||||||
|
explanation = _compare_eq_any(left, right, highlighter, verbose)
|
||||||
|
elif op == "not in":
|
||||||
|
if istext(left) and istext(right):
|
||||||
|
explanation = _notin_text(left, right, verbose)
|
||||||
|
elif op == "!=":
|
||||||
|
if isset(left) and isset(right):
|
||||||
|
explanation = ["Both sets are equal"]
|
||||||
|
elif op == ">=":
|
||||||
|
if isset(left) and isset(right):
|
||||||
|
explanation = _compare_gte_set(left, right, highlighter, verbose)
|
||||||
|
elif op == "<=":
|
||||||
|
if isset(left) and isset(right):
|
||||||
|
explanation = _compare_lte_set(left, right, highlighter, verbose)
|
||||||
|
elif op == ">":
|
||||||
|
if isset(left) and isset(right):
|
||||||
|
explanation = _compare_gt_set(left, right, highlighter, verbose)
|
||||||
|
elif op == "<":
|
||||||
|
if isset(left) and isset(right):
|
||||||
|
explanation = _compare_lt_set(left, right, highlighter, verbose)
|
||||||
|
|
||||||
|
except outcomes.Exit:
|
||||||
|
raise
|
||||||
|
except Exception:
|
||||||
|
explanation = [
|
||||||
|
"(pytest_assertion plugin: representation of details failed: {}.".format(
|
||||||
|
_pytest._code.ExceptionInfo.from_current()._getreprcrash()
|
||||||
|
),
|
||||||
|
" Probably an object has a faulty __repr__.)",
|
||||||
|
]
|
||||||
|
|
||||||
|
if not explanation:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if explanation[0] != "":
|
||||||
|
explanation = ["", *explanation]
|
||||||
|
return [summary, *explanation]
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_eq_any(
|
||||||
|
left: Any, right: Any, highlighter: _HighlightFunc, verbose: int = 0
|
||||||
|
) -> List[str]:
|
||||||
|
explanation = []
|
||||||
|
if istext(left) and istext(right):
|
||||||
|
explanation = _diff_text(left, right, verbose)
|
||||||
|
else:
|
||||||
|
from _pytest.python_api import ApproxBase
|
||||||
|
|
||||||
|
if isinstance(left, ApproxBase) or isinstance(right, ApproxBase):
|
||||||
|
# Although the common order should be obtained == expected, this ensures both ways
|
||||||
|
approx_side = left if isinstance(left, ApproxBase) else right
|
||||||
|
other_side = right if isinstance(left, ApproxBase) else left
|
||||||
|
|
||||||
|
explanation = approx_side._repr_compare(other_side)
|
||||||
|
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.
|
||||||
|
explanation = _compare_eq_cls(left, right, highlighter, verbose)
|
||||||
|
elif issequence(left) and issequence(right):
|
||||||
|
explanation = _compare_eq_sequence(left, right, highlighter, verbose)
|
||||||
|
elif isset(left) and isset(right):
|
||||||
|
explanation = _compare_eq_set(left, right, highlighter, verbose)
|
||||||
|
elif isdict(left) and isdict(right):
|
||||||
|
explanation = _compare_eq_dict(left, right, highlighter, verbose)
|
||||||
|
|
||||||
|
if isiterable(left) and isiterable(right):
|
||||||
|
expl = _compare_eq_iterable(left, right, highlighter, verbose)
|
||||||
|
explanation.extend(expl)
|
||||||
|
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]:
|
||||||
|
"""Return 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.
|
||||||
|
"""
|
||||||
|
from difflib import ndiff
|
||||||
|
|
||||||
|
explanation: List[str] = []
|
||||||
|
|
||||||
|
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
|
||||||
|
explanation = [
|
||||||
|
"Skipping %s identical leading characters in diff, use -v to show" % i
|
||||||
|
]
|
||||||
|
left = left[i:]
|
||||||
|
right = right[i:]
|
||||||
|
if len(left) == len(right):
|
||||||
|
for i in range(len(left)):
|
||||||
|
if left[-i] != right[-i]:
|
||||||
|
break
|
||||||
|
if i > 42:
|
||||||
|
i -= 10 # Provide some context
|
||||||
|
explanation += [
|
||||||
|
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))
|
||||||
|
explanation += ["Strings contain only whitespace, escaping them using repr()"]
|
||||||
|
# "right" is the expected base against which we compare "left",
|
||||||
|
# see https://github.com/pytest-dev/pytest/issues/3333
|
||||||
|
explanation += [
|
||||||
|
line.strip("\n")
|
||||||
|
for line in ndiff(right.splitlines(keepends), left.splitlines(keepends))
|
||||||
|
]
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_eq_iterable(
|
||||||
|
left: Iterable[Any],
|
||||||
|
right: Iterable[Any],
|
||||||
|
highligher: _HighlightFunc,
|
||||||
|
verbose: int = 0,
|
||||||
|
) -> List[str]:
|
||||||
|
if verbose <= 0 and not running_on_ci():
|
||||||
|
return ["Use -v to get more diff"]
|
||||||
|
# dynamic import to speedup pytest
|
||||||
|
import difflib
|
||||||
|
|
||||||
|
left_formatting = PrettyPrinter().pformat(left).splitlines()
|
||||||
|
right_formatting = PrettyPrinter().pformat(right).splitlines()
|
||||||
|
|
||||||
|
explanation = ["", "Full diff:"]
|
||||||
|
# "right" is the expected base against which we compare "left",
|
||||||
|
# see https://github.com/pytest-dev/pytest/issues/3333
|
||||||
|
explanation.extend(
|
||||||
|
highligher(
|
||||||
|
"\n".join(
|
||||||
|
line.rstrip()
|
||||||
|
for line in difflib.ndiff(right_formatting, left_formatting)
|
||||||
|
),
|
||||||
|
lexer="diff",
|
||||||
|
).splitlines()
|
||||||
|
)
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_eq_sequence(
|
||||||
|
left: Sequence[Any],
|
||||||
|
right: Sequence[Any],
|
||||||
|
highlighter: _HighlightFunc,
|
||||||
|
verbose: int = 0,
|
||||||
|
) -> List[str]:
|
||||||
|
comparing_bytes = isinstance(left, bytes) and isinstance(right, bytes)
|
||||||
|
explanation: List[str] = []
|
||||||
|
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 = left[i : i + 1]
|
||||||
|
right_value = right[i : i + 1]
|
||||||
|
else:
|
||||||
|
left_value = left[i]
|
||||||
|
right_value = right[i]
|
||||||
|
|
||||||
|
explanation.append(
|
||||||
|
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 explanation
|
||||||
|
|
||||||
|
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:
|
||||||
|
explanation += [
|
||||||
|
f"{dir_with_more} contains one more item: {highlighter(extra)}"
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
explanation += [
|
||||||
|
"%s contains %d more items, first extra item: %s"
|
||||||
|
% (dir_with_more, len_diff, highlighter(extra))
|
||||||
|
]
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_eq_set(
|
||||||
|
left: AbstractSet[Any],
|
||||||
|
right: AbstractSet[Any],
|
||||||
|
highlighter: _HighlightFunc,
|
||||||
|
verbose: int = 0,
|
||||||
|
) -> List[str]:
|
||||||
|
explanation = []
|
||||||
|
explanation.extend(_set_one_sided_diff("left", left, right, highlighter))
|
||||||
|
explanation.extend(_set_one_sided_diff("right", right, left, highlighter))
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_gt_set(
|
||||||
|
left: AbstractSet[Any],
|
||||||
|
right: AbstractSet[Any],
|
||||||
|
highlighter: _HighlightFunc,
|
||||||
|
verbose: int = 0,
|
||||||
|
) -> List[str]:
|
||||||
|
explanation = _compare_gte_set(left, right, highlighter)
|
||||||
|
if not explanation:
|
||||||
|
return ["Both sets are equal"]
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_lt_set(
|
||||||
|
left: AbstractSet[Any],
|
||||||
|
right: AbstractSet[Any],
|
||||||
|
highlighter: _HighlightFunc,
|
||||||
|
verbose: int = 0,
|
||||||
|
) -> List[str]:
|
||||||
|
explanation = _compare_lte_set(left, right, highlighter)
|
||||||
|
if not explanation:
|
||||||
|
return ["Both sets are equal"]
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_gte_set(
|
||||||
|
left: AbstractSet[Any],
|
||||||
|
right: AbstractSet[Any],
|
||||||
|
highlighter: _HighlightFunc,
|
||||||
|
verbose: int = 0,
|
||||||
|
) -> List[str]:
|
||||||
|
return _set_one_sided_diff("right", right, left, highlighter)
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_lte_set(
|
||||||
|
left: AbstractSet[Any],
|
||||||
|
right: AbstractSet[Any],
|
||||||
|
highlighter: _HighlightFunc,
|
||||||
|
verbose: int = 0,
|
||||||
|
) -> List[str]:
|
||||||
|
return _set_one_sided_diff("left", left, right, highlighter)
|
||||||
|
|
||||||
|
|
||||||
|
def _set_one_sided_diff(
|
||||||
|
posn: str,
|
||||||
|
set1: AbstractSet[Any],
|
||||||
|
set2: AbstractSet[Any],
|
||||||
|
highlighter: _HighlightFunc,
|
||||||
|
) -> List[str]:
|
||||||
|
explanation = []
|
||||||
|
diff = set1 - set2
|
||||||
|
if diff:
|
||||||
|
explanation.append(f"Extra items in the {posn} set:")
|
||||||
|
for item in diff:
|
||||||
|
explanation.append(highlighter(saferepr(item)))
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_eq_dict(
|
||||||
|
left: Mapping[Any, Any],
|
||||||
|
right: Mapping[Any, Any],
|
||||||
|
highlighter: _HighlightFunc,
|
||||||
|
verbose: int = 0,
|
||||||
|
) -> List[str]:
|
||||||
|
explanation: List[str] = []
|
||||||
|
set_left = set(left)
|
||||||
|
set_right = set(right)
|
||||||
|
common = set_left.intersection(set_right)
|
||||||
|
same = {k: left[k] for k in common if left[k] == right[k]}
|
||||||
|
if same and verbose < 2:
|
||||||
|
explanation += ["Omitting %s identical items, use -vv to show" % len(same)]
|
||||||
|
elif same:
|
||||||
|
explanation += ["Common items:"]
|
||||||
|
explanation += highlighter(pprint.pformat(same)).splitlines()
|
||||||
|
diff = {k for k in common if left[k] != right[k]}
|
||||||
|
if diff:
|
||||||
|
explanation += ["Differing items:"]
|
||||||
|
for k in diff:
|
||||||
|
explanation += [
|
||||||
|
highlighter(saferepr({k: left[k]}))
|
||||||
|
+ " != "
|
||||||
|
+ highlighter(saferepr({k: right[k]}))
|
||||||
|
]
|
||||||
|
extra_left = set_left - set_right
|
||||||
|
len_extra_left = len(extra_left)
|
||||||
|
if len_extra_left:
|
||||||
|
explanation.append(
|
||||||
|
"Left contains %d more item%s:"
|
||||||
|
% (len_extra_left, "" if len_extra_left == 1 else "s")
|
||||||
|
)
|
||||||
|
explanation.extend(
|
||||||
|
highlighter(pprint.pformat({k: left[k] for k in extra_left})).splitlines()
|
||||||
|
)
|
||||||
|
extra_right = set_right - set_left
|
||||||
|
len_extra_right = len(extra_right)
|
||||||
|
if len_extra_right:
|
||||||
|
explanation.append(
|
||||||
|
"Right contains %d more item%s:"
|
||||||
|
% (len_extra_right, "" if len_extra_right == 1 else "s")
|
||||||
|
)
|
||||||
|
explanation.extend(
|
||||||
|
highlighter(pprint.pformat({k: right[k] for k in extra_right})).splitlines()
|
||||||
|
)
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _compare_eq_cls(
|
||||||
|
left: Any, right: Any, highlighter: _HighlightFunc, verbose: int
|
||||||
|
) -> List[str]:
|
||||||
|
if not has_default_eq(left):
|
||||||
|
return []
|
||||||
|
if isdatacls(left):
|
||||||
|
import dataclasses
|
||||||
|
|
||||||
|
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__
|
||||||
|
fields_to_check = [field.name for field in all_fields if getattr(field, "eq")]
|
||||||
|
elif isnamedtuple(left):
|
||||||
|
fields_to_check = left._fields
|
||||||
|
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)
|
||||||
|
|
||||||
|
explanation = []
|
||||||
|
if same or diff:
|
||||||
|
explanation += [""]
|
||||||
|
if same and verbose < 2:
|
||||||
|
explanation.append("Omitting %s identical items, use -vv to show" % len(same))
|
||||||
|
elif same:
|
||||||
|
explanation += ["Matching attributes:"]
|
||||||
|
explanation += highlighter(pprint.pformat(same)).splitlines()
|
||||||
|
if diff:
|
||||||
|
explanation += ["Differing attributes:"]
|
||||||
|
explanation += highlighter(pprint.pformat(diff)).splitlines()
|
||||||
|
for field in diff:
|
||||||
|
field_left = getattr(left, field)
|
||||||
|
field_right = getattr(right, field)
|
||||||
|
explanation += [
|
||||||
|
"",
|
||||||
|
f"Drill down into differing attribute {field}:",
|
||||||
|
f"{indent}{field}: {highlighter(repr(field_left))} != {highlighter(repr(field_right))}",
|
||||||
|
]
|
||||||
|
explanation += [
|
||||||
|
indent + line
|
||||||
|
for line in _compare_eq_any(
|
||||||
|
field_left, field_right, highlighter, verbose
|
||||||
|
)
|
||||||
|
]
|
||||||
|
return explanation
|
||||||
|
|
||||||
|
|
||||||
|
def _notin_text(term: str, text: str, verbose: int = 0) -> List[str]:
|
||||||
|
index = text.find(term)
|
||||||
|
head = text[:index]
|
||||||
|
tail = text[index + len(term) :]
|
||||||
|
correct_text = head + tail
|
||||||
|
diff = _diff_text(text, correct_text, verbose)
|
||||||
|
newdiff = ["%s is contained here:" % saferepr(term, maxsize=42)]
|
||||||
|
for line in diff:
|
||||||
|
if line.startswith("Skipping"):
|
||||||
|
continue
|
||||||
|
if line.startswith("- "):
|
||||||
|
continue
|
||||||
|
if line.startswith("+ "):
|
||||||
|
newdiff.append(" " + line[2:])
|
||||||
|
else:
|
||||||
|
newdiff.append(line)
|
||||||
|
return newdiff
|
||||||
|
|
||||||
|
|
||||||
|
def running_on_ci() -> bool:
|
||||||
|
"""Check if we're currently running on a CI system."""
|
||||||
|
env_vars = ["CI", "BUILD_NUMBER"]
|
||||||
|
return any(var in os.environ for var in env_vars)
|
||||||
597
.venv/lib/python3.10/site-packages/_pytest/cacheprovider.py
Normal file
597
.venv/lib/python3.10/site-packages/_pytest/cacheprovider.py
Normal file
|
|
@ -0,0 +1,597 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Implementation of the cache provider."""
|
||||||
|
# This plugin was not named "cache" to avoid conflicts with the external
|
||||||
|
# pytest-cache version.
|
||||||
|
import dataclasses
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Dict
|
||||||
|
from typing import final
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Set
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
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.nodes import Directory
|
||||||
|
from _pytest.nodes import File
|
||||||
|
from _pytest.reports import TestReport
|
||||||
|
|
||||||
|
|
||||||
|
README_CONTENT = """\
|
||||||
|
# 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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
CACHEDIR_TAG_CONTENT = 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
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@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, 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.
|
||||||
|
"""
|
||||||
|
path = Path(name)
|
||||||
|
if len(path.parts) > 1:
|
||||||
|
raise ValueError("name is not allowed to contain path separators")
|
||||||
|
res = self._cachedir.joinpath(self._CACHE_PREFIX_DIRS, path)
|
||||||
|
res.mkdir(exist_ok=True, parents=True)
|
||||||
|
return res
|
||||||
|
|
||||||
|
def _getvaluepath(self, key: str) -> Path:
|
||||||
|
return self._cachedir.joinpath(self._CACHE_PREFIX_VALUES, Path(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. 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. 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:
|
||||||
|
if path.parent.is_dir():
|
||||||
|
cache_dir_exists_already = True
|
||||||
|
else:
|
||||||
|
cache_dir_exists_already = self._cachedir.exists()
|
||||||
|
path.parent.mkdir(exist_ok=True, parents=True)
|
||||||
|
except OSError as exc:
|
||||||
|
self.warn(
|
||||||
|
f"could not create cache path {path}: {exc}",
|
||||||
|
_ispytest=True,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
if not cache_dir_exists_already:
|
||||||
|
self._ensure_supporting_files()
|
||||||
|
data = json.dumps(value, ensure_ascii=False, indent=2)
|
||||||
|
try:
|
||||||
|
f = path.open("w", encoding="UTF-8")
|
||||||
|
except OSError as exc:
|
||||||
|
self.warn(
|
||||||
|
f"cache could not write path {path}: {exc}",
|
||||||
|
_ispytest=True,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
with f:
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
|
def _ensure_supporting_files(self) -> None:
|
||||||
|
"""Create supporting files in the cache dir that are not really part of the cache."""
|
||||||
|
readme_path = self._cachedir / "README.md"
|
||||||
|
readme_path.write_text(README_CONTENT, encoding="UTF-8")
|
||||||
|
|
||||||
|
gitignore_path = self._cachedir.joinpath(".gitignore")
|
||||||
|
msg = "# Created by pytest automatically.\n*\n"
|
||||||
|
gitignore_path.write_text(msg, encoding="UTF-8")
|
||||||
|
|
||||||
|
cachedir_tag_path = self._cachedir.joinpath("CACHEDIR.TAG")
|
||||||
|
cachedir_tag_path.write_bytes(CACHEDIR_TAG_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
|
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 priorize last failed.
|
||||||
|
def sort_key(node: Union[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.nodeid 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.nodeid 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
|
||||||
|
) -> Optional[CollectReport]:
|
||||||
|
if isinstance(collector, File):
|
||||||
|
if collector.path not in self.lfplugin._last_failed_paths:
|
||||||
|
self.lfplugin._skipped_files += 1
|
||||||
|
|
||||||
|
return CollectReport(
|
||||||
|
collector.nodeid, "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[str, bool] = config.cache.get("cache/lastfailed", {})
|
||||||
|
self._previously_failed_count: Optional[int] = None
|
||||||
|
self._report_status: Optional[str] = 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.split("::")[0]
|
||||||
|
result.add(path)
|
||||||
|
result.update(path.parents)
|
||||||
|
return {x for x in result if x.exists()}
|
||||||
|
|
||||||
|
def pytest_report_collectionfinish(self) -> Optional[str]:
|
||||||
|
if self.active and self.config.getoption("verbose") >= 0:
|
||||||
|
return "run-last-failure: %s" % 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.nodeid, None)
|
||||||
|
elif report.failed:
|
||||||
|
self.lastfailed[report.nodeid] = True
|
||||||
|
|
||||||
|
def pytest_collectreport(self, report: CollectReport) -> None:
|
||||||
|
passed = report.outcome in ("passed", "skipped")
|
||||||
|
if passed:
|
||||||
|
if report.nodeid in self.lastfailed:
|
||||||
|
self.lastfailed.pop(report.nodeid)
|
||||||
|
self.lastfailed.update((item.nodeid, True) for item in report.result)
|
||||||
|
else:
|
||||||
|
self.lastfailed[report.nodeid] = True
|
||||||
|
|
||||||
|
@hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_collection_modifyitems(
|
||||||
|
self, config: Config, items: List[nodes.Item]
|
||||||
|
) -> Generator[None, None, None]:
|
||||||
|
res = yield
|
||||||
|
|
||||||
|
if not self.active:
|
||||||
|
return res
|
||||||
|
|
||||||
|
if self.lastfailed:
|
||||||
|
previously_failed = []
|
||||||
|
previously_passed = []
|
||||||
|
for item in items:
|
||||||
|
if item.nodeid 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 = "%d known failures not in selected tests" % (
|
||||||
|
len(self.lastfailed),
|
||||||
|
)
|
||||||
|
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
|
||||||
|
saved_lastfailed = config.cache.get("cache/lastfailed", {})
|
||||||
|
if saved_lastfailed != self.lastfailed:
|
||||||
|
config.cache.set("cache/lastfailed", self.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(config.cache.get("cache/nodeids", []))
|
||||||
|
|
||||||
|
@hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_collection_modifyitems(
|
||||||
|
self, items: List[nodes.Item]
|
||||||
|
) -> Generator[None, None, None]:
|
||||||
|
res = yield
|
||||||
|
|
||||||
|
if self.active:
|
||||||
|
new_items: Dict[str, nodes.Item] = {}
|
||||||
|
other_items: Dict[str, nodes.Item] = {}
|
||||||
|
for item in items:
|
||||||
|
if item.nodeid not in self.cached_nodeids:
|
||||||
|
new_items[item.nodeid] = item
|
||||||
|
else:
|
||||||
|
other_items[item.nodeid] = 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.nodeid 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) # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
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(self.cached_nodeids))
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_addoption(parser: Parser) -> None:
|
||||||
|
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) -> Optional[Union[int, ExitCode]]:
|
||||||
|
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:
|
||||||
|
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) -> Optional[str]:
|
||||||
|
"""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:
|
||||||
|
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 = "*"
|
||||||
|
|
||||||
|
dummy = object()
|
||||||
|
basedir = config.cache._cachedir
|
||||||
|
vdir = basedir / Cache._CACHE_PREFIX_VALUES
|
||||||
|
tw.sep("-", "cache values for %r" % glob)
|
||||||
|
for valpath in sorted(x for x in vdir.rglob(glob) if x.is_file()):
|
||||||
|
key = str(valpath.relative_to(vdir))
|
||||||
|
val = config.cache.get(key, dummy)
|
||||||
|
if val is dummy:
|
||||||
|
tw.line("%s contains unreadable content, will be ignored" % key)
|
||||||
|
else:
|
||||||
|
tw.line("%s contains:" % key)
|
||||||
|
for line in pformat(val).splitlines():
|
||||||
|
tw.line(" " + line)
|
||||||
|
|
||||||
|
ddir = basedir / Cache._CACHE_PREFIX_DIRS
|
||||||
|
if ddir.is_dir():
|
||||||
|
contents = sorted(ddir.rglob(glob))
|
||||||
|
tw.sep("-", "cache directories for %r" % glob)
|
||||||
|
for p in contents:
|
||||||
|
# if p.is_dir():
|
||||||
|
# print("%s/" % p.relative_to(basedir))
|
||||||
|
if p.is_file():
|
||||||
|
key = str(p.relative_to(basedir))
|
||||||
|
tw.line(f"{key} is a file of length {p.stat().st_size:d}")
|
||||||
|
return 0
|
||||||
1086
.venv/lib/python3.10/site-packages/_pytest/capture.py
Normal file
1086
.venv/lib/python3.10/site-packages/_pytest/capture.py
Normal file
File diff suppressed because it is too large
Load diff
353
.venv/lib/python3.10/site-packages/_pytest/compat.py
Normal file
353
.venv/lib/python3.10/site-packages/_pytest/compat.py
Normal file
|
|
@ -0,0 +1,353 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Python version compatibility code."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import dataclasses
|
||||||
|
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 Callable
|
||||||
|
from typing import Final
|
||||||
|
from typing import NoReturn
|
||||||
|
|
||||||
|
import py
|
||||||
|
|
||||||
|
|
||||||
|
#: 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 is_generator(func: object) -> bool:
|
||||||
|
genfunc = inspect.isgeneratorfunction(func)
|
||||||
|
return genfunc and not iscoroutinefunction(func)
|
||||||
|
|
||||||
|
|
||||||
|
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 couroutines.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 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 "%s:%d" % (relfn, lineno + 1)
|
||||||
|
return "%s:%d" % (fn, lineno + 1)
|
||||||
|
|
||||||
|
|
||||||
|
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 = "",
|
||||||
|
is_method: bool = False,
|
||||||
|
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 is_method and cls arguments indicate that the function should
|
||||||
|
be treated as a bound method even though it's not unless, only in
|
||||||
|
the case of cls, 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
|
||||||
|
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.values()
|
||||||
|
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, remove the first
|
||||||
|
# parameter name.
|
||||||
|
if is_method or (
|
||||||
|
# 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 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)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class _PytestWrapper:
|
||||||
|
"""Dummy wrapper around a function object for internal use only.
|
||||||
|
|
||||||
|
Used to correctly unwrap the underlying function object when we are
|
||||||
|
creating fixtures, because we wrap the function object ourselves with a
|
||||||
|
decorator to issue warnings when the fixture function is called directly.
|
||||||
|
"""
|
||||||
|
|
||||||
|
obj: Any
|
||||||
|
|
||||||
|
|
||||||
|
def get_real_func(obj):
|
||||||
|
"""Get the real function object of the (possibly) wrapped object by
|
||||||
|
functools.wraps or functools.partial."""
|
||||||
|
start_obj = obj
|
||||||
|
for i in range(100):
|
||||||
|
# __pytest_wrapped__ is set by @pytest.fixture when wrapping the fixture function
|
||||||
|
# to trigger a warning if it gets called directly instead of by pytest: we don't
|
||||||
|
# want to unwrap further than this otherwise we lose useful wrappings like @mock.patch (#3774)
|
||||||
|
new_obj = getattr(obj, "__pytest_wrapped__", None)
|
||||||
|
if isinstance(new_obj, _PytestWrapper):
|
||||||
|
obj = new_obj.obj
|
||||||
|
break
|
||||||
|
new_obj = getattr(obj, "__wrapped__", None)
|
||||||
|
if new_obj is None:
|
||||||
|
break
|
||||||
|
obj = new_obj
|
||||||
|
else:
|
||||||
|
from _pytest._io.saferepr import saferepr
|
||||||
|
|
||||||
|
raise ValueError(
|
||||||
|
f"could not find real function of {saferepr(start_obj)}\nstopped at {saferepr(obj)}"
|
||||||
|
)
|
||||||
|
if isinstance(obj, functools.partial):
|
||||||
|
obj = obj.func
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
def get_real_method(obj, holder):
|
||||||
|
"""Attempt to obtain the real function object that might be wrapping
|
||||||
|
``obj``, while at the same time returning a bound method to ``holder`` if
|
||||||
|
the original object was a bound method."""
|
||||||
|
try:
|
||||||
|
is_method = hasattr(obj, "__func__")
|
||||||
|
obj = get_real_func(obj)
|
||||||
|
except Exception: # pragma: no cover
|
||||||
|
return obj
|
||||||
|
if is_method and hasattr(obj, "__get__") and callable(obj.__get__):
|
||||||
|
obj = obj.__get__(holder)
|
||||||
|
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": # noqa: PLR1714
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
|
# Perform exhaustiveness checking.
|
||||||
|
#
|
||||||
|
# Consider this example:
|
||||||
|
#
|
||||||
|
# MyUnion = Union[int, str]
|
||||||
|
#
|
||||||
|
# def handle(x: MyUnion) -> int {
|
||||||
|
# if isinstance(x, int):
|
||||||
|
# return 1
|
||||||
|
# elif isinstance(x, str):
|
||||||
|
# return 2
|
||||||
|
# else:
|
||||||
|
# raise Exception('unreachable')
|
||||||
|
#
|
||||||
|
# Now suppose we add a new variant:
|
||||||
|
#
|
||||||
|
# MyUnion = Union[int, str, bytes]
|
||||||
|
#
|
||||||
|
# After doing this, we must remember ourselves to go and update the handle
|
||||||
|
# function to handle the new variant.
|
||||||
|
#
|
||||||
|
# With `assert_never` we can do better:
|
||||||
|
#
|
||||||
|
# // raise Exception('unreachable')
|
||||||
|
# return assert_never(x)
|
||||||
|
#
|
||||||
|
# Now, if we forget to handle the new variant, the type-checker will emit a
|
||||||
|
# compile-time error, instead of the runtime error we would have gotten
|
||||||
|
# previously.
|
||||||
|
#
|
||||||
|
# This also work for Enums (if you use `is` to compare) and Literals.
|
||||||
|
def assert_never(value: NoReturn) -> NoReturn:
|
||||||
|
assert False, f"Unhandled value: {value} ({type(value).__name__})"
|
||||||
1967
.venv/lib/python3.10/site-packages/_pytest/config/__init__.py
Normal file
1967
.venv/lib/python3.10/site-packages/_pytest/config/__init__.py
Normal file
File diff suppressed because it is too large
Load diff
552
.venv/lib/python3.10/site-packages/_pytest/config/argparsing.py
Normal file
552
.venv/lib/python3.10/site-packages/_pytest/config/argparsing.py
Normal file
|
|
@ -0,0 +1,552 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
import argparse
|
||||||
|
from gettext import gettext
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import cast
|
||||||
|
from typing import Dict
|
||||||
|
from typing import final
|
||||||
|
from typing import List
|
||||||
|
from typing import Literal
|
||||||
|
from typing import Mapping
|
||||||
|
from typing import NoReturn
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Sequence
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
import _pytest._io
|
||||||
|
from _pytest.config.exceptions import UsageError
|
||||||
|
from _pytest.deprecated import check_ispytest
|
||||||
|
|
||||||
|
|
||||||
|
FILE_OR_DIR = "file_or_dir"
|
||||||
|
|
||||||
|
|
||||||
|
class NotSet:
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return "<notset>"
|
||||||
|
|
||||||
|
|
||||||
|
NOT_SET = NotSet()
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class Parser:
|
||||||
|
"""Parser for command line arguments and ini-file values.
|
||||||
|
|
||||||
|
:ivar extra_info: Dict of generic param -> value to display in case
|
||||||
|
there's an error processing the command line arguments.
|
||||||
|
"""
|
||||||
|
|
||||||
|
prog: Optional[str] = None
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
usage: Optional[str] = None,
|
||||||
|
processopt: Optional[Callable[["Argument"], None]] = None,
|
||||||
|
*,
|
||||||
|
_ispytest: bool = False,
|
||||||
|
) -> None:
|
||||||
|
check_ispytest(_ispytest)
|
||||||
|
self._anonymous = OptionGroup("Custom options", parser=self, _ispytest=True)
|
||||||
|
self._groups: List[OptionGroup] = []
|
||||||
|
self._processopt = processopt
|
||||||
|
self._usage = usage
|
||||||
|
self._inidict: Dict[str, Tuple[str, Optional[str], Any]] = {}
|
||||||
|
self._ininames: List[str] = []
|
||||||
|
self.extra_info: Dict[str, Any] = {}
|
||||||
|
|
||||||
|
def processoption(self, option: "Argument") -> None:
|
||||||
|
if self._processopt:
|
||||||
|
if option.dest:
|
||||||
|
self._processopt(option)
|
||||||
|
|
||||||
|
def getgroup(
|
||||||
|
self, name: str, description: str = "", after: Optional[str] = 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
|
||||||
|
group = OptionGroup(name, description, parser=self, _ispytest=True)
|
||||||
|
i = 0
|
||||||
|
for i, grp in enumerate(self._groups):
|
||||||
|
if grp.name == after:
|
||||||
|
break
|
||||||
|
self._groups.insert(i + 1, group)
|
||||||
|
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[Union[str, "os.PathLike[str]"]],
|
||||||
|
namespace: Optional[argparse.Namespace] = None,
|
||||||
|
) -> argparse.Namespace:
|
||||||
|
from _pytest._argcomplete import try_argcomplete
|
||||||
|
|
||||||
|
self.optparser = self._getparser()
|
||||||
|
try_argcomplete(self.optparser)
|
||||||
|
strargs = [os.fspath(x) for x in args]
|
||||||
|
return self.optparser.parse_args(strargs, namespace=namespace)
|
||||||
|
|
||||||
|
def _getparser(self) -> "MyOptionParser":
|
||||||
|
from _pytest._argcomplete import filescompleter
|
||||||
|
|
||||||
|
optparser = MyOptionParser(self, self.extra_info, prog=self.prog)
|
||||||
|
groups = [*self._groups, self._anonymous]
|
||||||
|
for group in groups:
|
||||||
|
if group.options:
|
||||||
|
desc = group.description or group.name
|
||||||
|
arggroup = optparser.add_argument_group(desc)
|
||||||
|
for option in group.options:
|
||||||
|
n = option.names()
|
||||||
|
a = option.attrs()
|
||||||
|
arggroup.add_argument(*n, **a)
|
||||||
|
file_or_dir_arg = optparser.add_argument(FILE_OR_DIR, nargs="*")
|
||||||
|
# bash like autocompletion for dirs (appending '/')
|
||||||
|
# Type ignored because typeshed doesn't know about argcomplete.
|
||||||
|
file_or_dir_arg.completer = filescompleter # type: ignore
|
||||||
|
return optparser
|
||||||
|
|
||||||
|
def parse_setoption(
|
||||||
|
self,
|
||||||
|
args: Sequence[Union[str, "os.PathLike[str]"]],
|
||||||
|
option: argparse.Namespace,
|
||||||
|
namespace: Optional[argparse.Namespace] = None,
|
||||||
|
) -> List[str]:
|
||||||
|
parsedoption = self.parse(args, namespace=namespace)
|
||||||
|
for name, value in parsedoption.__dict__.items():
|
||||||
|
setattr(option, name, value)
|
||||||
|
return cast(List[str], getattr(parsedoption, FILE_OR_DIR))
|
||||||
|
|
||||||
|
def parse_known_args(
|
||||||
|
self,
|
||||||
|
args: Sequence[Union[str, "os.PathLike[str]"]],
|
||||||
|
namespace: Optional[argparse.Namespace] = 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[Union[str, "os.PathLike[str]"]],
|
||||||
|
namespace: Optional[argparse.Namespace] = None,
|
||||||
|
) -> Tuple[argparse.Namespace, List[str]]:
|
||||||
|
"""Parse the known arguments at this point, and also return the
|
||||||
|
remaining unknown arguments.
|
||||||
|
|
||||||
|
:returns:
|
||||||
|
A tuple containing an argparse namespace object for the known
|
||||||
|
arguments, and a list of the unknown arguments.
|
||||||
|
"""
|
||||||
|
optparser = self._getparser()
|
||||||
|
strargs = [os.fspath(x) for x in args]
|
||||||
|
return optparser.parse_known_args(strargs, namespace=namespace)
|
||||||
|
|
||||||
|
def addini(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
help: str,
|
||||||
|
type: Optional[
|
||||||
|
Literal["string", "paths", "pathlist", "args", "linelist", "bool"]
|
||||||
|
] = None,
|
||||||
|
default: Any = NOT_SET,
|
||||||
|
) -> None:
|
||||||
|
"""Register an ini-file option.
|
||||||
|
|
||||||
|
:param name:
|
||||||
|
Name of the ini-variable.
|
||||||
|
:param type:
|
||||||
|
Type of the variable. 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
|
||||||
|
|
||||||
|
For ``paths`` and ``pathlist`` types, they are considered relative to the ini-file.
|
||||||
|
In case the execution is happening without an ini-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 an ini-file.
|
||||||
|
|
||||||
|
Defaults to ``string`` if ``None`` or not passed.
|
||||||
|
:param default:
|
||||||
|
Default value if no ini-file option exists but is queried.
|
||||||
|
|
||||||
|
The value of ini-variables can be retrieved via a call to
|
||||||
|
:py:func:`config.getini(name) <pytest.Config.getini>`.
|
||||||
|
"""
|
||||||
|
assert type in (None, "string", "paths", "pathlist", "args", "linelist", "bool")
|
||||||
|
if default is NOT_SET:
|
||||||
|
default = get_ini_default_for_type(type)
|
||||||
|
|
||||||
|
self._inidict[name] = (help, type, default)
|
||||||
|
self._ininames.append(name)
|
||||||
|
|
||||||
|
|
||||||
|
def get_ini_default_for_type(
|
||||||
|
type: Optional[Literal["string", "paths", "pathlist", "args", "linelist", "bool"]],
|
||||||
|
) -> Any:
|
||||||
|
"""
|
||||||
|
Used by addini to get the default value for a given ini-option type, when
|
||||||
|
default is not supplied.
|
||||||
|
"""
|
||||||
|
if type is None:
|
||||||
|
return ""
|
||||||
|
elif type in ("paths", "pathlist", "args", "linelist"):
|
||||||
|
return []
|
||||||
|
elif type == "bool":
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
class ArgumentError(Exception):
|
||||||
|
"""Raised if an Argument instance is created with invalid or
|
||||||
|
inconsistent arguments."""
|
||||||
|
|
||||||
|
def __init__(self, msg: str, option: Union["Argument", str]) -> None:
|
||||||
|
self.msg = msg
|
||||||
|
self.option_id = str(option)
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
if self.option_id:
|
||||||
|
return f"option {self.option_id}: {self.msg}"
|
||||||
|
else:
|
||||||
|
return self.msg
|
||||||
|
|
||||||
|
|
||||||
|
class Argument:
|
||||||
|
"""Class that mimics the necessary behaviour of optparse.Option.
|
||||||
|
|
||||||
|
It's currently a least effort implementation and ignoring choices
|
||||||
|
and integer prefixes.
|
||||||
|
|
||||||
|
https://docs.python.org/3/library/optparse.html#optparse-standard-option-types
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *names: str, **attrs: Any) -> None:
|
||||||
|
"""Store params in private vars for use in add_argument."""
|
||||||
|
self._attrs = attrs
|
||||||
|
self._short_opts: List[str] = []
|
||||||
|
self._long_opts: List[str] = []
|
||||||
|
try:
|
||||||
|
self.type = attrs["type"]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
# Attribute existence is tested in Config._processopt.
|
||||||
|
self.default = attrs["default"]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
self._set_opt_strings(names)
|
||||||
|
dest: Optional[str] = attrs.get("dest")
|
||||||
|
if dest:
|
||||||
|
self.dest = dest
|
||||||
|
elif self._long_opts:
|
||||||
|
self.dest = self._long_opts[0][2:].replace("-", "_")
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
self.dest = self._short_opts[0][1:]
|
||||||
|
except IndexError as e:
|
||||||
|
self.dest = "???" # Needed for the error repr.
|
||||||
|
raise ArgumentError("need a long or short option", self) from e
|
||||||
|
|
||||||
|
def names(self) -> List[str]:
|
||||||
|
return self._short_opts + self._long_opts
|
||||||
|
|
||||||
|
def attrs(self) -> Mapping[str, Any]:
|
||||||
|
# Update any attributes set by processopt.
|
||||||
|
attrs = "default dest help".split()
|
||||||
|
attrs.append(self.dest)
|
||||||
|
for attr in attrs:
|
||||||
|
try:
|
||||||
|
self._attrs[attr] = getattr(self, attr)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
return self._attrs
|
||||||
|
|
||||||
|
def _set_opt_strings(self, opts: Sequence[str]) -> None:
|
||||||
|
"""Directly from optparse.
|
||||||
|
|
||||||
|
Might not be necessary as this is passed to argparse later on.
|
||||||
|
"""
|
||||||
|
for opt in opts:
|
||||||
|
if len(opt) < 2:
|
||||||
|
raise ArgumentError(
|
||||||
|
"invalid option string %r: "
|
||||||
|
"must be at least two characters long" % opt,
|
||||||
|
self,
|
||||||
|
)
|
||||||
|
elif len(opt) == 2:
|
||||||
|
if not (opt[0] == "-" and opt[1] != "-"):
|
||||||
|
raise ArgumentError(
|
||||||
|
"invalid short option string %r: "
|
||||||
|
"must be of the form -x, (x any non-dash char)" % opt,
|
||||||
|
self,
|
||||||
|
)
|
||||||
|
self._short_opts.append(opt)
|
||||||
|
else:
|
||||||
|
if not (opt[0:2] == "--" and opt[2] != "-"):
|
||||||
|
raise ArgumentError(
|
||||||
|
"invalid long option string %r: "
|
||||||
|
"must start with --, followed by non-dash" % opt,
|
||||||
|
self,
|
||||||
|
)
|
||||||
|
self._long_opts.append(opt)
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
args: List[str] = []
|
||||||
|
if self._short_opts:
|
||||||
|
args += ["_short_opts: " + repr(self._short_opts)]
|
||||||
|
if self._long_opts:
|
||||||
|
args += ["_long_opts: " + repr(self._long_opts)]
|
||||||
|
args += ["dest: " + repr(self.dest)]
|
||||||
|
if hasattr(self, "type"):
|
||||||
|
args += ["type: " + repr(self.type)]
|
||||||
|
if hasattr(self, "default"):
|
||||||
|
args += ["default: " + repr(self.default)]
|
||||||
|
return "Argument({})".format(", ".join(args))
|
||||||
|
|
||||||
|
|
||||||
|
class OptionGroup:
|
||||||
|
"""A group of options shown in its own section."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
description: str = "",
|
||||||
|
parser: Optional[Parser] = None,
|
||||||
|
*,
|
||||||
|
_ispytest: bool = False,
|
||||||
|
) -> None:
|
||||||
|
check_ispytest(_ispytest)
|
||||||
|
self.name = name
|
||||||
|
self.description = description
|
||||||
|
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.
|
||||||
|
: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("option names %s already added" % conflict)
|
||||||
|
option = Argument(*opts, **attrs)
|
||||||
|
self._addoption_instance(option, shortupper=False)
|
||||||
|
|
||||||
|
def _addoption(self, *opts: str, **attrs: Any) -> None:
|
||||||
|
option = Argument(*opts, **attrs)
|
||||||
|
self._addoption_instance(option, shortupper=True)
|
||||||
|
|
||||||
|
def _addoption_instance(self, option: "Argument", shortupper: bool = False) -> None:
|
||||||
|
if not shortupper:
|
||||||
|
for opt in option._short_opts:
|
||||||
|
if opt[0] == "-" and opt[1].islower():
|
||||||
|
raise ValueError("lowercase shortoptions reserved")
|
||||||
|
if self.parser:
|
||||||
|
self.parser.processoption(option)
|
||||||
|
self.options.append(option)
|
||||||
|
|
||||||
|
|
||||||
|
class MyOptionParser(argparse.ArgumentParser):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
parser: Parser,
|
||||||
|
extra_info: Optional[Dict[str, Any]] = None,
|
||||||
|
prog: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
self._parser = parser
|
||||||
|
super().__init__(
|
||||||
|
prog=prog,
|
||||||
|
usage=parser._usage,
|
||||||
|
add_help=False,
|
||||||
|
formatter_class=DropShorterLongHelpFormatter,
|
||||||
|
allow_abbrev=False,
|
||||||
|
)
|
||||||
|
# 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 if extra_info else {}
|
||||||
|
|
||||||
|
def error(self, message: str) -> NoReturn:
|
||||||
|
"""Transform argparse error message into UsageError."""
|
||||||
|
msg = f"{self.prog}: error: {message}"
|
||||||
|
|
||||||
|
if hasattr(self._parser, "_config_source_hint"):
|
||||||
|
# Type ignored because the attribute is set dynamically.
|
||||||
|
msg = f"{msg} ({self._parser._config_source_hint})" # type: ignore
|
||||||
|
|
||||||
|
raise UsageError(self.format_usage() + msg)
|
||||||
|
|
||||||
|
# Type ignored because typeshed has a very complex type in the superclass.
|
||||||
|
def parse_args( # type: ignore
|
||||||
|
self,
|
||||||
|
args: Optional[Sequence[str]] = None,
|
||||||
|
namespace: Optional[argparse.Namespace] = None,
|
||||||
|
) -> argparse.Namespace:
|
||||||
|
"""Allow splitting of positional arguments."""
|
||||||
|
parsed, unrecognized = self.parse_known_args(args, namespace)
|
||||||
|
if unrecognized:
|
||||||
|
for arg in unrecognized:
|
||||||
|
if arg and arg[0] == "-":
|
||||||
|
lines = ["unrecognized arguments: %s" % (" ".join(unrecognized))]
|
||||||
|
for k, v in sorted(self.extra_info.items()):
|
||||||
|
lines.append(f" {k}: {v}")
|
||||||
|
self.error("\n".join(lines))
|
||||||
|
getattr(parsed, FILE_OR_DIR).extend(unrecognized)
|
||||||
|
return parsed
|
||||||
|
|
||||||
|
if sys.version_info[:2] < (3, 9): # pragma: no cover
|
||||||
|
# Backport of https://github.com/python/cpython/pull/14316 so we can
|
||||||
|
# disable long --argument abbreviations without breaking short flags.
|
||||||
|
def _parse_optional(
|
||||||
|
self, arg_string: str
|
||||||
|
) -> Optional[Tuple[Optional[argparse.Action], str, Optional[str]]]:
|
||||||
|
if not arg_string:
|
||||||
|
return None
|
||||||
|
if arg_string[0] not in self.prefix_chars:
|
||||||
|
return None
|
||||||
|
if arg_string in self._option_string_actions:
|
||||||
|
action = self._option_string_actions[arg_string]
|
||||||
|
return action, arg_string, None
|
||||||
|
if len(arg_string) == 1:
|
||||||
|
return None
|
||||||
|
if "=" in arg_string:
|
||||||
|
option_string, explicit_arg = arg_string.split("=", 1)
|
||||||
|
if option_string in self._option_string_actions:
|
||||||
|
action = self._option_string_actions[option_string]
|
||||||
|
return action, option_string, explicit_arg
|
||||||
|
if self.allow_abbrev or not arg_string.startswith("--"):
|
||||||
|
option_tuples = self._get_option_tuples(arg_string)
|
||||||
|
if len(option_tuples) > 1:
|
||||||
|
msg = gettext(
|
||||||
|
"ambiguous option: %(option)s could match %(matches)s"
|
||||||
|
)
|
||||||
|
options = ", ".join(option for _, option, _ in option_tuples)
|
||||||
|
self.error(msg % {"option": arg_string, "matches": options})
|
||||||
|
elif len(option_tuples) == 1:
|
||||||
|
(option_tuple,) = option_tuples
|
||||||
|
return option_tuple
|
||||||
|
if self._negative_number_matcher.match(arg_string):
|
||||||
|
if not self._has_negative_number_optionals:
|
||||||
|
return None
|
||||||
|
if " " in arg_string:
|
||||||
|
return None
|
||||||
|
return None, arg_string, None
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
- Cache result on the action object as this is called at least 2 times.
|
||||||
|
"""
|
||||||
|
|
||||||
|
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
|
||||||
|
res: Optional[str] = getattr(action, "_formatted_action_invocation", None)
|
||||||
|
if res:
|
||||||
|
return res
|
||||||
|
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'
|
||||||
|
action._formatted_action_invocation = orgstr # type: ignore
|
||||||
|
return orgstr
|
||||||
|
return_list = []
|
||||||
|
short_long: Dict[str, str] = {}
|
||||||
|
for option in options:
|
||||||
|
if len(option) == 2 or option[2] == " ":
|
||||||
|
continue
|
||||||
|
if not option.startswith("--"):
|
||||||
|
raise ArgumentError(
|
||||||
|
'long optional argument without "--": [%s]' % (option), 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))
|
||||||
|
formatted_action_invocation = ", ".join(return_list)
|
||||||
|
action._formatted_action_invocation = formatted_action_invocation # type: ignore
|
||||||
|
return formatted_action_invocation
|
||||||
|
|
||||||
|
def _split_lines(self, text, width):
|
||||||
|
"""Wrap lines after splitting on original newlines.
|
||||||
|
|
||||||
|
This allows to have explicit line breaks in the help text.
|
||||||
|
"""
|
||||||
|
import textwrap
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
for line in text.splitlines():
|
||||||
|
lines.extend(textwrap.wrap(line.strip(), width))
|
||||||
|
return lines
|
||||||
85
.venv/lib/python3.10/site-packages/_pytest/config/compat.py
Normal file
85
.venv/lib/python3.10/site-packages/_pytest/config/compat.py
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import functools
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
from typing import Mapping
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
import pluggy
|
||||||
|
|
||||||
|
from ..compat import LEGACY_PATH
|
||||||
|
from ..compat import legacy_path
|
||||||
|
from ..deprecated import HOOK_LEGACY_PATH_ARG
|
||||||
|
|
||||||
|
|
||||||
|
# hookname: (Path, LEGACY_PATH)
|
||||||
|
imply_paths_hooks: Mapping[str, tuple[str, str]] = {
|
||||||
|
"pytest_ignore_collect": ("collection_path", "path"),
|
||||||
|
"pytest_collect_file": ("file_path", "path"),
|
||||||
|
"pytest_pycollect_makemodule": ("module_path", "path"),
|
||||||
|
"pytest_report_header": ("start_path", "startdir"),
|
||||||
|
"pytest_report_collectionfinish": ("start_path", "startdir"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _check_path(path: Path, fspath: LEGACY_PATH) -> None:
|
||||||
|
if Path(fspath) != path:
|
||||||
|
raise ValueError(
|
||||||
|
f"Path({fspath!r}) != {path!r}\n"
|
||||||
|
"if both path and fspath are given they need to be equal"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class PathAwareHookProxy:
|
||||||
|
"""
|
||||||
|
this helper wraps around hook callers
|
||||||
|
until pluggy supports fixingcalls, this one will do
|
||||||
|
|
||||||
|
it currently doesn't return full hook caller proxies for fixed hooks,
|
||||||
|
this may have to be changed later depending on bugs
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, hook_relay: pluggy.HookRelay) -> None:
|
||||||
|
self._hook_relay = hook_relay
|
||||||
|
|
||||||
|
def __dir__(self) -> list[str]:
|
||||||
|
return dir(self._hook_relay)
|
||||||
|
|
||||||
|
def __getattr__(self, key: str) -> pluggy.HookCaller:
|
||||||
|
hook: pluggy.HookCaller = getattr(self._hook_relay, key)
|
||||||
|
if key not in imply_paths_hooks:
|
||||||
|
self.__dict__[key] = hook
|
||||||
|
return hook
|
||||||
|
else:
|
||||||
|
path_var, fspath_var = imply_paths_hooks[key]
|
||||||
|
|
||||||
|
@functools.wraps(hook)
|
||||||
|
def fixed_hook(**kw: Any) -> Any:
|
||||||
|
path_value: Path | None = kw.pop(path_var, None)
|
||||||
|
fspath_value: LEGACY_PATH | None = kw.pop(fspath_var, None)
|
||||||
|
if fspath_value is not None:
|
||||||
|
warnings.warn(
|
||||||
|
HOOK_LEGACY_PATH_ARG.format(
|
||||||
|
pylib_path_arg=fspath_var, pathlib_path_arg=path_var
|
||||||
|
),
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
if path_value is not None:
|
||||||
|
if fspath_value is not None:
|
||||||
|
_check_path(path_value, fspath_value)
|
||||||
|
else:
|
||||||
|
fspath_value = legacy_path(path_value)
|
||||||
|
else:
|
||||||
|
assert fspath_value is not None
|
||||||
|
path_value = Path(fspath_value)
|
||||||
|
|
||||||
|
kw[path_var] = path_value
|
||||||
|
kw[fspath_var] = fspath_value
|
||||||
|
return hook(**kw)
|
||||||
|
|
||||||
|
fixed_hook.name = hook.name # type: ignore[attr-defined]
|
||||||
|
fixed_hook.spec = hook.spec # type: ignore[attr-defined]
|
||||||
|
fixed_hook.__name__ = key
|
||||||
|
self.__dict__[key] = fixed_hook
|
||||||
|
return fixed_hook # type: ignore[return-value]
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
from typing import final
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class UsageError(Exception):
|
||||||
|
"""Error in pytest usage or invocation."""
|
||||||
|
|
||||||
|
|
||||||
|
class PrintHelp(Exception):
|
||||||
|
"""Raised when pytest should print its help to skip the rest of the
|
||||||
|
argument parsing and validation."""
|
||||||
231
.venv/lib/python3.10/site-packages/_pytest/config/findpaths.py
Normal file
231
.venv/lib/python3.10/site-packages/_pytest/config/findpaths.py
Normal file
|
|
@ -0,0 +1,231 @@
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
|
from typing import Dict
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Sequence
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
import iniconfig
|
||||||
|
|
||||||
|
from .exceptions import UsageError
|
||||||
|
from _pytest.outcomes import fail
|
||||||
|
from _pytest.pathlib import absolutepath
|
||||||
|
from _pytest.pathlib import commonpath
|
||||||
|
from _pytest.pathlib import safe_exists
|
||||||
|
|
||||||
|
|
||||||
|
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 load_config_dict_from_file(
|
||||||
|
filepath: Path,
|
||||||
|
) -> Optional[Dict[str, Union[str, List[str]]]]:
|
||||||
|
"""Load pytest configuration from the given file path, if supported.
|
||||||
|
|
||||||
|
Return None if the file does not contain valid pytest configuration.
|
||||||
|
"""
|
||||||
|
# Configuration from ini files are obtained from the [pytest] section, if present.
|
||||||
|
if filepath.suffix == ".ini":
|
||||||
|
iniconfig = _parse_ini_config(filepath)
|
||||||
|
|
||||||
|
if "pytest" in iniconfig:
|
||||||
|
return dict(iniconfig["pytest"].items())
|
||||||
|
else:
|
||||||
|
# "pytest.ini" files are always the source of configuration, even if empty.
|
||||||
|
if filepath.name == "pytest.ini":
|
||||||
|
return {}
|
||||||
|
|
||||||
|
# '.cfg' files are considered if they contain a "[tool:pytest]" section.
|
||||||
|
elif filepath.suffix == ".cfg":
|
||||||
|
iniconfig = _parse_ini_config(filepath)
|
||||||
|
|
||||||
|
if "tool:pytest" in iniconfig.sections:
|
||||||
|
return dict(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)
|
||||||
|
|
||||||
|
# '.toml' files are considered if they contain a [tool.pytest.ini_options] table.
|
||||||
|
elif filepath.suffix == ".toml":
|
||||||
|
if sys.version_info >= (3, 11):
|
||||||
|
import tomllib
|
||||||
|
else:
|
||||||
|
import tomli as tomllib
|
||||||
|
|
||||||
|
toml_text = filepath.read_text(encoding="utf-8")
|
||||||
|
try:
|
||||||
|
config = tomllib.loads(toml_text)
|
||||||
|
except tomllib.TOMLDecodeError as exc:
|
||||||
|
raise UsageError(f"{filepath}: {exc}") from exc
|
||||||
|
|
||||||
|
result = config.get("tool", {}).get("pytest", {}).get("ini_options", None)
|
||||||
|
if result is not None:
|
||||||
|
# TOML supports richer data types than ini files (strings, arrays, floats, ints, etc),
|
||||||
|
# however we need to convert all scalar values to str for compatibility with the rest
|
||||||
|
# of the configuration system, which expects strings only.
|
||||||
|
def make_scalar(v: object) -> Union[str, List[str]]:
|
||||||
|
return v if isinstance(v, list) else str(v)
|
||||||
|
|
||||||
|
return {k: make_scalar(v) for k, v in result.items()}
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def locate_config(
|
||||||
|
invocation_dir: Path,
|
||||||
|
args: Iterable[Path],
|
||||||
|
) -> Tuple[Optional[Path], Optional[Path], Dict[str, Union[str, List[str]]]]:
|
||||||
|
"""Search in the list of arguments for a valid ini-file for pytest,
|
||||||
|
and return a tuple of (rootdir, inifile, cfg-dict)."""
|
||||||
|
config_names = [
|
||||||
|
"pytest.ini",
|
||||||
|
".pytest.ini",
|
||||||
|
"pyproject.toml",
|
||||||
|
"tox.ini",
|
||||||
|
"setup.cfg",
|
||||||
|
]
|
||||||
|
args = [x for x in args if not str(x).startswith("-")]
|
||||||
|
if not args:
|
||||||
|
args = [invocation_dir]
|
||||||
|
found_pyproject_toml: Optional[Path] = None
|
||||||
|
for arg in args:
|
||||||
|
argpath = absolutepath(arg)
|
||||||
|
for base in (argpath, *argpath.parents):
|
||||||
|
for config_name in config_names:
|
||||||
|
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 = load_config_dict_from_file(p)
|
||||||
|
if ini_config is not None:
|
||||||
|
return base, p, ini_config
|
||||||
|
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: Optional[Path] = 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_file_part_from_node_id(x: str) -> str:
|
||||||
|
return x.split("::")[0]
|
||||||
|
|
||||||
|
def get_dir_from_path(path: Path) -> Path:
|
||||||
|
if path.is_dir():
|
||||||
|
return path
|
||||||
|
return path.parent
|
||||||
|
|
||||||
|
# These look like paths but may not exist
|
||||||
|
possible_paths = (
|
||||||
|
absolutepath(get_file_part_from_node_id(arg))
|
||||||
|
for arg in args
|
||||||
|
if not is_option(arg)
|
||||||
|
)
|
||||||
|
|
||||||
|
return [get_dir_from_path(path) for path in possible_paths if safe_exists(path)]
|
||||||
|
|
||||||
|
|
||||||
|
CFG_PYTEST_SECTION = "[pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead."
|
||||||
|
|
||||||
|
|
||||||
|
def determine_setup(
|
||||||
|
*,
|
||||||
|
inifile: Optional[str],
|
||||||
|
args: Sequence[str],
|
||||||
|
rootdir_cmd_arg: Optional[str],
|
||||||
|
invocation_dir: Path,
|
||||||
|
) -> Tuple[Path, Optional[Path], Dict[str, Union[str, List[str]]]]:
|
||||||
|
"""Determine the rootdir, inifile and ini configuration values from the
|
||||||
|
command line arguments.
|
||||||
|
|
||||||
|
:param inifile:
|
||||||
|
The `--inifile` command line argument, 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.
|
||||||
|
"""
|
||||||
|
rootdir = None
|
||||||
|
dirs = get_dirs_from_args(args)
|
||||||
|
if inifile:
|
||||||
|
inipath_ = absolutepath(inifile)
|
||||||
|
inipath: Optional[Path] = inipath_
|
||||||
|
inicfg = load_config_dict_from_file(inipath_) or {}
|
||||||
|
if rootdir_cmd_arg is None:
|
||||||
|
rootdir = inipath_.parent
|
||||||
|
else:
|
||||||
|
ancestor = get_common_ancestor(invocation_dir, dirs)
|
||||||
|
rootdir, inipath, inicfg = 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):
|
||||||
|
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."
|
||||||
|
)
|
||||||
|
assert rootdir is not None
|
||||||
|
return rootdir, inipath, inicfg or {}
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
393
.venv/lib/python3.10/site-packages/_pytest/debugging.py
Normal file
393
.venv/lib/python3.10/site-packages/_pytest/debugging.py
Normal file
|
|
@ -0,0 +1,393 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Interactive debugging with PDB, the Python Debugger."""
|
||||||
|
import argparse
|
||||||
|
import functools
|
||||||
|
import sys
|
||||||
|
import types
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Generator
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import Union
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from _pytest import outcomes
|
||||||
|
from _pytest._code import ExceptionInfo
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from _pytest.capture import CaptureManager
|
||||||
|
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: Optional[PytestPluginManager] = None
|
||||||
|
_config: Optional[Config] = None
|
||||||
|
_saved: List[
|
||||||
|
Tuple[Callable[..., None], Optional[PytestPluginManager], Optional[Config]]
|
||||||
|
] = []
|
||||||
|
_recursive_debug = 0
|
||||||
|
_wrapped_pdb_cls: Optional[Tuple[Type[Any], Type[Any]]] = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _is_capturing(cls, capman: Optional["CaptureManager"]) -> Union[str, bool]:
|
||||||
|
if capman:
|
||||||
|
return capman.is_capturing()
|
||||||
|
return False
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _import_pdb_cls(cls, capman: Optional["CaptureManager"]):
|
||||||
|
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:
|
||||||
|
__import__(modname)
|
||||||
|
mod = sys.modules[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: Optional["CaptureManager"]):
|
||||||
|
import _pytest.config
|
||||||
|
|
||||||
|
# Type ignored because mypy doesn't support "dynamic"
|
||||||
|
# inheritance like this.
|
||||||
|
class PytestPdbWrapper(pdb_cls): # type: ignore[valid-type,misc]
|
||||||
|
_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
|
||||||
|
|
||||||
|
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(
|
||||||
|
">",
|
||||||
|
"PDB continue (IO-capturing resumed for %s)"
|
||||||
|
% 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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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: Optional[CaptureManager] = 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
|
||||||
|
|
||||||
|
if not isinstance(call.excinfo.value, unittest.SkipTest):
|
||||||
|
_enter_pdb(node, call.excinfo, report)
|
||||||
|
|
||||||
|
def pytest_internalerror(self, excinfo: ExceptionInfo[BaseException]) -> None:
|
||||||
|
tb = _postmortem_traceback(excinfo)
|
||||||
|
post_mortem(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):
|
||||||
|
"""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):
|
||||||
|
func = functools.partial(testfunction, *args, **kwargs)
|
||||||
|
_pdb.runcall(func)
|
||||||
|
|
||||||
|
pyfuncitem.obj = wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def maybe_wrap_pytest_function_for_tracing(pyfuncitem):
|
||||||
|
"""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 re-use 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 = _postmortem_traceback(excinfo)
|
||||||
|
rep._pdbshown = True # type: ignore[attr-defined]
|
||||||
|
post_mortem(tb)
|
||||||
|
return rep
|
||||||
|
|
||||||
|
|
||||||
|
def _postmortem_traceback(excinfo: ExceptionInfo[BaseException]) -> types.TracebackType:
|
||||||
|
from doctest import UnexpectedException
|
||||||
|
|
||||||
|
if isinstance(excinfo.value, UnexpectedException):
|
||||||
|
# A doctest.UnexpectedException is not useful for post_mortem.
|
||||||
|
# Use the underlying exception instead:
|
||||||
|
return excinfo.value.exc_info[2]
|
||||||
|
elif isinstance(excinfo.value, ConftestImportFailure):
|
||||||
|
# A config.ConftestImportFailure is not useful for post_mortem.
|
||||||
|
# Use the underlying exception instead:
|
||||||
|
assert excinfo.value.cause.__traceback__ is not None
|
||||||
|
return excinfo.value.cause.__traceback__
|
||||||
|
else:
|
||||||
|
assert excinfo._excinfo is not None
|
||||||
|
return excinfo._excinfo[2]
|
||||||
|
|
||||||
|
|
||||||
|
def post_mortem(t: types.TracebackType) -> None:
|
||||||
|
p = pytestPDB._init_pdb("post_mortem")
|
||||||
|
p.reset()
|
||||||
|
p.interaction(None, t)
|
||||||
|
if p.quitting:
|
||||||
|
outcomes.exit("Quitting debugger")
|
||||||
89
.venv/lib/python3.10/site-packages/_pytest/deprecated.py
Normal file
89
.venv/lib/python3.10/site-packages/_pytest/deprecated.py
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
"""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 either instances of
|
||||||
|
:class:`PytestWarning`, or :class:`UnformattedWarning`
|
||||||
|
in case of warnings which need to format their messages.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from warnings import warn
|
||||||
|
|
||||||
|
from _pytest.warning_types import PytestDeprecationWarning
|
||||||
|
from _pytest.warning_types import PytestRemovedIn9Warning
|
||||||
|
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",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# This can be* removed pytest 8, but it's harmless and common, so no rush to remove.
|
||||||
|
# * If you're in the future: "could have been".
|
||||||
|
YIELD_FIXTURE = PytestDeprecationWarning(
|
||||||
|
"@pytest.yield_fixture is deprecated.\n"
|
||||||
|
"Use @pytest.fixture instead; they are the same."
|
||||||
|
)
|
||||||
|
|
||||||
|
# This deprecation is never really meant to be removed.
|
||||||
|
PRIVATE = PytestDeprecationWarning("A private pytest class or function was used.")
|
||||||
|
|
||||||
|
|
||||||
|
HOOK_LEGACY_PATH_ARG = UnformattedWarning(
|
||||||
|
PytestRemovedIn9Warning,
|
||||||
|
"The ({pylib_path_arg}: py.path.local) argument is deprecated, please use ({pathlib_path_arg}: pathlib.Path)\n"
|
||||||
|
"see https://docs.pytest.org/en/latest/deprecations.html"
|
||||||
|
"#py-path-local-arguments-for-hooks-replaced-with-pathlib-path",
|
||||||
|
)
|
||||||
|
|
||||||
|
NODE_CTOR_FSPATH_ARG = UnformattedWarning(
|
||||||
|
PytestRemovedIn9Warning,
|
||||||
|
"The (fspath: py.path.local) argument to {node_type_name} is deprecated. "
|
||||||
|
"Please use the (path: pathlib.Path) argument instead.\n"
|
||||||
|
"See https://docs.pytest.org/en/latest/deprecations.html"
|
||||||
|
"#fspath-argument-for-node-constructors-replaced-with-pathlib-path",
|
||||||
|
)
|
||||||
|
|
||||||
|
HOOK_LEGACY_MARKING = UnformattedWarning(
|
||||||
|
PytestDeprecationWarning,
|
||||||
|
"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",
|
||||||
|
)
|
||||||
|
|
||||||
|
MARKED_FIXTURE = PytestRemovedIn9Warning(
|
||||||
|
"Marks applied to fixtures have no effect\n"
|
||||||
|
"See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 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).
|
||||||
|
|
||||||
|
|
||||||
|
def check_ispytest(ispytest: bool) -> None:
|
||||||
|
if not ispytest:
|
||||||
|
warn(PRIVATE, stacklevel=3)
|
||||||
751
.venv/lib/python3.10/site-packages/_pytest/doctest.py
Normal file
751
.venv/lib/python3.10/site-packages/_pytest/doctest.py
Normal file
|
|
@ -0,0 +1,751 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Discover and run doctests in modules and test files."""
|
||||||
|
import bdb
|
||||||
|
from contextlib import contextmanager
|
||||||
|
import functools
|
||||||
|
import inspect
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import platform
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
import types
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Dict
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Pattern
|
||||||
|
from typing import Sequence
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import Union
|
||||||
|
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.compat import safe_getattr
|
||||||
|
from _pytest.config import Config
|
||||||
|
from _pytest.config.argparsing import Parser
|
||||||
|
from _pytest.fixtures import fixture
|
||||||
|
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.python_api import approx
|
||||||
|
from _pytest.warning_types import PytestWarning
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import doctest
|
||||||
|
from typing 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: Optional[Type["doctest.OutputChecker"]] = 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,
|
||||||
|
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
|
||||||
|
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: Optional["doctest.OutputChecker"] = None,
|
||||||
|
verbose: Optional[bool] = 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: Optional["doctest.OutputChecker"] = None,
|
||||||
|
verbose: Optional[bool] = 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: "Union[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: "Union[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"] = []
|
||||||
|
# Type ignored because we change the type of `out` from what
|
||||||
|
# doctest expects.
|
||||||
|
self.runner.run(self.dtest, out=failures) # type: ignore[arg-type]
|
||||||
|
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],
|
||||||
|
) -> Union[str, TerminalRepr]:
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
failures: Optional[
|
||||||
|
Sequence[Union[doctest.DocTestFailure, doctest.UnexpectedException]]
|
||||||
|
] = 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 = [
|
||||||
|
"%03d %s" % (i + test.lineno + 1, 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 += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)]
|
||||||
|
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[Union["os.PathLike[str]", str], Optional[int], str]:
|
||||||
|
return self.path, self.dtest.lineno, "[doctest] %s" % 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, None, 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: Optional[Callable[[Any], Any]] = 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):
|
||||||
|
"""A hackish doctest finder that overrides stdlib internals to fix a stdlib bug.
|
||||||
|
|
||||||
|
https://github.com/pytest-dev/pytest/issues/3456
|
||||||
|
https://bugs.python.org/issue25532
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _find_lineno(self, obj, source_lines):
|
||||||
|
"""Doctest code does not take into account `@property`, this
|
||||||
|
is a hackish way to fix it. https://bugs.python.org/issue17446
|
||||||
|
|
||||||
|
Wrapped Doctests will need to be unwrapped so the correct
|
||||||
|
line number is returned. This will be reported upstream. #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,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _find(
|
||||||
|
self, tests, obj, name, module, source_lines, globs, seen
|
||||||
|
) -> None:
|
||||||
|
if _is_mocked(obj):
|
||||||
|
return
|
||||||
|
with _patch_unwrap_mock_aware():
|
||||||
|
# Type ignored because this is a private function.
|
||||||
|
super()._find( # type:ignore[misc]
|
||||||
|
tests, obj, name, module, source_lines, globs, seen
|
||||||
|
)
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
else: # pragma: no cover
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
module = self.obj
|
||||||
|
except Collector.CollectError:
|
||||||
|
if self.config.getvalue("doctest_ignore_import_errors"):
|
||||||
|
skip("unable to import module %r" % self.path)
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
# While doctests currently don't support fixtures directly, we still
|
||||||
|
# need to pick up autouse fixtures.
|
||||||
|
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
|
||||||
|
import re
|
||||||
|
|
||||||
|
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: 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):
|
||||||
|
fraction: Optional[str] = w.group("fraction")
|
||||||
|
exponent: Optional[str] = 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()
|
||||||
102
.venv/lib/python3.10/site-packages/_pytest/faulthandler.py
Normal file
102
.venv/lib/python3.10/site-packages/_pytest/faulthandler.py
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import Generator
|
||||||
|
|
||||||
|
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 = (
|
||||||
|
"Dump the traceback of all threads if a test takes "
|
||||||
|
"more than TIMEOUT seconds to finish"
|
||||||
|
)
|
||||||
|
parser.addini("faulthandler_timeout", help, default=0.0)
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
return sys.__stderr__.fileno()
|
||||||
|
|
||||||
|
|
||||||
|
def get_timeout_config_value(config: Config) -> float:
|
||||||
|
return float(config.getini("faulthandler_timeout") or 0.0)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True, trylast=True)
|
||||||
|
def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]:
|
||||||
|
timeout = get_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)
|
||||||
|
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()
|
||||||
1773
.venv/lib/python3.10/site-packages/_pytest/fixtures.py
Normal file
1773
.venv/lib/python3.10/site-packages/_pytest/fixtures.py
Normal file
File diff suppressed because it is too large
Load diff
45
.venv/lib/python3.10/site-packages/_pytest/freeze_support.py
Normal file
45
.venv/lib/python3.10/site-packages/_pytest/freeze_support.py
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
"""Provides a function to report all internal modules for using freezing
|
||||||
|
tools."""
|
||||||
|
|
||||||
|
import types
|
||||||
|
from typing import Iterator
|
||||||
|
from typing import List
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
|
||||||
|
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: Union[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__ # type: ignore[attr-defined]
|
||||||
|
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
|
||||||
271
.venv/lib/python3.10/site-packages/_pytest/helpconfig.py
Normal file
271
.venv/lib/python3.10/site-packages/_pytest/helpconfig.py
Normal file
|
|
@ -0,0 +1,271 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Version info, help messages, tracing configuration."""
|
||||||
|
from argparse import Action
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import Generator
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from _pytest.config import Config
|
||||||
|
from _pytest.config import ExitCode
|
||||||
|
from _pytest.config import PrintHelp
|
||||||
|
from _pytest.config.argparsing import Parser
|
||||||
|
from _pytest.terminal import TerminalReporter
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
class HelpAction(Action):
|
||||||
|
"""An argparse Action that will raise an exception in order to skip the
|
||||||
|
rest of the argument parsing when --help is passed.
|
||||||
|
|
||||||
|
This prevents argparse from quitting due to 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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, option_strings, dest=None, default=False, help=None):
|
||||||
|
super().__init__(
|
||||||
|
option_strings=option_strings,
|
||||||
|
dest=dest,
|
||||||
|
const=True,
|
||||||
|
default=default,
|
||||||
|
nargs=0,
|
||||||
|
help=help,
|
||||||
|
)
|
||||||
|
|
||||||
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
|
setattr(namespace, self.dest, self.const)
|
||||||
|
|
||||||
|
# We should only skip the rest of the parsing after preparse is done.
|
||||||
|
if getattr(parser._parser, "after_preparse", False):
|
||||||
|
raise PrintHelp
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_addoption(parser: Parser) -> None:
|
||||||
|
group = parser.getgroup("debugconfig")
|
||||||
|
group.addoption(
|
||||||
|
"--version",
|
||||||
|
"-V",
|
||||||
|
action="count",
|
||||||
|
default=0,
|
||||||
|
dest="version",
|
||||||
|
help="Display pytest version and information about plugins. "
|
||||||
|
"When given twice, also display information about plugins.",
|
||||||
|
)
|
||||||
|
group._addoption(
|
||||||
|
"-h",
|
||||||
|
"--help",
|
||||||
|
action=HelpAction,
|
||||||
|
dest="help",
|
||||||
|
help="Show help message and configuration info",
|
||||||
|
)
|
||||||
|
group._addoption(
|
||||||
|
"-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`.",
|
||||||
|
)
|
||||||
|
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(
|
||||||
|
"-o",
|
||||||
|
"--override-ini",
|
||||||
|
dest="override_ini",
|
||||||
|
action="append",
|
||||||
|
help='Override ini option with "option=value" style, '
|
||||||
|
"e.g. `-o xfail_strict=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("writing pytest debug information to %s\n" % path)
|
||||||
|
|
||||||
|
def unset_tracing() -> None:
|
||||||
|
debugfile.close()
|
||||||
|
sys.stderr.write("wrote pytest debug information to %s\n" % debugfile.name)
|
||||||
|
config.trace.root.setwriter(None)
|
||||||
|
undo_tracing()
|
||||||
|
|
||||||
|
config.add_cleanup(unset_tracing)
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def showversion(config: Config) -> None:
|
||||||
|
if config.option.version > 1:
|
||||||
|
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")
|
||||||
|
else:
|
||||||
|
sys.stdout.write(f"pytest {pytest.__version__}\n")
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
|
||||||
|
if config.option.version > 0:
|
||||||
|
showversion(config)
|
||||||
|
return 0
|
||||||
|
elif config.option.help:
|
||||||
|
config._do_configure()
|
||||||
|
showhelp(config)
|
||||||
|
config._ensure_unconfigure()
|
||||||
|
return 0
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def showhelp(config: Config) -> None:
|
||||||
|
import textwrap
|
||||||
|
|
||||||
|
reporter: Optional[TerminalReporter] = 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] ini-options in the first "
|
||||||
|
"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._ininames:
|
||||||
|
help, type, default = config._parser._inidict[name]
|
||||||
|
if type is None:
|
||||||
|
type = "string"
|
||||||
|
if help is None:
|
||||||
|
raise TypeError(f"help argument cannot be None for {name}")
|
||||||
|
spec = f"{name} ({type}):"
|
||||||
|
tw.write(" %s" % spec)
|
||||||
|
spec_len = len(spec)
|
||||||
|
if spec_len > (indent_len - 3):
|
||||||
|
# Display help starting at a new line.
|
||||||
|
tw.line()
|
||||||
|
helplines = textwrap.wrap(
|
||||||
|
help,
|
||||||
|
columns,
|
||||||
|
initial_indent=indent,
|
||||||
|
subsequent_indent=indent,
|
||||||
|
break_on_hyphens=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
for line in helplines:
|
||||||
|
tw.line(line)
|
||||||
|
else:
|
||||||
|
# Display help starting after the spec, following lines indented.
|
||||||
|
tw.write(" " * (indent_len - spec_len - 2))
|
||||||
|
wrapped = textwrap.wrap(help, columns - indent_len, break_on_hyphens=False)
|
||||||
|
|
||||||
|
if wrapped:
|
||||||
|
tw.line(wrapped[0])
|
||||||
|
for line in wrapped[1:]:
|
||||||
|
tw.line(indent + line)
|
||||||
|
|
||||||
|
tw.line()
|
||||||
|
tw.line("Environment variables:")
|
||||||
|
vars = [
|
||||||
|
("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"),
|
||||||
|
]
|
||||||
|
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)
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
conftest_options = [("pytest_plugins", "list of plugin names to load")]
|
||||||
|
|
||||||
|
|
||||||
|
def getpluginversioninfo(config: Config) -> List[str]:
|
||||||
|
lines = []
|
||||||
|
plugininfo = config.pluginmanager.list_plugin_distinfo()
|
||||||
|
if plugininfo:
|
||||||
|
lines.append("setuptools registered 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
|
||||||
1283
.venv/lib/python3.10/site-packages/_pytest/hookspec.py
Normal file
1283
.venv/lib/python3.10/site-packages/_pytest/hookspec.py
Normal file
File diff suppressed because it is too large
Load diff
700
.venv/lib/python3.10/site-packages/_pytest/junitxml.py
Normal file
700
.venv/lib/python3.10/site-packages/_pytest/junitxml.py
Normal file
|
|
@ -0,0 +1,700 @@
|
||||||
|
# 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 datetime import datetime
|
||||||
|
import functools
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
import re
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Dict
|
||||||
|
from typing import List
|
||||||
|
from typing import Match
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Union
|
||||||
|
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.config import Config
|
||||||
|
from _pytest.config import filename_arg
|
||||||
|
from _pytest.config.argparsing import Parser
|
||||||
|
from _pytest.fixtures import FixtureRequest
|
||||||
|
from _pytest.reports import TestReport
|
||||||
|
from _pytest.stash import StashKey
|
||||||
|
from _pytest.terminal import TerminalReporter
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
xml_key = StashKey["LogXML"]()
|
||||||
|
|
||||||
|
|
||||||
|
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: Match[str]) -> str:
|
||||||
|
i = ord(matchobj.group())
|
||||||
|
if i <= 0xFF:
|
||||||
|
return "#x%02X" % i
|
||||||
|
else:
|
||||||
|
return "#x%04X" % i
|
||||||
|
|
||||||
|
# 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\u10000-\u10FFFF]"
|
||||||
|
)
|
||||||
|
return re.sub(illegal_xml_re, repl, str(arg))
|
||||||
|
|
||||||
|
|
||||||
|
def merge_family(left, right) -> None:
|
||||||
|
result = {}
|
||||||
|
for kl, vl in left.items():
|
||||||
|
for kr, vr in right.items():
|
||||||
|
if not isinstance(vl, list):
|
||||||
|
raise TypeError(type(vl))
|
||||||
|
result[kl] = vl + vr
|
||||||
|
left.update(result)
|
||||||
|
|
||||||
|
|
||||||
|
families = {}
|
||||||
|
families["_base"] = {"testcase": ["classname", "name"]}
|
||||||
|
families["_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, nodeid: Union[str, TestReport], xml: "LogXML") -> None:
|
||||||
|
self.id = nodeid
|
||||||
|
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) -> Optional[ET.Element]:
|
||||||
|
"""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) -> None:
|
||||||
|
names = mangle_test_address(testreport.nodeid)
|
||||||
|
existing_attrs = self.attrs
|
||||||
|
classnames = names[:-1]
|
||||||
|
if self.xml.prefix:
|
||||||
|
classnames.insert(0, self.xml.prefix)
|
||||||
|
attrs: Dict[str, str] = {
|
||||||
|
"classname": ".".join(classnames),
|
||||||
|
"name": bin_xml_escape(names[-1]),
|
||||||
|
"file": testreport.location[0],
|
||||||
|
}
|
||||||
|
if testreport.location[1] is not None:
|
||||||
|
attrs["line"] = str(testreport.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.keys():
|
||||||
|
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="%.3f" % self.duration)
|
||||||
|
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: Optional[str] = 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: Optional[ReprFileLocation] = getattr(
|
||||||
|
report.longrepr, "reprcrash", None
|
||||||
|
)
|
||||||
|
if reprcrash is not None:
|
||||||
|
message = reprcrash.message
|
||||||
|
else:
|
||||||
|
message = str(report.longrepr)
|
||||||
|
message = bin_xml_escape(message)
|
||||||
|
self._add_simple("failure", message, str(report.longrepr))
|
||||||
|
|
||||||
|
def append_collect_error(self, report: TestReport) -> 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: TestReport) -> None:
|
||||||
|
self._add_simple("skipped", "collection skipped", str(report.longrepr))
|
||||||
|
|
||||||
|
def append_error(self, report: TestReport) -> None:
|
||||||
|
assert report.longrepr is not None
|
||||||
|
reprcrash: Optional[ReprFileLocation] = 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(msg), str(report.longrepr))
|
||||||
|
|
||||||
|
def append_skipped(self, report: TestReport) -> None:
|
||||||
|
if hasattr(report, "wasxfail"):
|
||||||
|
xfailreason = report.wasxfail
|
||||||
|
if xfailreason.startswith("reason: "):
|
||||||
|
xfailreason = xfailreason[8:]
|
||||||
|
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
|
||||||
|
if skipreason.startswith("Skipped: "):
|
||||||
|
skipreason = skipreason[9:]
|
||||||
|
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[assignment]
|
||||||
|
|
||||||
|
|
||||||
|
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.nodeid)
|
||||||
|
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: "
|
||||||
|
"one of no|log|system-out|system-err|out-err|all",
|
||||||
|
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: one of total|call",
|
||||||
|
default="total",
|
||||||
|
) # choices=['total', 'call'])
|
||||||
|
parser.addini(
|
||||||
|
"junit_family",
|
||||||
|
"Emit XML for schema: one of legacy|xunit1|xunit2",
|
||||||
|
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"):
|
||||||
|
junit_family = config.getini("junit_family")
|
||||||
|
config.stash[xml_key] = LogXML(
|
||||||
|
xmlpath,
|
||||||
|
config.option.junitprefix,
|
||||||
|
config.getini("junit_suite_name"),
|
||||||
|
config.getini("junit_logging"),
|
||||||
|
config.getini("junit_duration_report"),
|
||||||
|
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]:
|
||||||
|
path, possible_open_bracket, params = address.partition("[")
|
||||||
|
names = path.split("::")
|
||||||
|
# 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.
|
||||||
|
names[-1] += possible_open_bracket + params
|
||||||
|
return names
|
||||||
|
|
||||||
|
|
||||||
|
class LogXML:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
logfile,
|
||||||
|
prefix: Optional[str],
|
||||||
|
suite_name: str = "pytest",
|
||||||
|
logging: str = "no",
|
||||||
|
report_duration: str = "total",
|
||||||
|
family="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[Union[str, TestReport], 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:
|
||||||
|
nodeid = getattr(report, "nodeid", report)
|
||||||
|
# Local hack to handle xdist report order.
|
||||||
|
workernode = getattr(report, "node", None)
|
||||||
|
reporter = self.node_reporters.pop((nodeid, 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: Union[TestReport, str]) -> _NodeReporter:
|
||||||
|
nodeid: Union[str, TestReport] = getattr(report, "nodeid", report)
|
||||||
|
# Local hack to handle xdist report order.
|
||||||
|
workernode = getattr(report, "node", None)
|
||||||
|
|
||||||
|
key = nodeid, workernode
|
||||||
|
|
||||||
|
if key in self.node_reporters:
|
||||||
|
# TODO: breaks for --dist=each
|
||||||
|
return self.node_reporters[key]
|
||||||
|
|
||||||
|
reporter = _NodeReporter(nodeid, 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) -> _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.nodeid == report.nodeid
|
||||||
|
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)
|
||||||
|
self.cnt_double_fail_tests += 1
|
||||||
|
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.nodeid == report.nodeid
|
||||||
|
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: TestReport) -> 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_time = timing.time()
|
||||||
|
|
||||||
|
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:
|
||||||
|
suite_stop_time = timing.time()
|
||||||
|
suite_time_delta = suite_stop_time - self.suite_start_time
|
||||||
|
|
||||||
|
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="%.3f" % suite_time_delta,
|
||||||
|
timestamp=datetime.fromtimestamp(self.suite_start_time).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.append(suite_node)
|
||||||
|
logfile.write(ET.tostring(testsuites, encoding="unicode"))
|
||||||
|
|
||||||
|
def pytest_terminal_summary(self, terminalreporter: TerminalReporter) -> None:
|
||||||
|
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) -> Optional[ET.Element]:
|
||||||
|
"""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
|
||||||
480
.venv/lib/python3.10/site-packages/_pytest/legacypath.py
Normal file
480
.venv/lib/python3.10/site-packages/_pytest/legacypath.py
Normal file
|
|
@ -0,0 +1,480 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Add backward compatibility support for the legacy py path type."""
|
||||||
|
|
||||||
|
import dataclasses
|
||||||
|
from pathlib import Path
|
||||||
|
import shlex
|
||||||
|
import subprocess
|
||||||
|
from typing import Final
|
||||||
|
from typing import final
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
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) -> Optional[Union[Item, Collector]]:
|
||||||
|
"""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[Union[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
|
||||||
|
) -> Optional[Union[Item, Collector]]:
|
||||||
|
"""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:
|
||||||
|
@staticmethod
|
||||||
|
@fixture
|
||||||
|
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:
|
||||||
|
@staticmethod
|
||||||
|
@fixture(scope="session")
|
||||||
|
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
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@fixture
|
||||||
|
def tmpdir(tmp_path: Path) -> LEGACY_PATH:
|
||||||
|
"""Return a temporary directory path object which is unique to each test
|
||||||
|
function invocation, created as a sub directory of the base temporary
|
||||||
|
directory.
|
||||||
|
|
||||||
|
By default, a new base temporary directory is created each test session,
|
||||||
|
and old bases are removed after 3 sessions, to aid in debugging. If
|
||||||
|
``--basetemp`` is used then it is cleared each session. See
|
||||||
|
:ref:`temporary directory location and retention`.
|
||||||
|
|
||||||
|
The returned object is a `legacy_path`_ object.
|
||||||
|
|
||||||
|
.. 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) -> Optional[LEGACY_PATH]:
|
||||||
|
"""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_stardir(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: Union[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_stardir), 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)
|
||||||
|
|
||||||
|
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.is_registered(LegacyTestdirPlugin):
|
||||||
|
manager.register(LegacyTestdirPlugin, "legacypath-pytester")
|
||||||
958
.venv/lib/python3.10/site-packages/_pytest/logging.py
Normal file
958
.venv/lib/python3.10/site-packages/_pytest/logging.py
Normal file
|
|
@ -0,0 +1,958 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Access and control log capturing."""
|
||||||
|
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 AbstractSet
|
||||||
|
from typing import Dict
|
||||||
|
from typing import final
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Generic
|
||||||
|
from typing import List
|
||||||
|
from typing import Literal
|
||||||
|
from typing import Mapping
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
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: Optional[str] = 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.WARN: {"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: Union[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: Union[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).
|
||||||
|
"""
|
||||||
|
if auto_indent_option is None:
|
||||||
|
return 0
|
||||||
|
elif isinstance(auto_indent_option, bool):
|
||||||
|
if auto_indent_option:
|
||||||
|
return -1
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
elif isinstance(auto_indent_option, int):
|
||||||
|
return int(auto_indent_option)
|
||||||
|
elif isinstance(auto_indent_option, str):
|
||||||
|
try:
|
||||||
|
return int(auto_indent_option)
|
||||||
|
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) # type: ignore[attr-defined]
|
||||||
|
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,
|
||||||
|
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, 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",
|
||||||
|
choices=["w", "a"],
|
||||||
|
help="Log file open mode",
|
||||||
|
)
|
||||||
|
add_option_ini(
|
||||||
|
"--log-file-level",
|
||||||
|
dest="log_file_level",
|
||||||
|
default=None,
|
||||||
|
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,
|
||||||
|
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__ = ("handler", "level", "orig_level")
|
||||||
|
|
||||||
|
def __init__(self, handler: _HandlerType, level: Optional[int] = None) -> None:
|
||||||
|
self.handler = handler
|
||||||
|
self.level = level
|
||||||
|
|
||||||
|
def __enter__(self) -> _HandlerType:
|
||||||
|
root_logger = logging.getLogger()
|
||||||
|
if self.level is not None:
|
||||||
|
self.handler.setLevel(self.level)
|
||||||
|
root_logger.addHandler(self.handler)
|
||||||
|
if self.level is not None:
|
||||||
|
self.orig_level = root_logger.level
|
||||||
|
root_logger.setLevel(min(self.orig_level, self.level))
|
||||||
|
return self.handler
|
||||||
|
|
||||||
|
def __exit__(
|
||||||
|
self,
|
||||||
|
exc_type: Optional[Type[BaseException]],
|
||||||
|
exc_val: Optional[BaseException],
|
||||||
|
exc_tb: Optional[TracebackType],
|
||||||
|
) -> None:
|
||||||
|
root_logger = logging.getLogger()
|
||||||
|
if self.level is not None:
|
||||||
|
root_logger.setLevel(self.orig_level)
|
||||||
|
root_logger.removeHandler(self.handler)
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
@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: Optional[int] = None
|
||||||
|
# Dict of log name -> log level.
|
||||||
|
self._initial_logger_levels: Dict[Optional[str], int] = {}
|
||||||
|
self._initial_disabled_logging_level: Optional[int] = 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: Union[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 # type: ignore[attr-defined]
|
||||||
|
|
||||||
|
if isinstance(level, str):
|
||||||
|
# Try to translate the level string to an int for `logging.disable()`
|
||||||
|
level = logging.getLevelName(level)
|
||||||
|
|
||||||
|
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: Union[int, str], logger: Optional[str] = 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: Union[int, str], logger: Optional[str] = None
|
||||||
|
) -> Generator[None, None, 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, None, 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
|
||||||
|
"""
|
||||||
|
self.handler.addFilter(filter_)
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
self.handler.removeFilter(filter_)
|
||||||
|
|
||||||
|
|
||||||
|
@fixture
|
||||||
|
def caplog(request: FixtureRequest) -> Generator[LogCaptureFixture, None, None]:
|
||||||
|
"""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) -> Optional[int]:
|
||||||
|
for setting_name in setting_names:
|
||||||
|
log_level = config.getoption(setting_name)
|
||||||
|
if log_level is None:
|
||||||
|
log_level = config.getini(setting_name)
|
||||||
|
if log_level:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if isinstance(log_level, str):
|
||||||
|
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: Union[
|
||||||
|
_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, None, 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, None, 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.getoption("verbose") < 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")
|
||||||
|
|
||||||
|
def _runtest_for(self, item: nodes.Item, when: str) -> Generator[None, None, 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, None, None]:
|
||||||
|
self.log_cli_handler.set_when("setup")
|
||||||
|
|
||||||
|
empty: Dict[str, List[logging.LogRecord]] = {}
|
||||||
|
item.stash[caplog_records_key] = empty
|
||||||
|
yield from self._runtest_for(item, "setup")
|
||||||
|
|
||||||
|
@hookimpl(wrapper=True)
|
||||||
|
def pytest_runtest_call(self, item: nodes.Item) -> Generator[None, None, None]:
|
||||||
|
self.log_cli_handler.set_when("call")
|
||||||
|
|
||||||
|
yield from self._runtest_for(item, "call")
|
||||||
|
|
||||||
|
@hookimpl(wrapper=True)
|
||||||
|
def pytest_runtest_teardown(self, item: nodes.Item) -> Generator[None, None, None]:
|
||||||
|
self.log_cli_handler.set_when("teardown")
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield from self._runtest_for(item, "teardown")
|
||||||
|
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, None, 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: Optional[CaptureManager],
|
||||||
|
) -> 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: Optional[str]) -> 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
|
||||||
1078
.venv/lib/python3.10/site-packages/_pytest/main.py
Normal file
1078
.venv/lib/python3.10/site-packages/_pytest/main.py
Normal file
File diff suppressed because it is too large
Load diff
278
.venv/lib/python3.10/site-packages/_pytest/mark/__init__.py
Normal file
278
.venv/lib/python3.10/site-packages/_pytest/mark/__init__.py
Normal file
|
|
@ -0,0 +1,278 @@
|
||||||
|
"""Generic mechanism for marking and selecting python functions."""
|
||||||
|
|
||||||
|
import dataclasses
|
||||||
|
from typing import AbstractSet
|
||||||
|
from typing import Collection
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from .expression import Expression
|
||||||
|
from .expression import ParseError
|
||||||
|
from .structures import EMPTY_PARAMETERSET_OPTION
|
||||||
|
from .structures import get_empty_parameterset_mark
|
||||||
|
from .structures import Mark
|
||||||
|
from .structures import MARK_GEN
|
||||||
|
from .structures import MarkDecorator
|
||||||
|
from .structures import MarkGenerator
|
||||||
|
from .structures import ParameterSet
|
||||||
|
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__ = [
|
||||||
|
"MARK_GEN",
|
||||||
|
"Mark",
|
||||||
|
"MarkDecorator",
|
||||||
|
"MarkGenerator",
|
||||||
|
"ParameterSet",
|
||||||
|
"get_empty_parameterset_mark",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
old_mark_config_key = StashKey[Optional[Config]]()
|
||||||
|
|
||||||
|
|
||||||
|
def param(
|
||||||
|
*values: object,
|
||||||
|
marks: Union[MarkDecorator, Collection[Union[MarkDecorator, Mark]]] = (),
|
||||||
|
id: Optional[str] = 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.
|
||||||
|
:param id: The id to attribute to this parameter set.
|
||||||
|
"""
|
||||||
|
return ParameterSet.param(*values, marks=marks, id=id)
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_addoption(parser: Parser) -> None:
|
||||||
|
group = parser.getgroup("general")
|
||||||
|
group._addoption(
|
||||||
|
"-k",
|
||||||
|
action="store",
|
||||||
|
dest="keyword",
|
||||||
|
default="",
|
||||||
|
metavar="EXPRESSION",
|
||||||
|
help="Only run tests which match the given substring expression. "
|
||||||
|
"An expression is a Python evaluatable expression "
|
||||||
|
"where all names are substring-matched against test names "
|
||||||
|
"and their parent classes. Example: -k 'test_method or test_"
|
||||||
|
"other' matches all test functions and classes whose name "
|
||||||
|
"contains 'test_method' or 'test_other', while -k 'not test_method' "
|
||||||
|
"matches those that don't contain 'test_method' in their names. "
|
||||||
|
"-k 'not test_method and not test_other' will eliminate the matches. "
|
||||||
|
"Additionally keywords are matched to classes and functions "
|
||||||
|
"containing extra names in their 'extra_keyword_matches' set, "
|
||||||
|
"as well as functions which have names assigned directly to them. "
|
||||||
|
"The matching is case-insensitive.",
|
||||||
|
)
|
||||||
|
|
||||||
|
group._addoption(
|
||||||
|
"-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")
|
||||||
|
|
||||||
|
|
||||||
|
@hookimpl(tryfirst=True)
|
||||||
|
def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
|
||||||
|
import _pytest.config
|
||||||
|
|
||||||
|
if config.option.markers:
|
||||||
|
config._do_configure()
|
||||||
|
tw = _pytest.config.create_terminal_writer(config)
|
||||||
|
for line in config.getini("markers"):
|
||||||
|
parts = line.split(":", 1)
|
||||||
|
name = parts[0]
|
||||||
|
rest = parts[1] if len(parts) == 2 else ""
|
||||||
|
tw.write("@pytest.mark.%s:" % name, bold=True)
|
||||||
|
tw.line(rest)
|
||||||
|
tw.line()
|
||||||
|
config._ensure_unconfigure()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class KeywordMatcher:
|
||||||
|
"""A matcher for keywords.
|
||||||
|
|
||||||
|
Given a list of names, matches any substring of one of these names. The
|
||||||
|
string inclusion check is case-insensitive.
|
||||||
|
|
||||||
|
Will match on the name of colitem, including the names of its parents.
|
||||||
|
Only matches names of items which are either a :class:`Class` or a
|
||||||
|
:class:`Function`.
|
||||||
|
|
||||||
|
Additionally, matches on names in the 'extra_keyword_matches' set of
|
||||||
|
any item, as well as names directly assigned to test functions.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__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.
|
||||||
|
function_obj = getattr(item, "function", None)
|
||||||
|
if function_obj:
|
||||||
|
mapped_names.update(function_obj.__dict__)
|
||||||
|
|
||||||
|
# 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) -> bool:
|
||||||
|
subname = subname.lower()
|
||||||
|
names = (name.lower() for name in self._names)
|
||||||
|
|
||||||
|
for name in names:
|
||||||
|
if subname in name:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
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_names",)
|
||||||
|
|
||||||
|
own_mark_names: AbstractSet[str]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_item(cls, item: "Item") -> "MarkMatcher":
|
||||||
|
mark_names = {mark.name for mark in item.iter_markers()}
|
||||||
|
return cls(mark_names)
|
||||||
|
|
||||||
|
def __call__(self, name: str) -> bool:
|
||||||
|
return name in self.own_mark_names
|
||||||
|
|
||||||
|
|
||||||
|
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'")
|
||||||
|
remaining: List[Item] = []
|
||||||
|
deselected: List[Item] = []
|
||||||
|
for item in items:
|
||||||
|
if expr.evaluate(MarkMatcher.from_item(item)):
|
||||||
|
remaining.append(item)
|
||||||
|
else:
|
||||||
|
deselected.append(item)
|
||||||
|
if deselected:
|
||||||
|
config.hook.pytest_deselected(items=deselected)
|
||||||
|
items[:] = remaining
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_expression(expr: str, exc_message: str) -> Expression:
|
||||||
|
try:
|
||||||
|
return Expression.compile(expr)
|
||||||
|
except ParseError as e:
|
||||||
|
raise UsageError(f"{exc_message}: {expr}: {e}") 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
|
||||||
|
|
||||||
|
empty_parameterset = config.getini(EMPTY_PARAMETERSET_OPTION)
|
||||||
|
|
||||||
|
if empty_parameterset not in ("skip", "xfail", "fail_at_collect", None, ""):
|
||||||
|
raise UsageError(
|
||||||
|
f"{EMPTY_PARAMETERSET_OPTION!s} must be one of skip, xfail or fail_at_collect"
|
||||||
|
f" but it is {empty_parameterset!r}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_unconfigure(config: Config) -> None:
|
||||||
|
MARK_GEN._config = config.stash.get(old_mark_config_key, None)
|
||||||
223
.venv/lib/python3.10/site-packages/_pytest/mark/expression.py
Normal file
223
.venv/lib/python3.10/site-packages/_pytest/mark/expression.py
Normal file
|
|
@ -0,0 +1,223 @@
|
||||||
|
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
|
||||||
|
ident: (\w|:|\+|-|\.|\[|\]|\\|/)+
|
||||||
|
|
||||||
|
The semantics are:
|
||||||
|
|
||||||
|
- Empty expression evaluates to False.
|
||||||
|
- ident evaluates to True of False according to a provided matcher function.
|
||||||
|
- or/and/not evaluate according to the usual boolean semantics.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import ast
|
||||||
|
import dataclasses
|
||||||
|
import enum
|
||||||
|
import re
|
||||||
|
import types
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Iterator
|
||||||
|
from typing import Mapping
|
||||||
|
from typing import NoReturn
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Sequence
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"Expression",
|
||||||
|
"ParseError",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class TokenType(enum.Enum):
|
||||||
|
LPAREN = "left parenthesis"
|
||||||
|
RPAREN = "right parenthesis"
|
||||||
|
OR = "or"
|
||||||
|
AND = "and"
|
||||||
|
NOT = "not"
|
||||||
|
IDENT = "identifier"
|
||||||
|
EOF = "end of input"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass(frozen=True)
|
||||||
|
class Token:
|
||||||
|
__slots__ = ("type", "value", "pos")
|
||||||
|
type: TokenType
|
||||||
|
value: str
|
||||||
|
pos: int
|
||||||
|
|
||||||
|
|
||||||
|
class ParseError(Exception):
|
||||||
|
"""The expression contains invalid syntax.
|
||||||
|
|
||||||
|
:param column: The column in the line where the error occurred (1-based).
|
||||||
|
:param message: A description of the error.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, column: int, message: str) -> None:
|
||||||
|
self.column = column
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return f"at column {self.column}: {self.message}"
|
||||||
|
|
||||||
|
|
||||||
|
class Scanner:
|
||||||
|
__slots__ = ("tokens", "current")
|
||||||
|
|
||||||
|
def __init__(self, input: str) -> None:
|
||||||
|
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
|
||||||
|
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 ParseError(
|
||||||
|
pos + 1,
|
||||||
|
f'unexpected character "{input[pos]}"',
|
||||||
|
)
|
||||||
|
yield Token(TokenType.EOF, "", pos)
|
||||||
|
|
||||||
|
def accept(self, type: TokenType, *, reject: bool = False) -> Optional[Token]:
|
||||||
|
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 ParseError(
|
||||||
|
self.current.pos + 1,
|
||||||
|
"expected {}; got {}".format(
|
||||||
|
" OR ".join(type.value for type in expected),
|
||||||
|
self.current.type.value,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# 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) -> ast.Expression:
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
return ast.Name(IDENT_PREFIX + ident.value, ast.Load())
|
||||||
|
s.reject((TokenType.NOT, TokenType.LPAREN, TokenType.IDENT))
|
||||||
|
|
||||||
|
|
||||||
|
class MatcherAdapter(Mapping[str, bool]):
|
||||||
|
"""Adapts a matcher function to a locals mapping as required by eval()."""
|
||||||
|
|
||||||
|
def __init__(self, matcher: Callable[[str], bool]) -> None:
|
||||||
|
self.matcher = matcher
|
||||||
|
|
||||||
|
def __getitem__(self, key: str) -> bool:
|
||||||
|
return self.matcher(key[len(IDENT_PREFIX) :])
|
||||||
|
|
||||||
|
def __iter__(self) -> Iterator[str]:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def __len__(self) -> int:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
class Expression:
|
||||||
|
"""A compiled match expression as used by -k and -m.
|
||||||
|
|
||||||
|
The expression can be evaluated against different matchers.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__slots__ = ("code",)
|
||||||
|
|
||||||
|
def __init__(self, code: types.CodeType) -> None:
|
||||||
|
self.code = code
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def compile(self, input: str) -> "Expression":
|
||||||
|
"""Compile a match expression.
|
||||||
|
|
||||||
|
:param input: The input expression - one line.
|
||||||
|
"""
|
||||||
|
astexpr = expression(Scanner(input))
|
||||||
|
code: types.CodeType = compile(
|
||||||
|
astexpr,
|
||||||
|
filename="<pytest match expression>",
|
||||||
|
mode="eval",
|
||||||
|
)
|
||||||
|
return Expression(code)
|
||||||
|
|
||||||
|
def evaluate(self, matcher: Callable[[str], bool]) -> bool:
|
||||||
|
"""Evaluate the match expression.
|
||||||
|
|
||||||
|
:param matcher:
|
||||||
|
Given an identifier, should return whether it matches or not.
|
||||||
|
Should be prepared to handle arbitrary strings as input.
|
||||||
|
|
||||||
|
:returns: Whether the expression matches or not.
|
||||||
|
"""
|
||||||
|
ret: bool = eval(self.code, {"__builtins__": {}}, MatcherAdapter(matcher))
|
||||||
|
return ret
|
||||||
629
.venv/lib/python3.10/site-packages/_pytest/mark/structures.py
Normal file
629
.venv/lib/python3.10/site-packages/_pytest/mark/structures.py
Normal file
|
|
@ -0,0 +1,629 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
import collections.abc
|
||||||
|
import dataclasses
|
||||||
|
import inspect
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Collection
|
||||||
|
from typing import final
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import Iterator
|
||||||
|
from typing import List
|
||||||
|
from typing import Mapping
|
||||||
|
from typing import MutableMapping
|
||||||
|
from typing import NamedTuple
|
||||||
|
from typing import Optional
|
||||||
|
from typing import overload
|
||||||
|
from typing import Sequence
|
||||||
|
from typing import Set
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import Union
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
from .._code import getfslineno
|
||||||
|
from ..compat import ascii_escaped
|
||||||
|
from ..compat import NOTSET
|
||||||
|
from ..compat import NotSetType
|
||||||
|
from _pytest.config import Config
|
||||||
|
from _pytest.deprecated import check_ispytest
|
||||||
|
from _pytest.deprecated import MARKED_FIXTURE
|
||||||
|
from _pytest.outcomes import fail
|
||||||
|
from _pytest.warning_types import PytestUnknownMarkWarning
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ..nodes import Node
|
||||||
|
|
||||||
|
|
||||||
|
EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark"
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
fs, lineno = getfslineno(func)
|
||||||
|
reason = "got empty parameter set %r, function %s at %s:%d" % (
|
||||||
|
argnames,
|
||||||
|
func.__name__,
|
||||||
|
fs,
|
||||||
|
lineno,
|
||||||
|
)
|
||||||
|
|
||||||
|
requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION)
|
||||||
|
if requested_mark in ("", None, "skip"):
|
||||||
|
mark = MARK_GEN.skip(reason=reason)
|
||||||
|
elif requested_mark == "xfail":
|
||||||
|
mark = MARK_GEN.xfail(reason=reason, run=False)
|
||||||
|
elif requested_mark == "fail_at_collect":
|
||||||
|
f_name = func.__name__
|
||||||
|
_, lineno = getfslineno(func)
|
||||||
|
raise Collector.CollectError(
|
||||||
|
"Empty parameter set in '%s' at line %d" % (f_name, lineno + 1)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise LookupError(requested_mark)
|
||||||
|
return mark
|
||||||
|
|
||||||
|
|
||||||
|
class ParameterSet(NamedTuple):
|
||||||
|
values: Sequence[Union[object, NotSetType]]
|
||||||
|
marks: Collection[Union["MarkDecorator", "Mark"]]
|
||||||
|
id: Optional[str]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def param(
|
||||||
|
cls,
|
||||||
|
*values: object,
|
||||||
|
marks: Union["MarkDecorator", Collection[Union["MarkDecorator", "Mark"]]] = (),
|
||||||
|
id: Optional[str] = None,
|
||||||
|
) -> "ParameterSet":
|
||||||
|
if isinstance(marks, MarkDecorator):
|
||||||
|
marks = (marks,)
|
||||||
|
else:
|
||||||
|
assert isinstance(marks, collections.abc.Collection)
|
||||||
|
|
||||||
|
if id is not None:
|
||||||
|
if not isinstance(id, str):
|
||||||
|
raise TypeError(f"Expected id to be a string, got {type(id)}: {id!r}")
|
||||||
|
id = ascii_escaped(id)
|
||||||
|
return cls(values, marks, id)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def extract_from(
|
||||||
|
cls,
|
||||||
|
parameterset: Union["ParameterSet", Sequence[object], object],
|
||||||
|
force_tuple: bool = False,
|
||||||
|
) -> "ParameterSet":
|
||||||
|
"""Extract from an object or objects.
|
||||||
|
|
||||||
|
: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: Union[str, Sequence[str]],
|
||||||
|
argvalues: Iterable[Union["ParameterSet", Sequence[object], object]],
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
) -> Tuple[Sequence[str], bool]:
|
||||||
|
if isinstance(argnames, str):
|
||||||
|
argnames = [x.strip() for x in argnames.split(",") if x.strip()]
|
||||||
|
force_tuple = len(argnames) == 1
|
||||||
|
else:
|
||||||
|
force_tuple = False
|
||||||
|
return argnames, force_tuple
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_parametrize_parameters(
|
||||||
|
argvalues: Iterable[Union["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: Union[str, Sequence[str]],
|
||||||
|
argvalues: Iterable[Union["ParameterSet", Sequence[object], object]],
|
||||||
|
func,
|
||||||
|
config: Config,
|
||||||
|
nodeid: str,
|
||||||
|
) -> Tuple[Sequence[str], List["ParameterSet"]]:
|
||||||
|
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:
|
||||||
|
if len(param.values) != 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=len(param.values),
|
||||||
|
),
|
||||||
|
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=None)
|
||||||
|
)
|
||||||
|
return argnames, parameters
|
||||||
|
|
||||||
|
|
||||||
|
@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: Optional["Mark"] = dataclasses.field(default=None, repr=False)
|
||||||
|
#: Resolved/generated ids with parametrize Marks.
|
||||||
|
_param_ids_generated: Optional[Sequence[str]] = dataclasses.field(
|
||||||
|
default=None, repr=False
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
args: Tuple[Any, ...],
|
||||||
|
kwargs: Mapping[str, Any],
|
||||||
|
param_ids_from: Optional["Mark"] = None,
|
||||||
|
param_ids_generated: Optional[Sequence[str]] = 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: Optional[Mark] = 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=Union[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[misc]
|
||||||
|
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)
|
||||||
|
if len(args) == 1 and (istestfunc(func) or is_class):
|
||||||
|
store_mark(func, self.mark, stacklevel=3)
|
||||||
|
return func
|
||||||
|
return self.with_args(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def get_unpacked_marks(
|
||||||
|
obj: Union[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 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[Union[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, *, stacklevel: int = 2) -> 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:
|
||||||
|
warnings.warn(MARKED_FIXTURE, stacklevel=stacklevel)
|
||||||
|
|
||||||
|
# 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:
|
||||||
|
from _pytest.scope import _ScopeName
|
||||||
|
|
||||||
|
class _SkipMarkDecorator(MarkDecorator):
|
||||||
|
@overload # type: ignore[override,misc,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: Union[str, bool] = ...,
|
||||||
|
*conditions: Union[str, bool],
|
||||||
|
reason: str = ...,
|
||||||
|
) -> MarkDecorator:
|
||||||
|
...
|
||||||
|
|
||||||
|
class _XfailMarkDecorator(MarkDecorator):
|
||||||
|
@overload # type: ignore[override,misc,no-overload-impl]
|
||||||
|
def __call__(self, arg: Markable) -> Markable:
|
||||||
|
...
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def __call__(
|
||||||
|
self,
|
||||||
|
condition: Union[str, bool] = False,
|
||||||
|
*conditions: Union[str, bool],
|
||||||
|
reason: str = ...,
|
||||||
|
run: bool = ...,
|
||||||
|
raises: Union[
|
||||||
|
None, Type[BaseException], Tuple[Type[BaseException], ...]
|
||||||
|
] = ...,
|
||||||
|
strict: bool = ...,
|
||||||
|
) -> MarkDecorator:
|
||||||
|
...
|
||||||
|
|
||||||
|
class _ParametrizeMarkDecorator(MarkDecorator):
|
||||||
|
def __call__( # type: ignore[override]
|
||||||
|
self,
|
||||||
|
argnames: Union[str, Sequence[str]],
|
||||||
|
argvalues: Iterable[Union[ParameterSet, Sequence[object], object]],
|
||||||
|
*,
|
||||||
|
indirect: Union[bool, Sequence[str]] = ...,
|
||||||
|
ids: Optional[
|
||||||
|
Union[
|
||||||
|
Iterable[Union[None, str, float, int, bool]],
|
||||||
|
Callable[[Any], Optional[object]],
|
||||||
|
]
|
||||||
|
] = ...,
|
||||||
|
scope: Optional[_ScopeName] = ...,
|
||||||
|
) -> 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: Optional[Config] = 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:
|
||||||
|
for line in self._config.getini("markers"):
|
||||||
|
# example lines: "skipif(condition): skip the given test if..."
|
||||||
|
# or "hypothesis: tests which use Hypothesis", so to get the
|
||||||
|
# marker name we split on both `:` and `(`.
|
||||||
|
marker = line.split(":")[0].split("(")[0].strip()
|
||||||
|
self._markers.add(marker)
|
||||||
|
|
||||||
|
# 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:
|
||||||
|
if self._config.option.strict_markers or self._config.option.strict:
|
||||||
|
fail(
|
||||||
|
f"{name!r} not found in `markers` configuration option",
|
||||||
|
pytrace=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 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'?")
|
||||||
|
|
||||||
|
warnings.warn(
|
||||||
|
"Unknown pytest.mark.%s - 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" % name,
|
||||||
|
PytestUnknownMarkWarning,
|
||||||
|
2,
|
||||||
|
)
|
||||||
|
|
||||||
|
return MarkDecorator(Mark(name, (), {}, _ispytest=True), _ispytest=True)
|
||||||
|
|
||||||
|
|
||||||
|
MARK_GEN = MarkGenerator(_ispytest=True)
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class NodeKeywords(MutableMapping[str, Any]):
|
||||||
|
__slots__ = ("node", "parent", "_markers")
|
||||||
|
|
||||||
|
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: Union[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}>"
|
||||||
417
.venv/lib/python3.10/site-packages/_pytest/monkeypatch.py
Normal file
417
.venv/lib/python3.10/site-packages/_pytest/monkeypatch.py
Normal file
|
|
@ -0,0 +1,417 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Monkeypatching and mocking functionality."""
|
||||||
|
from contextlib import contextmanager
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from typing import Any
|
||||||
|
from typing import final
|
||||||
|
from typing import Generator
|
||||||
|
from typing import List
|
||||||
|
from typing import Mapping
|
||||||
|
from typing import MutableMapping
|
||||||
|
from typing import Optional
|
||||||
|
from typing import overload
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import Union
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
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", None, None]:
|
||||||
|
"""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 = __import__(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:
|
||||||
|
__import__(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
|
||||||
|
|
||||||
|
|
||||||
|
class Notset:
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return "<notset>"
|
||||||
|
|
||||||
|
|
||||||
|
notset = Notset()
|
||||||
|
|
||||||
|
|
||||||
|
@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: Optional[str] = None
|
||||||
|
self._savesyspath: Optional[List[str]] = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
@contextmanager
|
||||||
|
def context(cls) -> Generator["MonkeyPatch", None, None]:
|
||||||
|
"""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: Notset = ...,
|
||||||
|
raising: bool = ...,
|
||||||
|
) -> None:
|
||||||
|
...
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def setattr(
|
||||||
|
self,
|
||||||
|
target: object,
|
||||||
|
name: str,
|
||||||
|
value: object,
|
||||||
|
raising: bool = ...,
|
||||||
|
) -> None:
|
||||||
|
...
|
||||||
|
|
||||||
|
def setattr(
|
||||||
|
self,
|
||||||
|
target: Union[str, object],
|
||||||
|
name: Union[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 isinstance(value, 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)
|
||||||
|
self._setattr.append((target, name, oldval))
|
||||||
|
setattr(target, name, value)
|
||||||
|
|
||||||
|
def delattr(
|
||||||
|
self,
|
||||||
|
target: Union[object, str],
|
||||||
|
name: Union[str, Notset] = 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 isinstance(name, 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)
|
||||||
|
self._setattr.append((target, name, oldval))
|
||||||
|
delattr(target, name)
|
||||||
|
|
||||||
|
def setitem(self, dic: Mapping[K, V], name: K, value: V) -> None:
|
||||||
|
"""Set dictionary entry ``name`` to value."""
|
||||||
|
self._setitem.append((dic, name, dic.get(name, notset)))
|
||||||
|
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
|
||||||
|
dic[name] = value # type: ignore[index]
|
||||||
|
|
||||||
|
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:
|
||||||
|
self._setitem.append((dic, name, dic.get(name, notset)))
|
||||||
|
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
|
||||||
|
del dic[name] # type: ignore[attr-defined]
|
||||||
|
|
||||||
|
def setenv(self, name: str, value: str, prepend: Optional[str] = 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:
|
||||||
|
from pkg_resources import fixup_namespace_packages
|
||||||
|
|
||||||
|
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: Union[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] # type: ignore[attr-defined]
|
||||||
|
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 # type: ignore[index]
|
||||||
|
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
|
||||||
773
.venv/lib/python3.10/site-packages/_pytest/nodes.py
Normal file
773
.venv/lib/python3.10/site-packages/_pytest/nodes.py
Normal file
|
|
@ -0,0 +1,773 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
import abc
|
||||||
|
from functools import cached_property
|
||||||
|
from inspect import signature
|
||||||
|
import os
|
||||||
|
import pathlib
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import cast
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import Iterator
|
||||||
|
from typing import List
|
||||||
|
from typing import MutableMapping
|
||||||
|
from typing import NoReturn
|
||||||
|
from typing import Optional
|
||||||
|
from typing import overload
|
||||||
|
from typing import Set
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import Union
|
||||||
|
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.compat import LEGACY_PATH
|
||||||
|
from _pytest.config import Config
|
||||||
|
from _pytest.config import ConftestImportFailure
|
||||||
|
from _pytest.config.compat import _check_path
|
||||||
|
from _pytest.deprecated import NODE_CTOR_FSPATH_ARG
|
||||||
|
from _pytest.mark.structures import Mark
|
||||||
|
from _pytest.mark.structures import MarkDecorator
|
||||||
|
from _pytest.mark.structures import NodeKeywords
|
||||||
|
from _pytest.outcomes import fail
|
||||||
|
from _pytest.pathlib import absolutepath
|
||||||
|
from _pytest.pathlib import commonpath
|
||||||
|
from _pytest.stash import Stash
|
||||||
|
from _pytest.warning_types import PytestWarning
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from typing import Self
|
||||||
|
|
||||||
|
# Imported here due to circular import.
|
||||||
|
from _pytest._code.code import _TracebackStyle
|
||||||
|
from _pytest.main import Session
|
||||||
|
|
||||||
|
|
||||||
|
SEP = "/"
|
||||||
|
|
||||||
|
tracebackcutdir = Path(_pytest.__file__).parent
|
||||||
|
|
||||||
|
|
||||||
|
_T = TypeVar("_T")
|
||||||
|
|
||||||
|
|
||||||
|
def _imply_path(
|
||||||
|
node_type: Type["Node"],
|
||||||
|
path: Optional[Path],
|
||||||
|
fspath: Optional[LEGACY_PATH],
|
||||||
|
) -> Path:
|
||||||
|
if fspath is not None:
|
||||||
|
warnings.warn(
|
||||||
|
NODE_CTOR_FSPATH_ARG.format(
|
||||||
|
node_type_name=node_type.__name__,
|
||||||
|
),
|
||||||
|
stacklevel=6,
|
||||||
|
)
|
||||||
|
if path is not None:
|
||||||
|
if fspath is not None:
|
||||||
|
_check_path(path, fspath)
|
||||||
|
return path
|
||||||
|
else:
|
||||||
|
assert fspath is not None
|
||||||
|
return Path(fspath)
|
||||||
|
|
||||||
|
|
||||||
|
_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__ = (
|
||||||
|
"name",
|
||||||
|
"parent",
|
||||||
|
"config",
|
||||||
|
"session",
|
||||||
|
"path",
|
||||||
|
"_nodeid",
|
||||||
|
"_store",
|
||||||
|
"__dict__",
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
parent: "Optional[Node]" = None,
|
||||||
|
config: Optional[Config] = None,
|
||||||
|
session: "Optional[Session]" = None,
|
||||||
|
fspath: Optional[LEGACY_PATH] = None,
|
||||||
|
path: Optional[Path] = None,
|
||||||
|
nodeid: Optional[str] = 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 and fspath is None:
|
||||||
|
path = getattr(parent, "path", None)
|
||||||
|
#: Filesystem path where this node was collected from (can be None).
|
||||||
|
self.path: pathlib.Path = _imply_path(type(self), path, fspath=fspath)
|
||||||
|
|
||||||
|
# The explicit annotation is to avoid publicly exposing NodeKeywords.
|
||||||
|
#: Keywords/markers collected from all scopes.
|
||||||
|
self.keywords: MutableMapping[str, Any] = NodeKeywords(self)
|
||||||
|
|
||||||
|
#: The marker objects belonging to this node.
|
||||||
|
self.own_markers: List[Mark] = []
|
||||||
|
|
||||||
|
#: Allow adding of extra keywords to use for matching.
|
||||||
|
self.extra_keyword_matches: Set[str] = set()
|
||||||
|
|
||||||
|
if nodeid is not None:
|
||||||
|
assert "::()" not in nodeid
|
||||||
|
self._nodeid = nodeid
|
||||||
|
else:
|
||||||
|
if not self.parent:
|
||||||
|
raise TypeError("nodeid or parent must be provided")
|
||||||
|
self._nodeid = self.parent.nodeid + "::" + 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: "Node", **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:
|
||||||
|
"""fspath-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 self._nodeid
|
||||||
|
|
||||||
|
def __hash__(self) -> int:
|
||||||
|
return hash(self._nodeid)
|
||||||
|
|
||||||
|
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: Optional[Node] = 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: Optional[Node] = self
|
||||||
|
while item is not None:
|
||||||
|
chain.append(item)
|
||||||
|
item = item.parent
|
||||||
|
chain.reverse()
|
||||||
|
return chain
|
||||||
|
|
||||||
|
def add_marker(
|
||||||
|
self, marker: Union[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_
|
||||||
|
if append:
|
||||||
|
self.own_markers.append(marker_.mark)
|
||||||
|
else:
|
||||||
|
self.own_markers.insert(0, marker_.mark)
|
||||||
|
|
||||||
|
def iter_markers(self, name: Optional[str] = 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: Optional[str] = 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) -> Optional[Mark]:
|
||||||
|
...
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def get_closest_marker(self, name: str, default: Mark) -> Mark:
|
||||||
|
...
|
||||||
|
|
||||||
|
def get_closest_marker(
|
||||||
|
self, name: str, default: Optional[Mark] = None
|
||||||
|
) -> Optional[Mark]:
|
||||||
|
"""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.
|
||||||
|
:param name: Name to filter by.
|
||||||
|
"""
|
||||||
|
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]) -> Optional[_NodeType]:
|
||||||
|
"""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: "Optional[_TracebackStyle]" = 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: Union[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.getoption("verbose", 0) > 1:
|
||||||
|
truncate_locals = False
|
||||||
|
else:
|
||||||
|
truncate_locals = 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,
|
||||||
|
)
|
||||||
|
|
||||||
|
def repr_failure(
|
||||||
|
self,
|
||||||
|
excinfo: ExceptionInfo[BaseException],
|
||||||
|
style: "Optional[_TracebackStyle]" = None,
|
||||||
|
) -> Union[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[Union[str, Path], Optional[int]]:
|
||||||
|
"""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: Optional[Tuple[str, Optional[int], str]] = 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[Union["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]
|
||||||
|
) -> Union[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
|
||||||
|
|
||||||
|
|
||||||
|
def _check_initialpaths_for_relpath(session: "Session", path: Path) -> Optional[str]:
|
||||||
|
for initial_path in session._initialpaths:
|
||||||
|
if commonpath(path, initial_path) == initial_path:
|
||||||
|
rel = str(path.relative_to(initial_path))
|
||||||
|
return "" if rel == "." else rel
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class FSCollector(Collector, abc.ABC):
|
||||||
|
"""Base class for filesystem collectors."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
fspath: Optional[LEGACY_PATH] = None,
|
||||||
|
path_or_parent: Optional[Union[Path, Node]] = None,
|
||||||
|
path: Optional[Path] = None,
|
||||||
|
name: Optional[str] = None,
|
||||||
|
parent: Optional[Node] = None,
|
||||||
|
config: Optional[Config] = None,
|
||||||
|
session: Optional["Session"] = None,
|
||||||
|
nodeid: Optional[str] = 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
|
||||||
|
|
||||||
|
path = _imply_path(type(self), path, fspath=fspath)
|
||||||
|
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 = name.replace(os.sep, SEP)
|
||||||
|
self.path = path
|
||||||
|
|
||||||
|
if session is None:
|
||||||
|
assert parent is not None
|
||||||
|
session = parent.session
|
||||||
|
|
||||||
|
if nodeid is None:
|
||||||
|
try:
|
||||||
|
nodeid = str(self.path.relative_to(session.config.rootpath))
|
||||||
|
except ValueError:
|
||||||
|
nodeid = _check_initialpaths_for_relpath(session, path)
|
||||||
|
|
||||||
|
if nodeid and os.sep != SEP:
|
||||||
|
nodeid = nodeid.replace(os.sep, SEP)
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
name=name,
|
||||||
|
parent=parent,
|
||||||
|
config=config,
|
||||||
|
session=session,
|
||||||
|
nodeid=nodeid,
|
||||||
|
path=path,
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_parent(
|
||||||
|
cls,
|
||||||
|
parent,
|
||||||
|
*,
|
||||||
|
fspath: Optional[LEGACY_PATH] = None,
|
||||||
|
path: Optional[Path] = 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: Optional[Config] = None,
|
||||||
|
session: Optional["Session"] = None,
|
||||||
|
nodeid: Optional[str] = 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[Union["os.PathLike[str]", str], Optional[int], 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, Optional[int], 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(os.fspath(location[0]))
|
||||||
|
relfspath = self.session._node_location_to_relpath(path)
|
||||||
|
assert type(location[2]) is str
|
||||||
|
return (relfspath, location[1], location[2])
|
||||||
245
.venv/lib/python3.10/site-packages/_pytest/outcomes.py
Normal file
245
.venv/lib/python3.10/site-packages/_pytest/outcomes.py
Normal file
|
|
@ -0,0 +1,245 @@
|
||||||
|
"""Exception classes and constants handling test outcomes as well as
|
||||||
|
functions creating them."""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import cast
|
||||||
|
from typing import NoReturn
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Protocol
|
||||||
|
from typing import Type
|
||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
|
|
||||||
|
class OutcomeException(BaseException):
|
||||||
|
"""OutcomeException and its subclass instances indicate and contain info
|
||||||
|
about test and collection outcomes."""
|
||||||
|
|
||||||
|
def __init__(self, msg: Optional[str] = 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: Optional[str] = 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: Optional[int] = None
|
||||||
|
) -> None:
|
||||||
|
self.msg = msg
|
||||||
|
self.returncode = returncode
|
||||||
|
super().__init__(msg)
|
||||||
|
|
||||||
|
|
||||||
|
# Elaborate hack to work around https://github.com/python/mypy/issues/2087.
|
||||||
|
# Ideally would just be `exit.Exception = Exit` etc.
|
||||||
|
|
||||||
|
_F = TypeVar("_F", bound=Callable[..., object])
|
||||||
|
_ET = TypeVar("_ET", bound=Type[BaseException])
|
||||||
|
|
||||||
|
|
||||||
|
class _WithException(Protocol[_F, _ET]):
|
||||||
|
Exception: _ET
|
||||||
|
__call__: _F
|
||||||
|
|
||||||
|
|
||||||
|
def _with_exception(exception_type: _ET) -> Callable[[_F], _WithException[_F, _ET]]:
|
||||||
|
def decorate(func: _F) -> _WithException[_F, _ET]:
|
||||||
|
func_with_exception = cast(_WithException[_F, _ET], func)
|
||||||
|
func_with_exception.Exception = exception_type
|
||||||
|
return func_with_exception
|
||||||
|
|
||||||
|
return decorate
|
||||||
|
|
||||||
|
|
||||||
|
# Exposed helper methods.
|
||||||
|
|
||||||
|
|
||||||
|
@_with_exception(Exit)
|
||||||
|
def exit(
|
||||||
|
reason: str = "",
|
||||||
|
returncode: Optional[int] = None,
|
||||||
|
) -> NoReturn:
|
||||||
|
"""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`.
|
||||||
|
"""
|
||||||
|
__tracebackhide__ = True
|
||||||
|
raise Exit(reason, returncode)
|
||||||
|
|
||||||
|
|
||||||
|
@_with_exception(Skipped)
|
||||||
|
def skip(
|
||||||
|
reason: str = "",
|
||||||
|
*,
|
||||||
|
allow_module_level: bool = False,
|
||||||
|
) -> NoReturn:
|
||||||
|
"""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.
|
||||||
|
|
||||||
|
.. 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.
|
||||||
|
"""
|
||||||
|
__tracebackhide__ = True
|
||||||
|
raise Skipped(msg=reason, allow_module_level=allow_module_level)
|
||||||
|
|
||||||
|
|
||||||
|
@_with_exception(Failed)
|
||||||
|
def fail(reason: str = "", pytrace: bool = True) -> NoReturn:
|
||||||
|
"""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.
|
||||||
|
"""
|
||||||
|
__tracebackhide__ = True
|
||||||
|
raise Failed(msg=reason, pytrace=pytrace)
|
||||||
|
|
||||||
|
|
||||||
|
class XFailed(Failed):
|
||||||
|
"""Raised from an explicit call to pytest.xfail()."""
|
||||||
|
|
||||||
|
|
||||||
|
@_with_exception(XFailed)
|
||||||
|
def xfail(reason: str = "") -> NoReturn:
|
||||||
|
"""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.
|
||||||
|
"""
|
||||||
|
__tracebackhide__ = True
|
||||||
|
raise XFailed(reason)
|
||||||
|
|
||||||
|
|
||||||
|
def importorskip(
|
||||||
|
modname: str, minversion: Optional[str] = None, reason: Optional[str] = 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.
|
||||||
|
|
||||||
|
:returns:
|
||||||
|
The imported module. This should be assigned to its canonical name.
|
||||||
|
|
||||||
|
Example::
|
||||||
|
|
||||||
|
docutils = pytest.importorskip("docutils")
|
||||||
|
"""
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
__tracebackhide__ = True
|
||||||
|
compile(modname, "", "eval") # to catch syntaxerrors
|
||||||
|
|
||||||
|
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:
|
||||||
|
__import__(modname)
|
||||||
|
except ImportError as exc:
|
||||||
|
if reason is None:
|
||||||
|
reason = f"could not import {modname!r}: {exc}"
|
||||||
|
raise Skipped(reason, allow_module_level=True) from None
|
||||||
|
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
|
||||||
111
.venv/lib/python3.10/site-packages/_pytest/pastebin.py
Normal file
111
.venv/lib/python3.10/site-packages/_pytest/pastebin.py
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Submit failure or test session information to a pastebin service."""
|
||||||
|
from io import StringIO
|
||||||
|
import tempfile
|
||||||
|
from typing import IO
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from _pytest.config import Config
|
||||||
|
from _pytest.config import create_terminal_writer
|
||||||
|
from _pytest.config.argparsing import Parser
|
||||||
|
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 == "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("pastebin session-log: %s\n" % pastebinurl)
|
||||||
|
|
||||||
|
|
||||||
|
def create_new_paste(contents: Union[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.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 OSError as exc_info: # urllib errors
|
||||||
|
return "bad response: %s" % exc_info
|
||||||
|
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}")
|
||||||
900
.venv/lib/python3.10/site-packages/_pytest/pathlib.py
Normal file
900
.venv/lib/python3.10/site-packages/_pytest/pathlib.py
Normal file
|
|
@ -0,0 +1,900 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
import atexit
|
||||||
|
import contextlib
|
||||||
|
from enum import Enum
|
||||||
|
from errno import EBADF
|
||||||
|
from errno import ELOOP
|
||||||
|
from errno import ENOENT
|
||||||
|
from errno import ENOTDIR
|
||||||
|
import fnmatch
|
||||||
|
from functools import partial
|
||||||
|
import importlib.util
|
||||||
|
import itertools
|
||||||
|
import os
|
||||||
|
from os.path import expanduser
|
||||||
|
from os.path import expandvars
|
||||||
|
from os.path import isabs
|
||||||
|
from os.path import sep
|
||||||
|
from pathlib import Path
|
||||||
|
from pathlib import PurePath
|
||||||
|
from posixpath import sep as posix_sep
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import types
|
||||||
|
from types import ModuleType
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Dict
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import Iterator
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Set
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import Union
|
||||||
|
import uuid
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
from _pytest.compat import assert_never
|
||||||
|
from _pytest.outcomes import skip
|
||||||
|
from _pytest.warning_types import PytestWarning
|
||||||
|
|
||||||
|
|
||||||
|
LOCK_TIMEOUT = 60 * 60 * 24 * 3
|
||||||
|
|
||||||
|
|
||||||
|
_AnyPurePath = TypeVar("_AnyPurePath", bound=PurePath)
|
||||||
|
|
||||||
|
# The following function, variables and comments were
|
||||||
|
# copied from cpython 3.9 Lib/pathlib.py file.
|
||||||
|
|
||||||
|
# EBADF - guard against macOS `stat` throwing EBADF
|
||||||
|
_IGNORED_ERRORS = (ENOENT, ENOTDIR, EBADF, ELOOP)
|
||||||
|
|
||||||
|
_IGNORED_WINERRORS = (
|
||||||
|
21, # ERROR_NOT_READY - drive exists but is not accessible
|
||||||
|
1921, # ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _ignore_error(exception):
|
||||||
|
return (
|
||||||
|
getattr(exception, "errno", None) in _IGNORED_ERRORS
|
||||||
|
or getattr(exception, "winerror", None) in _IGNORED_WINERRORS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_lock_path(path: _AnyPurePath) -> _AnyPurePath:
|
||||||
|
return path.joinpath(".lock")
|
||||||
|
|
||||||
|
|
||||||
|
def on_rm_rf_error(
|
||||||
|
func,
|
||||||
|
path: str,
|
||||||
|
excinfo: Union[
|
||||||
|
BaseException,
|
||||||
|
Tuple[Type[BaseException], BaseException, Optional[types.TracebackType]],
|
||||||
|
],
|
||||||
|
*,
|
||||||
|
start_path: Path,
|
||||||
|
) -> bool:
|
||||||
|
"""Handle known read-only errors during rmtree.
|
||||||
|
|
||||||
|
The returned value is used only by our own tests.
|
||||||
|
"""
|
||||||
|
if isinstance(excinfo, BaseException):
|
||||||
|
exc = excinfo
|
||||||
|
else:
|
||||||
|
exc = excinfo[1]
|
||||||
|
|
||||||
|
# Another process removed the file in the middle of the "rm_rf" (xdist for example).
|
||||||
|
# More context: https://github.com/pytest-dev/pytest/issues/5974#issuecomment-543799018
|
||||||
|
if isinstance(exc, FileNotFoundError):
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not isinstance(exc, PermissionError):
|
||||||
|
warnings.warn(
|
||||||
|
PytestWarning(f"(rm_rf) error removing {path}\n{type(exc)}: {exc}")
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
if func not in (os.rmdir, os.remove, os.unlink):
|
||||||
|
if func not in (os.open,):
|
||||||
|
warnings.warn(
|
||||||
|
PytestWarning(
|
||||||
|
f"(rm_rf) unknown function {func} when removing {path}:\n{type(exc)}: {exc}"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Chmod + retry.
|
||||||
|
import stat
|
||||||
|
|
||||||
|
def chmod_rw(p: str) -> None:
|
||||||
|
mode = os.stat(p).st_mode
|
||||||
|
os.chmod(p, mode | stat.S_IRUSR | stat.S_IWUSR)
|
||||||
|
|
||||||
|
# For files, we need to recursively go upwards in the directories to
|
||||||
|
# ensure they all are also writable.
|
||||||
|
p = Path(path)
|
||||||
|
if p.is_file():
|
||||||
|
for parent in p.parents:
|
||||||
|
chmod_rw(str(parent))
|
||||||
|
# Stop when we reach the original path passed to rm_rf.
|
||||||
|
if parent == start_path:
|
||||||
|
break
|
||||||
|
chmod_rw(str(path))
|
||||||
|
|
||||||
|
func(path)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_extended_length_path(path: Path) -> Path:
|
||||||
|
"""Get the extended-length version of a path (Windows).
|
||||||
|
|
||||||
|
On Windows, by default, the maximum length of a path (MAX_PATH) is 260
|
||||||
|
characters, and operations on paths longer than that fail. But it is possible
|
||||||
|
to overcome this by converting the path to "extended-length" form before
|
||||||
|
performing the operation:
|
||||||
|
https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
|
||||||
|
|
||||||
|
On Windows, this function returns the extended-length absolute version of path.
|
||||||
|
On other platforms it returns path unchanged.
|
||||||
|
"""
|
||||||
|
if sys.platform.startswith("win32"):
|
||||||
|
path = path.resolve()
|
||||||
|
path = Path(get_extended_length_path_str(str(path)))
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def get_extended_length_path_str(path: str) -> str:
|
||||||
|
"""Convert a path to a Windows extended length path."""
|
||||||
|
long_path_prefix = "\\\\?\\"
|
||||||
|
unc_long_path_prefix = "\\\\?\\UNC\\"
|
||||||
|
if path.startswith((long_path_prefix, unc_long_path_prefix)):
|
||||||
|
return path
|
||||||
|
# UNC
|
||||||
|
if path.startswith("\\\\"):
|
||||||
|
return unc_long_path_prefix + path[2:]
|
||||||
|
return long_path_prefix + path
|
||||||
|
|
||||||
|
|
||||||
|
def rm_rf(path: Path) -> None:
|
||||||
|
"""Remove the path contents recursively, even if some elements
|
||||||
|
are read-only."""
|
||||||
|
path = ensure_extended_length_path(path)
|
||||||
|
onerror = partial(on_rm_rf_error, start_path=path)
|
||||||
|
if sys.version_info >= (3, 12):
|
||||||
|
shutil.rmtree(str(path), onexc=onerror)
|
||||||
|
else:
|
||||||
|
shutil.rmtree(str(path), onerror=onerror)
|
||||||
|
|
||||||
|
|
||||||
|
def find_prefixed(root: Path, prefix: str) -> Iterator["os.DirEntry[str]"]:
|
||||||
|
"""Find all elements in root that begin with the prefix, case insensitive."""
|
||||||
|
l_prefix = prefix.lower()
|
||||||
|
for x in os.scandir(root):
|
||||||
|
if x.name.lower().startswith(l_prefix):
|
||||||
|
yield x
|
||||||
|
|
||||||
|
|
||||||
|
def extract_suffixes(iter: Iterable["os.DirEntry[str]"], prefix: str) -> Iterator[str]:
|
||||||
|
"""Return the parts of the paths following the prefix.
|
||||||
|
|
||||||
|
:param iter: Iterator over path names.
|
||||||
|
:param prefix: Expected prefix of the path names.
|
||||||
|
"""
|
||||||
|
p_len = len(prefix)
|
||||||
|
for entry in iter:
|
||||||
|
yield entry.name[p_len:]
|
||||||
|
|
||||||
|
|
||||||
|
def find_suffixes(root: Path, prefix: str) -> Iterator[str]:
|
||||||
|
"""Combine find_prefixes and extract_suffixes."""
|
||||||
|
return extract_suffixes(find_prefixed(root, prefix), prefix)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_num(maybe_num) -> int:
|
||||||
|
"""Parse number path suffixes, returns -1 on error."""
|
||||||
|
try:
|
||||||
|
return int(maybe_num)
|
||||||
|
except ValueError:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
def _force_symlink(
|
||||||
|
root: Path, target: Union[str, PurePath], link_to: Union[str, Path]
|
||||||
|
) -> None:
|
||||||
|
"""Helper to create the current symlink.
|
||||||
|
|
||||||
|
It's full of race conditions that are reasonably OK to ignore
|
||||||
|
for the context of best effort linking to the latest test run.
|
||||||
|
|
||||||
|
The presumption being that in case of much parallelism
|
||||||
|
the inaccuracy is going to be acceptable.
|
||||||
|
"""
|
||||||
|
current_symlink = root.joinpath(target)
|
||||||
|
try:
|
||||||
|
current_symlink.unlink()
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
current_symlink.symlink_to(link_to)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def make_numbered_dir(root: Path, prefix: str, mode: int = 0o700) -> Path:
|
||||||
|
"""Create a directory with an increased number as suffix for the given prefix."""
|
||||||
|
for i in range(10):
|
||||||
|
# try up to 10 times to create the folder
|
||||||
|
max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1)
|
||||||
|
new_number = max_existing + 1
|
||||||
|
new_path = root.joinpath(f"{prefix}{new_number}")
|
||||||
|
try:
|
||||||
|
new_path.mkdir(mode=mode)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
_force_symlink(root, prefix + "current", new_path)
|
||||||
|
return new_path
|
||||||
|
else:
|
||||||
|
raise OSError(
|
||||||
|
"could not create numbered dir with prefix "
|
||||||
|
f"{prefix} in {root} after 10 tries"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def create_cleanup_lock(p: Path) -> Path:
|
||||||
|
"""Create a lock to prevent premature folder cleanup."""
|
||||||
|
lock_path = get_lock_path(p)
|
||||||
|
try:
|
||||||
|
fd = os.open(str(lock_path), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644)
|
||||||
|
except FileExistsError as e:
|
||||||
|
raise OSError(f"cannot create lockfile in {p}") from e
|
||||||
|
else:
|
||||||
|
pid = os.getpid()
|
||||||
|
spid = str(pid).encode()
|
||||||
|
os.write(fd, spid)
|
||||||
|
os.close(fd)
|
||||||
|
if not lock_path.is_file():
|
||||||
|
raise OSError("lock path got renamed after successful creation")
|
||||||
|
return lock_path
|
||||||
|
|
||||||
|
|
||||||
|
def register_cleanup_lock_removal(lock_path: Path, register=atexit.register):
|
||||||
|
"""Register a cleanup function for removing a lock, by default on atexit."""
|
||||||
|
pid = os.getpid()
|
||||||
|
|
||||||
|
def cleanup_on_exit(lock_path: Path = lock_path, original_pid: int = pid) -> None:
|
||||||
|
current_pid = os.getpid()
|
||||||
|
if current_pid != original_pid:
|
||||||
|
# fork
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
lock_path.unlink()
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return register(cleanup_on_exit)
|
||||||
|
|
||||||
|
|
||||||
|
def maybe_delete_a_numbered_dir(path: Path) -> None:
|
||||||
|
"""Remove a numbered directory if its lock can be obtained and it does
|
||||||
|
not seem to be in use."""
|
||||||
|
path = ensure_extended_length_path(path)
|
||||||
|
lock_path = None
|
||||||
|
try:
|
||||||
|
lock_path = create_cleanup_lock(path)
|
||||||
|
parent = path.parent
|
||||||
|
|
||||||
|
garbage = parent.joinpath(f"garbage-{uuid.uuid4()}")
|
||||||
|
path.rename(garbage)
|
||||||
|
rm_rf(garbage)
|
||||||
|
except OSError:
|
||||||
|
# known races:
|
||||||
|
# * other process did a cleanup at the same time
|
||||||
|
# * deletable folder was found
|
||||||
|
# * process cwd (Windows)
|
||||||
|
return
|
||||||
|
finally:
|
||||||
|
# If we created the lock, ensure we remove it even if we failed
|
||||||
|
# to properly remove the numbered dir.
|
||||||
|
if lock_path is not None:
|
||||||
|
try:
|
||||||
|
lock_path.unlink()
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_deletable(path: Path, consider_lock_dead_if_created_before: float) -> bool:
|
||||||
|
"""Check if `path` is deletable based on whether the lock file is expired."""
|
||||||
|
if path.is_symlink():
|
||||||
|
return False
|
||||||
|
lock = get_lock_path(path)
|
||||||
|
try:
|
||||||
|
if not lock.is_file():
|
||||||
|
return True
|
||||||
|
except OSError:
|
||||||
|
# we might not have access to the lock file at all, in this case assume
|
||||||
|
# we don't have access to the entire directory (#7491).
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
lock_time = lock.stat().st_mtime
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
if lock_time < consider_lock_dead_if_created_before:
|
||||||
|
# We want to ignore any errors while trying to remove the lock such as:
|
||||||
|
# - PermissionDenied, like the file permissions have changed since the lock creation;
|
||||||
|
# - FileNotFoundError, in case another pytest process got here first;
|
||||||
|
# and any other cause of failure.
|
||||||
|
with contextlib.suppress(OSError):
|
||||||
|
lock.unlink()
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def try_cleanup(path: Path, consider_lock_dead_if_created_before: float) -> None:
|
||||||
|
"""Try to cleanup a folder if we can ensure it's deletable."""
|
||||||
|
if ensure_deletable(path, consider_lock_dead_if_created_before):
|
||||||
|
maybe_delete_a_numbered_dir(path)
|
||||||
|
|
||||||
|
|
||||||
|
def cleanup_candidates(root: Path, prefix: str, keep: int) -> Iterator[Path]:
|
||||||
|
"""List candidates for numbered directories to be removed - follows py.path."""
|
||||||
|
max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1)
|
||||||
|
max_delete = max_existing - keep
|
||||||
|
entries = find_prefixed(root, prefix)
|
||||||
|
entries, entries2 = itertools.tee(entries)
|
||||||
|
numbers = map(parse_num, extract_suffixes(entries2, prefix))
|
||||||
|
for entry, number in zip(entries, numbers):
|
||||||
|
if number <= max_delete:
|
||||||
|
yield Path(entry)
|
||||||
|
|
||||||
|
|
||||||
|
def cleanup_dead_symlinks(root: Path):
|
||||||
|
for left_dir in root.iterdir():
|
||||||
|
if left_dir.is_symlink():
|
||||||
|
if not left_dir.resolve().exists():
|
||||||
|
left_dir.unlink()
|
||||||
|
|
||||||
|
|
||||||
|
def cleanup_numbered_dir(
|
||||||
|
root: Path, prefix: str, keep: int, consider_lock_dead_if_created_before: float
|
||||||
|
) -> None:
|
||||||
|
"""Cleanup for lock driven numbered directories."""
|
||||||
|
if not root.exists():
|
||||||
|
return
|
||||||
|
for path in cleanup_candidates(root, prefix, keep):
|
||||||
|
try_cleanup(path, consider_lock_dead_if_created_before)
|
||||||
|
for path in root.glob("garbage-*"):
|
||||||
|
try_cleanup(path, consider_lock_dead_if_created_before)
|
||||||
|
|
||||||
|
cleanup_dead_symlinks(root)
|
||||||
|
|
||||||
|
|
||||||
|
def make_numbered_dir_with_cleanup(
|
||||||
|
root: Path,
|
||||||
|
prefix: str,
|
||||||
|
keep: int,
|
||||||
|
lock_timeout: float,
|
||||||
|
mode: int,
|
||||||
|
) -> Path:
|
||||||
|
"""Create a numbered dir with a cleanup lock and remove old ones."""
|
||||||
|
e = None
|
||||||
|
for i in range(10):
|
||||||
|
try:
|
||||||
|
p = make_numbered_dir(root, prefix, mode)
|
||||||
|
# Only lock the current dir when keep is not 0
|
||||||
|
if keep != 0:
|
||||||
|
lock_path = create_cleanup_lock(p)
|
||||||
|
register_cleanup_lock_removal(lock_path)
|
||||||
|
except Exception as exc:
|
||||||
|
e = exc
|
||||||
|
else:
|
||||||
|
consider_lock_dead_if_created_before = p.stat().st_mtime - lock_timeout
|
||||||
|
# Register a cleanup for program exit
|
||||||
|
atexit.register(
|
||||||
|
cleanup_numbered_dir,
|
||||||
|
root,
|
||||||
|
prefix,
|
||||||
|
keep,
|
||||||
|
consider_lock_dead_if_created_before,
|
||||||
|
)
|
||||||
|
return p
|
||||||
|
assert e is not None
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_from_str(input: str, rootpath: Path) -> Path:
|
||||||
|
input = expanduser(input)
|
||||||
|
input = expandvars(input)
|
||||||
|
if isabs(input):
|
||||||
|
return Path(input)
|
||||||
|
else:
|
||||||
|
return rootpath.joinpath(input)
|
||||||
|
|
||||||
|
|
||||||
|
def fnmatch_ex(pattern: str, path: Union[str, "os.PathLike[str]"]) -> bool:
|
||||||
|
"""A port of FNMatcher from py.path.common which works with PurePath() instances.
|
||||||
|
|
||||||
|
The difference between this algorithm and PurePath.match() is that the
|
||||||
|
latter matches "**" glob expressions for each part of the path, while
|
||||||
|
this algorithm uses the whole path instead.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
"tests/foo/bar/doc/test_foo.py" matches pattern "tests/**/doc/test*.py"
|
||||||
|
with this algorithm, but not with PurePath.match().
|
||||||
|
|
||||||
|
This algorithm was ported to keep backward-compatibility with existing
|
||||||
|
settings which assume paths match according this logic.
|
||||||
|
|
||||||
|
References:
|
||||||
|
* https://bugs.python.org/issue29249
|
||||||
|
* https://bugs.python.org/issue34731
|
||||||
|
"""
|
||||||
|
path = PurePath(path)
|
||||||
|
iswin32 = sys.platform.startswith("win")
|
||||||
|
|
||||||
|
if iswin32 and sep not in pattern and posix_sep in pattern:
|
||||||
|
# Running on Windows, the pattern has no Windows path separators,
|
||||||
|
# and the pattern has one or more Posix path separators. Replace
|
||||||
|
# the Posix path separators with the Windows path separator.
|
||||||
|
pattern = pattern.replace(posix_sep, sep)
|
||||||
|
|
||||||
|
if sep not in pattern:
|
||||||
|
name = path.name
|
||||||
|
else:
|
||||||
|
name = str(path)
|
||||||
|
if path.is_absolute() and not os.path.isabs(pattern):
|
||||||
|
pattern = f"*{os.sep}{pattern}"
|
||||||
|
return fnmatch.fnmatch(name, pattern)
|
||||||
|
|
||||||
|
|
||||||
|
def parts(s: str) -> Set[str]:
|
||||||
|
parts = s.split(sep)
|
||||||
|
return {sep.join(parts[: i + 1]) or sep for i in range(len(parts))}
|
||||||
|
|
||||||
|
|
||||||
|
def symlink_or_skip(src, dst, **kwargs):
|
||||||
|
"""Make a symlink, or skip the test in case symlinks are not supported."""
|
||||||
|
try:
|
||||||
|
os.symlink(str(src), str(dst), **kwargs)
|
||||||
|
except OSError as e:
|
||||||
|
skip(f"symlinks not supported: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
class ImportMode(Enum):
|
||||||
|
"""Possible values for `mode` parameter of `import_path`."""
|
||||||
|
|
||||||
|
prepend = "prepend"
|
||||||
|
append = "append"
|
||||||
|
importlib = "importlib"
|
||||||
|
|
||||||
|
|
||||||
|
class ImportPathMismatchError(ImportError):
|
||||||
|
"""Raised on import_path() if there is a mismatch of __file__'s.
|
||||||
|
|
||||||
|
This can happen when `import_path` is called multiple times with different filenames that has
|
||||||
|
the same basename but reside in packages
|
||||||
|
(for example "/tests1/test_foo.py" and "/tests2/test_foo.py").
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def import_path(
|
||||||
|
path: Union[str, "os.PathLike[str]"],
|
||||||
|
*,
|
||||||
|
mode: Union[str, ImportMode] = ImportMode.prepend,
|
||||||
|
root: Path,
|
||||||
|
consider_namespace_packages: bool,
|
||||||
|
) -> ModuleType:
|
||||||
|
"""
|
||||||
|
Import and return a module from the given path, which can be a file (a module) or
|
||||||
|
a directory (a package).
|
||||||
|
|
||||||
|
:param path:
|
||||||
|
Path to the file to import.
|
||||||
|
|
||||||
|
:param mode:
|
||||||
|
Controls the underlying import mechanism that will be used:
|
||||||
|
|
||||||
|
* ImportMode.prepend: the directory containing the module (or package, taking
|
||||||
|
`__init__.py` files into account) will be put at the *start* of `sys.path` before
|
||||||
|
being imported with `importlib.import_module`.
|
||||||
|
|
||||||
|
* ImportMode.append: same as `prepend`, but the directory will be appended
|
||||||
|
to the end of `sys.path`, if not already in `sys.path`.
|
||||||
|
|
||||||
|
* ImportMode.importlib: uses more fine control mechanisms provided by `importlib`
|
||||||
|
to import the module, which avoids having to muck with `sys.path` at all. It effectively
|
||||||
|
allows having same-named test modules in different places.
|
||||||
|
|
||||||
|
:param root:
|
||||||
|
Used as an anchor when mode == ImportMode.importlib to obtain
|
||||||
|
a unique name for the module being imported so it can safely be stored
|
||||||
|
into ``sys.modules``.
|
||||||
|
|
||||||
|
:param consider_namespace_packages:
|
||||||
|
If True, consider namespace packages when resolving module names.
|
||||||
|
|
||||||
|
:raises ImportPathMismatchError:
|
||||||
|
If after importing the given `path` and the module `__file__`
|
||||||
|
are different. Only raised in `prepend` and `append` modes.
|
||||||
|
"""
|
||||||
|
path = Path(path)
|
||||||
|
mode = ImportMode(mode)
|
||||||
|
|
||||||
|
if not path.exists():
|
||||||
|
raise ImportError(path)
|
||||||
|
|
||||||
|
if mode is ImportMode.importlib:
|
||||||
|
# Try to import this module using the standard import mechanisms, but
|
||||||
|
# without touching sys.path.
|
||||||
|
try:
|
||||||
|
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
||||||
|
path, consider_namespace_packages=consider_namespace_packages
|
||||||
|
)
|
||||||
|
except CouldNotResolvePathError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# If the given module name is already in sys.modules, do not import it again.
|
||||||
|
with contextlib.suppress(KeyError):
|
||||||
|
return sys.modules[module_name]
|
||||||
|
|
||||||
|
mod = _import_module_using_spec(
|
||||||
|
module_name, path, pkg_root, insert_modules=False
|
||||||
|
)
|
||||||
|
if mod is not None:
|
||||||
|
return mod
|
||||||
|
|
||||||
|
# Could not import the module with the current sys.path, so we fall back
|
||||||
|
# to importing the file as a single module, not being a part of a package.
|
||||||
|
module_name = module_name_from_path(path, root)
|
||||||
|
with contextlib.suppress(KeyError):
|
||||||
|
return sys.modules[module_name]
|
||||||
|
|
||||||
|
mod = _import_module_using_spec(
|
||||||
|
module_name, path, path.parent, insert_modules=True
|
||||||
|
)
|
||||||
|
if mod is None:
|
||||||
|
raise ImportError(f"Can't find module {module_name} at location {path}")
|
||||||
|
return mod
|
||||||
|
|
||||||
|
try:
|
||||||
|
pkg_root, module_name = resolve_pkg_root_and_module_name(
|
||||||
|
path, consider_namespace_packages=consider_namespace_packages
|
||||||
|
)
|
||||||
|
except CouldNotResolvePathError:
|
||||||
|
pkg_root, module_name = path.parent, path.stem
|
||||||
|
|
||||||
|
# Change sys.path permanently: restoring it at the end of this function would cause surprising
|
||||||
|
# problems because of delayed imports: for example, a conftest.py file imported by this function
|
||||||
|
# might have local imports, which would fail at runtime if we restored sys.path.
|
||||||
|
if mode is ImportMode.append:
|
||||||
|
if str(pkg_root) not in sys.path:
|
||||||
|
sys.path.append(str(pkg_root))
|
||||||
|
elif mode is ImportMode.prepend:
|
||||||
|
if str(pkg_root) != sys.path[0]:
|
||||||
|
sys.path.insert(0, str(pkg_root))
|
||||||
|
else:
|
||||||
|
assert_never(mode)
|
||||||
|
|
||||||
|
importlib.import_module(module_name)
|
||||||
|
|
||||||
|
mod = sys.modules[module_name]
|
||||||
|
if path.name == "__init__.py":
|
||||||
|
return mod
|
||||||
|
|
||||||
|
ignore = os.environ.get("PY_IGNORE_IMPORTMISMATCH", "")
|
||||||
|
if ignore != "1":
|
||||||
|
module_file = mod.__file__
|
||||||
|
if module_file is None:
|
||||||
|
raise ImportPathMismatchError(module_name, module_file, path)
|
||||||
|
|
||||||
|
if module_file.endswith((".pyc", ".pyo")):
|
||||||
|
module_file = module_file[:-1]
|
||||||
|
if module_file.endswith(os.sep + "__init__.py"):
|
||||||
|
module_file = module_file[: -(len(os.sep + "__init__.py"))]
|
||||||
|
|
||||||
|
try:
|
||||||
|
is_same = _is_same(str(path), module_file)
|
||||||
|
except FileNotFoundError:
|
||||||
|
is_same = False
|
||||||
|
|
||||||
|
if not is_same:
|
||||||
|
raise ImportPathMismatchError(module_name, module_file, path)
|
||||||
|
|
||||||
|
return mod
|
||||||
|
|
||||||
|
|
||||||
|
def _import_module_using_spec(
|
||||||
|
module_name: str, module_path: Path, module_location: Path, *, insert_modules: bool
|
||||||
|
) -> Optional[ModuleType]:
|
||||||
|
"""
|
||||||
|
Tries to import a module by its canonical name, path to the .py file, and its
|
||||||
|
parent location.
|
||||||
|
|
||||||
|
:param insert_modules:
|
||||||
|
If True, will call insert_missing_modules to create empty intermediate modules
|
||||||
|
for made-up module names (when importing test files not reachable from sys.path).
|
||||||
|
Note: we can probably drop insert_missing_modules altogether: instead of
|
||||||
|
generating module names such as "src.tests.test_foo", which require intermediate
|
||||||
|
empty modules, we might just as well generate unique module names like
|
||||||
|
"src_tests_test_foo".
|
||||||
|
"""
|
||||||
|
# Checking with sys.meta_path first in case one of its hooks can import this module,
|
||||||
|
# such as our own assertion-rewrite hook.
|
||||||
|
for meta_importer in sys.meta_path:
|
||||||
|
spec = meta_importer.find_spec(module_name, [str(module_location)])
|
||||||
|
if spec is not None:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
spec = importlib.util.spec_from_file_location(module_name, str(module_path))
|
||||||
|
if spec is not None:
|
||||||
|
mod = importlib.util.module_from_spec(spec)
|
||||||
|
sys.modules[module_name] = mod
|
||||||
|
spec.loader.exec_module(mod) # type: ignore[union-attr]
|
||||||
|
if insert_modules:
|
||||||
|
insert_missing_modules(sys.modules, module_name)
|
||||||
|
return mod
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# Implement a special _is_same function on Windows which returns True if the two filenames
|
||||||
|
# compare equal, to circumvent os.path.samefile returning False for mounts in UNC (#7678).
|
||||||
|
if sys.platform.startswith("win"):
|
||||||
|
|
||||||
|
def _is_same(f1: str, f2: str) -> bool:
|
||||||
|
return Path(f1) == Path(f2) or os.path.samefile(f1, f2)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
def _is_same(f1: str, f2: str) -> bool:
|
||||||
|
return os.path.samefile(f1, f2)
|
||||||
|
|
||||||
|
|
||||||
|
def module_name_from_path(path: Path, root: Path) -> str:
|
||||||
|
"""
|
||||||
|
Return a dotted module name based on the given path, anchored on root.
|
||||||
|
|
||||||
|
For example: path="projects/src/tests/test_foo.py" and root="/projects", the
|
||||||
|
resulting module name will be "src.tests.test_foo".
|
||||||
|
"""
|
||||||
|
path = path.with_suffix("")
|
||||||
|
try:
|
||||||
|
relative_path = path.relative_to(root)
|
||||||
|
except ValueError:
|
||||||
|
# If we can't get a relative path to root, use the full path, except
|
||||||
|
# for the first part ("d:\\" or "/" depending on the platform, for example).
|
||||||
|
path_parts = path.parts[1:]
|
||||||
|
else:
|
||||||
|
# Use the parts for the relative path to the root path.
|
||||||
|
path_parts = relative_path.parts
|
||||||
|
|
||||||
|
# Module name for packages do not contain the __init__ file, unless
|
||||||
|
# the `__init__.py` file is at the root.
|
||||||
|
if len(path_parts) >= 2 and path_parts[-1] == "__init__":
|
||||||
|
path_parts = path_parts[:-1]
|
||||||
|
|
||||||
|
# Module names cannot contain ".", normalize them to "_". This prevents
|
||||||
|
# a directory having a "." in the name (".env.310" for example) causing extra intermediate modules.
|
||||||
|
# Also, important to replace "." at the start of paths, as those are considered relative imports.
|
||||||
|
path_parts = tuple(x.replace(".", "_") for x in path_parts)
|
||||||
|
|
||||||
|
return ".".join(path_parts)
|
||||||
|
|
||||||
|
|
||||||
|
def insert_missing_modules(modules: Dict[str, ModuleType], module_name: str) -> None:
|
||||||
|
"""
|
||||||
|
Used by ``import_path`` to create intermediate modules when using mode=importlib.
|
||||||
|
|
||||||
|
When we want to import a module as "src.tests.test_foo" for example, we need
|
||||||
|
to create empty modules "src" and "src.tests" after inserting "src.tests.test_foo",
|
||||||
|
otherwise "src.tests.test_foo" is not importable by ``__import__``.
|
||||||
|
"""
|
||||||
|
module_parts = module_name.split(".")
|
||||||
|
child_module: Union[ModuleType, None] = None
|
||||||
|
module: Union[ModuleType, None] = None
|
||||||
|
child_name: str = ""
|
||||||
|
while module_name:
|
||||||
|
if module_name not in modules:
|
||||||
|
try:
|
||||||
|
# If sys.meta_path is empty, calling import_module will issue
|
||||||
|
# a warning and raise ModuleNotFoundError. To avoid the
|
||||||
|
# warning, we check sys.meta_path explicitly and raise the error
|
||||||
|
# ourselves to fall back to creating a dummy module.
|
||||||
|
if not sys.meta_path:
|
||||||
|
raise ModuleNotFoundError
|
||||||
|
module = importlib.import_module(module_name)
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
module = ModuleType(
|
||||||
|
module_name,
|
||||||
|
doc="Empty module created by pytest's importmode=importlib.",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
module = modules[module_name]
|
||||||
|
if child_module:
|
||||||
|
# Add child attribute to the parent that can reference the child
|
||||||
|
# modules.
|
||||||
|
if not hasattr(module, child_name):
|
||||||
|
setattr(module, child_name, child_module)
|
||||||
|
modules[module_name] = module
|
||||||
|
# Keep track of the child module while moving up the tree.
|
||||||
|
child_module, child_name = module, module_name.rpartition(".")[-1]
|
||||||
|
module_parts.pop(-1)
|
||||||
|
module_name = ".".join(module_parts)
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_package_path(path: Path) -> Optional[Path]:
|
||||||
|
"""Return the Python package path by looking for the last
|
||||||
|
directory upwards which still contains an __init__.py.
|
||||||
|
|
||||||
|
Returns None if it can not be determined.
|
||||||
|
"""
|
||||||
|
result = None
|
||||||
|
for parent in itertools.chain((path,), path.parents):
|
||||||
|
if parent.is_dir():
|
||||||
|
if not (parent / "__init__.py").is_file():
|
||||||
|
break
|
||||||
|
if not parent.name.isidentifier():
|
||||||
|
break
|
||||||
|
result = parent
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_pkg_root_and_module_name(
|
||||||
|
path: Path, *, consider_namespace_packages: bool = False
|
||||||
|
) -> Tuple[Path, str]:
|
||||||
|
"""
|
||||||
|
Return the path to the directory of the root package that contains the
|
||||||
|
given Python file, and its module name:
|
||||||
|
|
||||||
|
src/
|
||||||
|
app/
|
||||||
|
__init__.py
|
||||||
|
core/
|
||||||
|
__init__.py
|
||||||
|
models.py
|
||||||
|
|
||||||
|
Passing the full path to `models.py` will yield Path("src") and "app.core.models".
|
||||||
|
|
||||||
|
If consider_namespace_packages is True, then we additionally check upwards in the hierarchy
|
||||||
|
until we find a directory that is reachable from sys.path, which marks it as a namespace package:
|
||||||
|
|
||||||
|
https://packaging.python.org/en/latest/guides/packaging-namespace-packages
|
||||||
|
|
||||||
|
Raises CouldNotResolvePathError if the given path does not belong to a package (missing any __init__.py files).
|
||||||
|
"""
|
||||||
|
pkg_path = resolve_package_path(path)
|
||||||
|
if pkg_path is not None:
|
||||||
|
pkg_root = pkg_path.parent
|
||||||
|
# https://packaging.python.org/en/latest/guides/packaging-namespace-packages/
|
||||||
|
if consider_namespace_packages:
|
||||||
|
# Go upwards in the hierarchy, if we find a parent path included
|
||||||
|
# in sys.path, it means the package found by resolve_package_path()
|
||||||
|
# actually belongs to a namespace package.
|
||||||
|
for parent in pkg_root.parents:
|
||||||
|
# If any of the parent paths has a __init__.py, it means it is not
|
||||||
|
# a namespace package (see the docs linked above).
|
||||||
|
if (parent / "__init__.py").is_file():
|
||||||
|
break
|
||||||
|
if str(parent) in sys.path:
|
||||||
|
# Point the pkg_root to the root of the namespace package.
|
||||||
|
pkg_root = parent
|
||||||
|
break
|
||||||
|
|
||||||
|
names = list(path.with_suffix("").relative_to(pkg_root).parts)
|
||||||
|
if names[-1] == "__init__":
|
||||||
|
names.pop()
|
||||||
|
module_name = ".".join(names)
|
||||||
|
return pkg_root, module_name
|
||||||
|
|
||||||
|
raise CouldNotResolvePathError(f"Could not resolve for {path}")
|
||||||
|
|
||||||
|
|
||||||
|
class CouldNotResolvePathError(Exception):
|
||||||
|
"""Custom exception raised by resolve_pkg_root_and_module_name."""
|
||||||
|
|
||||||
|
|
||||||
|
def scandir(
|
||||||
|
path: Union[str, "os.PathLike[str]"],
|
||||||
|
sort_key: Callable[["os.DirEntry[str]"], object] = lambda entry: entry.name,
|
||||||
|
) -> List["os.DirEntry[str]"]:
|
||||||
|
"""Scan a directory recursively, in breadth-first order.
|
||||||
|
|
||||||
|
The returned entries are sorted according to the given key.
|
||||||
|
The default is to sort by name.
|
||||||
|
"""
|
||||||
|
entries = []
|
||||||
|
with os.scandir(path) as s:
|
||||||
|
# Skip entries with symlink loops and other brokenness, so the caller
|
||||||
|
# doesn't have to deal with it.
|
||||||
|
for entry in s:
|
||||||
|
try:
|
||||||
|
entry.is_file()
|
||||||
|
except OSError as err:
|
||||||
|
if _ignore_error(err):
|
||||||
|
continue
|
||||||
|
raise
|
||||||
|
entries.append(entry)
|
||||||
|
entries.sort(key=sort_key) # type: ignore[arg-type]
|
||||||
|
return entries
|
||||||
|
|
||||||
|
|
||||||
|
def visit(
|
||||||
|
path: Union[str, "os.PathLike[str]"], recurse: Callable[["os.DirEntry[str]"], bool]
|
||||||
|
) -> Iterator["os.DirEntry[str]"]:
|
||||||
|
"""Walk a directory recursively, in breadth-first order.
|
||||||
|
|
||||||
|
The `recurse` predicate determines whether a directory is recursed.
|
||||||
|
|
||||||
|
Entries at each directory level are sorted.
|
||||||
|
"""
|
||||||
|
entries = scandir(path)
|
||||||
|
yield from entries
|
||||||
|
for entry in entries:
|
||||||
|
if entry.is_dir() and recurse(entry):
|
||||||
|
yield from visit(entry.path, recurse)
|
||||||
|
|
||||||
|
|
||||||
|
def absolutepath(path: Union[Path, str]) -> Path:
|
||||||
|
"""Convert a path to an absolute path using os.path.abspath.
|
||||||
|
|
||||||
|
Prefer this over Path.resolve() (see #6523).
|
||||||
|
Prefer this over Path.absolute() (not public, doesn't normalize).
|
||||||
|
"""
|
||||||
|
return Path(os.path.abspath(str(path)))
|
||||||
|
|
||||||
|
|
||||||
|
def commonpath(path1: Path, path2: Path) -> Optional[Path]:
|
||||||
|
"""Return the common part shared with the other path, or None if there is
|
||||||
|
no common part.
|
||||||
|
|
||||||
|
If one path is relative and one is absolute, returns None.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return Path(os.path.commonpath((str(path1), str(path2))))
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def bestrelpath(directory: Path, dest: Path) -> str:
|
||||||
|
"""Return a string which is a relative path from directory to dest such
|
||||||
|
that directory/bestrelpath == dest.
|
||||||
|
|
||||||
|
The paths must be either both absolute or both relative.
|
||||||
|
|
||||||
|
If no such path can be determined, returns dest.
|
||||||
|
"""
|
||||||
|
assert isinstance(directory, Path)
|
||||||
|
assert isinstance(dest, Path)
|
||||||
|
if dest == directory:
|
||||||
|
return os.curdir
|
||||||
|
# Find the longest common directory.
|
||||||
|
base = commonpath(directory, dest)
|
||||||
|
# Can be the case on Windows for two absolute paths on different drives.
|
||||||
|
# Can be the case for two relative paths without common prefix.
|
||||||
|
# Can be the case for a relative path and an absolute path.
|
||||||
|
if not base:
|
||||||
|
return str(dest)
|
||||||
|
reldirectory = directory.relative_to(base)
|
||||||
|
reldest = dest.relative_to(base)
|
||||||
|
return os.path.join(
|
||||||
|
# Back from directory to base.
|
||||||
|
*([os.pardir] * len(reldirectory.parts)),
|
||||||
|
# Forward from base to dest.
|
||||||
|
*reldest.parts,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def safe_exists(p: Path) -> bool:
|
||||||
|
"""Like Path.exists(), but account for input arguments that might be too long (#11394)."""
|
||||||
|
try:
|
||||||
|
return p.exists()
|
||||||
|
except (ValueError, OSError):
|
||||||
|
# ValueError: stat: path too long for Windows
|
||||||
|
# OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect
|
||||||
|
return False
|
||||||
0
.venv/lib/python3.10/site-packages/_pytest/py.typed
Normal file
0
.venv/lib/python3.10/site-packages/_pytest/py.typed
Normal file
1775
.venv/lib/python3.10/site-packages/_pytest/pytester.py
Normal file
1775
.venv/lib/python3.10/site-packages/_pytest/pytester.py
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,76 @@
|
||||||
|
"""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 typing import Dict
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Sequence
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from _pytest.reports import CollectReport
|
||||||
|
from _pytest.reports import TestReport
|
||||||
|
|
||||||
|
|
||||||
|
def assertoutcome(
|
||||||
|
outcomes: Tuple[
|
||||||
|
Sequence[TestReport],
|
||||||
|
Sequence[Union[CollectReport, TestReport]],
|
||||||
|
Sequence[Union[CollectReport, TestReport]],
|
||||||
|
],
|
||||||
|
passed: int = 0,
|
||||||
|
skipped: int = 0,
|
||||||
|
failed: int = 0,
|
||||||
|
) -> None:
|
||||||
|
__tracebackhide__ = True
|
||||||
|
|
||||||
|
realpassed, realskipped, realfailed = outcomes
|
||||||
|
obtained = {
|
||||||
|
"passed": len(realpassed),
|
||||||
|
"skipped": len(realskipped),
|
||||||
|
"failed": len(realfailed),
|
||||||
|
}
|
||||||
|
expected = {"passed": passed, "skipped": skipped, "failed": failed}
|
||||||
|
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: Optional[int] = None,
|
||||||
|
deselected: Optional[int] = 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
|
||||||
1824
.venv/lib/python3.10/site-packages/_pytest/python.py
Normal file
1824
.venv/lib/python3.10/site-packages/_pytest/python.py
Normal file
File diff suppressed because it is too large
Load diff
1004
.venv/lib/python3.10/site-packages/_pytest/python_api.py
Normal file
1004
.venv/lib/python3.10/site-packages/_pytest/python_api.py
Normal file
File diff suppressed because it is too large
Load diff
24
.venv/lib/python3.10/site-packages/_pytest/python_path.py
Normal file
24
.venv/lib/python3.10/site-packages/_pytest/python_path.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from pytest import Config
|
||||||
|
from pytest import Parser
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_addoption(parser: Parser) -> None:
|
||||||
|
parser.addini("pythonpath", type="paths", help="Add paths to sys.path", default=[])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(tryfirst=True)
|
||||||
|
def pytest_load_initial_conftests(early_config: Config) -> None:
|
||||||
|
# `pythonpath = a b` will set `sys.path` to `[a, b, x, y, z, ...]`
|
||||||
|
for path in reversed(early_config.getini("pythonpath")):
|
||||||
|
sys.path.insert(0, str(path))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(trylast=True)
|
||||||
|
def pytest_unconfigure(config: Config) -> None:
|
||||||
|
for path in config.getini("pythonpath"):
|
||||||
|
path_str = str(path)
|
||||||
|
if path_str in sys.path:
|
||||||
|
sys.path.remove(path_str)
|
||||||
370
.venv/lib/python3.10/site-packages/_pytest/recwarn.py
Normal file
370
.venv/lib/python3.10/site-packages/_pytest/recwarn.py
Normal file
|
|
@ -0,0 +1,370 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Record warnings during test function execution."""
|
||||||
|
from pprint import pformat
|
||||||
|
import re
|
||||||
|
from types import TracebackType
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import final
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Iterator
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import overload
|
||||||
|
from typing import Pattern
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import Union
|
||||||
|
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", None, None]:
|
||||||
|
"""Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions.
|
||||||
|
|
||||||
|
See https://docs.pytest.org/en/latest/how-to/capture-warnings.html for information
|
||||||
|
on warning categories.
|
||||||
|
"""
|
||||||
|
wrec = WarningsRecorder(_ispytest=True)
|
||||||
|
with wrec:
|
||||||
|
warnings.simplefilter("default")
|
||||||
|
yield wrec
|
||||||
|
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def deprecated_call(
|
||||||
|
*, match: Optional[Union[str, Pattern[str]]] = ...
|
||||||
|
) -> "WarningsRecorder":
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def deprecated_call(func: Callable[..., T], *args: Any, **kwargs: Any) -> T:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def deprecated_call(
|
||||||
|
func: Optional[Callable[..., Any]] = None, *args: Any, **kwargs: Any
|
||||||
|
) -> Union["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
|
||||||
|
|
||||||
|
It can also be used by passing a function and ``*args`` and ``**kwargs``,
|
||||||
|
in which case it will ensure calling ``func(*args, **kwargs)`` produces one of
|
||||||
|
the warnings types above. The return value is the return value of the function.
|
||||||
|
|
||||||
|
In the context manager form you may use the keyword argument ``match`` to assert
|
||||||
|
that the warning matches a text or regex.
|
||||||
|
|
||||||
|
The context manager produces a list of :class:`warnings.WarningMessage` objects,
|
||||||
|
one for each warning raised.
|
||||||
|
"""
|
||||||
|
__tracebackhide__ = True
|
||||||
|
if func is not None:
|
||||||
|
args = (func, *args)
|
||||||
|
return warns(
|
||||||
|
(DeprecationWarning, PendingDeprecationWarning, FutureWarning), *args, **kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def warns(
|
||||||
|
expected_warning: Union[Type[Warning], Tuple[Type[Warning], ...]] = ...,
|
||||||
|
*,
|
||||||
|
match: Optional[Union[str, Pattern[str]]] = ...,
|
||||||
|
) -> "WarningsChecker":
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@overload
|
||||||
|
def warns(
|
||||||
|
expected_warning: Union[Type[Warning], Tuple[Type[Warning], ...]],
|
||||||
|
func: Callable[..., T],
|
||||||
|
*args: Any,
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> T:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def warns(
|
||||||
|
expected_warning: Union[Type[Warning], Tuple[Type[Warning], ...]] = Warning,
|
||||||
|
*args: Any,
|
||||||
|
match: Optional[Union[str, Pattern[str]]] = None,
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> Union["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 can be used as a context manager::
|
||||||
|
|
||||||
|
>>> import pytest
|
||||||
|
>>> with pytest.warns(RuntimeWarning):
|
||||||
|
... warnings.warn("my warning", RuntimeWarning)
|
||||||
|
|
||||||
|
In the context manager form you may use the keyword argument ``match`` 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: DID NOT WARN. No warnings of type ...UserWarning... were emitted...
|
||||||
|
|
||||||
|
**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 not args:
|
||||||
|
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:
|
||||||
|
func = args[0]
|
||||||
|
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[1:], **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class WarningsRecorder(warnings.catch_warnings): # type:ignore[type-arg]
|
||||||
|
"""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)
|
||||||
|
# Type ignored due to the way typeshed handles warnings.catch_warnings.
|
||||||
|
super().__init__(record=True) # type: ignore[call-arg]
|
||||||
|
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: Optional[int] = 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 it doesn't exactly warnings.catch_warnings.__enter__
|
||||||
|
# -- it returns a List but we only emulate one.
|
||||||
|
def __enter__(self) -> "WarningsRecorder": # type: ignore
|
||||||
|
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: Optional[Type[BaseException]],
|
||||||
|
exc_val: Optional[BaseException],
|
||||||
|
exc_tb: Optional[TracebackType],
|
||||||
|
) -> 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: Union[Type[Warning], Tuple[Type[Warning], ...]] = Warning,
|
||||||
|
match_expr: Optional[Union[str, Pattern[str]]] = 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: Optional[Type[BaseException]],
|
||||||
|
exc_val: Optional[BaseException],
|
||||||
|
exc_tb: Optional[TracebackType],
|
||||||
|
) -> 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):
|
||||||
|
fail(
|
||||||
|
f"DID NOT WARN. No warnings of type {self.expected_warning} matching the regex were emitted.\n"
|
||||||
|
f" Regex: {self.match_expr}\n"
|
||||||
|
f" Emitted warnings: {found_str()}."
|
||||||
|
)
|
||||||
|
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,
|
||||||
|
module=w.__module__,
|
||||||
|
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__})"
|
||||||
|
)
|
||||||
622
.venv/lib/python3.10/site-packages/_pytest/reports.py
Normal file
622
.venv/lib/python3.10/site-packages/_pytest/reports.py
Normal file
|
|
@ -0,0 +1,622 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
import dataclasses
|
||||||
|
from io import StringIO
|
||||||
|
import os
|
||||||
|
from pprint import pprint
|
||||||
|
from typing import Any
|
||||||
|
from typing import cast
|
||||||
|
from typing import Dict
|
||||||
|
from typing import final
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import Iterator
|
||||||
|
from typing import List
|
||||||
|
from typing import Literal
|
||||||
|
from typing import Mapping
|
||||||
|
from typing import NoReturn
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
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.nodes import Collector
|
||||||
|
from _pytest.nodes import Item
|
||||||
|
from _pytest.outcomes import skip
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
_R = TypeVar("_R", bound="BaseReport")
|
||||||
|
|
||||||
|
|
||||||
|
class BaseReport:
|
||||||
|
when: Optional[str]
|
||||||
|
location: Optional[Tuple[str, Optional[int], str]]
|
||||||
|
longrepr: Union[
|
||||||
|
None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr
|
||||||
|
]
|
||||||
|
sections: List[Tuple[str, str]]
|
||||||
|
nodeid: str
|
||||||
|
outcome: Literal["passed", "failed", "skipped"]
|
||||||
|
|
||||||
|
def __init__(self, **kw: Any) -> None:
|
||||||
|
self.__dict__.update(kw)
|
||||||
|
|
||||||
|
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.nodeid.split("::")[0]
|
||||||
|
|
||||||
|
@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) -> Optional[str]:
|
||||||
|
"""**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(self, config: Config):
|
||||||
|
_category, _short, verbose = config.hook.pytest_report_teststatus(
|
||||||
|
report=self, config=config
|
||||||
|
)
|
||||||
|
return verbose
|
||||||
|
|
||||||
|
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: Type[_R], reportdict: Dict[str, object]) -> _R:
|
||||||
|
"""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("INTERNALERROR: Unknown entry type returned: %s" % type_name, stream=stream)
|
||||||
|
pprint("report_name: %s" % report_class, stream=stream)
|
||||||
|
pprint(reportdict, stream=stream)
|
||||||
|
pprint("Please report this bug at %s" % url, stream=stream)
|
||||||
|
pprint("-" * 100, stream=stream)
|
||||||
|
raise RuntimeError(stream.getvalue())
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
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
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
nodeid: str,
|
||||||
|
location: Tuple[str, Optional[int], str],
|
||||||
|
keywords: Mapping[str, Any],
|
||||||
|
outcome: Literal["passed", "failed", "skipped"],
|
||||||
|
longrepr: Union[
|
||||||
|
None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr
|
||||||
|
],
|
||||||
|
when: Literal["setup", "call", "teardown"],
|
||||||
|
sections: Iterable[Tuple[str, str]] = (),
|
||||||
|
duration: float = 0,
|
||||||
|
start: float = 0,
|
||||||
|
stop: float = 0,
|
||||||
|
user_properties: Optional[Iterable[Tuple[str, object]]] = None,
|
||||||
|
**extra,
|
||||||
|
) -> None:
|
||||||
|
#: Normalized collection nodeid.
|
||||||
|
self.nodeid = 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, Optional[int], str] = location
|
||||||
|
|
||||||
|
#: A name -> value dictionary containing all keywords and
|
||||||
|
#: markers associated with a test invocation.
|
||||||
|
self.keywords: Mapping[str, Any] = 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 = 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 = {x: 1 for x in item.keywords}
|
||||||
|
excinfo = call.excinfo
|
||||||
|
sections = []
|
||||||
|
if not call.excinfo:
|
||||||
|
outcome: Literal["passed", "failed", "skipped"] = "passed"
|
||||||
|
longrepr: Union[
|
||||||
|
None,
|
||||||
|
ExceptionInfo[BaseException],
|
||||||
|
Tuple[str, int, str],
|
||||||
|
str,
|
||||||
|
TerminalRepr,
|
||||||
|
] = 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)
|
||||||
|
else:
|
||||||
|
outcome = "failed"
|
||||||
|
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")
|
||||||
|
)
|
||||||
|
for rwhen, key, content in item._report_sections:
|
||||||
|
sections.append((f"Captured {key} {rwhen}", content))
|
||||||
|
return cls(
|
||||||
|
item.nodeid,
|
||||||
|
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"
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
nodeid: str,
|
||||||
|
outcome: "Literal['passed', 'failed', 'skipped']",
|
||||||
|
longrepr: Union[
|
||||||
|
None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr
|
||||||
|
],
|
||||||
|
result: Optional[List[Union[Item, Collector]]],
|
||||||
|
sections: Iterable[Tuple[str, str]] = (),
|
||||||
|
**extra,
|
||||||
|
) -> None:
|
||||||
|
#: Normalized collection nodeid.
|
||||||
|
self.nodeid = 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,
|
||||||
|
) -> Optional[Tuple[str, Optional[int], str]]:
|
||||||
|
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: Union[CollectReport, TestReport],
|
||||||
|
) -> Optional[Dict[str, Any]]:
|
||||||
|
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],
|
||||||
|
) -> Optional[Union[CollectReport, TestReport]]:
|
||||||
|
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: Union[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: Optional[ReprFileLocation],
|
||||||
|
) -> Optional[Dict[str, Any]]:
|
||||||
|
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 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: Union[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: Optional[Dict[str, Any]]):
|
||||||
|
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: Union[
|
||||||
|
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
|
||||||
569
.venv/lib/python3.10/site-packages/_pytest/runner.py
Normal file
569
.venv/lib/python3.10/site-packages/_pytest/runner.py
Normal file
|
|
@ -0,0 +1,569 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Basic collect and runtest protocol implementations."""
|
||||||
|
import bdb
|
||||||
|
import dataclasses
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import Callable
|
||||||
|
from typing import cast
|
||||||
|
from typing import Dict
|
||||||
|
from typing import final
|
||||||
|
from typing import Generic
|
||||||
|
from typing import List
|
||||||
|
from typing import Literal
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
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[:2] < (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=0.005,
|
||||||
|
metavar="N",
|
||||||
|
help="Minimal duration in seconds for inclusion in slowest list. "
|
||||||
|
"Default: 0.005.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_terminal_summary(terminalreporter: "TerminalReporter") -> None:
|
||||||
|
durations = terminalreporter.config.option.durations
|
||||||
|
durations_min = terminalreporter.config.option.durations_min
|
||||||
|
verbose = terminalreporter.config.getvalue("verbose")
|
||||||
|
if durations is None:
|
||||||
|
return
|
||||||
|
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) # type: ignore[no-any-return]
|
||||||
|
if not durations:
|
||||||
|
tr.write_sep("=", "slowest durations")
|
||||||
|
else:
|
||||||
|
tr.write_sep("=", "slowest %s durations" % durations)
|
||||||
|
dlist = dlist[:durations]
|
||||||
|
|
||||||
|
for i, rep in enumerate(dlist):
|
||||||
|
if verbose < 2 and rep.duration < durations_min:
|
||||||
|
tr.write_line("")
|
||||||
|
tr.write_line(
|
||||||
|
f"({len(dlist) - i} durations < {durations_min:g}s hidden. Use -vv to show these durations.)"
|
||||||
|
)
|
||||||
|
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: Optional[Item]) -> 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: Optional[Item] = 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]
|
||||||
|
rep = call_and_report(item, "setup", log)
|
||||||
|
reports = [rep]
|
||||||
|
if rep.passed:
|
||||||
|
if item.config.getoption("setupshow", False):
|
||||||
|
show_test_item(item)
|
||||||
|
if not item.config.getoption("setuponly", False):
|
||||||
|
reports.append(call_and_report(item, "call", log))
|
||||||
|
reports.append(call_and_report(item, "teardown", log, nextitem=nextitem))
|
||||||
|
# After all teardown hooks have been called
|
||||||
|
# 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) -> 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(" (fixtures used: {})".format(", ".join(used_fixtures)))
|
||||||
|
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 e
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_runtest_teardown(item: Item, nextitem: Optional[Item]) -> 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: Optional[Literal["setup", "call", "teardown"]]
|
||||||
|
) -> 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) -> Optional[Tuple[str, str, str]]:
|
||||||
|
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}"
|
||||||
|
reraise: Tuple[Type[BaseException], ...] = (Exit,)
|
||||||
|
if not item.config.getoption("usepdb", False):
|
||||||
|
reraise += (KeyboardInterrupt,)
|
||||||
|
call = CallInfo.from_call(
|
||||||
|
lambda: runtest_hook(item=item, **kwds), when=when, reraise=reraise
|
||||||
|
)
|
||||||
|
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 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
|
||||||
|
if isinstance(call.excinfo.value, (Skipped, bdb.BdbQuit)):
|
||||||
|
# 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: Optional[TResult]
|
||||||
|
#: The captured exception of the call, if it raised.
|
||||||
|
excinfo: Optional[ExceptionInfo[BaseException]]
|
||||||
|
#: 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: Optional[TResult],
|
||||||
|
excinfo: Optional[ExceptionInfo[BaseException]],
|
||||||
|
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: Optional[
|
||||||
|
Union[Type[BaseException], Tuple[Type[BaseException], ...]]
|
||||||
|
] = None,
|
||||||
|
) -> "CallInfo[TResult]":
|
||||||
|
"""Call func, wrapping the result in a CallInfo.
|
||||||
|
|
||||||
|
:param func:
|
||||||
|
The function to call. Called without arguments.
|
||||||
|
: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
|
||||||
|
start = timing.time()
|
||||||
|
precise_start = timing.perf_counter()
|
||||||
|
try:
|
||||||
|
result: Optional[TResult] = func()
|
||||||
|
except BaseException:
|
||||||
|
excinfo = ExceptionInfo.from_current()
|
||||||
|
if reraise is not None and isinstance(excinfo.value, reraise):
|
||||||
|
raise
|
||||||
|
result = None
|
||||||
|
# use the perf counter
|
||||||
|
precise_stop = timing.perf_counter()
|
||||||
|
duration = precise_stop - precise_start
|
||||||
|
stop = timing.time()
|
||||||
|
return cls(
|
||||||
|
start=start,
|
||||||
|
stop=stop,
|
||||||
|
duration=duration,
|
||||||
|
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[Union[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")
|
||||||
|
longrepr: Union[None, Tuple[str, int, str], str, TerminalRepr] = 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:
|
||||||
|
# Type ignored because unittest is loaded dynamically.
|
||||||
|
skip_exceptions.append(unittest.SkipTest) # type: ignore
|
||||||
|
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.nodeid, 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, if its setup raised.
|
||||||
|
Optional[Union[OutcomeException, Exception]],
|
||||||
|
],
|
||||||
|
] = {}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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)
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
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: Optional[Item]) -> 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
|
||||||
90
.venv/lib/python3.10/site-packages/_pytest/scope.py
Normal file
90
.venv/lib/python3.10/site-packages/_pytest/scope.py
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
"""
|
||||||
|
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 enum import Enum
|
||||||
|
from functools import total_ordering
|
||||||
|
from typing import Literal
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
_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: _ScopeName = "function"
|
||||||
|
Class: _ScopeName = "class"
|
||||||
|
Module: _ScopeName = "module"
|
||||||
|
Package: _ScopeName = "package"
|
||||||
|
Session: _ScopeName = "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: Optional[str] = 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]
|
||||||
102
.venv/lib/python3.10/site-packages/_pytest/setuponly.py
Normal file
102
.venv/lib/python3.10/site-packages/_pytest/setuponly.py
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
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 # type: ignore[attr-defined]
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
tw.write(
|
||||||
|
"{step} {scope} {fixture}".format( # noqa: UP032 (Readability)
|
||||||
|
step=msg.ljust(8), # align the output to TEARDOWN
|
||||||
|
scope=fixturedef.scope[0].upper(),
|
||||||
|
fixture=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)}]") # type: ignore[attr-defined]
|
||||||
|
|
||||||
|
tw.flush()
|
||||||
|
|
||||||
|
if capman:
|
||||||
|
capman.resume_global_capture()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(tryfirst=True)
|
||||||
|
def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
|
||||||
|
if config.option.setuponly:
|
||||||
|
config.option.setupshow = True
|
||||||
|
return None
|
||||||
40
.venv/lib/python3.10/site-packages/_pytest/setupplan.py
Normal file
40
.venv/lib/python3.10/site-packages/_pytest/setupplan.py
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
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
|
||||||
|
) -> Optional[object]:
|
||||||
|
# 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) -> Optional[Union[int, ExitCode]]:
|
||||||
|
if config.option.setupplan:
|
||||||
|
config.option.setuponly = True
|
||||||
|
config.option.setupshow = True
|
||||||
|
return None
|
||||||
300
.venv/lib/python3.10/site-packages/_pytest/skipping.py
Normal file
300
.venv/lib/python3.10/site-packages/_pytest/skipping.py
Normal file
|
|
@ -0,0 +1,300 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Support for skip/xfail functions and markers."""
|
||||||
|
from collections.abc import Mapping
|
||||||
|
import dataclasses
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
|
||||||
|
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.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(
|
||||||
|
"xfail_strict",
|
||||||
|
"Default for the strict parameter of xfail "
|
||||||
|
"markers when not given explicitly (default: False)",
|
||||||
|
default=False,
|
||||||
|
type="bool",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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=xfail_strict): "
|
||||||
|
"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__) # type: ignore[attr-defined]
|
||||||
|
try:
|
||||||
|
filename = f"<{mark.name} condition>"
|
||||||
|
condition_code = compile(condition, filename, "eval")
|
||||||
|
result = eval(condition_code, globals_)
|
||||||
|
except SyntaxError as exc:
|
||||||
|
msglines = [
|
||||||
|
"Error evaluating %r condition" % mark.name,
|
||||||
|
" " + condition,
|
||||||
|
" " + " " * (exc.offset or 0) + "^",
|
||||||
|
"SyntaxError: invalid syntax",
|
||||||
|
]
|
||||||
|
fail("\n".join(msglines), pytrace=False)
|
||||||
|
except Exception as exc:
|
||||||
|
msglines = [
|
||||||
|
"Error evaluating %r condition" % mark.name,
|
||||||
|
" " + condition,
|
||||||
|
*traceback.format_exception_only(type(exc), exc),
|
||||||
|
]
|
||||||
|
fail("\n".join(msglines), pytrace=False)
|
||||||
|
|
||||||
|
# Boolean condition.
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
result = bool(condition)
|
||||||
|
except Exception as exc:
|
||||||
|
msglines = [
|
||||||
|
"Error evaluating %r condition as a boolean" % mark.name,
|
||||||
|
*traceback.format_exception_only(type(exc), 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 = (
|
||||||
|
"Error evaluating %r: " % mark.name
|
||||||
|
+ "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) -> Optional[Skip]:
|
||||||
|
"""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__ = ("reason", "run", "strict", "raises")
|
||||||
|
|
||||||
|
reason: str
|
||||||
|
run: bool
|
||||||
|
strict: bool
|
||||||
|
raises: Optional[Tuple[Type[BaseException], ...]]
|
||||||
|
|
||||||
|
|
||||||
|
def evaluate_xfail_marks(item: Item) -> Optional[Xfail]:
|
||||||
|
"""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", item.config.getini("xfail_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[Optional[Xfail]]()
|
||||||
|
|
||||||
|
|
||||||
|
@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:
|
||||||
|
xfail("[NOTRUN] " + xfailed.reason)
|
||||||
|
|
||||||
|
|
||||||
|
@hookimpl(wrapper=True)
|
||||||
|
def pytest_runtest_call(item: Item) -> Generator[None, None, 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:
|
||||||
|
xfail("[NOTRUN] " + xfailed.reason)
|
||||||
|
|
||||||
|
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 = "reason: " + call.excinfo.value.msg
|
||||||
|
rep.outcome = "skipped"
|
||||||
|
elif not rep.skipped and xfailed:
|
||||||
|
if call.excinfo:
|
||||||
|
raises = xfailed.raises
|
||||||
|
if raises is not None and not isinstance(call.excinfo.value, raises):
|
||||||
|
rep.outcome = "failed"
|
||||||
|
else:
|
||||||
|
rep.outcome = "skipped"
|
||||||
|
rep.wasxfail = xfailed.reason
|
||||||
|
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) -> Optional[Tuple[str, str, str]]:
|
||||||
|
if hasattr(report, "wasxfail"):
|
||||||
|
if report.skipped:
|
||||||
|
return "xfailed", "x", "XFAIL"
|
||||||
|
elif report.passed:
|
||||||
|
return "xpassed", "X", "XPASS"
|
||||||
|
return None
|
||||||
112
.venv/lib/python3.10/site-packages/_pytest/stash.py
Normal file
112
.venv/lib/python3.10/site-packages/_pytest/stash.py
Normal file
|
|
@ -0,0 +1,112 @@
|
||||||
|
from typing import Any
|
||||||
|
from typing import cast
|
||||||
|
from typing import Dict
|
||||||
|
from typing import Generic
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
|
||||||
|
__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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__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]
|
||||||
|
"""
|
||||||
|
|
||||||
|
__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) -> Union[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)
|
||||||
131
.venv/lib/python3.10/site-packages/_pytest/stepwise.py
Normal file
131
.venv/lib/python3.10/site-packages/_pytest/stepwise.py
Normal file
|
|
@ -0,0 +1,131 @@
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from _pytest import nodes
|
||||||
|
from _pytest.config import Config
|
||||||
|
from _pytest.config.argparsing import Parser
|
||||||
|
from _pytest.main import Session
|
||||||
|
from _pytest.reports import TestReport
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from _pytest.cacheprovider import Cache
|
||||||
|
|
||||||
|
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.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl
|
||||||
|
def pytest_configure(config: Config) -> None:
|
||||||
|
if config.option.stepwise_skip:
|
||||||
|
# allow --stepwise-skip to work on it's own merits.
|
||||||
|
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
|
||||||
|
# Clear the list of failing tests if the plugin is not active.
|
||||||
|
session.config.cache.set(STEPWISE_CACHE_DIR, [])
|
||||||
|
|
||||||
|
|
||||||
|
class StepwisePlugin:
|
||||||
|
def __init__(self, config: Config) -> None:
|
||||||
|
self.config = config
|
||||||
|
self.session: Optional[Session] = None
|
||||||
|
self.report_status = ""
|
||||||
|
assert config.cache is not None
|
||||||
|
self.cache: Cache = config.cache
|
||||||
|
self.lastfailed: Optional[str] = self.cache.get(STEPWISE_CACHE_DIR, None)
|
||||||
|
self.skip: bool = config.getoption("stepwise_skip")
|
||||||
|
|
||||||
|
def pytest_sessionstart(self, session: Session) -> None:
|
||||||
|
self.session = session
|
||||||
|
|
||||||
|
def pytest_collection_modifyitems(
|
||||||
|
self, config: Config, items: List[nodes.Item]
|
||||||
|
) -> None:
|
||||||
|
if not self.lastfailed:
|
||||||
|
self.report_status = "no previously failed tests, not skipping."
|
||||||
|
return
|
||||||
|
|
||||||
|
# check all item nodes until we find a match on last failed
|
||||||
|
failed_index = None
|
||||||
|
for index, item in enumerate(items):
|
||||||
|
if item.nodeid == self.lastfailed:
|
||||||
|
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 = "previously failed test not found, not skipping."
|
||||||
|
else:
|
||||||
|
self.report_status = f"skipping {failed_index} already passed items."
|
||||||
|
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.nodeid == self.lastfailed:
|
||||||
|
self.lastfailed = None
|
||||||
|
|
||||||
|
self.skip = False
|
||||||
|
else:
|
||||||
|
# Mark test as the last failing and interrupt the test session.
|
||||||
|
self.lastfailed = report.nodeid
|
||||||
|
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.nodeid == self.lastfailed:
|
||||||
|
self.lastfailed = None
|
||||||
|
|
||||||
|
def pytest_report_collectionfinish(self) -> Optional[str]:
|
||||||
|
if self.config.getoption("verbose") >= 0 and self.report_status:
|
||||||
|
return f"stepwise: {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.cache.set(STEPWISE_CACHE_DIR, self.lastfailed)
|
||||||
1515
.venv/lib/python3.10/site-packages/_pytest/terminal.py
Normal file
1515
.venv/lib/python3.10/site-packages/_pytest/terminal.py
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,92 @@
|
||||||
|
import threading
|
||||||
|
import traceback
|
||||||
|
from types import TracebackType
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Type
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
# Copied from cpython/Lib/test/support/threading_helper.py, with modifications.
|
||||||
|
class catch_threading_exception:
|
||||||
|
"""Context manager catching threading.Thread exception using
|
||||||
|
threading.excepthook.
|
||||||
|
|
||||||
|
Storing exc_value using a custom hook can create a reference cycle. The
|
||||||
|
reference cycle is broken explicitly when the context manager exits.
|
||||||
|
|
||||||
|
Storing thread using a custom hook can resurrect it if it is set to an
|
||||||
|
object which is being finalized. Exiting the context manager clears the
|
||||||
|
stored object.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
with threading_helper.catch_threading_exception() as cm:
|
||||||
|
# code spawning a thread which raises an exception
|
||||||
|
...
|
||||||
|
# check the thread exception: use cm.args
|
||||||
|
...
|
||||||
|
# cm.args attribute no longer exists at this point
|
||||||
|
# (to break a reference cycle)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.args: Optional["threading.ExceptHookArgs"] = None
|
||||||
|
self._old_hook: Optional[Callable[["threading.ExceptHookArgs"], Any]] = None
|
||||||
|
|
||||||
|
def _hook(self, args: "threading.ExceptHookArgs") -> None:
|
||||||
|
self.args = args
|
||||||
|
|
||||||
|
def __enter__(self) -> "catch_threading_exception":
|
||||||
|
self._old_hook = threading.excepthook
|
||||||
|
threading.excepthook = self._hook
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(
|
||||||
|
self,
|
||||||
|
exc_type: Optional[Type[BaseException]],
|
||||||
|
exc_val: Optional[BaseException],
|
||||||
|
exc_tb: Optional[TracebackType],
|
||||||
|
) -> None:
|
||||||
|
assert self._old_hook is not None
|
||||||
|
threading.excepthook = self._old_hook
|
||||||
|
self._old_hook = None
|
||||||
|
del self.args
|
||||||
|
|
||||||
|
|
||||||
|
def thread_exception_runtest_hook() -> Generator[None, None, None]:
|
||||||
|
with catch_threading_exception() as cm:
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
if cm.args:
|
||||||
|
thread_name = (
|
||||||
|
"<unknown>" if cm.args.thread is None else cm.args.thread.name
|
||||||
|
)
|
||||||
|
msg = f"Exception in thread {thread_name}\n\n"
|
||||||
|
msg += "".join(
|
||||||
|
traceback.format_exception(
|
||||||
|
cm.args.exc_type,
|
||||||
|
cm.args.exc_value,
|
||||||
|
cm.args.exc_traceback,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True, trylast=True)
|
||||||
|
def pytest_runtest_setup() -> Generator[None, None, None]:
|
||||||
|
yield from thread_exception_runtest_hook()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_runtest_call() -> Generator[None, None, None]:
|
||||||
|
yield from thread_exception_runtest_hook()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_runtest_teardown() -> Generator[None, None, None]:
|
||||||
|
yield from thread_exception_runtest_hook()
|
||||||
14
.venv/lib/python3.10/site-packages/_pytest/timing.py
Normal file
14
.venv/lib/python3.10/site-packages/_pytest/timing.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
"""Indirection for time functions.
|
||||||
|
|
||||||
|
We intentionally grab some "time" functions internally to avoid tests mocking "time" to affect
|
||||||
|
pytest runtime information (issue #185).
|
||||||
|
|
||||||
|
Fixture "mock_timing" also interacts with this module for pytest's own tests.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from time import perf_counter
|
||||||
|
from time import sleep
|
||||||
|
from time import time
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ["perf_counter", "sleep", "time"]
|
||||||
321
.venv/lib/python3.10/site-packages/_pytest/tmpdir.py
Normal file
321
.venv/lib/python3.10/site-packages/_pytest/tmpdir.py
Normal file
|
|
@ -0,0 +1,321 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Support for providing temporary directories to test functions."""
|
||||||
|
import dataclasses
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import re
|
||||||
|
from shutil import rmtree
|
||||||
|
import tempfile
|
||||||
|
from typing import Any
|
||||||
|
from typing import Dict
|
||||||
|
from typing import final
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Literal
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from .pathlib import cleanup_dead_symlinks
|
||||||
|
from .pathlib import LOCK_TIMEOUT
|
||||||
|
from .pathlib import make_numbered_dir
|
||||||
|
from .pathlib import make_numbered_dir_with_cleanup
|
||||||
|
from .pathlib import rm_rf
|
||||||
|
from _pytest.compat import get_user_id
|
||||||
|
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.monkeypatch import MonkeyPatch
|
||||||
|
from _pytest.nodes import Item
|
||||||
|
from _pytest.reports import TestReport
|
||||||
|
from _pytest.stash import StashKey
|
||||||
|
|
||||||
|
|
||||||
|
tmppath_result_key = StashKey[Dict[str, bool]]()
|
||||||
|
RetentionType = Literal["all", "failed", "none"]
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class TempPathFactory:
|
||||||
|
"""Factory for temporary directories under the common base temp directory.
|
||||||
|
|
||||||
|
The base directory can be configured using the ``--basetemp`` option.
|
||||||
|
"""
|
||||||
|
|
||||||
|
_given_basetemp: Optional[Path]
|
||||||
|
# pluggy TagTracerSub, not currently exposed, so Any.
|
||||||
|
_trace: Any
|
||||||
|
_basetemp: Optional[Path]
|
||||||
|
_retention_count: int
|
||||||
|
_retention_policy: RetentionType
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
given_basetemp: Optional[Path],
|
||||||
|
retention_count: int,
|
||||||
|
retention_policy: RetentionType,
|
||||||
|
trace,
|
||||||
|
basetemp: Optional[Path] = None,
|
||||||
|
*,
|
||||||
|
_ispytest: bool = False,
|
||||||
|
) -> None:
|
||||||
|
check_ispytest(_ispytest)
|
||||||
|
if given_basetemp is None:
|
||||||
|
self._given_basetemp = None
|
||||||
|
else:
|
||||||
|
# Use os.path.abspath() to get absolute path instead of resolve() as it
|
||||||
|
# does not work the same in all platforms (see #4427).
|
||||||
|
# Path.absolute() exists, but it is not public (see https://bugs.python.org/issue25012).
|
||||||
|
self._given_basetemp = Path(os.path.abspath(str(given_basetemp)))
|
||||||
|
self._trace = trace
|
||||||
|
self._retention_count = retention_count
|
||||||
|
self._retention_policy = retention_policy
|
||||||
|
self._basetemp = basetemp
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_config(
|
||||||
|
cls,
|
||||||
|
config: Config,
|
||||||
|
*,
|
||||||
|
_ispytest: bool = False,
|
||||||
|
) -> "TempPathFactory":
|
||||||
|
"""Create a factory according to pytest configuration.
|
||||||
|
|
||||||
|
:meta private:
|
||||||
|
"""
|
||||||
|
check_ispytest(_ispytest)
|
||||||
|
count = int(config.getini("tmp_path_retention_count"))
|
||||||
|
if count < 0:
|
||||||
|
raise ValueError(
|
||||||
|
f"tmp_path_retention_count must be >= 0. Current input: {count}."
|
||||||
|
)
|
||||||
|
|
||||||
|
policy = config.getini("tmp_path_retention_policy")
|
||||||
|
if policy not in ("all", "failed", "none"):
|
||||||
|
raise ValueError(
|
||||||
|
f"tmp_path_retention_policy must be either all, failed, none. Current input: {policy}."
|
||||||
|
)
|
||||||
|
|
||||||
|
return cls(
|
||||||
|
given_basetemp=config.option.basetemp,
|
||||||
|
trace=config.trace.get("tmpdir"),
|
||||||
|
retention_count=count,
|
||||||
|
retention_policy=policy,
|
||||||
|
_ispytest=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _ensure_relative_to_basetemp(self, basename: str) -> str:
|
||||||
|
basename = os.path.normpath(basename)
|
||||||
|
if (self.getbasetemp() / basename).resolve().parent != self.getbasetemp():
|
||||||
|
raise ValueError(f"{basename} is not a normalized and relative path")
|
||||||
|
return basename
|
||||||
|
|
||||||
|
def mktemp(self, basename: str, numbered: bool = True) -> Path:
|
||||||
|
"""Create a new temporary directory managed by the factory.
|
||||||
|
|
||||||
|
:param basename:
|
||||||
|
Directory base name, must be a relative path.
|
||||||
|
|
||||||
|
:param numbered:
|
||||||
|
If ``True``, ensure the directory is unique by adding a numbered
|
||||||
|
suffix greater than any existing one: ``basename="foo-"`` and ``numbered=True``
|
||||||
|
means that this function will create directories named ``"foo-0"``,
|
||||||
|
``"foo-1"``, ``"foo-2"`` and so on.
|
||||||
|
|
||||||
|
:returns:
|
||||||
|
The path to the new directory.
|
||||||
|
"""
|
||||||
|
basename = self._ensure_relative_to_basetemp(basename)
|
||||||
|
if not numbered:
|
||||||
|
p = self.getbasetemp().joinpath(basename)
|
||||||
|
p.mkdir(mode=0o700)
|
||||||
|
else:
|
||||||
|
p = make_numbered_dir(root=self.getbasetemp(), prefix=basename, mode=0o700)
|
||||||
|
self._trace("mktemp", p)
|
||||||
|
return p
|
||||||
|
|
||||||
|
def getbasetemp(self) -> Path:
|
||||||
|
"""Return the base temporary directory, creating it if needed.
|
||||||
|
|
||||||
|
:returns:
|
||||||
|
The base temporary directory.
|
||||||
|
"""
|
||||||
|
if self._basetemp is not None:
|
||||||
|
return self._basetemp
|
||||||
|
|
||||||
|
if self._given_basetemp is not None:
|
||||||
|
basetemp = self._given_basetemp
|
||||||
|
if basetemp.exists():
|
||||||
|
rm_rf(basetemp)
|
||||||
|
basetemp.mkdir(mode=0o700)
|
||||||
|
basetemp = basetemp.resolve()
|
||||||
|
else:
|
||||||
|
from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT")
|
||||||
|
temproot = Path(from_env or tempfile.gettempdir()).resolve()
|
||||||
|
user = get_user() or "unknown"
|
||||||
|
# use a sub-directory in the temproot to speed-up
|
||||||
|
# make_numbered_dir() call
|
||||||
|
rootdir = temproot.joinpath(f"pytest-of-{user}")
|
||||||
|
try:
|
||||||
|
rootdir.mkdir(mode=0o700, exist_ok=True)
|
||||||
|
except OSError:
|
||||||
|
# getuser() likely returned illegal characters for the platform, use unknown back off mechanism
|
||||||
|
rootdir = temproot.joinpath("pytest-of-unknown")
|
||||||
|
rootdir.mkdir(mode=0o700, exist_ok=True)
|
||||||
|
# Because we use exist_ok=True with a predictable name, make sure
|
||||||
|
# we are the owners, to prevent any funny business (on unix, where
|
||||||
|
# temproot is usually shared).
|
||||||
|
# Also, to keep things private, fixup any world-readable temp
|
||||||
|
# rootdir's permissions. Historically 0o755 was used, so we can't
|
||||||
|
# just error out on this, at least for a while.
|
||||||
|
uid = get_user_id()
|
||||||
|
if uid is not None:
|
||||||
|
rootdir_stat = rootdir.stat()
|
||||||
|
if rootdir_stat.st_uid != uid:
|
||||||
|
raise OSError(
|
||||||
|
f"The temporary directory {rootdir} is not owned by the current user. "
|
||||||
|
"Fix this and try again."
|
||||||
|
)
|
||||||
|
if (rootdir_stat.st_mode & 0o077) != 0:
|
||||||
|
os.chmod(rootdir, rootdir_stat.st_mode & ~0o077)
|
||||||
|
keep = self._retention_count
|
||||||
|
if self._retention_policy == "none":
|
||||||
|
keep = 0
|
||||||
|
basetemp = make_numbered_dir_with_cleanup(
|
||||||
|
prefix="pytest-",
|
||||||
|
root=rootdir,
|
||||||
|
keep=keep,
|
||||||
|
lock_timeout=LOCK_TIMEOUT,
|
||||||
|
mode=0o700,
|
||||||
|
)
|
||||||
|
assert basetemp is not None, basetemp
|
||||||
|
self._basetemp = basetemp
|
||||||
|
self._trace("new basetemp", basetemp)
|
||||||
|
return basetemp
|
||||||
|
|
||||||
|
|
||||||
|
def get_user() -> Optional[str]:
|
||||||
|
"""Return the current user name, or None if getuser() does not work
|
||||||
|
in the current environment (see #1010)."""
|
||||||
|
try:
|
||||||
|
# In some exotic environments, getpass may not be importable.
|
||||||
|
import getpass
|
||||||
|
|
||||||
|
return getpass.getuser()
|
||||||
|
except (ImportError, OSError, KeyError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_configure(config: Config) -> None:
|
||||||
|
"""Create a TempPathFactory 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 tmp_path_factory session fixture.
|
||||||
|
"""
|
||||||
|
mp = MonkeyPatch()
|
||||||
|
config.add_cleanup(mp.undo)
|
||||||
|
_tmp_path_factory = TempPathFactory.from_config(config, _ispytest=True)
|
||||||
|
mp.setattr(config, "_tmp_path_factory", _tmp_path_factory, raising=False)
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_addoption(parser: Parser) -> None:
|
||||||
|
parser.addini(
|
||||||
|
"tmp_path_retention_count",
|
||||||
|
help="How many sessions should we keep the `tmp_path` directories, according to `tmp_path_retention_policy`.",
|
||||||
|
default=3,
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.addini(
|
||||||
|
"tmp_path_retention_policy",
|
||||||
|
help="Controls which directories created by the `tmp_path` fixture are kept around, based on test outcome. "
|
||||||
|
"(all/failed/none)",
|
||||||
|
default="all",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@fixture(scope="session")
|
||||||
|
def tmp_path_factory(request: FixtureRequest) -> TempPathFactory:
|
||||||
|
"""Return a :class:`pytest.TempPathFactory` instance for the test session."""
|
||||||
|
# Set dynamically by pytest_configure() above.
|
||||||
|
return request.config._tmp_path_factory # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
def _mk_tmp(request: FixtureRequest, factory: TempPathFactory) -> Path:
|
||||||
|
name = request.node.name
|
||||||
|
name = re.sub(r"[\W]", "_", name)
|
||||||
|
MAXVAL = 30
|
||||||
|
name = name[:MAXVAL]
|
||||||
|
return factory.mktemp(name, numbered=True)
|
||||||
|
|
||||||
|
|
||||||
|
@fixture
|
||||||
|
def tmp_path(
|
||||||
|
request: FixtureRequest, tmp_path_factory: TempPathFactory
|
||||||
|
) -> Generator[Path, None, None]:
|
||||||
|
"""Return a temporary directory path object which is unique to each test
|
||||||
|
function invocation, created as a sub directory of the base temporary
|
||||||
|
directory.
|
||||||
|
|
||||||
|
By default, a new base temporary directory is created each test session,
|
||||||
|
and old bases are removed after 3 sessions, to aid in debugging.
|
||||||
|
This behavior can be configured with :confval:`tmp_path_retention_count` and
|
||||||
|
:confval:`tmp_path_retention_policy`.
|
||||||
|
If ``--basetemp`` is used then it is cleared each session. See
|
||||||
|
:ref:`temporary directory location and retention`.
|
||||||
|
|
||||||
|
The returned object is a :class:`pathlib.Path` object.
|
||||||
|
"""
|
||||||
|
path = _mk_tmp(request, tmp_path_factory)
|
||||||
|
yield path
|
||||||
|
|
||||||
|
# Remove the tmpdir if the policy is "failed" and the test passed.
|
||||||
|
tmp_path_factory: TempPathFactory = request.session.config._tmp_path_factory # type: ignore
|
||||||
|
policy = tmp_path_factory._retention_policy
|
||||||
|
result_dict = request.node.stash[tmppath_result_key]
|
||||||
|
|
||||||
|
if policy == "failed" and result_dict.get("call", True):
|
||||||
|
# We do a "best effort" to remove files, but it might not be possible due to some leaked resource,
|
||||||
|
# permissions, etc, in which case we ignore it.
|
||||||
|
rmtree(path, ignore_errors=True)
|
||||||
|
|
||||||
|
del request.node.stash[tmppath_result_key]
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_sessionfinish(session, exitstatus: Union[int, ExitCode]):
|
||||||
|
"""After each session, remove base directory if all the tests passed,
|
||||||
|
the policy is "failed", and the basetemp is not specified by a user.
|
||||||
|
"""
|
||||||
|
tmp_path_factory: TempPathFactory = session.config._tmp_path_factory
|
||||||
|
basetemp = tmp_path_factory._basetemp
|
||||||
|
if basetemp is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
policy = tmp_path_factory._retention_policy
|
||||||
|
if (
|
||||||
|
exitstatus == 0
|
||||||
|
and policy == "failed"
|
||||||
|
and tmp_path_factory._given_basetemp is None
|
||||||
|
):
|
||||||
|
if basetemp.is_dir():
|
||||||
|
# We do a "best effort" to remove files, but it might not be possible due to some leaked resource,
|
||||||
|
# permissions, etc, in which case we ignore it.
|
||||||
|
rmtree(basetemp, ignore_errors=True)
|
||||||
|
|
||||||
|
# Remove dead symlinks.
|
||||||
|
if basetemp.is_dir():
|
||||||
|
cleanup_dead_symlinks(basetemp)
|
||||||
|
|
||||||
|
|
||||||
|
@hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_runtest_makereport(
|
||||||
|
item: Item, call
|
||||||
|
) -> Generator[None, TestReport, TestReport]:
|
||||||
|
rep = yield
|
||||||
|
assert rep.when is not None
|
||||||
|
empty: Dict[str, bool] = {}
|
||||||
|
item.stash.setdefault(tmppath_result_key, empty)[rep.when] = rep.passed
|
||||||
|
return rep
|
||||||
407
.venv/lib/python3.10/site-packages/_pytest/unittest.py
Normal file
407
.venv/lib/python3.10/site-packages/_pytest/unittest.py
Normal file
|
|
@ -0,0 +1,407 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
"""Discover and run std-library "unittest" style tests."""
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
import types
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Iterable
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
import _pytest._code
|
||||||
|
from _pytest.compat import getimfunc
|
||||||
|
from _pytest.compat import is_async_function
|
||||||
|
from _pytest.config import hookimpl
|
||||||
|
from _pytest.fixtures import FixtureRequest
|
||||||
|
from _pytest.nodes import Collector
|
||||||
|
from _pytest.nodes import Item
|
||||||
|
from _pytest.outcomes import exit
|
||||||
|
from _pytest.outcomes import fail
|
||||||
|
from _pytest.outcomes import skip
|
||||||
|
from _pytest.outcomes import xfail
|
||||||
|
from _pytest.python import Class
|
||||||
|
from _pytest.python import Function
|
||||||
|
from _pytest.python import Module
|
||||||
|
from _pytest.runner import CallInfo
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import twisted.trial.unittest
|
||||||
|
|
||||||
|
_SysExcInfoType = Union[
|
||||||
|
Tuple[Type[BaseException], BaseException, types.TracebackType],
|
||||||
|
Tuple[None, None, None],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_pycollect_makeitem(
|
||||||
|
collector: Union[Module, Class], name: str, obj: object
|
||||||
|
) -> Optional["UnitTestCase"]:
|
||||||
|
# Has unittest been imported and is obj a subclass of its TestCase?
|
||||||
|
try:
|
||||||
|
ut = sys.modules["unittest"]
|
||||||
|
# Type ignored because `ut` is an opaque module.
|
||||||
|
if not issubclass(obj, ut.TestCase): # type: ignore
|
||||||
|
return None
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
# Yes, so let's collect it.
|
||||||
|
return UnitTestCase.from_parent(collector, name=name, obj=obj)
|
||||||
|
|
||||||
|
|
||||||
|
class UnitTestCase(Class):
|
||||||
|
# Marker for fixturemanger.getfixtureinfo()
|
||||||
|
# to declare that our children do not support funcargs.
|
||||||
|
nofuncargs = True
|
||||||
|
|
||||||
|
def collect(self) -> Iterable[Union[Item, Collector]]:
|
||||||
|
from unittest import TestLoader
|
||||||
|
|
||||||
|
cls = self.obj
|
||||||
|
if not getattr(cls, "__test__", True):
|
||||||
|
return
|
||||||
|
|
||||||
|
skipped = _is_skipped(cls)
|
||||||
|
if not skipped:
|
||||||
|
self._register_unittest_setup_method_fixture(cls)
|
||||||
|
self._register_unittest_setup_class_fixture(cls)
|
||||||
|
self._register_setup_class_fixture()
|
||||||
|
|
||||||
|
self.session._fixturemanager.parsefactories(self, unittest=True)
|
||||||
|
loader = TestLoader()
|
||||||
|
foundsomething = False
|
||||||
|
for name in loader.getTestCaseNames(self.obj):
|
||||||
|
x = getattr(self.obj, name)
|
||||||
|
if not getattr(x, "__test__", True):
|
||||||
|
continue
|
||||||
|
funcobj = getimfunc(x)
|
||||||
|
yield TestCaseFunction.from_parent(self, name=name, callobj=funcobj)
|
||||||
|
foundsomething = True
|
||||||
|
|
||||||
|
if not foundsomething:
|
||||||
|
runtest = getattr(self.obj, "runTest", None)
|
||||||
|
if runtest is not None:
|
||||||
|
ut = sys.modules.get("twisted.trial.unittest", None)
|
||||||
|
# Type ignored because `ut` is an opaque module.
|
||||||
|
if ut is None or runtest != ut.TestCase.runTest: # type: ignore
|
||||||
|
yield TestCaseFunction.from_parent(self, name="runTest")
|
||||||
|
|
||||||
|
def _register_unittest_setup_class_fixture(self, cls: type) -> None:
|
||||||
|
"""Register an auto-use fixture to invoke setUpClass and
|
||||||
|
tearDownClass (#517)."""
|
||||||
|
setup = getattr(cls, "setUpClass", None)
|
||||||
|
teardown = getattr(cls, "tearDownClass", None)
|
||||||
|
if setup is None and teardown is None:
|
||||||
|
return None
|
||||||
|
cleanup = getattr(cls, "doClassCleanups", lambda: None)
|
||||||
|
|
||||||
|
def unittest_setup_class_fixture(
|
||||||
|
request: FixtureRequest,
|
||||||
|
) -> Generator[None, None, None]:
|
||||||
|
cls = request.cls
|
||||||
|
if _is_skipped(cls):
|
||||||
|
reason = cls.__unittest_skip_why__
|
||||||
|
raise pytest.skip.Exception(reason, _use_item_location=True)
|
||||||
|
if setup is not None:
|
||||||
|
try:
|
||||||
|
setup()
|
||||||
|
# unittest does not call the cleanup function for every BaseException, so we
|
||||||
|
# follow this here.
|
||||||
|
except Exception:
|
||||||
|
cleanup()
|
||||||
|
raise
|
||||||
|
yield
|
||||||
|
try:
|
||||||
|
if teardown is not None:
|
||||||
|
teardown()
|
||||||
|
finally:
|
||||||
|
cleanup()
|
||||||
|
|
||||||
|
self.session._fixturemanager._register_fixture(
|
||||||
|
# Use a unique name to speed up lookup.
|
||||||
|
name=f"_unittest_setUpClass_fixture_{cls.__qualname__}",
|
||||||
|
func=unittest_setup_class_fixture,
|
||||||
|
nodeid=self.nodeid,
|
||||||
|
scope="class",
|
||||||
|
autouse=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _register_unittest_setup_method_fixture(self, cls: type) -> None:
|
||||||
|
"""Register an auto-use fixture to invoke setup_method and
|
||||||
|
teardown_method (#517)."""
|
||||||
|
setup = getattr(cls, "setup_method", None)
|
||||||
|
teardown = getattr(cls, "teardown_method", None)
|
||||||
|
if setup is None and teardown is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def unittest_setup_method_fixture(
|
||||||
|
request: FixtureRequest,
|
||||||
|
) -> Generator[None, None, None]:
|
||||||
|
self = request.instance
|
||||||
|
if _is_skipped(self):
|
||||||
|
reason = self.__unittest_skip_why__
|
||||||
|
raise pytest.skip.Exception(reason, _use_item_location=True)
|
||||||
|
if setup is not None:
|
||||||
|
setup(self, request.function)
|
||||||
|
yield
|
||||||
|
if teardown is not None:
|
||||||
|
teardown(self, request.function)
|
||||||
|
|
||||||
|
self.session._fixturemanager._register_fixture(
|
||||||
|
# Use a unique name to speed up lookup.
|
||||||
|
name=f"_unittest_setup_method_fixture_{cls.__qualname__}",
|
||||||
|
func=unittest_setup_method_fixture,
|
||||||
|
nodeid=self.nodeid,
|
||||||
|
scope="function",
|
||||||
|
autouse=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestCaseFunction(Function):
|
||||||
|
nofuncargs = True
|
||||||
|
_excinfo: Optional[List[_pytest._code.ExceptionInfo[BaseException]]] = None
|
||||||
|
_testcase: Optional["unittest.TestCase"] = None
|
||||||
|
|
||||||
|
def _getobj(self):
|
||||||
|
assert self.parent is not None
|
||||||
|
# Unlike a regular Function in a Class, where `item.obj` returns
|
||||||
|
# a *bound* method (attached to an instance), TestCaseFunction's
|
||||||
|
# `obj` returns an *unbound* method (not attached to an instance).
|
||||||
|
# This inconsistency is probably not desirable, but needs some
|
||||||
|
# consideration before changing.
|
||||||
|
return getattr(self.parent.obj, self.originalname) # type: ignore[attr-defined]
|
||||||
|
|
||||||
|
def setup(self) -> None:
|
||||||
|
# A bound method to be called during teardown() if set (see 'runtest()').
|
||||||
|
self._explicit_tearDown: Optional[Callable[[], None]] = None
|
||||||
|
assert self.parent is not None
|
||||||
|
self._testcase = self.parent.obj(self.name) # type: ignore[attr-defined]
|
||||||
|
self._obj = getattr(self._testcase, self.name)
|
||||||
|
super().setup()
|
||||||
|
|
||||||
|
def teardown(self) -> None:
|
||||||
|
super().teardown()
|
||||||
|
if self._explicit_tearDown is not None:
|
||||||
|
self._explicit_tearDown()
|
||||||
|
self._explicit_tearDown = None
|
||||||
|
self._testcase = None
|
||||||
|
self._obj = None
|
||||||
|
|
||||||
|
def startTest(self, testcase: "unittest.TestCase") -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _addexcinfo(self, rawexcinfo: "_SysExcInfoType") -> None:
|
||||||
|
# Unwrap potential exception info (see twisted trial support below).
|
||||||
|
rawexcinfo = getattr(rawexcinfo, "_rawexcinfo", rawexcinfo)
|
||||||
|
try:
|
||||||
|
excinfo = _pytest._code.ExceptionInfo[BaseException].from_exc_info(
|
||||||
|
rawexcinfo # type: ignore[arg-type]
|
||||||
|
)
|
||||||
|
# Invoke the attributes to trigger storing the traceback
|
||||||
|
# trial causes some issue there.
|
||||||
|
_ = excinfo.value
|
||||||
|
_ = excinfo.traceback
|
||||||
|
except TypeError:
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
values = traceback.format_exception(*rawexcinfo)
|
||||||
|
values.insert(
|
||||||
|
0,
|
||||||
|
"NOTE: Incompatible Exception Representation, "
|
||||||
|
"displaying natively:\n\n",
|
||||||
|
)
|
||||||
|
fail("".join(values), pytrace=False)
|
||||||
|
except (fail.Exception, KeyboardInterrupt):
|
||||||
|
raise
|
||||||
|
except BaseException:
|
||||||
|
fail(
|
||||||
|
"ERROR: Unknown Incompatible Exception "
|
||||||
|
f"representation:\n{rawexcinfo!r}",
|
||||||
|
pytrace=False,
|
||||||
|
)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
raise
|
||||||
|
except fail.Exception:
|
||||||
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
||||||
|
self.__dict__.setdefault("_excinfo", []).append(excinfo)
|
||||||
|
|
||||||
|
def addError(
|
||||||
|
self, testcase: "unittest.TestCase", rawexcinfo: "_SysExcInfoType"
|
||||||
|
) -> None:
|
||||||
|
try:
|
||||||
|
if isinstance(rawexcinfo[1], exit.Exception):
|
||||||
|
exit(rawexcinfo[1].msg)
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
|
self._addexcinfo(rawexcinfo)
|
||||||
|
|
||||||
|
def addFailure(
|
||||||
|
self, testcase: "unittest.TestCase", rawexcinfo: "_SysExcInfoType"
|
||||||
|
) -> None:
|
||||||
|
self._addexcinfo(rawexcinfo)
|
||||||
|
|
||||||
|
def addSkip(self, testcase: "unittest.TestCase", reason: str) -> None:
|
||||||
|
try:
|
||||||
|
raise pytest.skip.Exception(reason, _use_item_location=True)
|
||||||
|
except skip.Exception:
|
||||||
|
self._addexcinfo(sys.exc_info())
|
||||||
|
|
||||||
|
def addExpectedFailure(
|
||||||
|
self,
|
||||||
|
testcase: "unittest.TestCase",
|
||||||
|
rawexcinfo: "_SysExcInfoType",
|
||||||
|
reason: str = "",
|
||||||
|
) -> None:
|
||||||
|
try:
|
||||||
|
xfail(str(reason))
|
||||||
|
except xfail.Exception:
|
||||||
|
self._addexcinfo(sys.exc_info())
|
||||||
|
|
||||||
|
def addUnexpectedSuccess(
|
||||||
|
self,
|
||||||
|
testcase: "unittest.TestCase",
|
||||||
|
reason: Optional["twisted.trial.unittest.Todo"] = None,
|
||||||
|
) -> None:
|
||||||
|
msg = "Unexpected success"
|
||||||
|
if reason:
|
||||||
|
msg += f": {reason.reason}"
|
||||||
|
# Preserve unittest behaviour - fail the test. Explicitly not an XPASS.
|
||||||
|
try:
|
||||||
|
fail(msg, pytrace=False)
|
||||||
|
except fail.Exception:
|
||||||
|
self._addexcinfo(sys.exc_info())
|
||||||
|
|
||||||
|
def addSuccess(self, testcase: "unittest.TestCase") -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def stopTest(self, testcase: "unittest.TestCase") -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def addDuration(self, testcase: "unittest.TestCase", elapsed: float) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def runtest(self) -> None:
|
||||||
|
from _pytest.debugging import maybe_wrap_pytest_function_for_tracing
|
||||||
|
|
||||||
|
assert self._testcase is not None
|
||||||
|
|
||||||
|
maybe_wrap_pytest_function_for_tracing(self)
|
||||||
|
|
||||||
|
# Let the unittest framework handle async functions.
|
||||||
|
if is_async_function(self.obj):
|
||||||
|
# Type ignored because self acts as the TestResult, but is not actually one.
|
||||||
|
self._testcase(result=self) # type: ignore[arg-type]
|
||||||
|
else:
|
||||||
|
# When --pdb is given, we want to postpone calling tearDown() otherwise
|
||||||
|
# when entering the pdb prompt, tearDown() would have probably cleaned up
|
||||||
|
# instance variables, which makes it difficult to debug.
|
||||||
|
# Arguably we could always postpone tearDown(), but this changes the moment where the
|
||||||
|
# TestCase instance interacts with the results object, so better to only do it
|
||||||
|
# when absolutely needed.
|
||||||
|
# We need to consider if the test itself is skipped, or the whole class.
|
||||||
|
assert isinstance(self.parent, UnitTestCase)
|
||||||
|
skipped = _is_skipped(self.obj) or _is_skipped(self.parent.obj)
|
||||||
|
if self.config.getoption("usepdb") and not skipped:
|
||||||
|
self._explicit_tearDown = self._testcase.tearDown
|
||||||
|
setattr(self._testcase, "tearDown", lambda *args: None)
|
||||||
|
|
||||||
|
# We need to update the actual bound method with self.obj, because
|
||||||
|
# wrap_pytest_function_for_tracing replaces self.obj by a wrapper.
|
||||||
|
setattr(self._testcase, self.name, self.obj)
|
||||||
|
try:
|
||||||
|
self._testcase(result=self) # type: ignore[arg-type]
|
||||||
|
finally:
|
||||||
|
delattr(self._testcase, self.name)
|
||||||
|
|
||||||
|
def _traceback_filter(
|
||||||
|
self, excinfo: _pytest._code.ExceptionInfo[BaseException]
|
||||||
|
) -> _pytest._code.Traceback:
|
||||||
|
traceback = super()._traceback_filter(excinfo)
|
||||||
|
ntraceback = traceback.filter(
|
||||||
|
lambda x: not x.frame.f_globals.get("__unittest"),
|
||||||
|
)
|
||||||
|
if not ntraceback:
|
||||||
|
ntraceback = traceback
|
||||||
|
return ntraceback
|
||||||
|
|
||||||
|
|
||||||
|
@hookimpl(tryfirst=True)
|
||||||
|
def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> None:
|
||||||
|
if isinstance(item, TestCaseFunction):
|
||||||
|
if item._excinfo:
|
||||||
|
call.excinfo = item._excinfo.pop(0)
|
||||||
|
try:
|
||||||
|
del call.result
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Convert unittest.SkipTest to pytest.skip.
|
||||||
|
# This is actually only needed for nose, which reuses unittest.SkipTest for
|
||||||
|
# its own nose.SkipTest. For unittest TestCases, SkipTest is already
|
||||||
|
# handled internally, and doesn't reach here.
|
||||||
|
unittest = sys.modules.get("unittest")
|
||||||
|
if (
|
||||||
|
unittest and call.excinfo and isinstance(call.excinfo.value, unittest.SkipTest) # type: ignore[attr-defined]
|
||||||
|
):
|
||||||
|
excinfo = call.excinfo
|
||||||
|
call2 = CallInfo[None].from_call(
|
||||||
|
lambda: pytest.skip(str(excinfo.value)), call.when
|
||||||
|
)
|
||||||
|
call.excinfo = call2.excinfo
|
||||||
|
|
||||||
|
|
||||||
|
# Twisted trial support.
|
||||||
|
classImplements_has_run = False
|
||||||
|
|
||||||
|
|
||||||
|
@hookimpl(wrapper=True)
|
||||||
|
def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]:
|
||||||
|
if isinstance(item, TestCaseFunction) and "twisted.trial.unittest" in sys.modules:
|
||||||
|
ut: Any = sys.modules["twisted.python.failure"]
|
||||||
|
global classImplements_has_run
|
||||||
|
Failure__init__ = ut.Failure.__init__
|
||||||
|
if not classImplements_has_run:
|
||||||
|
from twisted.trial.itrial import IReporter
|
||||||
|
from zope.interface import classImplements
|
||||||
|
|
||||||
|
classImplements(TestCaseFunction, IReporter)
|
||||||
|
classImplements_has_run = True
|
||||||
|
|
||||||
|
def excstore(
|
||||||
|
self, exc_value=None, exc_type=None, exc_tb=None, captureVars=None
|
||||||
|
):
|
||||||
|
if exc_value is None:
|
||||||
|
self._rawexcinfo = sys.exc_info()
|
||||||
|
else:
|
||||||
|
if exc_type is None:
|
||||||
|
exc_type = type(exc_value)
|
||||||
|
self._rawexcinfo = (exc_type, exc_value, exc_tb)
|
||||||
|
try:
|
||||||
|
Failure__init__(
|
||||||
|
self, exc_value, exc_type, exc_tb, captureVars=captureVars
|
||||||
|
)
|
||||||
|
except TypeError:
|
||||||
|
Failure__init__(self, exc_value, exc_type, exc_tb)
|
||||||
|
|
||||||
|
ut.Failure.__init__ = excstore
|
||||||
|
try:
|
||||||
|
res = yield
|
||||||
|
finally:
|
||||||
|
ut.Failure.__init__ = Failure__init__
|
||||||
|
else:
|
||||||
|
res = yield
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def _is_skipped(obj) -> bool:
|
||||||
|
"""Return True if the given object has been marked with @unittest.skip."""
|
||||||
|
return bool(getattr(obj, "__unittest_skip__", False))
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
from types import TracebackType
|
||||||
|
from typing import Any
|
||||||
|
from typing import Callable
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Type
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
# Copied from cpython/Lib/test/support/__init__.py, with modifications.
|
||||||
|
class catch_unraisable_exception:
|
||||||
|
"""Context manager catching unraisable exception using sys.unraisablehook.
|
||||||
|
|
||||||
|
Storing the exception value (cm.unraisable.exc_value) creates a reference
|
||||||
|
cycle. The reference cycle is broken explicitly when the context manager
|
||||||
|
exits.
|
||||||
|
|
||||||
|
Storing the object (cm.unraisable.object) can resurrect it if it is set to
|
||||||
|
an object which is being finalized. Exiting the context manager clears the
|
||||||
|
stored object.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
with catch_unraisable_exception() as cm:
|
||||||
|
# code creating an "unraisable exception"
|
||||||
|
...
|
||||||
|
# check the unraisable exception: use cm.unraisable
|
||||||
|
...
|
||||||
|
# cm.unraisable attribute no longer exists at this point
|
||||||
|
# (to break a reference cycle)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.unraisable: Optional["sys.UnraisableHookArgs"] = None
|
||||||
|
self._old_hook: Optional[Callable[["sys.UnraisableHookArgs"], Any]] = None
|
||||||
|
|
||||||
|
def _hook(self, unraisable: "sys.UnraisableHookArgs") -> None:
|
||||||
|
# Storing unraisable.object can resurrect an object which is being
|
||||||
|
# finalized. Storing unraisable.exc_value creates a reference cycle.
|
||||||
|
self.unraisable = unraisable
|
||||||
|
|
||||||
|
def __enter__(self) -> "catch_unraisable_exception":
|
||||||
|
self._old_hook = sys.unraisablehook
|
||||||
|
sys.unraisablehook = self._hook
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(
|
||||||
|
self,
|
||||||
|
exc_type: Optional[Type[BaseException]],
|
||||||
|
exc_val: Optional[BaseException],
|
||||||
|
exc_tb: Optional[TracebackType],
|
||||||
|
) -> None:
|
||||||
|
assert self._old_hook is not None
|
||||||
|
sys.unraisablehook = self._old_hook
|
||||||
|
self._old_hook = None
|
||||||
|
del self.unraisable
|
||||||
|
|
||||||
|
|
||||||
|
def unraisable_exception_runtest_hook() -> Generator[None, None, None]:
|
||||||
|
with catch_unraisable_exception() as cm:
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
if cm.unraisable:
|
||||||
|
if cm.unraisable.err_msg is not None:
|
||||||
|
err_msg = cm.unraisable.err_msg
|
||||||
|
else:
|
||||||
|
err_msg = "Exception ignored in"
|
||||||
|
msg = f"{err_msg}: {cm.unraisable.object!r}\n\n"
|
||||||
|
msg += "".join(
|
||||||
|
traceback.format_exception(
|
||||||
|
cm.unraisable.exc_type,
|
||||||
|
cm.unraisable.exc_value,
|
||||||
|
cm.unraisable.exc_traceback,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_runtest_setup() -> Generator[None, None, None]:
|
||||||
|
yield from unraisable_exception_runtest_hook()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_runtest_call() -> Generator[None, None, None]:
|
||||||
|
yield from unraisable_exception_runtest_hook()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_runtest_teardown() -> Generator[None, None, None]:
|
||||||
|
yield from unraisable_exception_runtest_hook()
|
||||||
165
.venv/lib/python3.10/site-packages/_pytest/warning_types.py
Normal file
165
.venv/lib/python3.10/site-packages/_pytest/warning_types.py
Normal file
|
|
@ -0,0 +1,165 @@
|
||||||
|
import dataclasses
|
||||||
|
import inspect
|
||||||
|
from types import FunctionType
|
||||||
|
from typing import Any
|
||||||
|
from typing import final
|
||||||
|
from typing import Generic
|
||||||
|
from typing import Type
|
||||||
|
from typing import TypeVar
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
|
class PytestWarning(UserWarning):
|
||||||
|
"""Base class for all warnings emitted by pytest."""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class PytestAssertRewriteWarning(PytestWarning):
|
||||||
|
"""Warning emitted by the pytest assert rewrite module."""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class PytestCacheWarning(PytestWarning):
|
||||||
|
"""Warning emitted by the cache plugin in various situations."""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class PytestConfigWarning(PytestWarning):
|
||||||
|
"""Warning emitted for configuration issues."""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class PytestCollectionWarning(PytestWarning):
|
||||||
|
"""Warning emitted when pytest is not able to collect a file or symbol in a module."""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
class PytestDeprecationWarning(PytestWarning, DeprecationWarning):
|
||||||
|
"""Warning class for features that will be removed in a future version."""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
class PytestRemovedIn9Warning(PytestDeprecationWarning):
|
||||||
|
"""Warning class for features that will be removed in pytest 9."""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
class PytestReturnNotNoneWarning(PytestWarning):
|
||||||
|
"""Warning emitted when a test function is returning value other than None."""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class PytestExperimentalApiWarning(PytestWarning, FutureWarning):
|
||||||
|
"""Warning category used to denote experiments in pytest.
|
||||||
|
|
||||||
|
Use sparingly as the API might change or even be removed completely in a
|
||||||
|
future version.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def simple(cls, apiname: str) -> "PytestExperimentalApiWarning":
|
||||||
|
return cls(f"{apiname} is an experimental api that may change over time")
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class PytestUnhandledCoroutineWarning(PytestReturnNotNoneWarning):
|
||||||
|
"""Warning emitted for an unhandled coroutine.
|
||||||
|
|
||||||
|
A coroutine was encountered when collecting test functions, but was not
|
||||||
|
handled by any async-aware plugin.
|
||||||
|
Coroutine test functions are not natively supported.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class PytestUnknownMarkWarning(PytestWarning):
|
||||||
|
"""Warning emitted on use of unknown markers.
|
||||||
|
|
||||||
|
See :ref:`mark` for details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class PytestUnraisableExceptionWarning(PytestWarning):
|
||||||
|
"""An unraisable exception was reported.
|
||||||
|
|
||||||
|
Unraisable exceptions are exceptions raised in :meth:`__del__ <object.__del__>`
|
||||||
|
implementations and similar situations when the exception cannot be raised
|
||||||
|
as normal.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
class PytestUnhandledThreadExceptionWarning(PytestWarning):
|
||||||
|
"""An unhandled exception occurred in a :class:`~threading.Thread`.
|
||||||
|
|
||||||
|
Such exceptions don't propagate normally.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__module__ = "pytest"
|
||||||
|
|
||||||
|
|
||||||
|
_W = TypeVar("_W", bound=PytestWarning)
|
||||||
|
|
||||||
|
|
||||||
|
@final
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class UnformattedWarning(Generic[_W]):
|
||||||
|
"""A warning meant to be formatted during runtime.
|
||||||
|
|
||||||
|
This is used to hold warnings that need to format their message at runtime,
|
||||||
|
as opposed to a direct message.
|
||||||
|
"""
|
||||||
|
|
||||||
|
category: Type["_W"]
|
||||||
|
template: str
|
||||||
|
|
||||||
|
def format(self, **kwargs: Any) -> _W:
|
||||||
|
"""Return an instance of the warning category, formatted with given kwargs."""
|
||||||
|
return self.category(self.template.format(**kwargs))
|
||||||
|
|
||||||
|
|
||||||
|
def warn_explicit_for(method: FunctionType, message: PytestWarning) -> None:
|
||||||
|
"""
|
||||||
|
Issue the warning :param:`message` for the definition of the given :param:`method`
|
||||||
|
|
||||||
|
this helps to log warnings for functions defined prior to finding an issue with them
|
||||||
|
(like hook wrappers being marked in a legacy mechanism)
|
||||||
|
"""
|
||||||
|
lineno = method.__code__.co_firstlineno
|
||||||
|
filename = inspect.getfile(method)
|
||||||
|
module = method.__module__
|
||||||
|
mod_globals = method.__globals__
|
||||||
|
try:
|
||||||
|
warnings.warn_explicit(
|
||||||
|
message,
|
||||||
|
type(message),
|
||||||
|
filename=filename,
|
||||||
|
module=module,
|
||||||
|
registry=mod_globals.setdefault("__warningregistry__", {}),
|
||||||
|
lineno=lineno,
|
||||||
|
)
|
||||||
|
except Warning as w:
|
||||||
|
# If warnings are errors (e.g. -Werror), location information gets lost, so we add it to the message.
|
||||||
|
raise type(w)(f"{w}\n at {filename}:{lineno}") from None
|
||||||
150
.venv/lib/python3.10/site-packages/_pytest/warnings.py
Normal file
150
.venv/lib/python3.10/site-packages/_pytest/warnings.py
Normal file
|
|
@ -0,0 +1,150 @@
|
||||||
|
# mypy: allow-untyped-defs
|
||||||
|
from contextlib import contextmanager
|
||||||
|
import sys
|
||||||
|
from typing import Generator
|
||||||
|
from typing import Literal
|
||||||
|
from typing import Optional
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
from _pytest.config import apply_warning_filters
|
||||||
|
from _pytest.config import Config
|
||||||
|
from _pytest.config import parse_warning_filter
|
||||||
|
from _pytest.main import Session
|
||||||
|
from _pytest.nodes import Item
|
||||||
|
from _pytest.terminal import TerminalReporter
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_configure(config: Config) -> None:
|
||||||
|
config.addinivalue_line(
|
||||||
|
"markers",
|
||||||
|
"filterwarnings(warning): add a warning filter to the given test. "
|
||||||
|
"see https://docs.pytest.org/en/stable/how-to/capture-warnings.html#pytest-mark-filterwarnings ",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def catch_warnings_for_item(
|
||||||
|
config: Config,
|
||||||
|
ihook,
|
||||||
|
when: Literal["config", "collect", "runtest"],
|
||||||
|
item: Optional[Item],
|
||||||
|
) -> Generator[None, None, None]:
|
||||||
|
"""Context manager that catches warnings generated in the contained execution block.
|
||||||
|
|
||||||
|
``item`` can be None if we are not in the context of an item execution.
|
||||||
|
|
||||||
|
Each warning captured triggers the ``pytest_warning_recorded`` hook.
|
||||||
|
"""
|
||||||
|
config_filters = config.getini("filterwarnings")
|
||||||
|
cmdline_filters = config.known_args_namespace.pythonwarnings or []
|
||||||
|
with warnings.catch_warnings(record=True) as log:
|
||||||
|
# mypy can't infer that record=True means log is not None; help it.
|
||||||
|
assert log is not None
|
||||||
|
|
||||||
|
if not sys.warnoptions:
|
||||||
|
# If user is not explicitly configuring warning filters, show deprecation warnings by default (#2908).
|
||||||
|
warnings.filterwarnings("always", category=DeprecationWarning)
|
||||||
|
warnings.filterwarnings("always", category=PendingDeprecationWarning)
|
||||||
|
|
||||||
|
# To be enabled in pytest 9.0.0.
|
||||||
|
# warnings.filterwarnings("error", category=pytest.PytestRemovedIn9Warning)
|
||||||
|
|
||||||
|
apply_warning_filters(config_filters, cmdline_filters)
|
||||||
|
|
||||||
|
# apply filters from "filterwarnings" marks
|
||||||
|
nodeid = "" if item is None else item.nodeid
|
||||||
|
if item is not None:
|
||||||
|
for mark in item.iter_markers(name="filterwarnings"):
|
||||||
|
for arg in mark.args:
|
||||||
|
warnings.filterwarnings(*parse_warning_filter(arg, escape=False))
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
for warning_message in log:
|
||||||
|
ihook.pytest_warning_recorded.call_historic(
|
||||||
|
kwargs=dict(
|
||||||
|
warning_message=warning_message,
|
||||||
|
nodeid=nodeid,
|
||||||
|
when=when,
|
||||||
|
location=None,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def warning_record_to_str(warning_message: warnings.WarningMessage) -> str:
|
||||||
|
"""Convert a warnings.WarningMessage to a string."""
|
||||||
|
warn_msg = warning_message.message
|
||||||
|
msg = warnings.formatwarning(
|
||||||
|
str(warn_msg),
|
||||||
|
warning_message.category,
|
||||||
|
warning_message.filename,
|
||||||
|
warning_message.lineno,
|
||||||
|
warning_message.line,
|
||||||
|
)
|
||||||
|
if warning_message.source is not None:
|
||||||
|
try:
|
||||||
|
import tracemalloc
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
tb = tracemalloc.get_object_traceback(warning_message.source)
|
||||||
|
if tb is not None:
|
||||||
|
formatted_tb = "\n".join(tb.format())
|
||||||
|
# Use a leading new line to better separate the (large) output
|
||||||
|
# from the traceback to the previous warning text.
|
||||||
|
msg += f"\nObject allocated at:\n{formatted_tb}"
|
||||||
|
else:
|
||||||
|
# No need for a leading new line.
|
||||||
|
url = "https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings"
|
||||||
|
msg += "Enable tracemalloc to get traceback where the object was allocated.\n"
|
||||||
|
msg += f"See {url} for more info."
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]:
|
||||||
|
with catch_warnings_for_item(
|
||||||
|
config=item.config, ihook=item.ihook, when="runtest", item=item
|
||||||
|
):
|
||||||
|
return (yield)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True, tryfirst=True)
|
||||||
|
def pytest_collection(session: Session) -> Generator[None, object, object]:
|
||||||
|
config = session.config
|
||||||
|
with catch_warnings_for_item(
|
||||||
|
config=config, ihook=config.hook, when="collect", item=None
|
||||||
|
):
|
||||||
|
return (yield)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True)
|
||||||
|
def pytest_terminal_summary(
|
||||||
|
terminalreporter: TerminalReporter,
|
||||||
|
) -> Generator[None, None, None]:
|
||||||
|
config = terminalreporter.config
|
||||||
|
with catch_warnings_for_item(
|
||||||
|
config=config, ihook=config.hook, when="config", item=None
|
||||||
|
):
|
||||||
|
return (yield)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True)
|
||||||
|
def pytest_sessionfinish(session: Session) -> Generator[None, None, None]:
|
||||||
|
config = session.config
|
||||||
|
with catch_warnings_for_item(
|
||||||
|
config=config, ihook=config.hook, when="config", item=None
|
||||||
|
):
|
||||||
|
return (yield)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(wrapper=True)
|
||||||
|
def pytest_load_initial_conftests(
|
||||||
|
early_config: "Config",
|
||||||
|
) -> Generator[None, None, None]:
|
||||||
|
with catch_warnings_for_item(
|
||||||
|
config=early_config, ihook=early_config.hook, when="config", item=None
|
||||||
|
):
|
||||||
|
return (yield)
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
pip
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
Copyright (c) 2020 Anthony Sottile
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
@ -0,0 +1,227 @@
|
||||||
|
Metadata-Version: 2.1
|
||||||
|
Name: covdefaults
|
||||||
|
Version: 2.3.0
|
||||||
|
Summary: A coverage plugin to provide sensible default settings
|
||||||
|
Home-page: https://github.com/asottile/covdefaults
|
||||||
|
Author: Anthony Sottile
|
||||||
|
Author-email: asottile@umich.edu
|
||||||
|
License: MIT
|
||||||
|
Classifier: License :: OSI Approved :: MIT License
|
||||||
|
Classifier: Programming Language :: Python :: 3
|
||||||
|
Classifier: Programming Language :: Python :: 3 :: Only
|
||||||
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
||||||
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
||||||
|
Requires-Python: >=3.7
|
||||||
|
Description-Content-Type: text/markdown
|
||||||
|
License-File: LICENSE
|
||||||
|
Requires-Dist: coverage (>=6.0.2)
|
||||||
|
|
||||||
|
[](https://github.com/asottile/covdefaults/actions/workflows/main.yml)
|
||||||
|
[](https://results.pre-commit.ci/latest/github/asottile/covdefaults/main)
|
||||||
|
|
||||||
|
covdefaults
|
||||||
|
===========
|
||||||
|
|
||||||
|
A coverage plugin to provide sensible default settings
|
||||||
|
|
||||||
|
## installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install covdefaults
|
||||||
|
```
|
||||||
|
|
||||||
|
## usage
|
||||||
|
|
||||||
|
to enable the plugin, add `covdefaults` to your coverage plugins
|
||||||
|
|
||||||
|
in `.coveragerc`:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[run]
|
||||||
|
plugins = covdefaults
|
||||||
|
```
|
||||||
|
|
||||||
|
in `setup.cfg` / `tox.ini`:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[coverage:run]
|
||||||
|
plugins = covdefaults
|
||||||
|
```
|
||||||
|
|
||||||
|
in `pyproject.toml`:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[tool.coverage.run]
|
||||||
|
plugins = ["covdefaults"]
|
||||||
|
```
|
||||||
|
|
||||||
|
## default settings
|
||||||
|
|
||||||
|
### `[coverage:run]`
|
||||||
|
|
||||||
|
```ini
|
||||||
|
branch = True
|
||||||
|
source = .
|
||||||
|
omit =
|
||||||
|
*/__main__.py
|
||||||
|
*/setup.py
|
||||||
|
```
|
||||||
|
|
||||||
|
### `[coverage:report]`
|
||||||
|
|
||||||
|
```ini
|
||||||
|
show_missing = True
|
||||||
|
skip_covered = True
|
||||||
|
fail_under = 100
|
||||||
|
exclude_lines =
|
||||||
|
# a more strict default pragma
|
||||||
|
\# pragma: no cover\b
|
||||||
|
|
||||||
|
# allow defensive code
|
||||||
|
^\s*raise AssertionError\b
|
||||||
|
^\s*raise NotImplementedError\b
|
||||||
|
^\s*return NotImplemented\b
|
||||||
|
^\s*raise$
|
||||||
|
|
||||||
|
# typing-related code
|
||||||
|
^\s*if (False|TYPE_CHECKING):
|
||||||
|
: \.\.\.(\s*#.*)?$
|
||||||
|
^ +\.\.\.$
|
||||||
|
-> ['"]?NoReturn['"]?:
|
||||||
|
|
||||||
|
# non-runnable code
|
||||||
|
if __name__ == ['"]__main__['"]:$
|
||||||
|
|
||||||
|
# additional platform related pragmas (see below)
|
||||||
|
# additional version related pragmas (see below)
|
||||||
|
partial_branches =
|
||||||
|
# a more strict default pragma
|
||||||
|
\# pragma: no cover\b
|
||||||
|
|
||||||
|
# out platform pragmas
|
||||||
|
\# pragma: (nt|posix|cygwin|darwin|linux|msys|win32|cpython|pypy) (no )?cover\b
|
||||||
|
|
||||||
|
# our version pragmas
|
||||||
|
\# pragma: (>=?|<=?|==|!=)\d+\.\d+ cover\b
|
||||||
|
```
|
||||||
|
|
||||||
|
### platform specific `# pragma: no cover`
|
||||||
|
|
||||||
|
several `# pragma: no cover` tags will be added automatically based on the
|
||||||
|
platform and implementation.
|
||||||
|
|
||||||
|
these will be in the form of:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# pragma: TAG no cover
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```python
|
||||||
|
# pragma: TAG cover
|
||||||
|
```
|
||||||
|
|
||||||
|
these tags will be generated by the following values:
|
||||||
|
|
||||||
|
- `os.name`
|
||||||
|
- `nt` (windows)
|
||||||
|
- `posix` (linux, macOs, cygwin, etc.)
|
||||||
|
- `sys.platform`
|
||||||
|
- `cygwin`
|
||||||
|
- `darwin` (macOs)
|
||||||
|
- `linux`
|
||||||
|
- `msys`
|
||||||
|
- `win32`
|
||||||
|
- `sys.implementation.name`
|
||||||
|
- `cpython`
|
||||||
|
- `pypy`
|
||||||
|
|
||||||
|
for every tag which does not match, you can use negation. here's an example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
if sys.platform == 'win32': # pragma: win32 cover
|
||||||
|
bin_dir = 'Scripts'
|
||||||
|
else: # pragma: win32 no cover
|
||||||
|
bin_dir = 'bin'
|
||||||
|
```
|
||||||
|
|
||||||
|
note here that `# pragma: win32 cover` will become a "no cover" for everything
|
||||||
|
which is not `win32` -- whereas the `# pragma: win32 no cover` will be a
|
||||||
|
"no cover" only on `win32`.
|
||||||
|
|
||||||
|
### version specific `# pragma: no cover`
|
||||||
|
|
||||||
|
several `# pragma: no cover` tags will be added automatically based on the
|
||||||
|
platform and implementation.
|
||||||
|
|
||||||
|
these will be in the form of:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# pragma: >=#.# cover
|
||||||
|
```
|
||||||
|
|
||||||
|
where the comparison operator is one of `>`, `>=`, `<`, `<=`, `==`, `!=`
|
||||||
|
|
||||||
|
for example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
if sys.version_info >= (3, 9): # pragma: >=3.9 cover
|
||||||
|
print('3.9+')
|
||||||
|
else: # pragma: <3.9 cover
|
||||||
|
print('old')
|
||||||
|
```
|
||||||
|
|
||||||
|
### overriding options
|
||||||
|
|
||||||
|
several of the options can be overridden / extended in your coverage
|
||||||
|
configuration. the examples below assume `.coveragerc` however any of the
|
||||||
|
files `coverage` supports work as well.
|
||||||
|
|
||||||
|
#### `run:omit`
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[run]
|
||||||
|
omit =
|
||||||
|
pre_commit/resources/*
|
||||||
|
```
|
||||||
|
|
||||||
|
this will result in the `pre_commit/resources/*` being `omit`ted in addition
|
||||||
|
to the defaults provided by `covdefaults`.
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[covdefaults]
|
||||||
|
subtract_omit = */__main__.py
|
||||||
|
```
|
||||||
|
|
||||||
|
this will result in `*/__main__.py` not being `omit`ted (`*/__main__.py` is
|
||||||
|
among the defaults provided by `covdefaults`).
|
||||||
|
|
||||||
|
#### `run:source`
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[run]
|
||||||
|
source = $PWD
|
||||||
|
```
|
||||||
|
|
||||||
|
covdefaults will not override this value to `.` if it is set manually.
|
||||||
|
|
||||||
|
#### `report:exclude_lines`
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[report]
|
||||||
|
exclude_lines =
|
||||||
|
^if MYPY:$
|
||||||
|
```
|
||||||
|
|
||||||
|
this will result in lines matching `^if MYPY:$` to additionally be excluded
|
||||||
|
from coverage in addition to the defaults provided by `covdefaults`.
|
||||||
|
|
||||||
|
#### `report:fail_under`
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[report]
|
||||||
|
fail_under = 90
|
||||||
|
```
|
||||||
|
|
||||||
|
`covdefaults` will not change the value if you provide one for `fail_under`
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
__pycache__/covdefaults.cpython-310.pyc,,
|
||||||
|
covdefaults-2.3.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
||||||
|
covdefaults-2.3.0.dist-info/LICENSE,sha256=G7eRzPLM_d2UgMXIdA_IFEHOfnzGLeG6LnX6pdL1UBg,1059
|
||||||
|
covdefaults-2.3.0.dist-info/METADATA,sha256=habzHBR6PW9m_XxT7LBDoqa3Cm-ah6NKXAhvELeUmJE,4864
|
||||||
|
covdefaults-2.3.0.dist-info/RECORD,,
|
||||||
|
covdefaults-2.3.0.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
||||||
|
covdefaults-2.3.0.dist-info/WHEEL,sha256=z9j0xAa_JmUKMpmz72K0ZGALSM_n-wQVmGbleXx2VHg,110
|
||||||
|
covdefaults-2.3.0.dist-info/top_level.txt,sha256=dHv1JuKY17KaQLx-yleE1RzyT4n8R2vZhoW1zzwDB8w,12
|
||||||
|
covdefaults.py,sha256=ENAAm8fewxb4FB5vwh6resKCtCn7uZta8STOKDnByRU,4423
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
Wheel-Version: 1.0
|
||||||
|
Generator: bdist_wheel (0.37.1)
|
||||||
|
Root-Is-Purelib: true
|
||||||
|
Tag: py2-none-any
|
||||||
|
Tag: py3-none-any
|
||||||
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
covdefaults
|
||||||
151
.venv/lib/python3.10/site-packages/covdefaults.py
Normal file
151
.venv/lib/python3.10/site-packages/covdefaults.py
Normal file
|
|
@ -0,0 +1,151 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from coverage import CoveragePlugin
|
||||||
|
from coverage.config import CoverageConfig
|
||||||
|
from coverage.config import DEFAULT_EXCLUDE
|
||||||
|
from coverage.plugin_support import Plugins
|
||||||
|
|
||||||
|
_ALL = (
|
||||||
|
# os.name
|
||||||
|
'nt', 'posix',
|
||||||
|
# sys.platform
|
||||||
|
'cygwin', 'darwin', 'linux', 'msys', 'win32',
|
||||||
|
# sys.implementation.name
|
||||||
|
'cpython', 'pypy',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _plat_impl_pragmas() -> list[str]:
|
||||||
|
tags = {os.name, sys.platform, sys.implementation.name}
|
||||||
|
ret = [fr'# pragma: {tag} cover\b' for tag in _ALL if tag not in tags]
|
||||||
|
ret.extend(fr'# pragma: {tag} no cover\b' for tag in tags)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def _lt(n: int) -> str:
|
||||||
|
n_s = str(n)
|
||||||
|
digit = r'\d'
|
||||||
|
|
||||||
|
parts = [
|
||||||
|
f'{n_s[:i]}[0-{int(n_s[i]) - 1}]{len(n_s[i + 1:]) * digit}'
|
||||||
|
for i in range(len(n_s))
|
||||||
|
if n_s[i] != '0'
|
||||||
|
]
|
||||||
|
if len(n_s) > 1:
|
||||||
|
parts.append(f'{digit}{{1,{len(n_s) - 1}}}')
|
||||||
|
|
||||||
|
return f'({"|".join(parts)})'
|
||||||
|
|
||||||
|
|
||||||
|
def _gt(n: int) -> str:
|
||||||
|
n_s = str(n)
|
||||||
|
digit = r'\d'
|
||||||
|
|
||||||
|
parts = [
|
||||||
|
f'{n_s[:i]}[{int(n_s[i]) + 1}-9]{len(n_s[i + 1:]) * digit}'
|
||||||
|
for i in range(len(n_s))
|
||||||
|
if n_s[i] != '9'
|
||||||
|
]
|
||||||
|
parts.append(f'{digit}{{{len(n_s) + 1},}}')
|
||||||
|
|
||||||
|
return f'({"|".join(parts)})'
|
||||||
|
|
||||||
|
|
||||||
|
def _version_pragmas(
|
||||||
|
major: int = sys.version_info[0],
|
||||||
|
minor: int = sys.version_info[1],
|
||||||
|
) -> list[str]:
|
||||||
|
return [
|
||||||
|
# <
|
||||||
|
fr'# pragma: <=?{_lt(major)}\.\d+ cover\b',
|
||||||
|
fr'# pragma: <=?{major}\.{_lt(minor)} cover\b',
|
||||||
|
fr'# pragma: <{major}\.{minor} cover\b',
|
||||||
|
# >
|
||||||
|
fr'# pragma: >=?{_gt(major)}\.\d+ cover\b',
|
||||||
|
fr'# pragma: >=?{major}\.{_gt(minor)} cover\b',
|
||||||
|
fr'# pragma: >{major}\.{minor} cover\b',
|
||||||
|
# != / ==
|
||||||
|
fr'# pragma: !={major}\.{minor} cover\b',
|
||||||
|
fr'# pragma: ==(?!{major}\.{minor})\d+\.\d+ cover\b',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
OPTIONS: tuple[tuple[str, Any], ...] = (
|
||||||
|
('run:branch', True),
|
||||||
|
|
||||||
|
('report:show_missing', True),
|
||||||
|
('report:skip_covered', True),
|
||||||
|
)
|
||||||
|
EXTEND = (
|
||||||
|
('run:omit', ['*/__main__.py', '*/setup.py']),
|
||||||
|
(
|
||||||
|
'report:exclude_lines',
|
||||||
|
[
|
||||||
|
# a more strict default pragma
|
||||||
|
r'# pragma: no cover\b',
|
||||||
|
# allow defensive code
|
||||||
|
r'^\s*raise AssertionError\b',
|
||||||
|
r'^\s*raise NotImplementedError\b',
|
||||||
|
r'^\s*return NotImplemented\b',
|
||||||
|
r'^\s*raise$',
|
||||||
|
# typing-related code
|
||||||
|
r'^\s*if (False|TYPE_CHECKING):',
|
||||||
|
r': \.\.\.(\s*#.*)?$',
|
||||||
|
r'^ +\.\.\.$',
|
||||||
|
r'-> [\'"]?NoReturn[\'"]?:',
|
||||||
|
r'^\s*assert_never\b',
|
||||||
|
# non-runnable code
|
||||||
|
r'^if __name__ == [\'"]__main__[\'"]:$',
|
||||||
|
*_plat_impl_pragmas(),
|
||||||
|
*_version_pragmas(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
'report:partial_branches',
|
||||||
|
[
|
||||||
|
r'# pragma: no branch\b',
|
||||||
|
# platform specific no cover
|
||||||
|
fr'# pragma: ({"|".join(_ALL)}) (no )?cover\b',
|
||||||
|
# version specific no cover
|
||||||
|
r'# pragma: (>=?|<=?|==|!=)\d+\.\d+ cover\b',
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CovDefaults(CoveragePlugin):
|
||||||
|
def __init__(self, subtract_omit: str = '') -> None:
|
||||||
|
self._subtract_omit = subtract_omit.split()
|
||||||
|
|
||||||
|
def configure(self, config: CoverageConfig) -> None:
|
||||||
|
for k, v in OPTIONS:
|
||||||
|
config.set_option(k, v)
|
||||||
|
if config.get_option('run:source') is None:
|
||||||
|
config.set_option('run:source', ['.'])
|
||||||
|
for k, v in EXTEND:
|
||||||
|
before = set(config.get_option(k) or ())
|
||||||
|
before.update(v)
|
||||||
|
config.set_option(k, sorted(before))
|
||||||
|
|
||||||
|
# subtract omit settings if requested
|
||||||
|
if self._subtract_omit:
|
||||||
|
omit = set(config.get_option('run:omit'))
|
||||||
|
omit.difference_update(self._subtract_omit)
|
||||||
|
config.set_option('run:omit', sorted(omit))
|
||||||
|
|
||||||
|
# remove DEFAULT_EXCLUDE, we add a more-strict casing
|
||||||
|
exclude = set(config.get_option('report:exclude_lines'))
|
||||||
|
exclude.difference_update(DEFAULT_EXCLUDE)
|
||||||
|
config.set_option('report:exclude_lines', sorted(exclude))
|
||||||
|
|
||||||
|
# fail_under: if they specify a value then honor it
|
||||||
|
if not config.get_option('report:fail_under'):
|
||||||
|
config.set_option('report:fail_under', 100)
|
||||||
|
|
||||||
|
|
||||||
|
def coverage_init(reg: Plugins, options: dict[str, str]) -> None:
|
||||||
|
reg.add_configurer(CovDefaults(**options))
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
pip
|
||||||
|
|
@ -0,0 +1,177 @@
|
||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
@ -0,0 +1,197 @@
|
||||||
|
Metadata-Version: 2.1
|
||||||
|
Name: coverage
|
||||||
|
Version: 7.4.4
|
||||||
|
Summary: Code coverage measurement for Python
|
||||||
|
Home-page: https://github.com/nedbat/coveragepy
|
||||||
|
Author: Ned Batchelder and 224 others
|
||||||
|
Author-email: ned@nedbatchelder.com
|
||||||
|
License: Apache-2.0
|
||||||
|
Project-URL: Documentation, https://coverage.readthedocs.io/en/7.4.4
|
||||||
|
Project-URL: Funding, https://tidelift.com/subscription/pkg/pypi-coverage?utm_source=pypi-coverage&utm_medium=referral&utm_campaign=pypi
|
||||||
|
Project-URL: Issues, https://github.com/nedbat/coveragepy/issues
|
||||||
|
Project-URL: Mastodon, https://hachyderm.io/@coveragepy
|
||||||
|
Project-URL: Mastodon (nedbat), https://hachyderm.io/@nedbat
|
||||||
|
Keywords: code coverage testing
|
||||||
|
Classifier: Environment :: Console
|
||||||
|
Classifier: Intended Audience :: Developers
|
||||||
|
Classifier: License :: OSI Approved :: Apache Software License
|
||||||
|
Classifier: Operating System :: OS Independent
|
||||||
|
Classifier: Programming Language :: Python
|
||||||
|
Classifier: Programming Language :: Python :: 3
|
||||||
|
Classifier: Programming Language :: Python :: 3.8
|
||||||
|
Classifier: Programming Language :: Python :: 3.9
|
||||||
|
Classifier: Programming Language :: Python :: 3.10
|
||||||
|
Classifier: Programming Language :: Python :: 3.11
|
||||||
|
Classifier: Programming Language :: Python :: 3.12
|
||||||
|
Classifier: Programming Language :: Python :: 3.13
|
||||||
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
||||||
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
||||||
|
Classifier: Topic :: Software Development :: Quality Assurance
|
||||||
|
Classifier: Topic :: Software Development :: Testing
|
||||||
|
Classifier: Development Status :: 5 - Production/Stable
|
||||||
|
Requires-Python: >=3.8
|
||||||
|
Description-Content-Type: text/x-rst
|
||||||
|
License-File: LICENSE.txt
|
||||||
|
Provides-Extra: toml
|
||||||
|
Requires-Dist: tomli ; (python_full_version <= "3.11.0a6") and extra == 'toml'
|
||||||
|
|
||||||
|
.. Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
.. For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
|
||||||
|
|
||||||
|
===========
|
||||||
|
Coverage.py
|
||||||
|
===========
|
||||||
|
|
||||||
|
Code coverage testing for Python.
|
||||||
|
|
||||||
|
.. image:: https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg
|
||||||
|
:target: https://vshymanskyy.github.io/StandWithUkraine
|
||||||
|
:alt: Stand with Ukraine
|
||||||
|
|
||||||
|
-------------
|
||||||
|
|
||||||
|
| |kit| |license| |versions|
|
||||||
|
| |test-status| |quality-status| |docs| |metacov|
|
||||||
|
| |tidelift| |sponsor| |stars| |mastodon-coveragepy| |mastodon-nedbat|
|
||||||
|
|
||||||
|
Coverage.py measures code coverage, typically during test execution. It uses
|
||||||
|
the code analysis tools and tracing hooks provided in the Python standard
|
||||||
|
library to determine which lines are executable, and which have been executed.
|
||||||
|
|
||||||
|
Coverage.py runs on these versions of Python:
|
||||||
|
|
||||||
|
.. PYVERSIONS
|
||||||
|
|
||||||
|
* Python 3.8 through 3.12, and 3.13.0a3 and up.
|
||||||
|
* PyPy3 versions 3.8 through 3.10.
|
||||||
|
|
||||||
|
Documentation is on `Read the Docs`_. Code repository and issue tracker are on
|
||||||
|
`GitHub`_.
|
||||||
|
|
||||||
|
.. _Read the Docs: https://coverage.readthedocs.io/en/7.4.4/
|
||||||
|
.. _GitHub: https://github.com/nedbat/coveragepy
|
||||||
|
|
||||||
|
**New in 7.x:**
|
||||||
|
experimental support for sys.monitoring;
|
||||||
|
dropped support for Python 3.7;
|
||||||
|
added ``Coverage.collect()`` context manager;
|
||||||
|
improved data combining;
|
||||||
|
``[run] exclude_also`` setting;
|
||||||
|
``report --format=``;
|
||||||
|
type annotations.
|
||||||
|
|
||||||
|
**New in 6.x:**
|
||||||
|
dropped support for Python 2.7, 3.5, and 3.6;
|
||||||
|
write data on SIGTERM;
|
||||||
|
added support for 3.10 match/case statements.
|
||||||
|
|
||||||
|
|
||||||
|
For Enterprise
|
||||||
|
--------------
|
||||||
|
|
||||||
|
.. |tideliftlogo| image:: https://nedbatchelder.com/pix/Tidelift_Logo_small.png
|
||||||
|
:alt: Tidelift
|
||||||
|
:target: https://tidelift.com/subscription/pkg/pypi-coverage?utm_source=pypi-coverage&utm_medium=referral&utm_campaign=readme
|
||||||
|
|
||||||
|
.. list-table::
|
||||||
|
:widths: 10 100
|
||||||
|
|
||||||
|
* - |tideliftlogo|
|
||||||
|
- `Available as part of the Tidelift Subscription. <https://tidelift.com/subscription/pkg/pypi-coverage?utm_source=pypi-coverage&utm_medium=referral&utm_campaign=readme>`_
|
||||||
|
Coverage and thousands of other packages are working with
|
||||||
|
Tidelift to deliver one enterprise subscription that covers all of the open
|
||||||
|
source you use. If you want the flexibility of open source and the confidence
|
||||||
|
of commercial-grade software, this is for you.
|
||||||
|
`Learn more. <https://tidelift.com/subscription/pkg/pypi-coverage?utm_source=pypi-coverage&utm_medium=referral&utm_campaign=readme>`_
|
||||||
|
|
||||||
|
|
||||||
|
Getting Started
|
||||||
|
---------------
|
||||||
|
|
||||||
|
Looking to run ``coverage`` on your test suite? See the `Quick Start section`_
|
||||||
|
of the docs.
|
||||||
|
|
||||||
|
.. _Quick Start section: https://coverage.readthedocs.io/en/7.4.4/#quick-start
|
||||||
|
|
||||||
|
|
||||||
|
Change history
|
||||||
|
--------------
|
||||||
|
|
||||||
|
The complete history of changes is on the `change history page`_.
|
||||||
|
|
||||||
|
.. _change history page: https://coverage.readthedocs.io/en/7.4.4/changes.html
|
||||||
|
|
||||||
|
|
||||||
|
Code of Conduct
|
||||||
|
---------------
|
||||||
|
|
||||||
|
Everyone participating in the coverage.py project is expected to treat other
|
||||||
|
people with respect and to follow the guidelines articulated in the `Python
|
||||||
|
Community Code of Conduct`_.
|
||||||
|
|
||||||
|
.. _Python Community Code of Conduct: https://www.python.org/psf/codeofconduct/
|
||||||
|
|
||||||
|
|
||||||
|
Contributing
|
||||||
|
------------
|
||||||
|
|
||||||
|
Found a bug? Want to help improve the code or documentation? See the
|
||||||
|
`Contributing section`_ of the docs.
|
||||||
|
|
||||||
|
.. _Contributing section: https://coverage.readthedocs.io/en/7.4.4/contributing.html
|
||||||
|
|
||||||
|
|
||||||
|
Security
|
||||||
|
--------
|
||||||
|
|
||||||
|
To report a security vulnerability, please use the `Tidelift security
|
||||||
|
contact`_. Tidelift will coordinate the fix and disclosure.
|
||||||
|
|
||||||
|
.. _Tidelift security contact: https://tidelift.com/security
|
||||||
|
|
||||||
|
|
||||||
|
License
|
||||||
|
-------
|
||||||
|
|
||||||
|
Licensed under the `Apache 2.0 License`_. For details, see `NOTICE.txt`_.
|
||||||
|
|
||||||
|
.. _Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
.. _NOTICE.txt: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
|
||||||
|
|
||||||
|
|
||||||
|
.. |test-status| image:: https://github.com/nedbat/coveragepy/actions/workflows/testsuite.yml/badge.svg?branch=master&event=push
|
||||||
|
:target: https://github.com/nedbat/coveragepy/actions/workflows/testsuite.yml
|
||||||
|
:alt: Test suite status
|
||||||
|
.. |quality-status| image:: https://github.com/nedbat/coveragepy/actions/workflows/quality.yml/badge.svg?branch=master&event=push
|
||||||
|
:target: https://github.com/nedbat/coveragepy/actions/workflows/quality.yml
|
||||||
|
:alt: Quality check status
|
||||||
|
.. |docs| image:: https://readthedocs.org/projects/coverage/badge/?version=latest&style=flat
|
||||||
|
:target: https://coverage.readthedocs.io/en/7.4.4/
|
||||||
|
:alt: Documentation
|
||||||
|
.. |kit| image:: https://img.shields.io/pypi/v/coverage
|
||||||
|
:target: https://pypi.org/project/coverage/
|
||||||
|
:alt: PyPI status
|
||||||
|
.. |versions| image:: https://img.shields.io/pypi/pyversions/coverage.svg?logo=python&logoColor=FBE072
|
||||||
|
:target: https://pypi.org/project/coverage/
|
||||||
|
:alt: Python versions supported
|
||||||
|
.. |license| image:: https://img.shields.io/pypi/l/coverage.svg
|
||||||
|
:target: https://pypi.org/project/coverage/
|
||||||
|
:alt: License
|
||||||
|
.. |metacov| image:: https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/nedbat/8c6980f77988a327348f9b02bbaf67f5/raw/metacov.json
|
||||||
|
:target: https://nedbat.github.io/coverage-reports/latest.html
|
||||||
|
:alt: Coverage reports
|
||||||
|
.. |tidelift| image:: https://tidelift.com/badges/package/pypi/coverage
|
||||||
|
:target: https://tidelift.com/subscription/pkg/pypi-coverage?utm_source=pypi-coverage&utm_medium=referral&utm_campaign=readme
|
||||||
|
:alt: Tidelift
|
||||||
|
.. |stars| image:: https://img.shields.io/github/stars/nedbat/coveragepy.svg?logo=github
|
||||||
|
:target: https://github.com/nedbat/coveragepy/stargazers
|
||||||
|
:alt: GitHub stars
|
||||||
|
.. |mastodon-nedbat| image:: https://img.shields.io/badge/dynamic/json?style=flat&labelColor=450657&logo=mastodon&logoColor=ffffff&link=https%3A%2F%2Fhachyderm.io%2F%40nedbat&url=https%3A%2F%2Fhachyderm.io%2Fusers%2Fnedbat%2Ffollowers.json&query=totalItems&label=@nedbat
|
||||||
|
:target: https://hachyderm.io/@nedbat
|
||||||
|
:alt: nedbat on Mastodon
|
||||||
|
.. |mastodon-coveragepy| image:: https://img.shields.io/badge/dynamic/json?style=flat&labelColor=450657&logo=mastodon&logoColor=ffffff&link=https%3A%2F%2Fhachyderm.io%2F%40coveragepy&url=https%3A%2F%2Fhachyderm.io%2Fusers%2Fcoveragepy%2Ffollowers.json&query=totalItems&label=@coveragepy
|
||||||
|
:target: https://hachyderm.io/@coveragepy
|
||||||
|
:alt: coveragepy on Mastodon
|
||||||
|
.. |sponsor| image:: https://img.shields.io/badge/%E2%9D%A4-Sponsor%20me-brightgreen?style=flat&logo=GitHub
|
||||||
|
:target: https://github.com/sponsors/nedbat
|
||||||
|
:alt: Sponsor me on GitHub
|
||||||
|
|
@ -0,0 +1,102 @@
|
||||||
|
../../../bin/coverage,sha256=DMuSJ6-cQKLpqzSy5QMcZTbtpXKwtOesQVjN-2D7ywo,256
|
||||||
|
../../../bin/coverage-3.10,sha256=DMuSJ6-cQKLpqzSy5QMcZTbtpXKwtOesQVjN-2D7ywo,256
|
||||||
|
../../../bin/coverage3,sha256=DMuSJ6-cQKLpqzSy5QMcZTbtpXKwtOesQVjN-2D7ywo,256
|
||||||
|
coverage-7.4.4.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
||||||
|
coverage-7.4.4.dist-info/LICENSE.txt,sha256=DVQuDIgE45qn836wDaWnYhSdxoLXgpRRKH4RuTjpRZQ,10174
|
||||||
|
coverage-7.4.4.dist-info/METADATA,sha256=MmrJCWd-sGuzzx_PQoI0FQ6lEivk8aPGhNVolZecWFI,8186
|
||||||
|
coverage-7.4.4.dist-info/RECORD,,
|
||||||
|
coverage-7.4.4.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
||||||
|
coverage-7.4.4.dist-info/WHEEL,sha256=5gVbVe_w8vFuKIeI1RffqCqgT_cNTJucal8EyR-41SQ,111
|
||||||
|
coverage-7.4.4.dist-info/entry_points.txt,sha256=-SeH-nlgTLEWW1cmyqqCQneSw9cKYQOUHBXXYO-OWdY,123
|
||||||
|
coverage-7.4.4.dist-info/top_level.txt,sha256=BjhyiIvusb5OJkqCXjRncTF3soKF-mDOby-hxkWwwv0,9
|
||||||
|
coverage/__init__.py,sha256=dPoEOVoLi38T4A4gbRsL7cMut5MDsPHiCVI4itIEgD4,1232
|
||||||
|
coverage/__main__.py,sha256=AOoqxExrmj9NsTW1fZuHsFrNXQ69IbS6wUxfa_cxhaQ,293
|
||||||
|
coverage/__pycache__/__init__.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/__main__.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/annotate.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/bytecode.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/cmdline.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/collector.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/config.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/context.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/control.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/data.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/debug.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/disposition.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/env.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/exceptions.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/execfile.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/files.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/html.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/inorout.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/jsonreport.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/lcovreport.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/misc.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/multiproc.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/numbits.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/parser.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/phystokens.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/plugin.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/plugin_support.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/python.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/pytracer.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/report.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/report_core.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/results.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/sqldata.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/sqlitedb.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/sysmon.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/templite.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/tomlconfig.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/types.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/version.cpython-310.pyc,,
|
||||||
|
coverage/__pycache__/xmlreport.cpython-310.pyc,,
|
||||||
|
coverage/annotate.py,sha256=vQm1QSNn2-n5ToHtGAgY6MoJktITsnY5pRZF9v1Xuvo,3739
|
||||||
|
coverage/bytecode.py,sha256=m3amujrggn0HwgLn5kjiOsBats7Jhs6uufLhg7f8O1Y,713
|
||||||
|
coverage/cmdline.py,sha256=8Er6WIhs8wsSAPeixjCgDpwDU3IzEXR0RZQZkp80WFw,34260
|
||||||
|
coverage/collector.py,sha256=fZAK9Vamom0MOA7Sf3nhB65k7EERZnoR4TXGnr2TLsw,21784
|
||||||
|
coverage/config.py,sha256=LoDpXOIq_gehWeXCXpeaoiYlseRguhy7qP0XQBKrRA0,21938
|
||||||
|
coverage/context.py,sha256=O93Po3n4PkcViFUw091qyzPQBeN8wEaY5M5S13gN3SE,2455
|
||||||
|
coverage/control.py,sha256=DeHpxSa-efG7-ln9kzHcoFeyzflMM0E67dh6nPqf_Kc,51706
|
||||||
|
coverage/data.py,sha256=EHST20LoLYh9GO3sZ8Irlybm8qd2FmW2BxH1Tfw170o,7893
|
||||||
|
coverage/debug.py,sha256=12lhvoMgullpt7WvBzmQ3AkuTmL70VmsANdVxdEIsPg,20718
|
||||||
|
coverage/disposition.py,sha256=4WsOXrsLXrWqNOnESplYkqvu_s3hbwpborK2WPPsCUI,1894
|
||||||
|
coverage/env.py,sha256=4EzpRcw1IJlFwCvch04vAGQjb21t9poCk4Eqlvr2Zjc,5430
|
||||||
|
coverage/exceptions.py,sha256=rlBBNdo2m2YVBV85pgteVctY7FPkHU_q-MIoBqwsf1M,1397
|
||||||
|
coverage/execfile.py,sha256=t26g6hre1zMePQUoJJ8VO7bVNRf6T8aOjrcFurToizs,12090
|
||||||
|
coverage/files.py,sha256=b2VaMvAucNY91n4F9QQvh9RT95Fciz855IgaQpIdMuc,19386
|
||||||
|
coverage/html.py,sha256=O-HoN-FXWN98M316TQFEiVC1IRpbgYOySnUybyURqhk,23381
|
||||||
|
coverage/htmlfiles/coverage_html.js,sha256=uf5vr4EbDhxoMpoA9FHwYrdhB8sfm6Ee7ngtYpSLGn8,21865
|
||||||
|
coverage/htmlfiles/favicon_32.png,sha256=vIEA-odDwRvSQ-syWfSwEnWGUWEv2b-Tv4tzTRfwJWE,1732
|
||||||
|
coverage/htmlfiles/index.html,sha256=IqBoS7wvCSOAQF_qk7dhdMcSM_bBHr1Q5yTwpH0seP0,5400
|
||||||
|
coverage/htmlfiles/keybd_closed.png,sha256=fZv4rmY3DkNJtPQjrFJ5UBOE5DdNof3mdeCZWC7TOoo,9004
|
||||||
|
coverage/htmlfiles/keybd_open.png,sha256=SXXQLo9_roBN2LdSTXnW_TyfwiUkYBxt61no6s-o06w,9003
|
||||||
|
coverage/htmlfiles/pyfile.html,sha256=46x1lmPrZVqsZ2G9PIPf22JWE7Mbjf-kWAhqpTkIRDM,6434
|
||||||
|
coverage/htmlfiles/style.css,sha256=bW3iMypMqHz2zEVV83fW5G1l-RoPUukpAxGGzDAtjiU,12394
|
||||||
|
coverage/htmlfiles/style.scss,sha256=O-MCmtcP0owf4ytyzo5Wuy-ZJVNq-UDOi5I348dVtig,17342
|
||||||
|
coverage/inorout.py,sha256=lz7COTWdSicRKU7Iy1DcXsd7-mrMvwoPVgTLYmOqnJk,23690
|
||||||
|
coverage/jsonreport.py,sha256=FYZBv2vYfiGjdl8tY0As6OA4992pr8swx9ZVyyiqFIY,4858
|
||||||
|
coverage/lcovreport.py,sha256=wx8GhgMo-Zu2WRsGKmLeBOHWGc6KN-1_AZDoHYHE8AM,5166
|
||||||
|
coverage/misc.py,sha256=n5HdmMOE_lL8fjSsf2DhGemwkg_tr0shnZ9GD-laelE,12290
|
||||||
|
coverage/multiproc.py,sha256=3X01M6s9As-C82uVVZEYF7ukvRsLcC6f8exZmniX3X8,4195
|
||||||
|
coverage/numbits.py,sha256=CNfMasZGN0p3CvWQzUxWghXb9wyLDTU0DLF9Gg3FIEc,4663
|
||||||
|
coverage/parser.py,sha256=-vJ42zYLnbqeQ1YP2MzyS-DmYS_nqY8y5y3cA9wbKzg,54583
|
||||||
|
coverage/phystokens.py,sha256=XAeqFRJElfBKQdSE5OqR9dppVGSnb_nzZBVud-tZ7Ng,8028
|
||||||
|
coverage/plugin.py,sha256=esJJLWxEpxR5wIRVy9XauOQaF2SoDP-x2MTirNF7hzE,19371
|
||||||
|
coverage/plugin_support.py,sha256=Q4gSLCCfKJEbwGi0nqclJbceH5IFAWYNorHj3-TyStI,10284
|
||||||
|
coverage/py.typed,sha256=_B1ZXy5hKJZ2Zo3jWSXjqy1SO3rnLdZsUULnKGTplfc,72
|
||||||
|
coverage/python.py,sha256=DprWnrx6cWGoNFYdO_6Yryz0eXZ6I3w62ATyvh8g5rc,8027
|
||||||
|
coverage/pytracer.py,sha256=mCE4MM0ZYMV9Er3VYBlzyfJH8VDBskSTkoTlIO1wUWU,14680
|
||||||
|
coverage/report.py,sha256=LwlOHfUKiMFT7hqQFP0_9p3FArrDZk-FhX2q3LsNtNQ,10567
|
||||||
|
coverage/report_core.py,sha256=YpbHBP-PQqWJ5WRuyIX_4AbHwvxJVvToKEdppn2lk8Y,4051
|
||||||
|
coverage/results.py,sha256=5yJULMlQx8yvHJbwYC4BaeGBLRxZACIdK8to7MrGLQw,13359
|
||||||
|
coverage/sqldata.py,sha256=ZRnUsCpnEC0nlXTnmQ-MvgFx9SFF-oZSlj_uwqmrMNU,43636
|
||||||
|
coverage/sqlitedb.py,sha256=4sIfmUYku4uWXs0u7Ye1S2VLOWde06XHOquHwA-ASSs,9680
|
||||||
|
coverage/sysmon.py,sha256=GADCRxPtkkXpJ-PC6Nwt0nNFimpSZtdCMb2Lw6bSu1k,15436
|
||||||
|
coverage/templite.py,sha256=eRd-_N6btBD-v25vfaYjvO686fdHdq2mXE3YU7_wsrg,10814
|
||||||
|
coverage/tomlconfig.py,sha256=sYSSl2NpONLHfdDyJ94o0CZQDQ76HPwFYYD2oWxYI2w,7524
|
||||||
|
coverage/tracer.cpython-310-darwin.so,sha256=MjA55Cr9eftUWxngmqQV8NICyhlypkcZgZKwt466gE8,39832
|
||||||
|
coverage/tracer.pyi,sha256=KZKrG4E9L-B7kf4SC7ni-cCjW8ubcCq7aNEyeuxLPwY,1171
|
||||||
|
coverage/types.py,sha256=7J1NDUvAwaFB7TjYdVuBlvNCVi_IMo-BjWDit6y63Wc,5412
|
||||||
|
coverage/version.py,sha256=p7_jki12EyzzlqE0EKmmzDPgs2bn9lnOsOzIt_KnPgQ,1431
|
||||||
|
coverage/xmlreport.py,sha256=Kn_EVfpaY_Hu-cI_Wpe_NoeJJeBo2IEeqCKtV3PzCUM,9775
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue