B
    u9a`&  ã               @   s^   d dl Z d dlZd dlZd dlZd dlZdd„ Zdd„ ZG dd„ dejƒZe	dkrZe 
¡  dS )	é    Nc             C   s`   t  | ¡}xF|D ]>}t j | |¡}t j |¡rDt j |¡sDt|ƒ qt  |¡ qW t  | ¡ d S )N)	ÚosÚlistdirÚpathÚjoinÚisdirÚislinkÚcleanoutÚremoveÚrmdir)ÚrootÚnamesÚnameÚfullname© r   ú/usr/lib/python3.7/test_pkg.pyr      s    


r   c             C   s(   d| kr|   d¡ d| kr$|   d¡ | S )NÚ__builtins__Ú__initializing__)r	   )Zlstr   r   r   Úfixdir   s
    

r   c               @   s€   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Ze ejjdkd¡dd„ ƒZdS )ÚTestPkgc             C   s$   d | _ d | _ttjƒ| _tƒ | _d S )N)r   ÚpkgnameÚlistÚsysr   ÚsyspathÚsetÚmodules_to_cleanup)Úselfr   r   r   ÚsetUp0   s    zTestPkg.setUpc                sv   ˆ j tjd d …< x ˆ jD ]}|tjkrtj|= qW ˆ jrBtˆ jƒ ˆ jrr‡ fdd„tjD ƒ}x|D ]}tj|= qbW d S )Nc                s    g | ]}ˆ j | d ¡kr|‘qS )Ú.)r   Úsplit)Ú.0r   )r   r   r   ú
<listcomp>@   s    z$TestPkg.tearDown.<locals>.<listcomp>)r   r   r   r   Úmodulesr   r   r   )r   Z
modulenamer!   r   r   )r   r   ÚtearDown6   s    


zTestPkg.tearDownc             C   s   t t |¡tƒ d| iƒ d S )Nr   )ÚexecÚtextwrapÚdedentÚglobals)r   Úcoder   r   r   Úrun_codeE   s    zTestPkg.run_codec       	   
   C   sØ   t  ¡ }tj d|¡ tj |¡s,t |¡ x’|D ]Š\}}| ¡ }| j	 
d |¡¡ |}x|D ]}tj ||¡}q^W |d krˆt |¡ q2t|dƒ$}| |¡ | d¡s²| d¡ W d Q R X q2W || _|d d | _d S )Nr   r   ÚwÚ
)ÚtempfileZmkdtempr   r   Úinsertr   r   Úmkdirr   r   Úaddr   ÚopenÚwriteÚendswithr   r   )	r   Zdescrr   r   ÚcontentsÚcompsr   ÚcÚfr   r   r   ÚmkhierH   s$    



zTestPkg.mkhierc             C   s   ddg}|   |¡ dd l}d S )N)Út1N)zt1 __init__.pyÚ r   )r6   r7   )r   Úhierr7   r   r   r   Útest_1^   s    
zTestPkg.test_1c             C   s.  ddddddg}|   |¡ dd l}dd l}|  |jd¡ |  |jjd	¡ |  |jjjd
¡ d}|  |¡ ddlm} ddlm} ddlm	} |  |jd	¡ |  |jd
¡ |  |jjd
¡ x"dD ]}|  
tƒ d d| ¡ q¾W dd l}dd l}|  |jd¡ |  |jjd	¡ |  |jjjd
¡ d}|  |¡ d S )N)Út2N)zt2 __init__.pyz'doc for t2')zt2 subN)zt2 sub __init__.pyr8   )zt2 sub subsubN)zt2 sub subsub __init__.pyzspam = 1r   r;   zt2.subzt2.sub.subsubz{
            import t2
            from t2 import *
            self.assertEqual(dir(), ['self', 'sub', 't2'])
            )Úsub)Úsubsub)Úspam)r>   r<   r=   r;   r   zFailed to import %sz_
            from t2 import *
            self.assertEqual(dir(), ['self', 'sub'])
            )r6   Zt2.subZt2.sub.subsubÚassertEqualÚ__name__r<   r=   r(   r;   r>   Z
assertTrueÚlocals)r   r9   r;   Úsr<   r=   r>   r   r   r   r   Útest_2c   s:    


