U =_D,@sdZddlmZddlZddlZddlZddlZddlZddlZddl Z z ddl Z Wne k rldZ YnXz ddl Z Wne k rdZ YnXddl Z ddlmZddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZd ZdZe pdZe o*eZej ej!e"Z#e j$j%Z%d$ddZ&GdddZ'Gddde j(Z)Gddde j*Z+Gddde+Z,d%ddZ-ddZ.dd Z/d!d"Z0e1d#kre0dS)&aA Unit test runner, providing new features on top of unittest module: - colourized output - parallel run (UNIX only) - print failures/tracebacks on CTRL+C - re-run failed tests only (make test-failed) Invocation examples: - make test - make test-failed Parallel: - make test-parallel - make test-process ARGS=--parallel )print_functionN)hilite) print_color)term_supports_colors)super) CI_TESTING)import_module_by_path) print_sysinfo) reap_children) safe_rmpathz.failed-tests.txtFcCsB|dkr|dkrtjntj}tr2t||||dn t||ddS)Nred)boldfile)r)sysstderrstdout USE_COLORSrprint)msgZcolorrrrF/opt/alt/python38/lib64/python3.8/site-packages/psutil/tests/runner.pycprint>s rc@sXeZdZeZdgZdejkr,edddgddZ dd Z d d Z d d Z ddZ dS) TestLoaderztest_memleaks.pyZWHEELHOUSE_UPLOADER_USERNAMEz test_osx.pyz test_linux.pyz test_posix.pycsfddtjDS)Ncs:g|]2}|dr|dr|jkrtjj|qS)Ztest_.py) startswithendswith skip_filesospathjointestdir).0xselfrr Os   z,TestLoader._get_testmods..)rlistdirr"r%rr%r _get_testmodsNs  zTestLoader._get_testmodsccsN|D]@}t|}t|D]*}t||}t|trt|tjr|VqqdS)zgIterate over all test files in this directory and return all TestCase classes in them. N) r)rdirgetattr isinstancetype issubclassunittestTestCase)r&r modnameobjrrr_iter_testmod_classesTs     z TestLoader._iter_testmod_classescCs,t}|D]}t|}||q|SN)r/ TestSuiter4loadTestsFromTestCaseaddTest)r&suiter3testrrrall`s   zTestLoader.allc Cs^t}tjts|Sttd}|}W5QRX|D]}tj |}| |q>|S)NZrt) r/r6rr isfileFAILED_TESTS_FNAMEopenreadsplitdefaultTestLoaderloadTestsFromNamer8)r&r9fnamesnr:rrr last_failedgs    zTestLoader.last_failedcCs.|dr"tjtj|d}tj|S)Nrr)rrr splitextbasenamer/rArB)r&r2rrr from_namess zTestLoader.from_nameN)__name__ __module__ __qualname__HEREr"rrenvironextendr)r4r;rFrIrrrrrGs   rcs<eZdZddZddZddZddZfd d ZZS) ColouredResultcCstj||tdddS)NZOKgreen)r/ TestResult addSuccessr)r&r:rrrrS{szColouredResult.addSuccesscCs"tj|||tdddddS)NERRORrTr)r/rRaddErrorrr&r:errrrrrVszColouredResult.addErrorcCstj|||tdddS)NZFAILr)r/rR addFailurerrWrrrrYszColouredResult.addFailurecCs&tj|||td|ddS)Nz skipped: %sZbrown)r/rRaddSkiprstrip)r&r:reasonrrrrZszColouredResult.addSkipcs$t|d|dkd}t||dS)NrrTrU)rrprintErrorList)r&Zflavourerrors __class__rrr]szColouredResult.printErrorList) rJrKrLrSrVrYrZr] __classcell__rrr_rrPys rPcsfeZdZdZerenejZfddZ fddZ ddZ dd Z fd d Z d d ZddZZS)ColouredTextRunnerz A coloured text runner which also prints failed tests on KeyboardInterrupt and save failed tests in a file so that they can be re-run. cstj||t|_dSr5)r__init__set failed_tnames)r&argskwargsr_rrrcszColouredTextRunner.__init__cst|_|jSr5)r _makeResultresultr%r_rrrhs zColouredTextRunner._makeResultc Cs:|jr6ttd }|jD]}||dqW5QRXdS)NZwt )rer>r=write)r&rCtnamerrr_write_last_faileds  z%ColouredTextRunner._write_last_failedcCs6|s2|j|jD]}|d}|j|qdS)Nr) wasSuccessfulr^failuresidreadd)r&ritrlrrr _save_results zColouredTextRunner._save_resultc sVzt|}Wn2ttfk rB|jj}|tdYnX| ||SdS)Nr ) rrunKeyboardInterrupt SystemExitrunnerri printErrorsrexitrsr&r9rir_rr_runs zColouredTextRunner._runcCsJ|r&tddddtttdn tdddd|tddS) NSUCCESSrQTrUrZFAILEDrr )rr r=rryrm)r&successrrr_exits zColouredTextRunner._exitcCs||}||dSr5)r{r~rnrzrrrrts zColouredTextRunner.run)rJrKrL__doc__rrPr/TextTestResultZ resultclassrcrhrmrsr{r~rtrarrr_rrbs    rbc@s,eZdZeddZeddZddZdS)ParallelRunnercs0fdd}tj|tj_tt}t||S)Ncs||}t|j|Sr5)atexitregisterclose)fdmodekwdsstreamZ orig_fdopenrrfdopens  z+ParallelRunner._parallelize..fdopen)rrconcurrencytestZfork_for_testsNWORKERSZConcurrentTestSuite)r9rZforkerrrr _parallelizes   zParallelRunner._parallelizecCst}t}|D]n}|dkr(qn8t|tjrB|jdj}nt|tjrT|}n td|t|ddrx| |q| |q||fS)Nrzcan't recognize type %rZ _serialrunF) r/r6countTestCasesr,Z_testsr`r0 TypeErrorr+r8)r9serialparallelr:Z test_classrrr _split_suites       zParallelRunner._split_suitecCsX||\}}||}tdtdddt}||}t|}t}tj |dd\}} | r~td| dt t}||} t|} | s| d kr| tt|j|j|jf\} } }tt| j| j| jf\}}}ttd |j| | ||| j|||| f td |j| j|| tf| oH| }||dS) Nz(starting parallel tests using %s workersrQTrUr )Ztimeoutzalive processes %srra= +----------+----------+----------+----------+----------+----------+ | | total | failures | errors | skipped | time | +----------+----------+----------+----------+----------+----------+ | parallel | %3s | %3s | %3s | %3s | %.2fs | +----------+----------+----------+----------+----------+----------+ | serial | %3s | %3s | %3s | %3s | %.2fs | +----------+----------+----------+----------+----------+----------+ z&Ran %s tests in %.3fs using %s workers)rrrrtimer{psutilZProcessZchildrenZ wait_procsr rnrrxmaplenror^ZskippedrtextwrapdedentZtestsRunr~)r&r9Z ser_suiteZ par_suiterrZparZ par_elapsedZorphansZgoneZaliveZserZ ser_elapsedZ par_failsZpar_errsZ par_skipsZ ser_failsZser_errsZ ser_skipsokrrrrtsV           zParallelRunner.runN)rJrKrL staticmethodrrrtrrrrrs   rcCsTdd}|rJtjr|dn.tdkr.|dntdkr@|dn ttdSttdS)NcSst|dddS)Nz Running serial tests instead.r)r)rrrrwarnszget_runner..warnz$Can't run parallel tests on Windows.z(concurrencytest module is not installed.r zOnly 1 CPU available.) verbosity)rZWINDOWSrrr VERBOSITYrb)rrrrr get_runners    rcCs t|}t}||dSr5)rrIrrt)r2r9rwrrr run_from_name,s rcCstjjdSr5)rZ _psplatformZcextZ set_testingrrrrsetup2srcCstd}tj|dd}|jddddd|jd ddd d|\}}|jsVttt}|rt |d kr~| t d S| |d }n|jr|}n|}trtt|j}||dS) Nz*python3 -m psutil.tests [opts] [test-name]zrun unit tests)usage descriptionz --last-failed store_trueFzonly run last failed tests)actiondefaulthelpz --parallelzrun tests in parallelr r)roptparseZ OptionParserZ add_option parse_argsrFr r=rrZ print_usagerryrIr;rr rrrt)rparserZoptsrfloaderr9rwrrrmain9s:     r__main__)FN)F)2rZ __future__rrrrrrrr/Zctypes ImportErrorrrZpsutil._commonrrrZpsutil._compatrZ psutil.testsrrr r r rr= cpu_countrrr abspathdirname__file__rMrAr7rrrrPZTextTestRunnerrbrrrrrrJrrrrsV               27U "