
    VhC$                       d Z ddlmZ ddlZddlZddlZddlZddlZddl	m
Z
mZmZ ddlmZmZmZ ddlmZmZmZmZmZmZ ddlmZ dd	lmZmZmZ dd
lmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)  ejT                  d       G d d             Z+ e+dddd      fZ,	 e,d   Z-	  G d de      Z.d"dZ/d#dZ0d#dZ1	 	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d%dZ2d&dZ3	 	 	 	 	 	 	 	 d'dZ4e)d(d       Z5d(dZ6d)dZ7d(d Z8d*d!Z9 e9        y)+zMUtility code for facilitating collection of code coverage when running tests.    )annotationsN   )IntegrationConfigSanityConfig
TestConfig)write_text_file	make_dirsopen_binary_file)ApplicationErrorInternalErrorCOVERAGE_CONFIG_NAMEremove_treesanitize_host_namestr_to_version)data_context)ExitHandlerintercept_python
ResultType)DockerConfig
HostConfigOriginConfigPosixRemoteConfigPosixSshConfigPythonConfig)SUPPORTED_PYTHON_VERSIONSCONTROLLER_PYTHON_VERSIONS)mutexT)frozenc                  :    e Zd ZU dZded<   ded<   ded<   ded<   y	)
CoverageVersionzCDetails about a coverage version and its supported Python versions.strcoverage_versionintschema_versionztuple[int, int]
min_python
max_pythonN)__name__
__module____qualname____doc____annotations__     T/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/coverage_util.pyr    r    <   s    Mr-   r    z7.6.1   )      )r0      c                  $     e Zd ZdZd fdZ xZS )CoverageErrorz:Exception caused while attempting to read a coverage file.c                    || _         || _        t        |   dt        j                   j                  |       d|        y )NzError reading coverage file "z": )pathmessagesuper__init__osrelpath)selfr6   r7   	__class__s      r.   r9   zCoverageError.__init__U   s<    	89N8OsSZR[\]r-   )r6   r!   r7   r!   returnNone)r'   r(   r)   r*   r9   __classcell__)r=   s   @r.   r4   r4   R   s    D^ ^r-   r4   c                    t        |       }t        D cg c]'  }|j                  |cxk  r|j                  k  s#n n|) }}|st	        d|  d      t        |      dkD  rt	        d|  d      |d   }|S c c}w )zEReturn the coverage version to use with the specified Python version.zPython z, has no matching entry in COVERAGE_VERSIONS.r   z4 has multiple matching entries in COVERAGE_VERSIONS.r   )r   COVERAGE_VERSIONSr%   r&   r   len)versionpython_versionentrysupported_versionsr"   s        r.   get_coverage_versionrH   \   s    #G,N->yE%BRBRVdBxhmhxhxBx%yyggY.Z[\\
"ggY.bcdd)!, zs
   $A7A7c                    t        |       5 }|j                  d      }ddd       j                  d      ry|j                  d      rt        |       S t	        | d|      # 1 sw Y   FxY w)a  
    Return the schema version from the specified coverage file.
    SQLite based files report schema version 1 or later.
    JSON based files are reported as schema version 0.
    An exception is raised if the file is not recognized or the schema version cannot be determined.
       Ns   !coverage.py: r   s   SQLitezUnknown header: )r
   read
startswithget_sqlite_schema_versionr4   )r6   file_objheaders      r.    get_coverage_file_schema_versionrP   l   ss     
$	 #8r"# *+#(..
 0
;
<<# #s   A##A,c           	        	 t        j                  |       5 }|j                         }|j                  d       |j	                  d      d   d   }ddd       t        t              s"t        | dt        |       dt         d|       |dk  rt        | d	|       |S # 1 sw Y   QxY w# t
        $ r}t        | d|       |d}~ww xY w)
z<Return the schema version from a SQLite based coverage file.z#select version from coverage_schemar   r   NzSQLite error: zSchema version is z instead of z: z Schema version is out-of-range: )
sqlite3connectcursorexecute	fetchmany	Exceptionr4   
isinstancer#   type)r6   
connectionrT   r$   exs        r.   rM   rM      s    A__T" 	7j&&(FNN@A#--a03A6N	7 nc*D$6tN7K6LLY\X]]_`n_o"pqqD$D^DT"UVV	7 	7  ADN2$"78b@As.   B, 9B B,  B)%B, ,	C
5CC
c           	         | j                   r&|j                  t        | ||j                               t	        | ||||||      S )z4Run a command while collecting Python code coverage.)coverageupdateget_coverage_environmentrD   r   )argspythoncmdtarget_nameenvcapturedatacwds           r.   cover_pythonrh      s<     }}

+D+v~~NOD&#sGT3GGr-   c                T   t        | t              rdt        | j                         }|S t        | t              rdt        | j                         }|S t        | t
              rdt        | j                         }|S t        | t              rd}|S t        dt        |              )z4Return the platform label for the given host config.zremote-zdocker-zssh-originz.Coverage platform label not defined for type: )
