B
    u9a–t  ã               @   sî   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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mZmZmZmZmZmZmZ ejZdddgZdZefdd	„Zed
fdd„ZG dd„ dejƒZdd„ Zedkrêeƒ  dS )é    N)Úsupport)Úmake_pkgÚmake_scriptÚmake_zip_pkgÚmake_zip_scriptÚassert_python_okÚassert_python_failureÚspawn_pythonÚkill_pythonZtest1Ztest2Ztest3aå  # Script may be run with optimisation enabled, so don't rely on assert
# statements being executed
def assertEqual(lhs, rhs):
    if lhs != rhs:
        raise AssertionError('%r != %r' % (lhs, rhs))
def assertIdentical(lhs, rhs):
    if lhs is not rhs:
        raise AssertionError('%r is not %r' % (lhs, rhs))
# Check basic code execution
result = ['Top level assignment']
def f():
    result.append('Lower level reference')
f()
assertEqual(result, ['Top level assignment', 'Lower level reference'])
# Check population of magic variables
assertEqual(__name__, '__main__')
from importlib.machinery import BuiltinImporter
_loader = __loader__ if __loader__ is BuiltinImporter else type(__loader__)
print('__loader__==%a' % _loader)
print('__file__==%a' % __file__)
print('__cached__==%a' % __cached__)
print('__package__==%r' % __package__)
# Check PEP 451 details
import os.path
if __package__ is not None:
    print('__main__ was located through the import system')
    assertIdentical(__spec__.loader, __loader__)
    expected_spec_name = os.path.splitext(os.path.basename(__file__))[0]
    if __package__:
        expected_spec_name = __package__ + "." + expected_spec_name
    assertEqual(__spec__.name, expected_spec_name)
    assertEqual(__spec__.parent, __package__)
    assertIdentical(__spec__.submodule_search_locations, None)
    assertEqual(__spec__.origin, __file__)
    if __spec__.cached is not None:
        assertEqual(__spec__.cached, __cached__)
# Check the sys module
import sys
assertIdentical(globals(), sys.modules[__name__].__dict__)
if __spec__ is not None:
    # XXX: We're not currently making __main__ available under its real name
    pass # assertIdentical(globals(), sys.modules[__spec__.name].__dict__)
