o
    i"                     @   s  d dl mZ d dlZd dlmZ d dlZd dl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% ee&Z'e(dZ)G dd dZ*e* Z+G dd dZ,eG dd dZ-dej.de/fd d!Z0d"ej1de-fd#d$Z2e! d"ej1defd%d&Z3dej4e/ fd'd(Z5d"ej1defd)d*Z6d+ej7fd,d-Z8dej4e fd.d/Z9de:e;d0f fd1d2Z<de=fd3d4Z>de=fd5d6Z?de=fd7d8Z@d9d: ZAd;d< ZBd=d> ZCd?d@ ZDd"ej1de=fdAdBZEd"ej1de=fdCdDZFdEdF ZGdGdH ZHdIdJ ZIG dKdL dLejJZKdOdMdNZLi ZMi ZNdS )P    )	dataclassN)Path)ATR_MIN_SUPPORTED_VERSION)$ATTEMPT_TO_FIX_MIN_SUPPORTED_VERSION)EFD_MIN_SUPPORTED_VERSION)ITR_MIN_SUPPORTED_VERSION)RETRIES_MIN_SUPPORTED_VERSION)TestExcInfo)TestId)TestModuleId)TestSourceFileInfo)
TestStatus)TestSuiteId)ITR_UNSKIPPABLE_REASON) get_source_lines_for_test_method)
get_logger)_get_config)InternalTest)cached)asbool)undecoratedz6^(((?P<module>.*)/)?(?P<suite>[^/]*?))::(?P<name>.*?)$c                   @   s   e Zd ZdZdZdZdZdS )_PYTEST_STATUSerrorfailedpassedskippedN)__name__
__module____qualname__ERRORFAILEDPASSEDSKIPPED r#   r#   Z/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/pytest/_utils.pyr   #   s
    r   c                   @   s   e Zd ZdZdZdZdS )	TestPhasesetupcallteardownN)r   r   r   SETUPCALLTEARDOWNr#   r#   r#   r$   r%   -   s    r%   c                   @   s&   e Zd ZU eed< eed< eed< dS )	TestNamesmodulesuitetestN)r   r   r   str__annotations__r#   r#   r#   r$   r,   3   s   
 r,   	parameterreturnc                 C   s   t | }tdd|S )Nz at 0[xX][0-9a-fA-F]+ )reprresub)r2   
param_reprr#   r#   r$   _encode_test_parameter:   s   r9   itemc                 C   sZ   t t| j}|stdd| jdS |dpddd}|d}|d	}t|||dS )
zKGets an item's module, suite, and test names by leveraging the plugin hooksunknown_moduleunknown_suite)r-   r.   r/   r-   r4   /.r.   name)r6   match_NODEID_REGEXnodeidr,   r?   groupreplace)r:   matchesmodule_name
suite_name	test_namer#   r#   r$   _get_names_from_itemA   s   

rI   c                 C   sP   | j jj| d}| j jj| d}| j jj| d}t|}t||}t||}|S )zConverts an item to a CITestId, which recursively includes the parent IDs

    NOTE: it is mandatory that the session, module, suite, and test IDs for a given test and parameters combination
    be stable across test runs.
    r:   )confighook#pytest_ddtrace_get_item_module_name"pytest_ddtrace_get_item_suite_name!pytest_ddtrace_get_item_test_namer   r   r
   )r:   rF   rG   rH   	module_idsuite_idtest_idr#   r#   r$   _get_test_id_from_itemO   s   

rS   c              	   C   s   t | dd }|d u rd S i i d}| jj D ]%\}}z
t||d |< W q ty<   d|d |< tjd|dd Y qw ztj	|ddW S  t
yW   tjd	| dd Y d S w )
Ncallspec)	argumentsmetadatarU   zCould not encodezFailed to encode %rTexc_info)	sort_keysz*Failed to serialize parameters for test %s)getattrrT   paramsitemsr9   	Exceptionlogwarningjsondumps	TypeError)r:   rT   
parameters
param_name	param_valr#   r#   r$   _get_test_parameters_jsonc   s"   
rf   c                 C   sR   zt | dd }|d ur| j jW S t| jj jW S  ty(   t  Y S w )Npath)	rZ   rg   absoluteparentr   r-   __file__r]   cwd)r:   	item_pathr#   r#   r$   _get_module_path_from_itemy   s   rm   sessionc                 C   sR   d}t | jddr|dd| jjj7 }tddtr'|dtddt7 }|S )z@Extract and re-create pytest session command from pytest config.pytestinvocation_paramsNz {} PYTEST_ADDOPTSF)rZ   rK   formatjoinrp   argsr   r   )rn   commandr#   r#   r$   _get_session_command   s   rw   c              	   C   s|   z*t | drt| j| j|}t|}t||d |d }|W S t||  d }|W S  ty=   tj	d| |dd Y d S w )N_objr      z4Unable to get source file info for item %s (path %s)TrW   )
hasattrr   rx   r?   r   r   
reportinfor]   r^   debug)r:   rl   test_method_objectsource_linessource_file_infor#   r#   r$   _get_source_file_info   s   
r   .c                   C   s&   t tdrtjS ttttjdS )Nversion_tupler>   )rz   ro   r   tuplemapint__version__splitr#   r#   r#   r$   _get_pytest_version_tuple   s   
r   c                   C   s
   t  dkS )N)   r   r   )r   r#   r#   r#   r$   _is_pytest_8_or_later      
