o
    Á¿ixÆ  ã                   @   s  d dl Z d dlmZ d dlZd dlmZ d dlZd dlm	Z	 d dlm
Z d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl!m#Z# d dl!m$Z$ d dl!m%Z% d dl!m&Z& d dl!m'Z' d dl!m(Z( d dl!m)Z) d dl!m*Z* d dl!m+Z+ d dl!m,Z, d d l!m-Z- d d!l!m.Z. d d"l!m/Z/ d d#l!m0Z0 d d$l!m1Z1 d d%l!m2Z2 d d&l!m3Z3 d d'l!m4Z4 d d(l5m6Z6 d d)l5m7Z7 d d*l5m8Z8 d d+l5m9Z9 d d,l5m:Z: d d-l;m<Z< d d.l;m=Z= d d/l;m>Z> d d0l?m@ZA d d1lBmCZC d d2lDmEZE d d3lFmGZG d d4lFmHZH d d5lFmIZI d d6lFmJZJ d d7lFmKZK d d8lLmMZM d d9lNmOZO d d:lPmQZQ d d;lPmRZR d d<lPmSZS d d=lPmTZT d d>lUmVZV d d?lWmXZX d d@lYmZZ[ d dAl\m]Z] d dl^m
Z_ d dBl`maZa d dClbmcZc d dDlbmdZd d dElbmeZe d dFlbmfZf d dGlgmhZh d dHlimjZj d dIlkmlZl e1ƒ rÌd dJlmmnZn e/ƒ rèd dKlompZp d dLlomqZq d dMlomrZr d dNlomsZs e-ƒ rd dOltmuZu d dPltmvZv d dQltmwZw d dRltmxZx d dSltmyZy d dTltmzZz e.ƒ r&d dUl{m|Z| d dVl{m}Z} d dWl{m~Z~ e]eƒZ€dXZdYZ‚dZZƒe„ƒ Z…d[a†da‡d\eˆfd]d^„Z‰d\eˆfd_d`„ZŠd\eˆfdadb„Z‹dcdd„ ZŒd¸dedf„Zd¸dgdh„ZŽd\eeehf fdidj„Z‘d\eXj’fdkdl„Z“eEd¸dmdn„ƒZ”d¸dodp„Z•dqdr„ Z–ej—dsdsdtdudv„ ƒZ˜dwdx„ Z™dydz„ Zšd{ed\dfd|d}„Z›d{ed\dfd~d„Zœd€ejd\dfdd‚„Zžd¸dƒd„„ZŸd¸d…d†„Z d\ej¡eXj’ fd‡dˆ„Z¢d‰dŠ„ Z£ej—dsdsd‹dŒd¸ddŽ„ƒZ¤ej—d‹dd\ej¡eˆ fdd‹„ƒZ¥d‘d’„ Z¦d{ed\dfd“d”„Z§d\e2fd•d–„Z¨d\e2fd—d˜„Z©d™ejªdšed›e d\dfdœd„Z«ej—dsdžd™ejªdšed\dfdŸd „ƒZ¬d\e­fd¡d¢„Z®d¹d£ej¡e­ fd¤d¥„Z¯ej—dsdsd¦d§d¨„ ƒZ°d€ejd©e­d\dfdªd«„Z±ej—dsdsd¦d€ejd©e­d\dfd¬d­„ƒZ²d®e d\efd¯d°„Z³ej—dsd±d²d³„ ƒZ´ej—dsd±d´dµ„ ƒZµej—dsd±d¶d·„ ƒZ¶dS )ºé    N)ÚPath)Úruntestprotocol)ÚDDTraceDeprecationWarning©Úconfig)Ú_is_coverage_available)Úget_coverage_percentage)Úpatch)Úrun_coverage_report)Ústart_coverage)Ú$_is_coverage_invoked_by_coverage_run)Ú_is_coverage_patched)Ú_is_pytest_cov_enabled)Úhandle_coverage_report)Ú_set_benchmark_data_from_item)Úprint_test_report_links)Ú%_pytest_report_teststatus_return_type)Úpytest_CallInfo)Úpytest_Config)Úpytest_TestReport)ÚPYTEST_STATUS)Ú	TestPhase)Ú_get_module_path_from_item)Ú_get_names_from_item)Ú_get_session_command)Ú_get_source_file_info)Ú_get_test_id_from_item)Ú_get_test_parameters_json)Ú_is_enabled_early)Ú_is_test_unskippable)Ú_pytest_marked_to_skip)Ú_pytest_version_supports_atr)Ú'_pytest_version_supports_attempt_to_fix)Ú_pytest_version_supports_efd)Ú_pytest_version_supports_itr)Ú _pytest_version_supports_retries)Ú_TestOutcome)Úexcinfo_by_report)Úreports_by_item)ÚPYTEST_XDIST_WORKER_VALUE)ÚXDIST_UNSET)Ú
XdistHooks)Ú_parse_xdist_args_from_cmd)Ú._skipping_level_for_xdist_parallelization_mode)Ú	FRAMEWORK)ÚUSER_PROPERTY_QUARANTINED)ÚXFAIL_REASON)Úunpatch)Útest)Ú_catch_and_log_exceptions)ÚTestExcInfo)Ú
TestStatus)Údisable_test_visibility)Úenable_test_visibility)Úis_test_visibility_enabled)ÚSKIPPED_BY_ITR_REASON)Úrequire_ci_visibility_service)ÚCOVERAGE_LIBRARY)Úrecord_code_coverage_empty)Úrecord_code_coverage_finished)Úrecord_code_coverage_started)Útake_over_logger_stream_handler)ÚModuleCodeCollector)Úinstall)Ú
get_logger)ÚLibraryCapabilities)ÚInternalTest)ÚInternalTestModule)ÚInternalTestSession)ÚInternalTestSuite)ÚCoverageLines)Úasbool)Ú	deprecate)Úget_retry_num)Úefd_get_failed_reports)Úefd_get_teststatus)Úefd_handle_retries)Ú&efd_pytest_terminal_summary_post_yield)Úatr_get_failed_reports)Úatr_get_teststatus)Úatr_handle_retries)Ú&atr_pytest_terminal_summary_post_yield)Úquarantine_atr_get_teststatus)Ú-quarantine_pytest_terminal_summary_post_yield)Úattempt_to_fix_get_teststatus)Úattempt_to_fix_handle_retries)Ú1attempt_to_fix_pytest_terminal_summary_post_yieldÚquarantinedz!Flaky test is disabled by Datadog)ÚflakyÚrerunfailuresFÚreturnc                 C   sØ   t  ¡ sdS |j}tƒ }|j}|r!t |¡}t |¡pt 	|¡}nt |¡}t |¡p/t 	|¡}|rj|r;t 
|¡ dS t |¡ |  tjjtd¡ t| jdƒrhd| jjvr\d| jjd< |sh| jjd  d7  < dS dS )z—Checks whether a test should be skipped

    This function has the side effect of marking the test as skipped immediately if it should be skipped.
    F©ÚreasonÚworkeroutputÚitr_skipped_countr   é   T)rF   Úis_test_skipping_enabledÚ	parent_idr:   Ú_suite_skipping_moderG   Úis_itr_skippableÚis_itr_unskippablerD   Úis_attempt_to_fixÚmark_itr_forced_runÚmark_itr_skippedÚ