from test import test_cmd_line_script
example_args_list = test_cmd_line_script.example_args
assertEqual(sys.argv[1:], example_args_list)
print('sys.argv[0]==%a' % sys.argv[0])
print('sys.path[0]==%a' % sys.path[0])
# Check the working directory
import os
print('cwd==%a' % os.getcwd())
c             C   s   t | ||ƒ}t ¡  |S )N)r   Ú	importlibÚinvalidate_caches)Ú
script_dirÚscript_basenameÚsourceÚ	to_return© r   ú*/usr/lib/python3.7/test_cmd_line_script.pyÚ_make_test_scriptN   s    r   é   c             C   s   t | |||||ƒ}t ¡  |S )N)r   r   r   )Zzip_dirZzip_basenameZpkg_namer   r   Údepthr   r   r   r   Ú_make_test_zip_pkgS   s    
r   c               @   sœ  e Zd Zd`dd„Zddœdd„Zddœdd„Zd	d
„ Zdd„ Zej	dadd„ƒZ
dbdd„Zdcd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d*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Z d<d=„ Z!d>d?„ Z"d@dA„ Z#ej	dBdC„ ƒZ$dDdE„ Z%dFdG„ Z&dHdI„ Z'dJdK„ Z(dLdM„ Z)dNdO„ Z*dPdQ„ Z+dRdS„ Z,dTdU„ Z-dVdW„ Z.dXdY„ Z/dZd[„ Z0d\d]„ Z1d^d_„ Z2dS )dÚCmdLineTestNc
             C   s  t dkr td| ƒ tt|ƒƒ |  |d¡ d| }
d| }d| }d| }d| }|	d krdt ¡ }	d	|	 }t dkrœtd
ƒ t|ƒ t|ƒ t|ƒ t|ƒ |  |
 d¡|¡ |  | d¡|¡ |  | d¡|¡ |  | d¡|¡ |  | d¡|¡ |  | d¡|¡ d S )Nr   zOutput from test script %r:r   z__loader__==%az__file__==%az__package__==%rzsys.argv[0]==%azsys.path[0]==%azcwd==%azExpected output:zutf-8)ÚverboseÚprintÚreprÚassertEqualÚosÚgetcwdÚassertInÚencode)ÚselfÚscript_nameZ	exit_codeÚdataÚexpected_fileÚexpected_argv0Úexpected_path0Úexpected_packageÚexpected_loaderZexpected_cwdZprinted_loaderZprinted_fileZprinted_packageZprinted_argv0Zprinted_path0Zprinted_cwdr   r   r   Ú_check_output[   s0    zCmdLineTest._check_output)Úcwdc            O   s\   t |tƒr|g}t ¡ ||t•}
t|
d|dœ|	—Ž\}}}|  |||| ||||||¡	 d S )NF)Ú
__isolatedÚ__cwd)Ú
isinstanceÚstrr   Z!optim_args_from_interpreter_flagsÚexample_argsr   r(   )r    Úscript_exec_argsr#   r$   r%   r&   r'   r)   Úcmd_line_switchesÚenv_varsÚrun_argsÚrcÚoutÚerrr   r   r   Ú_check_scriptx   s    

zCmdLineTest._check_scriptc      
      O   s~   t |tƒr|f}nt|ƒ}|| }t|d|dœ|—Ž\}}}	tdkrhtd| ƒ tt|	ƒƒ td| ƒ |  | d¡|	¡ d S )NF)r*   r+   r   zOutput from test script %r:zExpected output: %rzutf-8)	r,   r-   Útupler   r   r   r   r   r   )
r    r/   Zexpected_msgr)   r0   r1   r2   r3   r4   r5   r   r   r   Ú_check_import_error‡   s    
zCmdLineTest._check_import_errorc             C   s2   t ddƒ\}}}ttjjƒ d¡}|  ||¡ d S )Nz-czprint(__loader__)zutf-8)r   r   r   Ú	machineryÚBuiltinImporterr   r   )r    r3   r4   r5   Úexpectedr   r   r   Útest_dash_c_loader—   s    zCmdLineTest.test_dash_c_loaderc             C   sN   t ƒ }z|j d¡ |j ¡  W d t|ƒ}X ttjjƒ 	d¡}|  
||¡ d S )Ns   print(__loader__)
zutf-8)r	   ÚstdinÚwriteÚflushr
   r   r   r9   r:   r   r   )r    Úpr4   r;   r   r   r   Útest_stdin_loaderœ   s    
zCmdLineTest.test_stdin_loaderFc             c   sv   |rt ddtjd}|j}nt ddtjd}|j}z,x | d¡}|dkrJP | ¡  q6W |V  W d t|ƒ | 	¡  X d S )Nz-ir   )ÚbufsizeÚstderré   s   >>> )
r	   Ú
subprocessÚPIPErC   ZSTDOUTÚstdoutÚreadÚreadliner
   Úclose)r    Úseparate_stderrr@   rC   r"   r   r   r   Úinteractive_python©   s    

zCmdLineTest.interactive_pythonc          	   C   sF   |   |¡2}|j d¡ |j ¡  |  d|j ¡  ¡ ¡ W d Q R X d S )Ns   print('foo')
s   foo)rL   r=   r>   r?   r   rG   rI   Ústrip)r    rK   r@   r   r   r   Úcheck_repl_stdout_flush½   s    
z#CmdLineTest.check_repl_stdout_flushc          	   C   sp   |   |¡\}|j d¡ |j ¡  |r,|jn|j}|  d| ¡ ¡ |  d| ¡ ¡ |  d| ¡ ¡ W d Q R X d S )Ns   1/0
s
   Traceback s   File "<stdin>"s   ZeroDivisionError)rL   r=   r>   r?   rC   rG   r   rI   )r    rK   r@   rC   r   r   r   Úcheck_repl_stderr_flushÃ   s    
z#CmdLineTest.check_repl_stderr_flushc             C   s   |   ¡  d S )N)rN   )r    r   r   r   Útest_repl_stdout_flushÌ   s    z"CmdLineTest.test_repl_stdout_flushc             C   s   |   d¡ d S )NT)rN   )r    r   r   r   Ú&test_repl_stdout_flush_separate_stderrÏ   s    z2CmdLineTest.test_repl_stdout_flush_separate_stderrc             C   s   |   ¡  d S )N)rO   )r    r   r   r   Útest_repl_stderr_flushÒ   s    z"CmdLineTest.test_repl_stderr_flushc             C   s   |   d¡ d S )NT)rO   )r    r   r   r   Ú&test_repl_stderr_flush_separate_stderrÕ   s    z2CmdLineTest.test_repl_stderr_flush_separate_stderrc          	   C   s:   t  ¡ (}t|dƒ}|  ||||d tjj¡ W d Q R X d S )NÚscript)r   Útemp_dirr   r6   r   r9   ÚSourceFileLoader)r    r   r!   r   r   r   Útest_basic_scriptØ   s
    


zCmdLineTest.test_basic_scriptc          	   C   s\   t  ¡ J}t|dƒ}tj|dd t |¡ t  |¡}|  ||||d t	j
j¡ W d Q R X d S )NrT   T)Údoraise)r   rU   r   Ú
py_compileÚcompiler   ÚremoveÚmake_legacy_pycr6   r   r9   ÚSourcelessFileLoader)r    r   r!   Úpyc_filer   r   r   Útest_script_compiledß   s    



z CmdLineTest.test_script_compiledc          	   C   s:   t  ¡ (}t|dƒ}|  ||||dtjj¡ W d Q R X d S )NÚ__main__Ú )r   rU   r   r6   r   r9   rV   )r    r   r!   r   r   r   Útest_directoryé   s
    


zCmdLineTest.test_directoryc          	   C   s\   t  ¡ J}t|dƒ}tj|dd t |¡ t  |¡}|  ||||dt	j
j¡ W d Q R X d S )Nr`   T)rX   ra   )r   rU   r   rY   rZ   r   r[   r\   r6   r   r9   r]   )r    r   r!   r^   r   r   r   Útest_directory_compiledð   s    




