o
    µ+ke9  ã                   @   sŠ   d 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 ddlmZ e	e
eeegZdZG d	d
„ d
eƒZdd„ ZdS )af  
Self testing module
===================

The selftest() function runs a small test suite of relatively fast tests that are meant to discover issues
with the way Borg was compiled or packaged and also bugs in Borg itself.

These tests are a subset of the borg/testsuite and are run with Pythons built-in unittest, hence none of
the tests used for this can or should be ported to py.test currently.

To assert that self test discovery works correctly the number of tests is kept in the SELFTEST_COUNT
variable. SELFTEST_COUNT must be updated if new tests are added or removed to or from any of the tests
used here.
é    N)Ú
TestResultÚ	TestSuiteÚdefaultTestLoaderé   )ÚHashIndexDataTestCaseÚHashIndexRefcountingTestCaseÚHashIndexTestCase)ÚCryptoTestCase)ÚChunkerTestCaseé!   c                       s@   e Zd Z‡ fdd„Z‡ fdd„Zdd„ Zdd„ Zd	d
„ Z‡  ZS )ÚSelfTestResultc                    s   t ƒ  ¡  g | _d S ©N)ÚsuperÚ__init__Ú	successes©Úself©Ú	__class__© ú1usr/lib/python3.10/site-packages/borg/selftest.pyr   (   s   

zSelfTestResult.__init__c                    s   t ƒ  |¡ | j |¡ d S r   )r   Ú
addSuccessr   Úappend©r   Útestr   r   r   r   ,   s   zSelfTestResult.addSuccessc                 C   s   |  ¡ pt|ƒS r   )ZshortDescriptionÚstrr   r   r   r   Ú	test_name0   s   zSelfTestResult.test_namec                 C   sX   | j | j | j D ]\}}| d|  |¡|¡ q	| jD ]\}}| d|  |¡|¡ qd S )Nzself test %s FAILED:
%szself test %s skipped: %s)ÚerrorsZfailuresZunexpectedSuccessesÚerrorr   ZskippedÚwarning)r   Úloggerr   ZfailureÚreasonr   r   r   Úlog_results3   s
   ÿzSelfTestResult.log_resultsc                 C   s
   t | jƒS r   )Úlenr   r   r   r   r   Úsuccessful_test_count9   s   
z$SelfTestResult.successful_test_count)	Ú__name__Ú
__module__Ú__qualname__r   r   r   r"   r$   Ú__classcell__r   r   r   r   r   '   s    r   c           	      C   sö   t j d¡dkr|  d¡ d S t ¡ }tƒ }tƒ }tD ]}t	j
|j }dt|ƒvs0J d|j ƒ‚| t |¡¡ q| |¡ | | ¡ | ¡ }|tk}| ¡ rX|rX|  d|t¡ | ¡ r^|rl|  d¡ t	 d¡ J d
ƒ‚t ¡ | }|  d||¡ d S )NZBORG_SELFTESTÚdisabledz5borg selftest disabled via BORG_SELFTEST env variableZpytestz!pytest must not be imported in %szwself test count (%d != %d) mismatch, either test discovery is broken or a test was added without updating borg.selftestzmself test failed
Could be a bug either in Borg, the package / distribution you use, your OS or your hardware.é   Fz.sanity assertion failed: ran beyond sys.exit()z'%d self tests completed in %.2f seconds)ÚosÚenvironÚgetÚdebugÚtimeÚperf_counterr   r   ÚSELFTEST_CASESÚsysÚmodulesr&   Údirr%   ZaddTestr   ZloadTestsFromTestCaseÚrunr"   r$   ÚSELFTEST_COUNTZwasSuccessfulr   Úexit)	r    Zselftest_startedÚresultZ
test_suiteZ	test_caseÚmoduleZsuccessful_testsZcount_mismatchZselftest_elapsedr   r   r   Úselftest=   s0   


þ

r:   )Ú__doc__r+   r2   r/   Zunittestr   r   r   Ztestsuite.hashindexr   r   r   Ztestsuite.cryptor	   Ztestsuite.chunkerr
   r1   r6   r   r:   r   r   r   r   Ú<module>   s"   û