add_markerÚpytestÚmarkÚskipr9   Úhasattrr   r_   )ÚitemÚtest_idÚsuite_idÚci_visibility_serviceÚis_suite_skipping_modeÚshould_skipÚitem_is_unskippable© rv   ú^/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/pytest/_plugin_v2.pyÚ_handle_itr_should_skipw   s.   



rx   c                 C   s\   |t v rt d¡ dS t| jdƒr'd| jjvrd| jjd< | jjd  d7  < t  |¡ dS )NzSuite is already skippedFr_   r`   r   ra   T)Úskipped_suitesÚlogÚdebugrn   r   r_   Úadd)ro   rq   rv   rv   rw   Ú_handle_itr_xdist_skipped_suite¥   s   

r}   c                  C   s`   d} zt ƒ }t|dƒr|jr|jj} W n ty#   tjddd Y nw tt 	dd¡ƒr.d} | S )a  
    Check if coverage report upload is enabled, following V3 pattern:
    1. First, get setting from API (via CI visibility service)
    2. Then, allow environment variable to override

    This should only be called after pytest_sessionstart when settings are available.
    FÚ_api_settingszBUnable to check if coverage report upload is enabled from settingsT©Úexc_infoÚ3DD_CIVISIBILITY_CODE_COVERAGE_REPORT_UPLOAD_ENABLEDÚfalse)
r:   rn   r~   Úcoverage_report_upload_enabledÚ	Exceptionrz   r{   rI   ÚosÚgetenv)rƒ   rr   rv   rv   rw   Ú"_is_coverage_report_upload_enabled²   s   	€ÿr‡   c                 C   s|   t  |¡}t  |¡}t  |¡}|rtt d¡ƒrd}|r*|s*|  tj	j
td¡ dS |s0|r:|r<|  jtg7  _dS dS dS )zAdd a user property to identify quarantined tests, and mark them for skipping if quarantine is enabled in
    skipping mode.
    Ú_DD_TEST_SKIP_QUARANTINED_TESTSTr]   N)rD   Úis_quarantined_testÚis_disabled_testrg   rI   r…   r†   rj   rk   rl   rm   Ú"DISABLED_BY_TEST_MANAGEMENT_REASONÚuser_propertiesr/   )ro   rp   Úis_quarantinedÚis_disabledrg   rv   rv   rw   Ú_handle_test_managementÊ   s   


þr   c                 C   ó   |   ¡  dS )zF
    Wraps coverage collector __enter__ call for testing purposes
    N)Ú	__enter__©Úcoverage_collectorrv   rv   rw   Ú_coverage_collector_enterß   ó   r”   c                 C   r   )zE
    Wraps coverage collector __exit__ call for testing purposes
    N)Ú__exit__r’   rv   rv   rw   Ú_coverage_collector_exitæ   r•   r—   c                 C   s   |   ¡ S )zN
    Wraps coverage collector get_covered_lines call for testing purposes
    )Úget_covered_linesr’   rv   rv   rw   Ú%_coverage_collector_get_covered_linesí   s   r™   c                  C   s    t  ¡ } ttjtƒ t| ƒ | S ©N)r@   ÚCollectInContextr>   r;   Ú
COVERAGEPYr.   r”   r’   rv   rv   rw   Ú_start_collecting_coverageô   s   r   c           
      C   sÆ   |s
t  d|¡ d S t ¡ }|sd S t|ƒ}t|ƒ ttjt	ƒ |s-t  d|¡ t
ƒ  d S i }| ¡ D ]\}}||t|ƒ ¡ < q3|sJt  d|¡ d S tƒ }|j}	|	r[t |j|¡ d S t ||¡ d S )Nz+No coverage collector available for test %sz"No covered lines found for test %sz"No coverage data found for test %s)rz   r{   rF   Úshould_collect_coverager™   r—   r=   r;   rœ   r.   r<   Úitemsr   Úabsoluter:   rd   rG   Úadd_coverage_datarc   rD   )
ro   rp   r“   rž   Útest_covered_linesÚcoverage_dataÚpath_strÚcovered_linesrr   rs   rv   rv   rw   Ú_handle_collected_coverageþ   s0   r¦   c                 C   s,   t  | ¡}| ¡ }t |¡}t  | |¡ d S rš   )rG   Úget_coverage_dataÚkeysr@   Úget_import_coverage_for_pathsr¡   )rq   r£   Úcoverage_pathsÚimport_coveragerv   rv   rw   Ú_handle_coverage_dependencies%  s   

r¬   c                   C   s0   zt ƒ  W d S  ty   tjddd Y d S w )Nz.encountered error during disable_ci_visibilityTr   )r6   r„   rz   r{   rv   rv   rv   rw   Ú_disable_ci_visibility,  s
   ÿr­   T)ÚtryfirstÚhookwrapperc                 c   s    t | ||ƒ dV  dS )a  Perform early initialization of the Test Optimization plugin.

    This has to happen early enough that `sys.stderr` has not been redirected by pytest, so that logging is configured
    properly. Setting the hook with `tryfirst=True` and `hookwrapper=True` achieves that.
    N)Ú(_pytest_load_initial_conftests_pre_yield)Úearly_configÚparserÚargsrv   rv   rw   Úpytest_load_initial_conftests3  s   €
r´   c                 C   sŽ  ddl m  m} d|_tƒ  tdu}|rdnd}t d|¡ t| |ƒs,t d|¡ dS z…t d|¡ t	|ƒ\}}t d	|¡ t
| ||d
}|tj_t d||j¡ ttjd t d|¡ t ¡ }	t d||	¡ |ddtfv}
|	rœ|
rv|rœt ¡ }|du r„t ¡  ¡ }t d||g¡ t|gdd t d|¡ W dS |
r©|s©t d|¡ W dS t d|¡ W dS  tyÆ   tjd|dd tƒ  Y dS w )a  Performs the bare-minimum to determine whether or ModuleCodeCollector should be enabled

    ModuleCodeCollector has a tangible impact on the time it takes to load modules, so it should only be installed if
    coverage collection is requested by the backend.
    r   NFÚWORKERÚMAINzGEARLY_INIT: %s process starting pytest_load_initial_conftests_pre_yieldz5EARLY_INIT: %s process - not enabled early, returningz4EARLY_INIT: %s process - parsing xdist configurationz,EARLY_INIT: %s process - detecting ITR level)r   Únum_workersÚ	dist_modez-EARLY_INIT: %s process - ITR level set to: %sr   z0EARLY_INIT: %s process - test visibility enabledz4EARLY_INIT: %s process - should_collect_coverage: %szMEARLY_INIT: %s process - Installing ModuleCodeCollector with include_paths=%sT)Úinclude_pathsÚcollect_import_time_coveragezCEARLY_INIT: %s process - ModuleCodeCollector installation completedzZEARLY_INIT: %s process - Skipping ModuleCodeCollector (xdist workers will handle coverage)zUEARLY_INIT: %s process - Coverage collection not needed, skipping ModuleCodeCollectorz\EARLY_INIT: %s process - encountered error during configure, disabling Datadog CI Visibilityr   )Úddtrace.bootstrap.cloningÚ	bootstrapÚcloningÚenabledr?   r)   rz   r{   r   r,   r-   Ú	dd_configÚtest_visibilityÚitr_skipping_levelÚnamer7   rk   rF   rž   r*   Úget_workspace_pathr   Úcwdr    Úinstall_coverager„   r­   )r±   r²   r³   r½   Ú	is_workerÚprocess_typer·   r¸   Údetected_itr_levelrž   Úusing_xdistÚworkspace_pathrv   rv   rw   r°   >  sh   
ýý
þ
ÿýúr°   c                 C   sf   t | ƒ}tt dd¡ƒ}|s|sdS tƒ  tƒ st d¡ dS |r&t d¡ |s*|r1t d¡ dS dS )z¶
    Handle coverage patching in pytest_configure (must be early for pytest-cov).
    Coverage start decision is deferred to pytest_sessionstart when API settings are available.
    r   r‚   NzVCoverage requested but coverage.py not available - install with 'pip install coverage'z5pytest-cov detected, coverage.py patched successfullyz1Coverage upload will be handled in session finish)r   rI   r…   r†   Úpatch_coverager   rz   r{   )r   Úpytest_cov_enabledÚenv_coverage_uploadrv   rv   rw   Ú_handle_coverage_patch_earlyŠ  s   

ÿrÎ   r   c                 C   s  t  d¡rtdddtd zlddlm} || ƒrttƒ  ttj	d t
| ƒ d	atD ]}| j |¡r:t d
||¡ daq)| j d¡rOddlm} | j |ƒ d¡ | j d¡rk| j tƒ ¡ t| dƒsntd u rqdt	_W d S W d S W d S W d S tƒ  W d S  ty   tjddd tƒ  Y d S w )NÚDD_PYTEST_USE_NEW_PLUGIN_BETAzDthe DD_PYTEST_USE_NEW_PLUGIN_BETA environment variable is deprecatedz\the new pytest plugin is now the default version. No additional configurations are required.z3.0.0)ÚmessageÚremoval_versionÚcategoryr   )Ú
is_enabledr   Fz¹The pytest `%s` plugin is in use; Test Optimization advanced features will be disabled. You can run `pytest` with `-p no:%s` to disable the plugin and enable Test Optimization features.Tz
pytest-bdd)Ú_PytestBddSubPluginz_datadog-pytest-bddÚxdistÚworkerinputzCencountered error during configure, disabling Datadog CI Visibilityr   )r…   r†   rJ   r   Ú&ddtrace.contrib.internal.pytest.pluginrÓ   Úunpatch_unittestr7   r¿   rk   rÎ   Úskip_pytest_runtest_protocolÚINCOMPATIBLE_PLUGINSÚpluginmanagerÚ	haspluginrz   ÚwarningÚ5ddtrace.contrib.internal.pytest._pytest_bdd_subpluginrÔ   Úregisterr+   rn   r)   Úglobal_worker_itr_resultsr­   r„   )r   rÓ   ÚpluginrÔ   rv   rv   rw   Úpytest_configure¨  sJ   
üû€ûþrâ   c                 C   s   t ƒ sd S tƒ  d S rš   )r8   r­   r   rv   rv   rw   Úpytest_unconfigureà  s   
rã   Úsessionc           
   
   C   s®  t ƒ sd S t d¡ z¸t| ƒ}ttƒ rdnd tƒ rdnd tƒ r!dnd ddtƒ r)dnd d}t	j
|ttjdddtjjdd	 t	 |¡ t	 ¡ }t| jƒsftƒ rftƒ rf|ratt|ƒgd
 t d¡ nt d¡ d }d}t| jdƒr¡ddlm} ddlm} | jj dd¡}zt|ƒ}	|d|	|d}W n t y    t d|¡ Y nw ttdƒr¨d}t	 !||¡ t	 "¡ r½tƒ sÀt #d¡ W d S W d S W d S  t$yÖ   tjddd t%ƒ  Y d S w )Nz-CI Visibility enabled - starting test sessionÚ1Ú5)Úearly_flake_detectionÚauto_test_retriesÚtest_impact_analysisÚtest_management_quarantineÚtest_management_disableÚtest_management_attempt_to_fixzpytest.test_sessionzpytest.test_modulezpytest.test_suiteF)Útest_commandÚtest_frameworkÚtest_framework_versionÚsession_operation_nameÚmodule_operation_nameÚsuite_operation_nameÚtest_operation_nameÚreject_duplicates)Úsourcez%Started coverage.py for report uploadz?Coverage report upload enabled but workspace path not availablerÖ   r   )ÚContext)Ú	USER_KEEPÚ	root_spanÚMISSING_SPANra   )Útrace_idÚspan_idÚsampling_priorityz:pytest_sessionstart: Could not convert root_span %s to intrà   Tz?Early Flake Detection disabled: pytest version is not supportedzGencountered error during session start, disabling Datadog CI Visibilityr   )&r8   rz   r{   r   rC   r#   r!   r$   r"   rF   Údiscoverr.   rk   Ú__version__r¿   Úoperation_nameÚset_library_capabilitiesrÃ   r   r   r   r‡   r   Ústrrn   Úddtrace._trace.contextrö   Úddtrace.constantsr÷   rÖ   ÚgetÚintÚ
ValueErrorÚstartÚefd_enabledrÝ   r„   r­   )
rä   ÚcommandÚlibrary_capabilitiesrÊ   Úextracted_contextÚdistributed_childrenrö   r÷   Úreceived_root_spanrø   rv   rv   rw   Úpytest_sessionstartç  s€   
ú	ø
ÿþý

ýþÿ
ÿþr  c              	   C   s0  | j D ]}t|ƒ}|j}|j}t |t|ƒ¡ t |¡ tt|dƒr%|j	n|j
ƒ ¡ }t ¡ }|rDz| |¡}W n tyC   |}Y nw |}|rMt |¡nd}t||ƒ}	tj|||	d dd„ |jddD ƒ}
|
D ]}t ||¡ qit ¡ r„t|ƒr„t |¡ t |¡ qt ¡ r”t ¡ r–t d¡ dS dS dS )	a  Discover modules, suites, and tests that have been selected by pytest

    NOTE: Using pytest_collection_finish instead of pytest_collection_modifyitems allows us to capture only the
    tests that pytest has selection for run (eg: with the use of -k as an argument).
    ÚpathN)Ú
codeownersÚsource_file_infoc                 S   s   g | ]}|j ‘qS rv   )Úkwargs)Ú.0Úmarkerrv   rv   rw   Ú
<listcomp>U  s    z-_pytest_collection_finish.<locals>.<listcomp>Údd_tags)rÂ   z;Early Flake Detection disabled: too many new tests detected)rŸ   r   rc   rE   rý   r   rG   r   rn   r  Úfspathr    rF   rÃ   Úrelative_tor  Úget_path_codeownersr   rD   Úiter_markersÚset_tagsrb   r   Úmark_itr_unskippabler  Úefd_is_faulty_sessionrz   rÝ   )rä   ro   rp   rq   Ú	module_idÚ	item_pathrÊ   Úrepo_relative_pathÚitem_codeownersr  ÚmarkersÚtagsrv   rv   rw   Ú_pytest_collection_finish6  s8   

ÿ


€ÿr$  c                 C   s>   t ƒ sd S zt| ƒW S  ty   tjddd tƒ  Y d S w )NzKencountered error during collection finish, disabling Datadog CI VisibilityTr   )r8   r$  r„   rz   r{   r­   )rä   rv   rv   rw   Úpytest_collection_finishe  s   
þr%  c                 C   s   t | ƒ}|j}|j}t |¡ t |¡ t| ƒ}|d ur"t ||¡ t |¡ t| |ƒ t	| |ƒ t
| ƒp9t |¡}t ¡ o@| }|rFtƒ S d S rš   )r   rc   rE   r  rG   r   rD   Úset_parametersr   rx   r    Úwas_itr_skippedrF   rž   r   )ro   rp   rq   r  Ú
parametersÚitem_will_skipÚcollect_test_coveragerv   rv   rw   Ú"_pytest_runtest_protocol_pre_yieldp  s    




r+  c           
      C   sL  t | ƒ}|j}|j}|d urt |¡st| ||ƒ t | d ¡}t |¡sMt d|¡ |rBt	| |ƒ}t 
||j|j|j¡ t |¡ nt d|¡ t |¡ |r\| ¡ D ]}t |d ¡ qS|rbt |ƒnd }	|	d u sm|	j|kr t |¡r‚t |¡s‚t |¡ t| |ƒ nt|ƒ t |¡ |d u s™|	d ur¢|	jj|kr¤t |¡ d S d S d S d S )NzRTest %s was not finished normally during pytest_runtest_protocol, finishing it nowz'Test %s has no entry in reports_by_item)r   rc   rD   Úis_finishedr¦   r(   Úpoprz   r{   Ú_process_reports_dictÚprepare_for_finishÚstatusÚskip_reasonr€   ÚfinishÚvaluesr'   rG   re   Úwas_itr_forced_runri   r}   r¬   rE   )
ro   Únextitemr“   rp   rq   r  Úreports_dictÚtest_outcomeÚreportÚnext_test_idrv   rv   rw   Ú#_pytest_runtest_protocol_post_yield  s<   

ÿ


ør:  Úpytest_runtest_protocol)r®   r¯   Úspecnamec                 c   s˜    t ƒ s	d V  d S zt| ƒaW n ty!   d atjddd Y nw d V  z#zt| |tƒ W n ty>   tjddd Y nw W d ad S W d ad S d aw )Nz!encountered error during pre-testTr   z"encountered error during post-test)r8   r+  Ú_current_coverage_collectorr„   rz   r{   r:  ©ro   r5  rv   rv   rw   Úpytest_runtest_protocol_wrapper»  s(   €þÿÿýr?  )r<  c                 C   sL   t ƒ sd S tr	d S zt| |ƒ W dS  ty%   tjddd tƒ  Y d S w )NTzNEncountered internal error while running test, disabling Datadog CI Visibilityr   )r8   rÙ   Ú_pytest_run_one_testr„   rz   rÝ   r­   r>  rv   rv   rw   r;  Ô  s   
ýc                 C   sÂ  | j j| j| jd t| |dd}dd„ |D ƒ}t| |ƒ}t| ƒ}t |¡}t 	|¡}t 
|¡}d}	t |¡sHt| |tƒ t ||j|j|j¡ |D ]0}
|
jrZ|
jtjtjfv rZd}	|
jtjksed|
jv rl|si|rlt|
_|
jsr|
jrzt |d|
j¡ qJd }|	r†t d	|¡ n|rŽtƒ rŽt }nt! "¡ ršt #|¡ršt$}nt! %¡ r¥t &|¡r¥t'}|r±||| |||d
 n|jd u r½t d|¡ nt (||j¡ |D ]	}
| j j)|
d qÆt *|¡ | j j+| j| jd d S )N)ÚnodeidÚlocationF)r5  rz   c                 S   s   i | ]}|j |“qS rv   )Úwhen)r  r8  rv   rv   rw   Ú
<dictcomp>í  s    z(_pytest_run_one_test.<locals>.<dictcomp>TÚfailedÚfailure_longreprz9Test %s failed during setup or teardown, skipping retries)rp   ro   Útest_reportsr7  r   zTest status for %s is None)r8  ),ÚihookÚpytest_runtest_logstartrA  rB  r   r.  r   rD   r‰   rŠ   rg   r,  r¦   r=  r/  r0  r1  r€   rE  rC  r   ÚSETUPÚTEARDOWNÚCALLÚoutcomeÚOUTCOME_QUARANTINEDÚskippedÚ	stash_setÚlongreprrz   r{   r"   rW   rF   r  Úefd_should_retryrN   Úatr_is_enabledÚatr_should_retryrR   Úset_final_statusÚpytest_runtest_logreportr2  Úpytest_runtest_logfinish)ro   r5  Úreportsr6  r7  rp   r   rŽ   rg   Úsetup_or_teardown_failedr8  Úretry_handlerrv   rv   rw   r@  ê  sZ   




€
û

r@  c                 C   s*   dt dtdtfdd„}t| |tdd dS )zEHandle coverage report upload if enabled using shared implementation.Úcoverage_report_bytesÚcoverage_formatr\   c              
   S   sH   z
t ƒ }| | |¡W S  ty# } zt d|¡ W Y d}~dS d}~ww )zFUpload coverage report using native V2 writer/recorder infrastructure.z Error during coverage upload: %sNF)r:   Úupload_coverage_reportr„   rz   r{   )r[  r\  rr   Úerv   rv   rw   Úupload_func1  s   €þz3_handle_coverage_report_upload.<locals>.upload_funcN)r   r_  Úis_pytest_cov_enabled_funcÚstop_coverage_func)Úbytesr  Úboolr   r   )r   r_  rv   rv   rw   Ú_handle_coverage_report_upload.  s   
ürd  c                 C   s`   d }t jt jt jfD ]#}| |¡}|sq
t| |ƒ}|d u s"|jd u r-|}|jd ur-|  S q
|S rš   )r   rJ  rL  rK  r  Ú_process_resultr0  )ro   rX  Úfinal_outcomerC  r8  rM  rv   rv   rw   r.  F  s   