z#CmdLineTest.test_directory_compiledc          	   C   s,   t  ¡ }d| }|  ||¡ W d Q R X d S )Nz"can't find '__main__' module in %r)r   rU   r8   )r    r   Úmsgr   r   r   Útest_directory_errorú   s    
z CmdLineTest.test_directory_errorc          	   C   sH   t  ¡ 6}t|dƒ}t|d|ƒ\}}|  ||||dtj¡ W d Q R X d S )Nr`   Útest_zipra   )r   rU   r   r   r6   Ú	zipimportÚzipimporter)r    r   r!   Úzip_nameÚrun_namer   r   r   Útest_zipfileÿ   s
    

zCmdLineTest.test_zipfilec          	   C   sV   t  ¡ D}t|dƒ}tj|dd}t|d|ƒ\}}|  ||||dtj¡ W d Q R X d S )Nr`   T)rX   rf   ra   )	r   rU   r   rY   rZ   r   r6   rg   rh   )r    r   r!   Úcompiled_nameri   rj   r   r   r   Útest_zipfile_compiled  s    

z!CmdLineTest.test_zipfile_compiledc          	   C   sF   t  ¡ 4}t|dƒ}t|d|ƒ\}}d| }|  ||¡ W d Q R X d S )NZnot_mainrf   z"can't find '__main__' module in %r)r   rU   r   r   r8   )r    r   r!   ri   rj   rd   r   r   r   Útest_zipfile_error  s
    

zCmdLineTest.test_zipfile_errorc          
   C   sX   t  ¡ F}tj |d¡}t|ƒ t|dƒ}| jddg|||dtj	j
