o
    §…ËYã,  ã                   @   s^  d dl Z de _[ dZdZd dlZd dlZd dlZd dlZzd dlm	Z	 W n e
y3   d dl	m	Z	 Y nw d dlZd dlmZmZmZmZ d dl mZ dd	„ Zdadadad
d„ Zdd„ Zd(dd„Zdd„ Zd)dd„ZG dd„ de	ƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ dejƒZ G dd „ d ejƒZ!G d!d"„ d"ejƒZ"d*d$d%„Z#d&d'„ Z$dS )+é    NTz3.3.0a  Provides support for the test suite.

The test suite as a whole, and individual tests, need to share
certain support functions.  We have to put these in here so they
can always be imported, and so that individual tests need to import
nothing more than "reportlab.whatever..."
)ÚConfigParser)ÚisCompactDistroÚ__rl_loader__Úrl_isdirÚ	asUnicode)Úasciic                 C   sP   z!d}t |dƒ}| d¡ | ¡  tj |¡rt |¡ W dS W d S    Y dS )Nz00DELETE.MEÚwz$test of writability - can be deletedé   r   )ÚopenÚwriteÚcloseÚosÚpathÚisfileÚremove)ÚDÚfnÚf© r   úI/var/www/html/env/lib/python3.10/site-packages/reportlab/lib/testutils.pyÚ
isWritable   s   


þr   c                 C   sf  t rt S dd„ tjD ƒ}|r*|d a zt t ¡ W n   Y |D ]}tj |¡ q n| dks2J dƒ‚tj tjd ¡}|sAt ¡ }|a t	t ƒsKt
dƒa ddl}|jd atj t¡sdtj tj t¡¡atj t¡}tj |d	¡atj t¡s‚tj tj |¡d	¡atj t¡s¤| dkr¢tj tjd ¡}|s›t ¡ }tj |¡andatr±tj dtj t¡¡ t S )
zÆIs it a writable file system distro being invoked within
    test directory?  If so, can write test output here.  If not,
    it had better go in a temp directory.  Only do this once per
    processc                 S   s"   g | ]}|  d ¡r|dd… ‘qS )z	--outdir=é	   N©Ú
startswith)Ú.0Údr   r   r   Ú
<listcomp>/   s   " zsetOutDir.<locals>.<listcomp>éÿÿÿÿÚ__main__z2setOutDir should only be called in the main scriptr   Úreportlab_testNÚtests)Ú_OUTDIRÚsysÚargvr   Úmakedirsr   r   ÚdirnameÚgetcwdr   Úget_rl_tempdirÚ	reportlabÚ__path__ÚRL_HOMEÚisabsÚnormpathÚabspathÚjoinÚtestsFolderÚisdirÚinsert)Únamer   r   Ú	scriptDirr(   ÚtopDirr   r   r   Ú	setOutDir(   s>   
 r5   c                 C   s   t tƒ}| rtj || ¡}|S )zêThis works out where to write test output.  If running
    code in a locked down file system, this will be a
    temp directory; otherwise, the output of 'test_foo.py' will
    normally be a file called 'test_foo.pdf', next door.
    )r5   Ú__name__r   r   r.   )r   r   r   r   r   Ú
outputfileR   s   r7   r	   c                 C   s>   t  | ¡j d¡dkrtdƒ}|tkrtd| ƒ d S d S d S )Nr6   r   Ú z,Logs and output files written to folder "%s")r"   Ú	_getframeÚf_localsÚgetr7   r!   Úprint)ÚdepthÚoutDirr   r   r   ÚprintLocation\   s   ýr?   c                  G   s.   t  ¡ }t  ¡ }| D ]
}| | |¡¡ q
|S )z<Return a test suite with tests loaded from provided classes.)ÚunittestÚ	TestSuiteÚ
TestLoaderÚaddTestÚloadTestsFromTestCase)ÚclassesÚsuiteÚloaderÚCr   r   r   ÚmakeSuiteForClassesb   s
   rI   c                 C   sŒ   t jj}z
t|| ddƒƒ}W n ty   g  Y S w g }| ¡ D ]#}|r*|d dks2|rC|d dkrC| d¡d }|rC| || |ƒ¡ q |S )aG  Returns a list of filenames as listed in the CVS/Entries file.

    'folder' is the folder that should contain the CVS subfolder.
    If there is no such subfolder an empty list is returned.
    'files' is a boolean; 1 and 0 means to return files or not.
    'folders' is a boolean; 1 and 0 means to return folders or not.
    ÚCVSÚEntriesr   r   ú/r	   )r   r   r.   r
   ÚIOErrorÚ	readlinesÚsplitÚappend)ÚfolderÚfilesÚfoldersr.   r   Ú