€r.  c           	         sL  t | ƒ}t ˆ d ¡}|d u}tˆ dd ƒrt |tˆ j¡ ndtˆ dg ƒv r3tˆ dd ƒr3t |tˆ j¡ ˆ j	t
jkrA|sAˆ jsAtƒ S tˆ dƒpJdˆ jv }|rSt |t¡nd }t‡ fdd„dD ƒƒ}ˆ jrŸt |¡rktƒ S |r“|s“| jjjs“t |tjtjjj¡ |d u rŽt |ttˆ ddƒ¡ ttjƒS ttj|rœ|jƒS d ƒS ˆ jrÆ|rÁ|sÁ| jjjsÁ|d u r¶t |td¡ t |tjtjj j¡ ttjƒS |rî|sî| jjjsî|d u rÞt |ttˆ ddƒ¡ t |tjtjj j¡ ttj!ƒS ˆ j	t
j"krÿˆ jrÿt #|d	d
¡ nˆ j	t
jkrˆ jrt #|dd
¡ |rt$|j%|j|j&ƒnd }ttj!|dS )NÚwasxfailÚxfailÚkeywordsrQ  c                 3   s    | ]}|ˆ j v V  qd S rš   )ri  )r  Úx©Úresultrv   rw   Ú	<genexpr>o  s   € z"_process_result.<locals>.<genexpr>)rm   ÚskipifrO  ÚXFailÚsetup_failedTÚteardown_failed)r0  r€   )'r   r'   r-  ÚgetattrrD   Úset_tagr0   rg  rQ  rC  r   rK  rE  r&   rn   ri  Úget_tagÚanyrO  r'  r   ÚoptionÚrunxfailr2   ÚRESULTÚStatusÚXFAILÚvaluer5   ÚPASSÚSKIPÚpassedÚXPASSÚFAILrJ  rP  r4   ÚtypeÚtb)	ro   rl  rp   Úreport_excinfoÚhas_exceptionrh  Úxfail_reason_tagÚhas_skip_keywordr€   rv   rk  rw   re  W  sN   