|d W d Q R X d S )NÚtest_pkgrT   z-mztest_pkg.script)r)   )r   rU   r   ÚpathÚjoinr   r   r6   r   r9   rV   )r    r   Úpkg_dirr!   r   r   r   Útest_module_in_package  s    

z"CmdLineTest.test_module_in_packagec             C   sJ   t  ¡ 8}t|dddƒ\}}| jddg|||dtj||d W d Q R X d S )Nrf   ro   rT   z-mztest_pkg.script)Ú
PYTHONPATHr)   )r   rU   r   r6   rg   rh   )r    r   ri   rj   r   r   r   Ú!test_module_in_package_in_zipfile  s
    
z-CmdLineTest.test_module_in_package_in_zipfilec             C   sN   t  ¡ <}t|ddddd\}}| jddg|||dtj||d	 W d Q R X d S )
Nrf   ro   rT   é   )r   z-mztest_pkg.test_pkg.scriptztest_pkg.test_pkg)rt   r)   )r   rU   r   r6   rg   rh   )r    r   ri   rj   r   r   r   Ú$test_module_in_subpackage_in_zipfile&  s    
z0CmdLineTest.test_module_in_subpackage_in_zipfilec          
   C   sX   t  ¡ F}tj |d¡}t|ƒ t|dƒ}| jddg|||dtj	j
|d W d Q R X d S )Nro   r`   z-m)r)   )r   rU   r   rp   rq   r   r   r6   r   r9   rV   )r    r   rr   r!   r   r   r   Útest_package.  s    

zCmdLineTest.test_packagec          
   C   sz   t  ¡ h}tj |d¡}t|ƒ t|dƒ}tj|dd}t 	|¡ t  
|¡}| jddg|||dtjj|d W d Q R X d S )Nro   r`   T)rX   z-m)r)   )r   rU   r   rp   rq   r   r   rY   rZ   r[   r\   r6   r   r9   r]   )r    r   rr   r!   rl   r^   r   r   r   Útest_package_compiled8  s    



z!CmdLineTest.test_package_compiledc          	   C   sF   t  ¡ 4}tj |d¡}t|ƒ d}| jddg||d W d Q R X d S )Nro   z7'test_pkg' is a package and cannot be directly executedz-m)r)   )r   rU   r   rp   rq   r   r8   )r    r   rr   rd   r   r   r   Útest_package_errorE  s
    
