U
    ui                     @   sL   d Z ddlZddlZddlZddlZddlZG dd dZG dd dZdS )zdevelop tests    Nc                   @   s0   e Zd Zdd Zedd Zdd Zdd Zd	S )
TestSandboxc              	   C   s,   t jt| | tj W 5 Q R X d S N)
setuptoolssandboxDirectorySandboxstr_file_writerosdevnull)selftmpdir r   A/tmp/pip-unpacked-wheel-9osl9suo/setuptools/tests/test_sandbox.pytest_devnull   s    zTestSandbox.test_devnullc                    s    fdd}|S )Nc               	      s(   t  ddd} | d W 5 Q R X d S )Nwutf-8encodingZxxx)openwrite)fpathr   r   do_write   s    z*TestSandbox._file_writer.<locals>.do_writer   )r   r   r   r   r   r      s    zTestSandbox._file_writerc                 C   s:   t td}td}tj|t| |j	dks6t
dS )zT
        It should be possible to execute a setup.py with a Byte Order Mark
        zscript-with-bom.py	namespaceZpassedN)pkg_resourcesresource_filename__name__types
ModuleTyper   r   	_execfilevarsresultAssertionError)r   targetr   r   r   r   test_setup_py_with_BOM   s    
z"TestSandbox.test_setup_py_with_BOMc              	   C   s@   |d }| d}|d W 5 Q R X tjt|t  d S )Nzsetup.pywbs   "degenerate script"
)r   r   r   r   r    r   globals)r   r   setup_pystreamr   r   r   test_setup_py_with_CRLF"   s    z#TestSandbox.test_setup_py_with_CRLFN)r   
__module____qualname__r   staticmethodr   r%   r*   r   r   r   r   r      s
   
	r   c                   @   sD   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S )TestExceptionSaverc              	   C   s"   t j  tdW 5 Q R X d S Ndetails)r   r   ExceptionSaver
ValueErrorr   r   r   r   test_exception_trapped*   s    z)TestExceptionSaver.test_exception_trappedc              	   C   sb   t j }tdW 5 Q R X tt}|  W 5 Q R X t|jtsLt	t
|jdks^t	d S r/   )r   r   r1   r2   pytestraisesresume
isinstancevaluer#   r   )r   	saved_exccaughtr   r   r   test_exception_resumed.   s    z)TestExceptionSaver.test_exception_resumedc              	   C   sb   t d}tj 
}|W 5 Q R X tt }|  W 5 Q R X t|jt sPt	|j|k	s^t	d S r/   )
r2   r   r   r1   r5   r6   r7   r8   r9   r#   )r   Zorig_excr:   r;   r   r   r   test_exception_reconstructed8   s    z/TestExceptionSaver.test_exception_reconstructedc              	   C   s"   t j }W 5 Q R X |  d S r   )r   r   r1   r7   )r   r:   r   r   r    test_no_exception_passes_quietlyD   s    
z3TestExceptionSaver.test_no_exception_passes_quietlyc              	   C   sf   G dd dt }tj }|dW 5 Q R X ttjj}|  W 5 Q R X t|j	dksbt
d S )Nc                   @   s   e Zd ZdZedddZdS )zFTestExceptionSaver.test_unpickleable_exception.<locals>.CantPickleThisz:This Exception is unpickleable because it's not in globals)returnc                 S   s   d| j f S )NzCantPickleThis%r)argsr3   r   r   r   __repr__N   s    zOTestExceptionSaver.test_unpickleable_exception.<locals>.CantPickleThis.__repr__N)r   r+   r,   __doc__r   rA   r   r   r   r   CantPickleThisK   s   rC   ZdetailzCantPickleThis('detail',))	Exceptionr   r   r1   r5   r6   UnpickleableExceptionr7   r   r9   r#   )r   rC   r:   r;   r   r   r   test_unpickleable_exceptionJ   s    z.TestExceptionSaver.test_unpickleable_exceptionc              
   C   sh   G dd dt }ttjj*}tj  tj  |W 5 Q R X W 5 Q R X |jj	\}|dksdt
dS )z
        As revealed in #440, an infinite recursion can occur if an unpickleable
        exception while setuptools is hidden. Ensure this doesn't happen.
        c                   @   s   e Zd ZdZdS )zaTestExceptionSaver.test_unpickleable_exception_when_hiding_setuptools.<locals>.ExceptionUnderTestzE
            An unpickleable exception (not in globals).
            N)r   r+   r,   rB   r   r   r   r   ExceptionUnderTest_   s   rG   zExceptionUnderTest()N)rD   r5   r6   r   r   rE   save_moduleshide_setuptoolsr9   r@   r#   )r   rG   r;   msgr   r   r   2test_unpickleable_exception_when_hiding_setuptoolsY   s    

zETestExceptionSaver.test_unpickleable_exception_when_hiding_setuptoolsc                 C   s   dd }t tjjH}tj 2 tj  tjt| |  W 5 Q R X W 5 Q R X W 5 Q R X |j	j
\}}}|dks~t|dkst|ddikstt|j	}d|kstd|kstd|kstd	S )
z
        When in a sandbox with setuptools hidden, a SandboxViolation
        should reflect a proper exception and not be wrapped in
        an UnpickleableException.
        c                	   S   s   t dddd W 5 Q R X dS )z9Trigger a SandboxViolation by writing outside the sandbox/etc/foor   r   r   N)r   r   r   r   r   
write_files   s    zVTestExceptionSaver.test_sandbox_violation_raised_hiding_setuptools.<locals>.write_filer   )rL   r   r   r   z('/etc/foo', 'w')z{'encoding': 'utf-8'}N)r5   r6   r   r   ZSandboxViolationrH   rI   r   r   r9   r@   r#   )r   r   rM   r;   cmdr@   kwargsrJ   r   r   r   /test_sandbox_violation_raised_hiding_setuptoolsl   s    
$
zBTestExceptionSaver.test_sandbox_violation_raised_hiding_setuptoolsN)
r   r+   r,   r4   r<   r=   r>   rF   rK   rP   r   r   r   r   r.   )   s   
r.   )	rB   r	   r   r5   r   Zsetuptools.sandboxr   r   r.   r   r   r   r   <module>   s   