o
    tBh/C                     @  s  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Zd dlZd dl	m
Z
mZmZmZmZ d dlmZ e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mZ d d	lm Z m!Z!m"Z"m#Z# ej$j%Z%d
Z&dpddZ'dqddZ(drddZ)dsddZ*dtd d!Z+dud$d%Z,dvd'd(Z-ej.dwd*d+Z/dxd/d0Z0dxd1d2Z1dyd5d6Z2dzd8d9Z3d{d|d?d@Z4d}dCdDZ5d~ddHdIZ6ddLdMZ7ddPdQZ8ddUdVZ9ddYdZZ:dd]d^Z;ddadbZ<	cdddfdgZ=ddidjZ>dddndoZ?dS )    )annotationsN)AnyCallableIterableIteratorPattern)TestCase)defaults)process_options)Options)test_data_prefixtest_temp_dir)DataDrivenTestCase
DeleteFile
UpdateFilefix_cobertura_filename   args	list[str]returnNonec                 C  sR   d}t | ddg \}}}|dkr'tj| tj| tjddd d S d S )NTz--show-tracebackz--no-silence-site-packagesr   zSample check failedF)msgpytrace)apirunsysstdoutwritestderrpytestfail)r   __tracebackhide__outvalerrvalstatus r%   h/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/mypy/test/helpers.pyrun_mypy#   s   r'   expectedactualr   strc                 C  sn  d}t |}dd |D }dd | D } || kr5t| |}t| |}tjd |dkr3tjd d}d	}t|t| | D ]F}|t|ksP| | || krd|dk rV|}tjd
| | dd n| | }	tjd
|	d|   t|	|krtjd tjd q@|dkrtjd tjd |dkrtjd t|t|| D ]@}
|
t| ks| |
 ||
 krtjd
||
 dd n||
 }tjd
|d|   t||krtjd tjd q|stjd |dkrtjd tjd d|  krt|k r1n t
|t| | tks(t|| tkr1t	| | ||  t
|dS )zAssert that two string arrays are equal.

    We consider "can't" and "cannot" equivalent, by replacing the
    former with the latter before comparing.

    Display any differences in a human-readable form.
    Tc                 S     g | ]}| d dqS zcan'tcannotreplace.0liner%   r%   r&   
<listcomp>9       z.assert_string_arrays_equal.<locals>.<listcomp>c                 S  r+   r,   r.   r0   r%   r%   r&   r3   :   r4   z
Expected:
r   z  ...
K   z  z<45z (diff)N...
zActual:
z
  (empty)
)clean_upnum_skipped_prefix_linesnum_skipped_suffix_linesr   r   r   rangelenMIN_LINE_LENGTH_FOR_ALIGNMENTshow_align_messageAssertionError)r(   r)   r   r!   num_skip_startnum_skip_end
first_diffwidthiejar%   r%   r&   assert_string_arrays_equal.   s^   


rI   nameIterable[str]c              	   C  sB   t |}t t|dh}t||dd|d||  d S )N__main__zEActual modules ({}) do not match expected modules ({}) for "[{} ...]", )sortedset
differencerI   formatjoin)rJ   r(   r)   expected_normalizedactual_normalizedr%   r%   r&   assert_module_equivalence{   s   rU   c              	   C  s&   t ||dd|d||  dS )z6Compare actual and expected targets (order sensitive).zEActual targets ({}) do not match expected targets ({}) for "[{} ...]"rM   N)rI   rQ   rR   )rJ   r(   r)   r%   r%   r&   assert_target_equivalence   s   rV   testcaser   outputc              
   C  s  | j d us	J dtj| j | j}t|dd}|  }W d    n1 s)w   Y  d|| j| j	 }i }t
| j|D ]9\}}d}	||	}
|
dkrq|d |
 |d |
 krq||
t|	 d  ||
t|	 d  }}||g | qA|D ]2}||t|| kr||}ddlm} |d gt|t
|| |d	d   }d
|}q}|g|| j| j	< d|}t|ddd}t||d W d    d S 1 sw   Y  d S )Nztest was not properly set uputf8encodingr8   zerror:r5   r   )chain    w)file)old_cwdospathrR   r`   openread
splitlinesr2   	last_lineziprX   findr=   
setdefaultappendcountsplit	itertoolsr\   listfrom_iterableprint)rW   rX   testcase_pathf
data_linestestmappingoldnewPREFIXindbetweensr\   interleaveddatar%   r%   r&   update_testcase_output   s6   
 *



"r~   s1s2c                 C  s`  t | dk rdS d}tjd d}| dd |dd kr8| dd } |dd }d}| dd |dd ks|rBd	|  } d	| }tt | t |}d
}||krSd	}tjd| d|  | d tjd|d|  | d tjd tt|tt | t |D ] }| ||d  |||d  krtjd  ntjd qtjd dS )aQ  Align s1 and s2 so that the their first difference is highlighted.

    For example, if s1 is 'foobar' and s2 is 'fobar', display the
    following lines:

      E: foobar
      A: fobar
           ^

    If s1 and s2 are long, only display a fragment of the strings around the
    first difference. If s1 is very short, do nothing.
       NH   z$Alignment of first line difference:
F   
   Tr7   r^   z  E: r8   z  A: z     r]   ^ )r=   r   r   r   maxr<   min)r   r   maxwtruncmax_lenextrarG   r%   r%   r&   r?      s4       r?   rH   c                 C  s   g }t  }|d }| D ]@}t j}|}||t jdfD ]}|dkr4|dkr4|dkr4|dkr4||d}qtdd|}||d}|td	d| q|S )
zRemove common directory prefix from all strings in a.

    This uses a naive string replace; it seems to work well enough. Also
    remove trailing carriage returns.
    z
/driver.py/z//\z\\r^   z +$z	driver.pyz\r$)rb   getcwdsepr/   resubrk   )rH   respwddriversprefixsspr%   r%   r&   r9      s    r9   Iterator[None]c                  c  sN    t jdd } dt jv sdt jv st jdd z	dV  W | t _dS | t _w )zTemporary insert current directory into sys.path.

    This can be used by test cases that do runtime imports, for example
    by the stubgen tests.
    Nr^   .r   )r   rc   insert)old_sys_pathr%   r%   r&   local_sys_path_set   s   r   a1a2intc                 C  sf   d}|t t| t|k r,| | || kr,|d7 }|t t| t|k r,| | || kstd|d S Nr   r]   r   r   r=   r   r   r   num_eqr%   r%   r&   r:     s
   &&r:   c                 C  s~   d}|t t| t|k r8| | d  || d  kr8|d7 }|t t| t|k r8| | d  || d  kstd|d S r   r   r   r%   r%   r&   r;     s
   22r;   rc   tuple[int, int]c                 C  s   |  drdS tjS )Nzpython310.test)   r   )endswithr	   PYTHON3_VERSIONrc   r%   r%   r&   testfile_pyversion  s   
r   messagesc                 C  s&   g }| D ]}| |tjd q|S )z@Translate an array of error messages to use / as path separator.r   )rk   r/   rb   r   )r   rH   mr%   r%   r&   normalize_error_messages#  s   r         ?funcCallable[[], Any]max_waitfloatc                 C  s^   t   }d}	 z|   W dS  ty-   t|d || t    }|dkr& t | Y nw q)a   Retry callback with exponential backoff when it raises OSError.

    If the function still generates an error after max_wait seconds, propagate
    the exception.

    This can be effective against random file system operation failures on
    Windows.
    g{Gz?TN   )timeOSErrorr   sleep)r   r   t0	wait_timer%   r%   r&   retry_on_error,  s   	r   objobjectc                 C  sj   t | tr1| ddkr1dg}| dD ]}|td| dd  q|d  d7  < d|S t| S )Nr8   r]   z'''\"r   r5   z''')
isinstancer*   rl   rm   rk   reprrR   )r   bitsr2   r%   r%   r&   	good_reprC  s   