zCmdLineTest.test_package_errorc          	   C   s\   t  ¡ J}tj |d¡}t|ƒ tj |d¡}t|ƒ d}| jddg||d W d Q R X d S )Nro   r`   z^Cannot use package as __main__ module; 'test_pkg' is a package and cannot be directly executedz-m)r)   )r   rU   r   rp   rq   r   r8   )r    r   rr   Zmain_dirrd   r   r   r   Útest_package_recursionM  s    
z"CmdLineTest.test_package_recursionc             C   s´   t  ¡ ¢}t j|dŠ tj |d¡}t|dƒ t|dƒ}tdt	žddiŽ\}}}t
d	krftt|ƒƒ d
d }|  | d¡|¡ |  ||||||dtjj¡ W d Q R X W d Q R X d S )N)rp   ro   z1import sys; print('init_argv0==%r' % sys.argv[0])rT   ú-mútest_pkg.scriptr*   Fr   zinit_argv0==%rzutf-8)r|   r}   )r   rU   Ú
change_cwdr   rp   rq   r   r   r   r.   r   r   r   r   r   r(   r   r9   rV   )r    r   rr   r!   r3   r4   r5   r;   r   r   r   Útest_issue8202X  s    



zCmdLineTest.test_issue8202c             C   s’   t  ¡ €}t j|dh tddƒR}| d¡ tdddd\}}}tdkrVtt|ƒƒ d	d
 }|  	| 
d¡|¡ W d Q R X W d Q R X W d Q R X d S )N)rp   z-cÚwr"   z2import sys; print("sys.path[0]==%r" % sys.path[0])F)r*   r   zsys.path[0]==%rra   zutf-8)r   rU   r~   Úopenr>   r   r   r   r   r   r   )r    r   Úfr3   r4   r5   r;   r   r   r   Ú"test_issue8202_dash_c_file_ignoredi  s    

z.CmdLineTest.test_issue8202_dash_c_file_ignoredc             C   sŽ   t  ¡ |}t|dƒ}t j|dZ tddƒD}| d¡ td	tžddiŽ\}}}|  ||||||dt	j
j¡ W d Q R X W d Q R X W d Q R X d S )
NÚother)rp   ú-mr€   r"   r*   Fra   )r…   r„   )r   rU   r   r~   r   r>   r   r.   r(   r   r9   rV   )r    r   r!   r‚   r3   r4   r5   r   r   r   Ú"test_issue8202_dash_m_file_ignoredx  s    




z.CmdLineTest.test_issue8202_dash_m_file_ignoredc          
   C   s¸   t  ¡ ¦}tj |d¡}t|ddd@}| d¡ | d¡ xtdƒD ]}| d¡ qFW | d¡ W d Q R X t j|d	 t	|ƒ\}}}W d Q R X |  
d
|¡ |  
d
|¡ W d Q R X d S )Nzissue20884.pyr€   Ú
)Únewlinez#coding: iso-8859-1
z"""
é   zQxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)rp   ó    )r   rU   r   rp   rq   r   r>   Úranger~   r   r   )r    r   r!   r‚   Ú_r3   r4   r5   r   r   r   Útest_issue20884†  s    


zCmdLineTest.test_issue20884c          
   g   sR   t  ¡ @}t j|d( tj |d¡}t|f|žŽ  |V  W d Q R X W d Q R X d S )N)rp   ro   )r   rU   r~   r   rp   rq   r   )r    Úargsr   rr   r   r   r   Úsetup_test_pkg—  s
    
zCmdLineTest.setup_test_pkgc             G   s<   t d|žddiŽ\}}}tdkr,tt|ƒƒ |  |d¡ |S )Nú-mr*   Fr   )r   )r   r   r   r   r   )r    rŽ   r3   r4   r5   r   r   r   Úcheck_dash_m_failureŸ  s
    z CmdLineTest.check_dash_m_failurec          	   C   s>   |   ¡ ,}t|ddƒ}| jdtžŽ }|  d|¡ W d Q R X d S )Nr„   z+if __name__ == '__main__': raise ValueErrorútest_pkg.others
   ValueError)r’   )r   r   r‘   r.   r   )r    rr   r!   r5   r   r   r   Útest_dash_m_error_code_is_one¦  s
    
z)CmdLineTest.test_dash_m_error_code_is_onec          
   C   sd   d}xZ|D ]R\}}|   |¡: td|ƒ\}}}|  |d¡ |  ||¡ |  d|¡ W d Q R X q
W d S )N))Úbuiltinss   No code object available)z
builtins.xs=   Error while finding module specification.*ModuleNotFoundError)zbuiltins.x.ys]   Error while finding module specification.*ModuleNotFoundError.*No module named.*not a package)zos.paths   loader.*cannot handle)r   s=   No module named.*is a package and cannot be directly executed)zimportlib.nonexistants   No module named)z	.unittests#   Relative module names not supportedz-mr   s	   Traceback)ÚsubTestr   r   ÚassertRegexÚassertNotIn)r    ZtestsÚnameZregexr3   rŒ   r5   r   r   r   Útest_dash_m_errors°  s    zCmdLineTest.test_dash_m_errorsc             C   s~   t  ¡ l}t j|dT t d¡ tddƒ W d Q R X |  d¡}|  |d¡ |  d|¡ |  d|¡ W d Q R X W d Q R X d S )N)rp   ro   ztest_pkg/__init__.pycÚwbsG   Error while finding module specification.*ImportError.*bad magic numbers   is a packages	   Traceback)	r   rU   r~   r   Úmkdirr   r‘   r–   r—   )r    r   r5   r   r   r   Útest_dash_m_bad_pycÅ  s    



zCmdLineTest.test_dash_m_bad_pycc             C   sŽ   t tttf}x||D ]t}|j}d |¡}|  |¡P |  |¡:}|  d¡}|  	| 
d¡|¡ |  	d|¡ |  	d|¡ W d Q R X W d Q R X qW d S )Nz%raise {0}('Exception in __init__.py')ro   Úasciis   Exception in __init__.pys	   Traceback)ÚImportErrorÚAttributeErrorÚ	TypeErrorÚ
ValueErrorÚ__name__Úformatr•   r   r‘   r   r   )r    Ú
exceptionsZ	exceptionZinitrr   r5   r   r   r   Útest_dash_m_init_tracebackÓ  s    


z&CmdLineTest.test_dash_m_init_tracebackc          	   C   sV   |   ¡ D}d}t|d|ƒ |  d¡}|  d|¡ |  d|¡ |  d|¡ W d Q R X d S )Nz1raise ImportError('Exception in __main__ module')r`   ro   s   ImportErrors   Exception in __main__ modules	   Traceback)r   r   r‘   r   )r    rr   Úmainr5   r   r   r   Útest_dash_m_main_tracebacká  s    