rX   r   r   namer   r   hostr   NotImplementedErrorrY   )configplatforms     r.   get_coverage_platformrp      s    &+,/<=> O 
FL	)/<=> O 
FN	+,V[[9:; O 
FL	) O "$RSWX^S_R`"abbr-   c                   t        |       }dj                  | j                  |t        | j                        d| df      }t
        j                  j                  t               j                  j                  t               j                  j                  t        j                  j                        }t
        j                  j                  ||      }t        |       | j                  rd}t!        ||      }|S )z=Return environment variables needed to collect code coverage.=zpython-r]    )COVERAGE_CONFCOVERAGE_FILE)get_coverage_configjoincommandrp   
controllerr:   r6   r   contentrootresults_pathr   COVERAGErk   r	   coverage_checkdict)r`   rc   rD   config_filecoverage_namecoverage_dircoverage_filerd   s           r.   r_   r_      s     &d+KHHdllK9Nt9_cjkrjsat  wA  B  CM77<< 6 6 ; ;\^=S=S=`=`blbububzbz{LGGLL}=Ml !#C
 Jr-   c                b   	 t         j                  S # t        $ r Y nw xY wt        |       }| j                  rdn,t        j                         t        j                  fd       t        j                  j                  t              }| j                  st        ||       |t         _        |S )zYReturn the path to the coverage config, creating the config if it does not already exist.z/tmp/coverage-temp-dirc                     t               S )N)r   )temp_dirs   r.   <lambda>z%get_coverage_config.<locals>.<lambda>   s    [%: r-   )rv   r6   AttributeErrorgenerate_coverage_configexplaintempfilemkdtempr   registerr:   rw   r   r   )r`   coverage_configr6   r   s      @r.   rv   rv      s    "'''  /t4O||+##%:;77<<"67D<<o.#Ks    	c                p    t               j                  j                  rt        |       }|S t	               }|S )z/Generate code coverage configuration for tests.)r   rz   
collection#generate_collection_coverage_config generate_ansible_coverage_configr`   r   s     r.   r   r      s5    ~((=dC  ;<r-   c                 
    d} | S )z7Generate code coverage configuration for Ansible tests.z
[run]
branch = True
concurrency =
    multiprocessing
    thread
parallel = True

omit =
    */python*/dist-packages/*
    */python*/site-packages/*
    */python*/distutils/*
    */pyshared/*
    */pytest
    */AnsiballZ_*.py
    */test/results/*
r,   )r   s    r.   r   r      s    O$ r-   c           	     2   d}t        | t              rP|dt               j                  j                  dt               j                  j
                  j                  dz  }|S t        | t              r|dt               j                  j                  dt        j                  j                  t               j                  j                  t               j                  j                        dz  }|S |dt               j                  j                  z  z  }|S )zBGenerate code coverage configuration for Ansible Collection tests.z{
[run]
branch = True
concurrency =
    multiprocessing
    thread
parallel = True
disable_warnings =
    no-data-collected
z
include =
    z	/*
    */z/*
z/*

omit =
    z
include =
     %s/*
)rX   r   r   rz   r{   r   	directoryr   r:   r6   rw   r|   r   s     r.   r   r     s    	O $)* ~""LN$:$:$E$E$O$O	Q 	Q(  
D,	' ~""BGGLL1G1G1L1LlnNdNdNqNq$rt 	t  	  n!!" 	"
 r-   c                     t         D ]  } t        |         t        D ]#  } t        |       t        k7  st	        d|  d       y)z8Check for internal errors due to incorrect code changes.zController Python version z. is not mapped to the latest coverage version.N)r   rH   r   CONTROLLER_COVERAGE_VERSIONr   )rD   s    r.   
self_checkr   3  sR     - &W%& . v(,GG"<WIEs tuuvr-   )rD   r!   r>   r    )r6   r!   r>   r#   )NN)r`   r   ra   r   rb   z	list[str]rc   r!   rd   dict[str, str]re   boolrf   t.Optional[str]rg   r   r>   z'tuple[t.Optional[str], t.Optional[str]])rn   r   r>   r!   )r`   r   rc   r!   rD   r!   r>   r   )r`   r   r>   r!   )r>   r!   )r>   r?   ):r*   
__future__r   dataclassesr:   rR   r   typingtrn   r   r   r   ior   r	   r
   utilr   r   r   r   r   r   rf   r   util_commonr   r   r   host_configsr   r   r   r   r   r   	constantsr   r   threadr   	dataclassr    rB   r   r4   rH   rP   rM   rh   rp   r_   rv   r   r   r   r   r,   r-   r.   <module>r      s   S "  	        

 d#    $  GQ0  02  7^$ ^ =&4 !H
HH 
H 	H
 
H H H 
H -H" 
  	@  2."J	v r-   