r   {} != {}bfmtc                 C  s(   d}| |krt |t| t|d S )NT)r@   rQ   r   )rH   r   r   r!   r%   r%   r&   assert_equalO  s   r   ttypec                 C  s4   dt | v rt | dd dS t | dd S )Nr   r5   z'>   )r*   rm   rstrip)r   r%   r%   r&   typenameU  s   r   typvaluec                 C  s4   d}t || krtdtt | dt|  d S )NTzInvalid type z, expected )r   r@   r   )r   r   r!   r%   r%   r&   assert_type\  s    r   program_textincremental_stepr   c                 C  s   t  }tjd| tjd}|dkr!tjd| d| tjd}|r!|}|r>|d }|d t|dd\}}|r=td	ng }t  }d|_	d|_
td
d |D rXt|j|_|jdre|jd|_|S )z4Parse comments like '# flags: --foo' in a test case.z# flags: (.*)$)flagsr]   z# flagsz: (.*)$z--no-site-packagesF)require_targetsz9Specifying targets via the flags pragma is not supported.c                 s  s"    | ]}| d d dvV  qdS )=r   )z--python-versionz-2z--py2N)rm   )r1   flagr%   r%   r&   	<genexpr>|  s     z parse_options.<locals>.<genexpr>z--mypy-verbose)r   r   search	MULTILINEgrouprm   rk   r
   RuntimeErrorstrict_optionalerror_summaryallr   r`   python_versionconfig	getoption	verbosity)r   rW   r   optionsr   flags2	flag_listtargetsr%   r%   r&   parse_optionsb  s,   
r   streamsbytesc                  G  s   dd | D S )zDReturns a single list of string lines from the byte streams in args.c                 S  s$   g | ]}| d  D ]}|qqS )rY   )decoderf   )r1   streamr   r%   r%   r&   r3     s   $ zsplit_lines.<locals>.<listcomp>r%   )r   r%   r%   r&   split_lines  s   r   contenttarget_pathc                 C  s   d }t j|rt |jd }t j|}t j|dd t|ddd}||  W d    n1 s4w   Y  |rFt j	|||fd d S d S )Nr]   T)exist_okr_   zutf-8rZ   )times)
rb   rc   isfilestatst_mtimedirnamemakedirsrd   r   utime)r   r   new_timedirtargetr%   r%   r&   write_and_fudge_mtime  s   r   
operationslist[UpdateFile | DeleteFile]c                   sj   | D ]0}t |trt|j|j qtj|jr'|jds J t	
|j q|j t fdd qd S )Ntmpc                     s
   t  S N)rb   remover%   r   r%   r&   <lambda>  s   
 z)perform_file_operations.<locals>.<lambda>)r   r   r   r   r   rb   rc   isdir
startswithshutilrmtreer   )r   opr%   r   r&   perform_file_operations  s   
r  r^   stepstrip_prefixc              	   C  s&  | j D ]\}}||r|t|d  }tj|s+td|| jr'd| dt	|dd}|
 }W d    n1 s@w   Y  t|trZ||d urRqtd||t| tjt}| jr}| jjrytjjdkrydd	 |D }t|}t| |d
|| jrd| nd qd S )Nz0Expected file {} was not produced by test case{}z on step %dr^   rY   rZ   zCOutput file {} did not match its expected output pattern
---
{}
---r   c                 S  s   g | ]}t |qS r%   )r   r0   r%   r%   r&   r3     s    z+check_test_output_files.<locals>.<listcomp>z2Output file {} did not match its expected output{})output_filesr   r=   rb   rc   existsr@   rQ   output2rd   re   r   r   	fullmatchnormalize_file_outputrf   abspathr   normalize_outputsuite
native_sepr   r   rI   )rW   r  r  rc   expected_contentoutput_fileactual_output_contentnormalized_outputr%   r%   r&   check_test_output_files  sL   


r  current_abs_pathc                   sl   t dfdd| D }tjjfdd|D }ttjd  fdd|D }fdd|D }|S )z%Normalize file output for comparison.z\d{10}c                   s   g | ]}|  d qS )z$PWDr.   r1   x)r  r%   r&   r3     r4   z)normalize_file_output.<locals>.<listcomp>c                   (   g | ]}t d t   d  d|qS z\bz$VERSIONr   r   escaper  )versionr%   r&   r3        ( base_versionc                   r  r  r  r  )r  r%   r&   r3     r  c                   s   g | ]}  d |qS )z
$TIMESTAMP)r   r  )timestamp_regexr%   r&   r3     r4   )r   compilemypyr  __version__getattr)r   r  resultr%   )r  r  r  r  r&   r    s   
r  patternexcludelist[str] | Nonec                   s    fddt t| D S )Nc                   s    g | ]}|j  p	g vr|j qS r%   )rJ   )r1   rc   r&  r%   r&   r3     s
    z#find_test_files.<locals>.<listcomp>)pathlibPathr   rglob)r%  r&  r%   r(  r&   find_test_files  s   
r,  )r   r   r   r   )r(   r   r)   r   r   r*   r   r   )rJ   r*   r(   rK   r)   rK   r   r   )rJ   r*   r(   r   r)   r   r   r   )rW   r   rX   r   r   r   )r   r*   r   r*   r   r   )rH   r   r   r   )r   r   )r   r   r   r   r   r   )rc   r*   r   r   )r   r   r   r   )r   )r   r   r   r   r   r   )r   r   r   r*   )r   )rH   r   r   r   r   r*   r   r   )r   r   r   r*   )r   r   r   r   r   r   )r   r*   rW   r   r   r   r   r   )r   r   r   r   )r   r*   r   r*   r   r   )r   r   r   r   )r^   )rW   r   r  r   r  r*   r   r   )r   r   r  r*   r   r   r   )r%  r*   r&  r'  r   r   )@
__future__r   
contextlibrb   r)  r   r  r   r   typingr   r   r   r   r   unittestr   Suiter   mypy.apir   mypy.versionr!  r	   	mypy.mainr
   mypy.optionsr   mypy.test.configr   r   mypy.test.datar   r   r   r   markskipr>   r'   rI   rU   rV   r~   r?   r9   contextmanagerr   r:   r;   r   r   r   r   r   r   r   r   r   r   r  r  r  r,  r%   r%   r%   r&   <module>   s^    


M


!
4



	




#


*