z&CmdLineTest.test_dash_m_main_tracebackc          	   C   s˜   t  d¡}t ¡ |}t|d|ƒ}t|ƒ\}}}| d¡ d¡}|  t	|ƒd¡ |  
|d  d¡¡ |  
|d  d	¡¡ |  
|d
  d¡¡ W d Q R X d S )Nz|            try:
                raise ValueError
            except:
                raise NameError from None
            rT   r   r‡   rD   r   Z	Tracebackr   z  File é   Ú	NameError)ÚtextwrapÚdedentr   rU   r   r   ÚdecodeÚsplitr   ÚlenZ
assertTrueÚ
startswith)r    rT   r   r!   ÚexitcoderG   rC   Útextr   r   r   Útest_pep_409_verbiageë  s    
z!CmdLineTest.test_pep_409_verbiagec             C   s˜   t jrtjdkrt t j¡}nt jr,t j}n
|  d¡ d}ttj	||ƒ}|  
t j|¡ t|ƒ\}}}|  t|ƒ| ¡  d¡d||f ¡ |  d|¡ d S )N)Úwin32Údarwinzneed support.TESTFN_NONASCIIzprint(ascii(__file__))
r   zstdout=%r stderr=%rr   )r   ZTESTFN_UNDECODABLEÚsysÚplatformr   ÚfsdecodeZTESTFN_NONASCIIZskipTestr   ÚcurdirZ
addCleanupÚunlinkr   r   r   Úrstripr¬   )r    r˜   r   r!   r3   rG   rC   r   r   r   Útest_non_asciiý  s    

zCmdLineTest.test_non_asciic          	   C   sR   t  d¡}t ¡ 6}t|d|ƒ}t|ƒ\}}}| d¡}|  |d¡ W d Q R X d S )Nzô            import sys
            error = None
            try:
                raise ValueError('some text')
            except ValueError as err:
                error = err

            if error:
                sys.exit(error)
            rT   r   z	some text)rª   r«   r   rU   r   r   r¬   r   )r    rT   r   r!   r°   rG   rC   r±   r   r   r   Ú)test_issue20500_exit_with_exception_value  s    


z5CmdLineTest.test_issue20500_exit_with_exception_valuec          	   C   sX   d}t  ¡ B}t|d|ƒ}t|ƒ\}}}t t |¡d¡ ¡ }|  d|¡ W d Q R X d S )Nz
1 + 1 = 2
rT   r   z
    1 + 1 = 2
    ^)	r   rU   r   r   ÚioÚTextIOWrapperÚBytesIOrH   r   )r    rT   r   r!   r°   rG   rC   r±   r   r   r   Ú*test_syntaxerror_unindented_caret_position&  s    
z6CmdLineTest.test_syntaxerror_unindented_caret_positionc          	   C   sª   t  d¡}t ¡ Ž}t|d|ƒ}t|ƒ\}}}t t |¡d¡ 	¡ }|  
d|¡ d}t|d|ƒ}t|ƒ\}}}t t |¡d¡ 	¡ }|  d|¡ |  
d|¡ W d Q R X d S )Nz;            if True:
                1 + 1 = 2
            rT   r   z
    1 + 1 = 2
    ^zif True:
    1 + 1 = 2