r   c                   C   
   t  tkS N)r   r   r#   r#   r#   r$   _pytest_version_supports_itr   r   r   c                   C   r   r   )r   r   r#   r#   r#   r$    _pytest_version_supports_retries   r   r   c                   C   r   r   )r   r   r#   r#   r#   r$   _pytest_version_supports_efd   r   r   c                   C   r   r   )r   r   r#   r#   r#   r$   _pytest_version_supports_atr   r   r   c                   C   r   r   )r   r   r#   r#   r#   r$   '_pytest_version_supports_attempt_to_fix   r   r   c                 C   s2   | j r
| j d }|S | jr| jd}|S d}|S )Nr   	conditionT)ru   kwargsget)markerr   r#   r#   r$   _get_skipif_condition   s   
r   c                 C   s,   |  ddur	dS tdd | jddD S )z'Checks whether Pytest will skip an itemskipNTc                 s   s    | ]	}t |d u V  qdS )TN)r   .0r   r#   r#   r$   	<genexpr>   s    z)_pytest_marked_to_skip.<locals>.<genexpr>skipifr?   )get_closest_markeranyiter_markersrJ   r#   r#   r$   _pytest_marked_to_skip   s   r   c                 C   s   t dd | jddD S )z]Returns True if a test has a skipif marker with value false and reason ITR_UNSKIPPABLE_REASONc                 s   s,    | ]}t |d u o|jdtkV  qdS )FreasonN)r   r   r   r   r   r#   r#   r$   r      s
    
z'_is_test_unskippable.<locals>.<genexpr>r   r   )r   r   rJ   r#   r#   r$   _is_test_unskippable   s   
r   c                 C   s   t | }t|S )z)Extract span from `pytest.Item` instance.)rS   r   get_span)r:   rR   r#   r#   r$   _extract_span   s   
r   c                 C   s&   t  sdS td| |rdS td| |S )aI  Checks if the ddtrace plugin is enabled before the config is fully populated.

    This is necessary because the module watchdog for coverage collection needs to be enabled as early as possible.

    Note: since coverage is used for ITR purposes, we only check if the plugin is enabled if the pytest version supports
    ITR
    Fz
no-ddtraceddtrace)r   _is_option_true)early_configru   r#   r#   r$   _is_enabled_early   s
   r   c                 C   s"   | | p|| pd|  |v S )Nz--)	getoptiongetini)optionr   ru   r#   r#   r$   r      s   "r   c                   @   sD   e Zd ZU dZeje ed< dZeje	 ed< dZ
eje ed< dS )_TestOutcomeNstatusskip_reasonrX   )r   r   r   r   tOptionalr   r1   r   r0   rX   r	   r#   r#   r#   r$   r      s   
 r   c                 C   s.   t | dg }|D ]\}}||kr|  S q|S )Nuser_properties)rZ   )reportkeydefaultr   kvr#   r#   r$   get_user_property   s   r   r   )Odataclassesr   r`   pathlibr   r6   typingr   ro   )ddtrace.contrib.internal.pytest.constantsr   r   r   r   r   ddtrace.ext.test_visibility.apir	   r
   r   r   r   r   (ddtrace.internal.ci_visibility.constantsr   $ddtrace.internal.ci_visibility.utilsr   ddtrace.internal.loggerr   !ddtrace.internal.settings._configr   $ddtrace.internal.test_visibility.apir   ddtrace.internal.utils.cacher   ddtrace.internal.utils.formatsr   !ddtrace.internal.utils.inspectionr   r   r^   compilerA   r   PYTEST_STATUSr%   r,   Anyr0   r9   ItemrI   rS   r   rf   rm   Sessionrw   r   r   r   r   boolr   r   r   r   r   r   r   r   r   r   r   r   
NamedTupler   r   excinfo_by_reportreports_by_itemr#   r#   r#   r$   <module>   sr    



	