o
    tBh9                     @  s  d Z ddlmZ ddlZddlZddlZddlmZ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m Z m!Z!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) dZ*G dd de Z+dddZ,dS )a  Test cases for fine-grained incremental checking.

Each test cases runs a batch build followed by one or more fine-grained
incremental steps. We verify that each step produces the expected output.

See the comment at the top of test-data/unit/fine-grained.test for more
information.

N.B.: Unlike most of the other test suites, testfinegrained does not
rely on an alt_lib_path for finding source files. This means that they
can test interactions with the lib_path that is built implicitly based
on specified sources.
    )annotationsN)Anycast)build)parse_config_file)Server)DEFAULT_STATUS_FILE)CompileError)create_source_list)BuildSource)Options)check_consistency)test_temp_dir)DataDrivenTestCase	DataSuite
DeleteFile
UpdateFile)assert_module_equivalenceassert_string_arrays_equalassert_target_equivalencefind_test_filesparse_optionsperform_file_operationsFc                   @  s   e Zd ZeddgdZdZd>d	d
Zd?ddZd@ddZdAddZ	dBddZ
dCd d!ZdDd$d%ZdEd-d.ZdFd0d1ZdGd4d5ZdHd6d7ZdId9d:ZdId;d<Zd=S )JFineGrainedSuitezfine-grained*.testz#fine-grained-cache-incremental.test)patternexcludeFtestcaser   returnboolc                 C  sX   | j r|jdkr
dS |jr|jd dkrdS n|jdkrdS d|jv r*tjdk r*dS dS )	Nz-only_when_nocacheTr   ==z-only_when_cacheInspect)      F)	use_cache	only_whenoutputnamesysversion_info)selfr    r*   p/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/mypy/test/testfinegrained.pyshould_skip9   s   

zFineGrainedSuite.should_skipNonec                 C  s  |  |rt  d S d|j}tjtd}t|ddd}|	| W d    n1 s/w   Y  | j
||dd}| j
||dd}t|t}| |}d	}	| ||	|}
|	|krb| ||
}n| ||
}g }|rs|t| |jsxJ || |	|||jj || |	|| |jrtrt|j | }g }|D ]#}|	d	7 }	| |||||||	|\}}|d
 || || qdd |D }t|j|d|j d|j  d |j!rt|j!| "|d|j d|j  d d S d S )N
mainwutf8)encodingF)build_cacheT   r   c                 S  s   g | ]}| d dqS )\/)replace).0liner*   r*   r+   
<listcomp>   s    z-FineGrainedSuite.run_case.<locals>.<listcomp>zInvalid output (z, line )zInvalid active triggers ()#r,   pytestskipjoininputospathr   openwriteget_optionsr   r   get_build_stepsparse_sourcesr   	run_checkextendnormalize_messagestmpdirmaybe_suggestr&   maybe_inspectfine_grained_managerCHECK_CONSISTENCYr   
find_stepsperform_stepappendr   r%   filer9   	triggeredformat_triggered)r)   r   main_src	main_pathfoptionsbuild_optionsservernum_regular_incremental_stepsstepsourcesmessagesastepsall_triggered
operationsr%   rS   r*   r*   r+   run_caseL   sl   







zFineGrainedSuite.run_casesourcestrr3   r   c                 C  s   t ||dd}d|_d|_d|_d|_| |_| jo| |_| j|_d|_	d|_
td|d u r3d|_|jD ]\}}d|v sBd|v rMt|d	d
 |  |S q6|S )Nr4   )incremental_stepTFzflags:.*--follow-importserrorzmypy.inizpyproject.tomlc                   S  s   d S )Nr*   r*   r*   r*   r+   <lambda>   s    z.FineGrainedSuite.get_options.<locals>.<lambda>)r   incrementaluse_builtins_fixturesshow_tracebackerror_summaryfine_grained_incrementalr#   use_fine_grained_cachecache_fine_grainedlocal_partial_typesenable_incomplete_featuresresearchfollow_importsfilesr   )r)   rd   r   r3   rX   r&   _r*   r*   r+   rD      s&   zFineGrainedSuite.get_optionsrZ   r   r]   list[BuildSource]	list[str]c                 C  s0   |j |dddd}tt|d p|d }| S )NTF)export_typesis_ttyterminal_widthouterr)checkr   re   
