B
    u9a©
  ã               @   sv   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 G dd„ dejƒZedkrre ¡  dS )é    N)Úcache_from_source)Úcreate_empty_filec               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú
TestImportc             O   sR   d| _ x&| j tjkr,|  j t tj¡7  _ qW | j d | _tj	j
| f|ž|Ž d S )NZPACKAGE_z.foo)Úpackage_nameÚsysÚmodulesÚrandomÚchooseÚstringÚascii_lettersÚmodule_nameÚunittestÚTestCaseÚ__init__)ÚselfÚargsÚkw© r   ú$/usr/lib/python3.7/test_pkgimport.pyr      s
    zTestImport.__init__c             C   s,   x&| j | jfD ]}|tjkrtj|= qW d S )N)r   r   r   r   )r   r   r   r   r   Úremove_modules   s    
zTestImport.remove_modulesc             C   sb   t  ¡ | _tj | j¡ tj | j| j¡| _	t 
| j	¡ ttj | j	d¡ƒ tj | j	d¡| _d S )Nz__init__.pyzfoo.py)ÚtempfileZmkdtempÚtest_dirr   ÚpathÚappendÚosÚjoinr   Zpackage_dirÚmkdirr   Úmodule_path)r   r   r   r   ÚsetUp   s    


zTestImport.setUpc             C   s<   t  | j¡ |  tj | j¡d¡ tj | j¡ |  ¡  d S )Nr   )	ÚshutilZrmtreer   ZassertNotEqualr   r   ÚcountÚremover   )r   r   r   r   ÚtearDown#   s    zTestImport.tearDownc          	   C   sF   t | jƒ}tj |¡r t |¡ t| jdƒ}| |¡ W d Q R X d S )NÚw)r   r   r   r   Úexistsr!   ÚopenÚwrite)r   ÚcontentsZcompiled_pathÚfr   r   r   Úrewrite_file)   s
    

zTestImport.rewrite_filec             C   sê   |   d¡ yt| jƒ W n tk
r,   Y n
X tdƒ‚|  | jtj¡ |  t	tj| j
 dƒ¡ d}x |ttƒkr‚|t tj¡7 }qdW |   |¡ yt| jƒ W n tk
r°   Y n
X tdƒ‚|   d| ¡ t| jƒj}|  t||ƒd¡ d S )NÚforzFailed to induce SyntaxErrorÚfooÚazFailed to induce NameError.z%s = 1é   )r)   Ú
__import__r   ÚSyntaxErrorÚRuntimeErrorZassertNotInr   r   ZassertFalseÚhasattrr   ÚdirÚ__builtins__r   r	   r
   r   Ú	NameErrorr+   ZassertEqualÚgetattr)r   ÚvarÚmoduler   r   r   Útest_package_import__semantics0   s(    
  
  z)TestImport.test_package_import__semanticsN)	Ú__name__Ú
__module__Ú__qualname__r   r   r   r"   r)   r8   r   r   r   r   r      s   	r   Ú__main__)r   r   r   r
   r   r   r   Úimportlib.utilr   Ztest.supportr   r   r   r9   Úmainr   r   r   r   Ú<module>   s   C