wL i22dZddlZddlmZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z mZddlmZddlmZdd lmZdd lmZdd lmZmZdd lmZd dlmZd dlmZej@dk(r ejBn ejDZ#e#de$de%e$de%e$fdZ&dZ'de'_(dZ)dZ*d)dZ+d*dZ, d+dZ-Gddej\Z/e Z0e1ejddZ3dZ4Gd d!Z5d"Z6Gd#d$e5Z7e d%Z8d&Z9d,d'Z:d,d(Z;y)-zWGeneric testing tools. Authors ------- - Fernando Perez N)Path)contextmanager)StringIO)PopenPIPE)patch)Config)get_output_error_code) list_strings) temp_pyfileTee) py3compat) decorators) skipdoctestwin32 startPathfilesreturnct|tsJtjj |d}|Dcgc]"}tjj ||$c}Scc}w)aMake full paths for all the listed files, based on startPath. Only the base part of startPath is kept, since this routine is typically used with a script's ``__file__`` variable as startPath. The base of startPath is then prepended to all the listed files, forming the output list. Parameters ---------- startPath : string Initial path to use as the base for the results. This path is split using os.path.split() and only its first component is kept. files : list One or more files. Examples -------- >>> full_path('/foo/bar.py',['a.txt','b.txt']) ['/foo/a.txt', '/foo/b.txt'] >>> full_path('/foo',['a.txt','b.txt']) ['/a.txt', '/b.txt'] r) isinstancelistospathsplitjoin)rrbasefs [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/IPython/testing/tools.py full_pathr &sN6 eT "" " 77== #A &D+0 2aRWW\\$q ! 22 2s'A#ctjd|tj}|r t|j d}d}||fStjd|tj}|r d}t|j d}||fStjd|tj}|r8t|j d}t|j d}||fSy)aParse the output of a test run and return errors, failures. Parameters ---------- txt : str Text output of a test run, assumed to contain a line of one of the following forms:: 'FAILED (errors=1)' 'FAILED (failures=1)' 'FAILED (errors=1, failures=1)' Returns ------- nerr, nfail number of errors and failures. z^FAILED \(errors=(\d+)\)rrz^FAILED \(failures=(\d+)\)z(^FAILED \(errors=(\d+), failures=(\d+)\))rr)research MULTILINEintgroup)txterr_mnerrnfailfail_mboth_ms rparse_test_outputr.Fs& II13 EE 5;;q>"e  YY4c2<< HF FLLO$e  YYBC||%F 6<<?#FLLO$e  Fc gdS)zEReturn a valid default argv for creating testing instances of ipython)z--quickz--colors=nocolorz--no-term-titlez --no-bannerz --autocall=0r1r/r default_argvr2ts  r/c8t}d|j_d|j_d|j_t jdd}t|j|j_ |jd|j_ |S)z6Return a config object with good defaults for testing.nocolorFrztest_hist.sqliteF)suffixdeletei')r TerminalInteractiveShellcolors TerminalTerminalInteractiveShell term_titleautocalltempfileNamedTemporaryFilernameHistoryManager hist_fileclose db_cache_size)configrs rdefault_configrEsx XF-6F##*9AF++6/0F##,##+=eLA&*166lF#GGI*/F' Mr/cRtjddg}|rdj|}|S)aW Return appropriate IPython command line name. By default, this will return a list that can be used with subprocess.Popen, for example, but passing `as_string=True` allows for returning the IPython command as a string. Parameters ---------- as_string: bool Flag to allow to return the command as a string. z-mIPython )sys executabler) as_string ipython_cmds rget_ipython_cmdrMs+>>43Khh{+ r/cd}|g}t|z}tjjt}t }tjj ||}||zd|gz}tjj} d| d<| jdd| jD]"\} } t| trt| | $t|ttt| } | j!t#j$dj |xsd \} }t#j&| t#j&|}} | rt)j*d d | } | |fS) aUtility to call 'ipython filename'. Starts IPython with a minimal and safe configuration to make startup as fast as possible. Note that this starts IPython in a subprocess! Parameters ---------- fname : str, Path Name of file to be executed (should have .py or .ipy extension). options : optional, list Extra command-line flags to be passed to IPython. commands : optional, list Commands to send in on stdin Returns ------- ``(stdout, stderr)`` of ipython subprocess. TNz--ignorePYTHONWARNINGSPYCHARM_HOSTED)stdoutstderrstdinenv )inputz \x1b\[[^h]+h)r2rrdirname__file__rMrenvironcopypopitemsrstrprintrr communicaterencodedecoder#sub)fnameoptionscommands__tracebackhide__cmdargstest_dirrL full_fnamefull_cmdrUkvpouterrs ripexecrrs>.nw&Gwwx(H!#Kh.JW$j'99H **// C%CGG d# 1!S! !QK  htD#FA}}9#3#3DIIh4G#H#PD}QHC$i&6&6s&;C ff_b#. 8Or/rXcd}t|||\}}|rp|r]dj|jjdj|jjk(sJt d|d|dj|jjdj|jjk(sJy)a9Utility to call 'ipython filename' and validate output/error. This function raises an AssertionError if the validation fails. Note that this starts IPython in a subprocess! Parameters ---------- fname : str, Path Name of the file to be executed (should have .py or .ipy extension). expected_out : str Expected stdout of the process. expected_err : optional, str Expected stderr of the process. options : optional, list Extra command-line flags to be passed to IPython. Returns ------- None TrVz Running file z produced error: N)rrrstrip splitlines ValueError)re expected_out expected_errrfrgrhrprqs ripexec_validaterys4eWh/HC  99SYY[3356$))""$//1; #S*+ + 99SYY[++- .$))'')3  r/c*eZdZdZddZdZdZdZy) TempFileMixinzcUtility class to create temporary Python/IPython files. Meant as a mixin class for test cases.ct||}t|dsg|_|jj|||_y)zMake a valid python temp file.tmpsN)r hasattrr}appendre)selfsrcextres rmktmpzTempFileMixin.mktmps8C%tV$DI  r/czt|dr(|jD]} tj|yy#Y!xYw)Nr})r~r}runlink)rres rtearDownzTempFileMixin.tearDownsC 4  IIe$   ! s6:c|SNr1rs r __enter__zTempFileMixin.__enter__$s r/c$|jyr)r)rexc_type exc_value tracebacks r__exit__zTempFileMixin.__exit__'s  r/N)z.py)__name__ __module__ __qualname____doc__rrrrr1r/rr{r{ s. r/r{zEDid not find {0!r} in printed output (on {1}): ------- {2!s} ------- c$eZdZdZddZdZdZy) AssertPrintszContext manager for testing that code prints certain text. Examples -------- >>> with AssertPrints("abc", suppress=False): ... print("abcd") ... print("def") ... abcd def c||_t|jttfr|jg|_||_||_yr)srr__re_typechannelsuppress)rrrrs r__init__zAssertPrints.__init__As6 dffsHo .ffXDF   r/c0tt|j|_t |_t |j |j|_tt|j|jr|j y|jy)N)r) getattrrIr orig_stream MyStringIObufferr teesetattrrrs rrzAssertPrints.__enter__HsW"3 5 l t{{DLL9T\\$--4;;NTXXNr/cd} | |jjy|jjtt|j |j |jj}|jD]~}t|trB|j|r%Jtj|j|j |||vrZJtj||j | |jjy#|jjwxYwNTF)rrBflushrrIrrrgetvaluerrrr$notprinted_msgformatpatternretypevaluerrhprintedrs rrzAssertPrints.__exit__Ns    HHNN  HHNN  Ct'7'7 8kk**,GVV Ya*88G,en.C.CAIIt||]d.ee,<X)>)>q$,,PW)XX<  Y  HHNN DHHNN sD$B D$,4D$!'D$$EN)rRT)rrrrrrrr1r/rrr5s !O r/rz>Found {0!r} in printed output (on {1}): ------- {2!s} ------- ceZdZdZdZy)AssertNotPrintszcContext manager for checking that certain output *isn't* produced. Counterpart of AssertPrintscd} |6|jj |jjy|jjtt|j |j |jj}|jD]~}t|trB|j|s%Jtj|j|j |||vrZJtj||j | |jjy#|jjwxYwr)rrBrrrIrrrrrrrr$ printed_msgrrrs rrzAssertNotPrints.__exit__ks     HHNN  HHNN  Ct'7'7 8kk**,GVV 2a* xx0:1C1C 4<<2::0G+2[-?-?4<<.22+  2 HHNN DHHNN sD>B D>4D>;'D>>EN)rrrrrr1r/rrrgs #r/rc#Kt|ddj dtj|y#tj|wxYww)zGCreate an empty, named, temporary file for the duration of the context.wzutf-8)encodingN)openrBrr)r?s r make_tempfilers; sW%++-  $ $sA:AAAc>t|dfd }td|S)aTemporarily replace the input() function to return the given values Use as a context manager: with fake_input(['result1', 'result2']): ... Values are returned in order. If input() is called again after the last value was used, EOFError is raised. cV tS#t$r}td|d}~wwxYw)NzNo more inputs given)next StopIterationEOFError)prompteits r mock_inputzfake_input..mock_inputs/ :8O :12 9 :s  ( #(zbuiltins.inputrX)iterr)inputsrrs @r fake_inputrs# fB: !: ..r/ct|dgz}t|\}}}|dk(sJ|d|vsJd|vsJd|vsJ||fS)z)test that `ipython [subcommand] -h` worksz-hr TracebackOptions --help-allrMr  subcommandcmdrprqrcs rhelp_output_testrsh  z40 0C(-LCb 7C7 c !! !    3   8Or/ct|dgz}t|\}}}|dk(sJ|d|vsJd|vsJd|vsJ||fS)z1test that `ipython [subcommand] --help-all` worksrrrrClassrrs rhelp_all_output_testrsf  z<8 8C(-LCb 7C7 c !! !    c>> 8Or/r5)Nr1)rXNr1r)rs6  %"*7+-# ,/<<7+B{''  33T#Y34933>'V#  $7t79+-,^H%%D   3  **X l4/(r/