splitlines)r)   rZ   r]   responser}   r*   r*   r+   rG      s   zFineGrainedSuite.run_checkrX   c              
   C  sB   zt j ||d}W |jS  ty  } z	|jW  Y d }~S d }~ww )N)r]   rX   )r   r	   r^   errors)r)   rX   r]   resulter*   r*   r+   r      s   zFineGrainedSuite.buildrS   list[list[str]]c                 C  sP   g }t |D ]\}}dd |D }t|}|d|d d|f   q|S )Nc                 S  s   g | ]	}| d s|qS )z__>)endswith)r8   triggerr*   r*   r+   r:          z5FineGrainedSuite.format_triggered.<locals>.<listcomp>z%d: %s   z, )	enumeratesortedrQ   r>   strip)r)   rS   r   ntriggersfilteredr*   r*   r+   rT      s   "z!FineGrainedSuite.format_triggeredprogram_textintc                 C  s6   | j sdS tjd|tjd}|durt|dS dS )zHGet the number of regular incremental steps to run, from the test sourcer   z# num_build_steps: ([0-9]+)$flagsNr4   )r#   rr   rs   	MULTILINEr   group)r)   r   mr*   r*   r+   rE      s   z FineGrainedSuite.get_build_stepsrb   list[UpdateFile | DeleteFile]rY   rU   r\   r[   !tuple[list[str], list[list[str]]]c	                 C  s\  t | | |||}	||kr| ||	}
n| ||	}
g }g }g }g }|jrFtr.t|j ||jj |jj	}dd |jj
D }|jj}|j|d }|dur^tdt|d  || |j|d }|durvtdt|d  || |j|}|rtdt| || t|
}
|
}|jsJ || ||||jj || ||| ||fS )zPerform one fine-grained incremental build step (after some file updates/deletions).

        Return (mypy output, triggered targets).
        c                 S  s   g | ]\}}|qS r*   r*   )r8   modrR   r*   r*   r+   r:      s    z1FineGrainedSuite.perform_step.<locals>.<listcomp>r4   Nstale	recheckedtargets)r   rF   r   rG   rM   rN   r   rQ   rS   updated_moduleschanged_modulesprocessed_targetsexpected_stale_modulesgetr   re   expected_rechecked_modulesexpected_fine_grained_targetsr   rI   rJ   rH   rK   r&   rL   )r)   rb   rZ   rX   rY   r   rU   r\   r[   r]   new_messagesupdatedchangedr   rS   expected_staleexpected_recheckedexpectedr_   r*   r*   r+   rP      s>   

zFineGrainedSuite.perform_steprf   c           	      C  s   t jd|t jd}d| d}t j||t jd}|dur|}|r3dd |d  D }t||S ttj	
td	d
d}|gttg|dd S )a!  Return target BuildSources for a test case.

        Normally, the unit tests will check all files included in the test
        case. This differs from how testcheck works by default, as dmypy
        doesn't currently support following imports.

        You can override this behavior and instruct the tests to check
        multiple modules by using a comment like this in the test case
        input:

          # cmd: main a.py

        You can also use `# cmdN:` to have a different cmd for incremental
        step N (2, 3, ...).

        z# cmd: mypy ([a-zA-Z0-9_./ ]+)$r   z# cmdz: mypy ([a-zA-Z0-9_./ ]+)$Nc                 S  s   g | ]	}t jt|qS r*   )r@   rA   r>   r   )r8   rA   r*   r*   r+   r:     r   z2FineGrainedSuite.parse_sources.<locals>.<listcomp>r4   r/   __main__T)allow_empty_dir)rr   rs   r   r   r   splitr
   r   r@   rA   r>   r   )	r)   r   rf   rX   r   regexalt_mpathsbaser*   r*   r+   rF      s   