ú)rª   r«   r   rU   r   r   r½   r¾   r¿   rH   r   r—   )r    rT   r   r!   r°   rG   rC   r±   r   r   r   Ú(test_syntaxerror_indented_caret_position/  s    
z4CmdLineTest.test_syntaxerror_indented_caret_positionc       	   
   C   sØ   t  d¡}d | _t ¡ ¶}t ¡ ¢}t|d|ƒ}td||d}t|ƒ ¡  	¡ }|  
|d |¡ |  ||¡ td||d}t|ƒ ¡  	¡ }|  
||¡ td||d}t|ƒ ¡  	¡ }|  
|||¡ W d Q R X W d Q R X d S )Nzc            import sys
            for entry in sys.path:
                print(entry)
            r`   z-Es)r)   r   z-I)rª   r«   ÚmaxDiffr   rU   r   r	   r
   r¬   Ú
splitlinesr   r—   )	r    rT   Úwork_dirr   r!   r@   Zout_by_nameZ
out_by_dirZout_by_dir_isolatedr   r   r   Ú-test_consistent_sys_path_for_direct_executionF  s    z9CmdLineTest.test_consistent_sys_path_for_direct_executionc          	   C   sà   t  d¡}d | _t ¡ ¾}tj |d¡}t |¡ t	|d|ƒ}t
dd|d}t|ƒ ¡  ¡ }|  |d |¡ |  ||¡ t
dd|d}t|ƒ ¡  ¡ }|  ||¡ tdd|d\}}	}
|
 ¡  ¡ }|  d	|d
 ¡ W d Q R X d S )Nzc            import sys
            for entry in sys.path:
                print(entry)
            Z
script_pkgr`   z-smzscript_pkg.__main__)r)   r   z-ImzNo module named script_pkgéÿÿÿÿ)rª   r«   rÃ   r   rU   r   rp   rq   r›   r   r	   r
   r¬   rÄ   r   r—   r   r   )r    rT   rÅ   r   r!   r@   Zout_by_moduleZout_by_packager°   rG   rC   Ztraceback_linesr   r   r   Ú-test_consistent_sys_path_for_module_executione  s$    

z9CmdLineTest.test_consistent_sys_path_for_module_executionc             C   sT   d}|   tj |¡¡ t|dtjtjd}| ¡ \}}|  d|¡ |  	|j
d¡ d S )Nznonexistingscript.pyT)r±   rG   rC   z: can't open file r   )ZassertFalser   rp   Úexistsr	   rE   rF   Zcommunicater   ZassertNotEqualÚ
returncode)r    rT   Úprocr4   r5   r   r   r   Útest_nonexisting_scriptŠ  s    
z#CmdLineTest.test_nonexisting_script)N)F)F)F)3r¢   Ú
__module__Ú__qualname__r(   r6   r8   r<   rA   Ú
contextlibÚcontextmanagerrL   rN   rO   rP   rQ   rR   rS   rW   r_   rb   rc   re   rk   rm   rn   rs   ru   rw   rx   ry   rz   r{   r   rƒ   r†   r   r   r‘   r“   r™   rœ   r¥   r§   r²   r»   r¼   rÀ   rÂ   rÆ   rÈ   rÌ   r   r   r   r   r   Z   s^   


	





	%r   c               C   s   t  t¡ t  ¡  d S )N)r   Zrun_unittestr   Zreap_childrenr   r   r   r   Ú	test_mainš  s    
rÑ   r`   ) rÏ   r   Zimportlib.machineryrg   Zunittestrµ   r   Zos.pathrY   rE   r½   rª   Ztestr   Ztest.support.script_helperr   r   r   r   r   r   r	   r
   r   r.   Ztest_sourcer   r   ZTestCaser   rÑ   r¢   r   r   r   r   Ú<module>   s4   (
5    D