zTestPkg.test_2c             C   sV   ddddddg}|   |¡ dd l}|  |jd¡ |  |jjd	¡ |  |jjjd
¡ d S )N)Út3N)zt3 __init__.pyr8   )zt3 subN)zt3 sub __init__.pyr8   )zt3 sub subsubN)zt3 sub subsub __init__.pyzspam = 1r   rD   zt3.subzt3.sub.subsub)r6   Zt3.sub.subsubr?   r@   r<   r=   )r   r9   rD   r   r   r   Útest_3’   s    
zTestPkg.test_3c          	   C   s2   ddddddddd	g	}|   |¡ d
}|  |¡ d S )N)zt4.pyz)raise RuntimeError('Shouldnt load t4.py'))Zt4N)zt4 __init__.pyr8   )z	t4 sub.pyz*raise RuntimeError('Shouldnt load sub.py'))zt4 subN)zt4 sub __init__.pyr8   )zt4 sub subsub.pyz-raise RuntimeError('Shouldnt load subsub.py'))zt4 sub subsubN)zt4 sub subsub __init__.pyzspam = 1z[
            from t4.sub.subsub import *
            self.assertEqual(spam, 1)
            )r6   r(   )r   r9   rB   r   r   r   Útest_4¢   s    
zTestPkg.test_4c             C   s®   ddddg}|   |¡ dd l}d}|  |¡ dd l}|  tt|ƒƒddd	d
dddddddg¡ |  tt|jƒƒddd	d
ddddg¡ |  tt|jƒƒddd	d
ddddg¡ d S )N)Út5N)zt5 __init__.pyzimport t5.foo)zt5 string.pyzspam = 1)z	t5 foo.pyz-from . import string; assert string.spam == 1r   zo
            from t5 import *
            self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
            Ú
__cached__Ú__doc__Ú__file__Ú
__loader__r@   Ú__package__Ú__path__Ú__spec__ÚfooÚstringrG   r>   )r6   rG   r(   r?   r   ÚdirrO   rP   )r   r9   rG   rB   r   r   r   Útest_5·   s&    

zTestPkg.test_5c             C   sX   dddddg}|   |¡ dd l}|  tt|ƒƒddd	d
dddddg	¡ d}|  |¡ d S )N)Út6N)zt6 __init__.pyz!__all__ = ['spam', 'ham', 'eggs'])z
t6 spam.pyr8   )z	t6 ham.pyr8   )z
t6 eggs.pyr8   r   Ú__all__rH   rI   rJ   rK   r@   rL   rM   rN   aœ  
            import t6
            from t6 import *
            self.assertEqual(fixdir(dir(t6)),
                             ['__all__', '__cached__', '__doc__', '__file__',
                              '__loader__', '__name__', '__package__',
                              '__path__', '__spec__', 'eggs', 'ham', 'spam'])
            self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
            )r6   rS   r?   r   rQ   r(   )r   r9   rS   rB   r   r   r   Útest_6Ô   s    
	zTestPkg.test_6c       	      C   s.  ddddddddd	g	}|   |¡ d
\}}}dd l}|  tt|ƒƒddddddddg¡ |  |¡ ddlm} |  tt|ƒƒddddddddg¡ |  |¡ |  |¡ ddlm} |  tt|ƒƒdddddddddg	¡ |  |¡ |  |¡ |  |¡ ddl	m
} |  |d¡ |  |¡ |  |¡ |  |¡ d S )N)zt7.pyr8   )Út7N)zt7 __init__.pyr8   )z	t7 sub.pyz*raise RuntimeError('Shouldnt load sub.py'))zt7 subN)zt7 sub __init__.pyr8   )z
t7 sub .pyz-raise RuntimeError('Shouldnt load subsub.py'))zt7 sub subsubN)zt7 sub subsub __init__.pyzspam = 1)NNNr   rH   rI   rJ   rK   r@   rL   rM   rN   )r<   )r=   r>   )r>   é   )r6   rV   r?   r   rQ   ZassertFalser<   Zt7.subr=   Zt7.sub.subsubr>   )	r   r9   rV   r<   r=   ZtasZsubparZ	subsubsubZhamr   r   r   Útest_7ï   sF    









zTestPkg.test_7é   z)Docstrings are omitted with -O2 and abovec             C   s:   ddt j d dfg}|  |¡ dd l}|  |jd¡ d S )N)Út8Nzt8 __init__Úpyz'doc for t8'r   z
doc for t8)r   Úextsepr6   rZ   r?   rI   )r   r9   rZ   r   r   r   Útest_8  s
    
zTestPkg.test_8N)r@   Ú
__module__Ú__qualname__r   r"   r(   r6   r:   rC   rE   rF   rR   rU   rX   ÚunittestZskipIfr   ÚflagsÚoptimizer]   r   r   r   r   r   .   s   /,r   Ú__main__)r   r   r+   r$   r`   r   r   ZTestCaser   r@   Úmainr   r   r   r   Ú<module>   s   
 z