zFineGrainedSuite.parse_sourcessrctmp_dirc                 C  s&  g }|  ||}|D ]\}}d|v }	d|v }
d|v }d|v }td|}|r-t|dnd }td|}|r<|dnd }td|}|rMt|dnd }|j| |	|||||
|d	}d
|v rf|d
 n|d |d  }|	r|dd}|t	j
|t	j
j d}|| d q
t|S )Nz--jsonz--callsitesz--no-anyz--no-errorsz--flex-any=([0-9.]+)r4   z--use-fixme=(\w+)z--max-guesses=([0-9]+))jsonno_any	no_errorsflex_any	use_fixme	callsitesmax_guessesrg   r}   r~   z\\r5    r.   )get_suggestrr   matchfloatr   r   cmd_suggestr   r7   r@   rA   realpathseprH   r   rI   )r)   r\   rZ   r   r   r%   r   r   targetr   r   r   r   r   r   r   r   resvalr*   r*   r+   rK   "  s:    
zFineGrainedSuite.maybe_suggestc                 C  s   g }|  ||}|D ]n\}}td|}|r|dnd}	d}
d|v r%d}
d|v r+d}
td|}|r:t|dnd}d	|v }d
|v }d|v }d|v }d|v }|j|	||
||||||d	}d|v rf|d n|d |d  }|| d q
t	|S )Nz--show=(\w+)r4   typer   z-vz-vvr   z--limit=([0-9]+)z--include-spanz--include-kindz--include-object-attrsz--union-attrsz--force-reload)	verbositylimitinclude_spaninclude_kindinclude_object_attrsunion_attrsforce_reloadrg   r}   r~   r.   )
get_inspectrr   r   r   r   cmd_inspectrH   r   r   rI   )r)   r\   rZ   r   r%   r   r   locationr   showr   r   r   r   r   r   r   r   r   r*   r*   r+   rL   B  s>    zFineGrainedSuite.maybe_inspectlist[tuple[str, str]]c                 C  6   |dkrdnt |}d| d}tj||tjd}|S )Nr4   1?z	# suggestz3: (--[a-zA-Z0-9_\-./=?^ ]+ )*([a-zA-Z0-9_.:/?^ ]+)$r   re   rr   findallr   r)   r   rf   step_bitr   r   r*   r*   r+   r   c     zFineGrainedSuite.get_suggestc                 C  r   )Nr4   r   z	# inspectz1: (--[a-zA-Z0-9_\-=?^ ]+ )*([a-zA-Z0-9_.:/?^ ]+)$r   r   r   r*   r*   r+   r   i  r   zFineGrainedSuite.get_inspectN)r   r   r   r   )r   r   r   r-   )rd   re   r   r   r3   r   r   r   )rZ   r   r]   rw   r   rx   )rX   r   r]   rw   r   rx   )rS   r   r   rx   )r   re   r   r   )rb   r   rZ   r   rX   r   rY   r   r   r   rU   re   r\   r   r[   r   r   r   )r   re   rf   r   rX   r   r   rw   )
r\   r   rZ   r   r   re   r   re   r   rx   )r\   r   rZ   r   r   re   r   rx   )r   re   rf   r   r   r   )__name__
__module____qualname__r   ru   r#   r,   rc   rD   rG   r   rT   rE   rP   rF   rK   rL   r   r   r*   r*   r*   r+   r   0   s$    


C




	
9
&
 
!r   r^   rx   r   c                 C  s   dd | D S )Nc                 S  s&   g | ]}t d t tj d|qS )z^tmpr   )rr   subescaper@   r   )r8   messager*   r*   r+   r:   q  s   & z&normalize_messages.<locals>.<listcomp>r*   )r^   r*   r*   r+   rI   p  s   rI   )r^   rx   r   rx   )-__doc__
__future__r   r@   rr   r'   typingr   r   r<   mypyr   mypy.config_parserr   mypy.dmypy_serverr   mypy.dmypy_utilr   mypy.errorsr	   mypy.find_sourcesr
   mypy.modulefinderr   mypy.optionsr   mypy.server.mergecheckr   mypy.test.configr   mypy.test.datar   r   r   r   mypy.test.helpersr   r   r   r   r   r   rN   r   rI   r*   r*   r*   r+   <module>   s0     
  B