re  ro   ÚcallrM  c                 C   sL   |  ¡ }tƒ rt|ƒd urd S |jtjkrd S | jj d¡r$t	| ƒ d S d S )NÚ	benchmark)
Ú
get_resultr%   rK   rC  r   rK  r   rÛ   rÜ   r   )ro   r‡  rM  Úoriginal_resultrv   rv   rw   Ú_pytest_runtest_makereport  s   ÿr‹  )r¯   c                 c   sh    dV }|j t| ¡ < | ¡ t | i ¡|j< tƒ sdS zt| ||ƒW S  ty3   t	j
ddd Y dS w )zStore outcome for tracing.Nz#encountered error during makereportTr   )Úexcinfor'   r‰  r(   Ú
setdefaultrC  r8   r‹  r„   rz   r{   )ro   r‡  rM  rv   rv   rw   Úpytest_runtest_makereport¬  s   €ÿrŽ  c                 C   sˆ   t | j tjg ¡ƒ}tƒ r&t ¡ r&t| ƒD ]}tj|_	| j 
dg ¡ |¡ qtƒ rBt ¡ rBt| ƒD ]}tj|_	| j 
dg ¡ |¡ q1|S )NrE  )ÚlenÚstatsr  r   ÚFAILEDr#   rF   r  rL   rM  r  Úappendr!   rS  rP   )ÚterminalreporterÚfailed_reports_initial_sizeÚfailed_reportrv   rv   rw   Ú"_pytest_terminal_summary_pre_yieldÀ  s   r–  r”  c                 C   s   |d u r
t  d¡ n|dkr| j dd ¡ n| jtj d |… | jtj< tƒ r/t ¡ r/t	| ƒ t
ƒ r:t ¡ r:t| ƒ t| ƒ t| ƒ t| ƒ d S )NzFCould not get initial failed report size, not restoring failed reportsr   rE  )rz   r{   r  r-  r   r‘  r#   rF   r  rO   r!   rS  rS   rU   rX   r   )r“  r”  rv   rv   rw   Ú#_pytest_terminal_summary_post_yieldÔ  s   
ÿr—  )r¯   r®   c                 c   s¾    zt jrddlm} || ƒ W n ty    tjddd Y nw tƒ s)dV  dS d}zt| ƒ}W n tyA   tjddd Y nw dV  zt	| |ƒ W dS  ty^   tjddd Y dS w )	zReport flaky or failed testsr   )Úprint_iast_reportz.Encountered error during code security summaryTr   Nz3Encountered error during terminal summary pre-yieldz4Encountered error during terminal summary post-yield)
Ú
asm_configÚ_iast_enabledÚ#ddtrace.appsec._iast._pytest_pluginr˜  r„   rz   r{   r8   r–  r—  )r“  Ú
exitstatusr   r˜  r”  rv   rv   rw   Úpytest_terminal_summaryñ  s4   €€ÿÿýýr  rœ  c                 C   sò   t ƒ sd S tƒ }t| jƒ}tƒ }tƒ rF|s|rF|r|stƒ  tƒ }|d u r+t 	d¡ nt
|ttfƒsAt|ƒ}t 	d|j|j|¡ nt |¡ |rMt| jƒ t ¡ rUt ¡  ttdƒrftj}|dkrft |¡ tjd| jtjjkrstjnd d d S )Nz5Unable to retrieve coverage data for the session spanzDUnexpected format for total covered percentage: type=%s.%s, value=%rrà   r   T)Úforce_finish_childrenÚoverride_status) r8   r   r   r   r‡   r   r
   r   rz   r{   Ú
isinstanceÚfloatr  r  Ú
__module__Ú__name__rF   Úset_covered_lines_pctrd  r@   Úis_installedÚ	uninstallrn   rk   rà   Úset_itr_skipped_countr2  rœ  ÚExitCodeÚTESTS_FAILEDr5   r€  )rä   rœ  Úinvoked_by_coverage_run_statusÚpytest_cov_statusrƒ   Úlines_pct_valueÚtÚskipped_countrv   rv   rw   Ú_pytest_sessionfinish  s@   
ü