allEntriesÚlineÚentryr   r   r   ÚgetCVSEntriesk   s"   	ÿÿ€rW   c                   @   s"   e Zd ZdZe d¡Zdd„ ZdS )ÚExtConfigParserz7A slightly extended version to return lists of strings.z\s*\[.*\]\s*c                 C   s2   t  | ||¡}| dd¡}| j |¡rt|ƒS |S )zECoerce option to a list of strings or return unchanged if that fails.Ú
r8   )r   r;   ÚreplaceÚpatÚmatchÚeval)ÚselfÚsectionÚoptionÚvalueÚvalr   r   r   ÚgetstringlistŽ   s
   zExtConfigParser.getstringlistN)r6   Ú
__module__Ú__qualname__Ú__doc__ÚreÚcompiler[   rc   r   r   r   r   rX   ‰   s    
rX   c                   @   s*   e Zd ZdZd
dd„Zdd„ Zdd„ Zd	S )ÚGlobDirectoryWalkerz<A forward iterator that traverses files in a directory tree.Ú*c              	   C   sÐ   d| _ || _| dtj¡ tj |¡r|g| _g | _d S t	ƒ r%t
r%t|ƒs+td| ƒ‚|tt
jƒttjƒ d … | _| jtj }t|ƒ}tt|fdd„tt|fdd„tt
j ¡ ƒƒƒƒƒ| _| j ¡  g | _d S )Nr   rL   z"%s" is not a directoryc                 S   s   | |d … S ©Nr   )ÚxÚnr   r   r   Ú<lambda>°   s    z.GlobDirectoryWalker.__init__.<locals>.<lambda>c                 S   s
   |   |¡S rk   r   )rl   Úpfxr   r   r   rn   °   s   
 )ÚindexÚpatternrZ   r   Úsepr   r0   ÚstackrR   r   r   r   Ú
ValueErrorÚlenÚarchiveÚ	directoryÚlistÚmapÚfilterÚ_filesÚkeysÚsort)r^   rw   rq   ro   rm   r   r   r   Ú__init__£   s   
4

zGlobDirectoryWalker.__init__c                 C   s¬   	 z| j | j }| jd | _W n" ty1   | j ¡ | _t | j¡| _ |  | j| j ¡| _ d| _Y n$w tj	 
| j|¡}tj	 |¡rLtj	 |¡sL| j |¡ t || j¡rU|S q)Nr	   r   )rR   rp   Ú
IndexErrorrs   Úpoprw   r   ÚlistdirÚfilterFilesr   r.   r0   ÚislinkrP   Úfnmatchrq   )r^   rp   ÚfileÚfullnamer   r   r   Ú__getitem__´   s    
ú	ïzGlobDirectoryWalker.__getitem__c                 C   s   |S )z/Filter hook, overwrite in subclasses as needed.r   )r^   rQ   rR   r   r   r   r‚   È   s   zGlobDirectoryWalker.filterFilesN)rj   )r6   rd   re   rf   r~   r‡   r‚   r   r   r   r   ri       s
    
ri   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )	ÚRestrictedGlobDirectoryWalkerz&An restricted directory tree iterator.rj   Nc                 C   sn   t  | ||¡ |d krg }g | _t|ƒtg ƒkr%|D ]}| j |¡ qd S t|ƒtdƒkr5| j |¡ d S d S )Nr8   )ri   r~   ÚignoredPatternsÚtyperP   )r^   rw   rq   ÚignoreÚpr   r   r   r~   Ñ   s   ÿÿz&RestrictedGlobDirectoryWalker.__init__c                 C   s\   g }t t|ƒƒD ]}|| }| jD ]}t ||¡r| |¡ qq| ¡  |D ]}||= q&|S )z9Filters all items from files matching patterns to ignore.)Úrangeru   r‰   r„   rP   Úreverse)r^   rQ   rR   ÚindicesToDeleteÚir   rŒ   r   r   r   r‚   Þ   s   

€þz)RestrictedGlobDirectoryWalker.filterFiles)rj   N)r6   rd   re   rf   r~   r‚   r   r   r   r   rˆ   Î   s    
rˆ   c                   @   s   e Zd ZdZdd„ ZdS )ÚCVSGlobDirectoryWalkerz4An directory tree iterator that checks for CVS data.c                 C   sf   t jj}t|ƒ}|r1g }tt|ƒƒD ]}|| }|||ƒ|vr$| |¡ q| ¡  |D ]}||= q+|S )zçFilters files not listed in CVS subfolder.

        This will look in the CVS subfolder of 'folder' for
        a file named 'Entries' and filter all elements from
        the 'files' list that are not listed in 'Entries'.
        )r   r   r.   rW   r�   ru   rP   rŽ   )r^   rQ   rR   r.   ÚcvsFilesr�   r�   r   r   r   r   r‚   ñ   s   
€z"CVSGlobDirectoryWalker.filterFilesN)r6   rd   re   rf   r‚   r   r   r   r   r‘   î   s    r‘   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSecureTestCaseaý  Secure testing base class with additional pre- and postconditions.

    We try to ensure that each test leaves the environment it has
    found unchanged after the test is performed, successful or not.

    Currently we restore sys.path and the working directory, but more
    of this could be added easily, like removing temporary files or
    similar things.

    Use this as a base class replacing unittest.TestCase and call
    these methods in subclassed versions before doing your own
    business!
    c                 C   s   t jdd… | _t ¡ | _dS )z0Remember sys.path and current working directory.N)r"   r   Ú_initialPathr   r&   Ú_initialWorkDir©r^   r   r   r   ÚsetUp  s   zSecureTestCase.setUpc                 C   s   | j t_t | j¡ dS )z0Restore previous sys.path and working directory.N)r”   r"   r   r   Úchdirr•   r–   r   r   r   ÚtearDown  s   zSecureTestCase.tearDownN)r6   rd   re   rf   r—   r™   r   r   r   r   r“   
  s    r“   c                   @   s   e Zd Zddd„ZeeƒZdS )ÚNearTestCaseçñhãˆµøä>c                 C   sj   t | ttfƒrt| | ƒ|krtd| |f ƒ‚d S t| |ƒD ]\}}t|| ƒ|kr2td| |f ƒ‚qd S )Nz%s not near %s)Ú
isinstanceÚfloatÚintÚabsÚAssertionErrorÚzip)ÚaÚbÚaccuracyÚaeÚber   r   r   Ú
assertNear$  s   ÿÿÿzNearTestCase.assertNearN)r›   )r6   rd   re   r§   Ústaticmethodr   r   r   r   rš   #  s    
rš   c                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚScriptThatMakesFileTestzyRuns a Python script at OS level, expecting it to produce a file.

    It CDs to the working directory to run the script.r   c                 C   s(   || _ || _|| _|| _tj | ¡ d S rk   )r3   Ú
scriptNameÚoutFileNameÚverboser@   ÚTestCaser~   )r^   r3   rª   r«   r¬   r   r   r   r~   2  s
   z ScriptThatMakesFileTest.__init__c                 C   sr   t  ¡ | _| j}t j |¡st j t|¡}t  |¡ t j 	| j
¡s(J d| j
 ƒ‚t j 	| j¡r7t  | j¡ d S d S )NzScript %s not found!)r   r&   Úcwdr3   r   r+   r.   r/   r˜   r   rª   r«   r   )r^   r3   r   r   r   r—   :  s   

ÿzScriptThatMakesFileTest.setUpc                 C   s   t  | j¡ d S rk   )r   r˜   r®   r–   r   r   r   r™   F  s   z ScriptThatMakesFileTest.tearDownc                 C   sh   t jdkrdpd}t |t j| jf d¡}| ¡ }| jr t|ƒ | 	¡ }tj
 | j¡s2J d| j ƒ‚d S )NÚwin32z"%s" %sz%s %sÚrzFile %s not created!)r"   Úplatformr   ÚpopenÚ
executablerª   Úreadr¬   r<   r   r   r   r«   )r^   ÚfmtrŒ   ÚoutÚstatusr   r   r   ÚrunTestI  s    zScriptThatMakesFileTest.runTestN)r   )r6   rd   re   rf   r~   r—   r™   r¸   r   r   r   r   r©   .  s    
r©   Úutf8c                 C   s,   t | ƒt |ƒkr| |kS t| |ƒt||ƒkS rk   )rŠ   r   )r¢   r£   Úencr   r   r   ÚequalStringsR  s   ,r»   c                 C   s(   | |krt dt|ƒt| ƒf ƒ d S d S )NzStrings unequal
exp: %s
got: %s)r<   r   )r°   rl   r   r   r   ÚeqCheckU  s   ÿr¼   )r	   )r	   r   )r¹   )%r(   Ú_rl_testingÚ__version__rf   r"   r   r„   rg   Úconfigparserr   ÚImportErrorr@   Úreportlab.lib.utilsr   r   r   r   r   r   r!   r*   r/   r5   r7   r?   rI   rW   rX   ri   rˆ   r‘   r­   r“   rš   r©   r»   r¼   r   r   r   r   Ú<module>   s@    ÿ*


	. 
$