þr¯  c                 c   sF    d V  t ƒ s	d S zt| |ƒ W d S  ty"   tjddd Y d S w )Nz'encountered error during session finishTr   )r8   r¯  r„   rz   r{   )rä   rœ  rv   rv   rw   Úpytest_sessionfinishC  s   €ÿr°  r8  c                 C   sª   t ƒ sd S tƒ rt| ƒ}|r|S tƒ r%t ¡ r%t| ƒpt| ƒ}|d ur%|S tƒ r6t 	¡ r6t
| ƒ}|d ur6|S t| dg ƒ}t|v }|rS| jtjkrQtddddiffS dS d S )NrŒ   ÚqÚQUARANTINEDÚblueT)Ú r´  r´  )r8   r"   rV   r!   rF   rS  rQ   rT   r#   r  rM   rr  r/   rC  r   rK  rN  )r8  Útest_statusrŒ   r   rv   rv   rw   Úpytest_report_teststatusP  s*   úr¶  )Útrylastc                 C   ó   t | ƒ}|jS rš   )r   Úmodule©ro   Únamesrv   rv   rw   Ú#pytest_ddtrace_get_item_module_namep  s   r¼  c                 C   r¸  )zŠ
    Extract suite name from a `pytest.Item` instance.
    If the module path doesn't exist, the suite path will be reported in full.
    )r   Úsuiterº  rv   rv   rw   Ú"pytest_ddtrace_get_item_suite_namev  s   r¾  c                 C   r¸  )z3Extract name from item, prepending class if desired)r   r2   rº  rv   rv   rw   Ú!pytest_ddtrace_get_item_test_name€  s   r¿  )r\   Nrš   )·r…   Úpathlibr   Útypingr­  Ú_pytest.runnerr   rk   Úddtracer   r   r¿   Ú'ddtrace.contrib.internal.coverage.patchr   r   r	   rË   r
   r   Ú'ddtrace.contrib.internal.coverage.utilsr   r   r   r   Ú0ddtrace.contrib.internal.pytest._benchmark_utilsr   Ú-ddtrace.contrib.internal.pytest._report_linksr   Ú&ddtrace.contrib.internal.pytest._typesr   r   r   r   Ú&ddtrace.contrib.internal.pytest._utilsr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   Ú&ddtrace.contrib.internal.pytest._xdistr)   r*   r+   r,   r-   Ú)ddtrace.contrib.internal.pytest.constantsr.   r/   r0   Ú'ddtrace.contrib.internal.unittest.patchr1   rØ   Úddtrace.extr2   Ú"ddtrace.ext.test_visibility._utilsr3   Úddtrace.ext.test_visibility.apir4   r5   r6   r7   r8   Ú(ddtrace.internal.ci_visibility.constantsr9   Ú/ddtrace.internal.ci_visibility.service_registryr:   Ú1ddtrace.internal.ci_visibility.telemetry.coverager;   r<   r=   r>   Ú$ddtrace.internal.ci_visibility.utilsr?   Úddtrace.internal.coverage.coder@   Ú#ddtrace.internal.coverage.installerrA   rÅ   Úddtrace.internal.loggerrB   Úddtrace.internal.settings.asmr™  Ú6ddtrace.internal.test_visibility._library_capabilitiesrC   Ú$ddtrace.internal.test_visibility.apirD   rE   rF   rG   Ú/ddtrace.internal.test_visibility.coverage_linesrH   Úddtrace.internal.utils.formatsrI   Úddtrace.vendor.debtcollectorrJ   Ú,ddtrace.contrib.internal.pytest._retry_utilsrK   Ú*ddtrace.contrib.internal.pytest._efd_utilsrL   rM   rN   rO   Ú*ddtrace.contrib.internal.pytest._atr_utilsrP   rQ   rR   rS   rT   rU   Ú/ddtrace.contrib.internal.pytest._attempt_to_fixrV   rW   rX   r£  rz   rN  r‹   rÚ   Úsetry   rÙ   r=  rc  rx   r}   r‡   r   r”   r—   Údictr  r™   r›   r   r¦   r¬   r­   Úhookimplr´   r°   rÎ   râ   rã   ÚSessionr  r$  r%  ÚOptionalr+  r:  r?  r;  r@  rd  r.  re  ÚItemr‹  rŽ  r  r–  r—  r  r¯  r°  r¶  r¼  r¾  r¿  rv   rv   rv   rw   Ú<module>   s2   .



&

L8
O
/.
DF

3ÿ
þ
 



	