B
    u9a)R  ã               @   s²   d 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	 ddl
mZ ddd„ZG d	d
„ d
ƒZG dd„ deejƒZG dd„ deƒZG dd„ dejƒZedkr®e ¡  dS )z<
Tests common to genericpath, macpath, ntpath and posixpath
é    N)Úsupport)Úassert_python_ok)ÚFakePathó   fooc          	   C   s&   t | ddƒ}| |¡ W d Q R X d S )NZxbr   )ÚopenÚwrite)ÚfilenameÚdataÚfp© r   ú&/usr/lib/python3.7/test_genericpath.pyÚcreate_file   s    r   c               @   sÊ   e Zd ZddddddddgZg Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Z	e
 eedƒd¡dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zejdd „ ƒZd!d"„ Zd#d$„ Zd%d&„ Zejd'd(„ ƒZd)d*„ Zd+d,„ Zd-S ).ÚGenericTestÚcommonprefixÚgetsizeÚgetatimeÚgetctimeÚgetmtimeÚexistsÚisdirÚisfilec          
   C   sT   xN| j | j D ]>}|  t¡* t| j|ƒƒ  |  d | jj|¡¡‚W d Q R X qW d S )Nz!{}.{}() did not raise a TypeError)	Úcommon_attributesÚ
attributesÚassertRaisesÚ	TypeErrorÚgetattrÚ
pathmoduleZfailÚformatÚ__name__)ÚselfÚattrr   r   r   Útest_no_argument   s
    zGenericTest.test_no_argumentc             C   s¨  | j j}|  |g ƒd¡ |  |ddgƒd¡ |  |ddgƒd¡ |  |ddgƒd¡ |  |ddgƒd	¡ |  |d
dgƒd¡ |  |d
d
gƒd
¡ |  |ddgƒd¡ |  |ddgƒd¡ |  |ddgƒd¡ |  |ddgƒd¡ |  |ddgƒd¡ |  |ddgƒd¡ dddddddddd d!g}x€|D ]x}xp|D ]h}|||gƒ}|  | |¡¡ |  | |¡¡ ||kr2t|ƒ}|  |||d" … |||d" … ¡ q2W q(W d S )#NÚ z/home/swenson/spamz/home/swen/spamz
/home/swenz/home/swen/eggsz/home/swen/zhome:swenson:spamzhome:swen:spamz	home:swenz:home:swen:spamz:home:swen:eggsz:home:swen:s   /home/swenson/spams   /home/swen/spams
   /home/swens   /home/swen/eggss   /home/swen/s   home:swenson:spams   home:swen:spams	   home:swens   :home:swen:spams   :home:swen:eggss   :home:swen:ÚabcZXbcdZXbZXYZabcdZaXcZabdÚabZaXZabcXé   )r   r   ÚassertEqualÚ
assertTrueÚ
startswithÚlenZassertNotEqual)r   r   ZtestlistÚs1Ús2ÚpÚnr   r   r   Útest_commonprefix    sd    














zGenericTest.test_commonprefixc             C   s^   t j}|  t j|¡ t|dƒ |  | j |¡d¡ t 	|¡ t|dƒ |  | j |¡d¡ d S )Ns   Helloé   s   Hello World!é   )
r   ÚTESTFNÚ
addCleanupÚunlinkr   r&   r   r   ÚosÚremove)r   r   r   r   r   Útest_getsizec   s    


zGenericTest.test_getsizec          	   C   sŒ   t j}|  t j|¡ t|dƒ t|ddƒ}| d¡ W d Q R X t|ddƒ}| ¡ }W d Q R X |  |d¡ |  	| j
 |¡| j
 |¡¡ d S )Ns   foor$   r   s   barÚrbs   foobar)r   r1   r2   r3   r   r   r   Úreadr&   ZassertLessEqualr   r   r   )r   r   Úfr	   r   r   r   Útest_filetimen   s    

zGenericTest.test_filetimec             C   s¬   t j}t |¡}|  t j|¡ |  | j |¡d¡ |  | j |¡d¡ t	|ƒ |  | j |¡d¡ |  | j |¡d¡ | jt
k	r¨|  | j |¡d¡ |  | j |¡d¡ d S )NFT)r   r1   r4   Úfsencoder2   r3   ÚassertIsr   r   r   ÚgenericpathÚlexists)r   r   Ú	bfilenamer   r   r   Útest_exists€   s    

zGenericTest.test_existsÚpipezrequires os.pipe()c          	   C   sP   t  ¡ \}}z|  | j |¡¡ W d t  |¡ t  |¡ X |  | j |¡¡ d S )N)r4   rA   r'   r   r   ÚcloseÚassertFalse)r   ÚrÚwr   r   r   Útest_exists_fd‘   s    
zGenericTest.test_exists_fdc          	   C   sÂ   t j}t |¡}|  | j |¡d¡ |  | j |¡d¡ z4t|ƒ |  | j |¡d¡ |  | j |¡d¡ W d t  |¡ X z6t 	|¡ |  | j |¡d¡ |  | j |¡d¡ W d t  
|¡ X d S )NFT)r   r1   r4   r;   r<   r   r   r   r3   ÚmkdirÚrmdir)r   r   r?   r   r   r   Ú
test_isdir›   s    

zGenericTest.test_isdirc          	   C   sÂ   t j}t |¡}|  | j |¡d¡ |  | j |¡d¡ z4t|ƒ |  | j |¡d¡ |  | j |¡d¡ W d t  |¡ X z6t 	|¡ |  | j |¡d¡ |  | j |¡d¡ W d t  
|¡ X d S )NFT)r   r1   r4   r;   r<   r   r   r   r3   rG   rH   )r   r   r?   r   r   r   Útest_isfile¯   s    

zGenericTest.test_isfilec             C   sx   t j}t jd }|  t j|¡ |  t j|¡ t|ƒ |  | j ||¡¡ t|ƒ |  | j ||¡¡ |  	t
| jj¡ d S )NÚ2)r   r1   r2   r3   r   r'   r   ÚsamefilerC   r   r   )r   Zfile1Zfile2r   r   r   Útest_samefileÃ   s    
zGenericTest.test_samefilec             C   s|   t j}t jd }|  t j|¡ |  t j|¡ t|ƒ |||ƒ |  | j ||¡¡ t 	|¡ t|ƒ |  
| j ||¡¡ d S )NrK   )r   r1   r2   r3   r   r'   r   rL   r4   r5   rC   )r   ÚfuncÚtest_fn1Útest_fn2r   r   r   Ú_test_samefile_on_link_funcÑ   s    


z'GenericTest._test_samefile_on_link_funcc             C   s   |   tj¡ d S )N)rQ   r4   Úsymlink)r   r   r   r   Útest_samefile_on_symlinkà   s    z$GenericTest.test_samefile_on_symlinkc          
   C   sF   y|   tj¡ W n0 tk
r@ } z|  d| ¡ W d d }~X Y nX d S )Nzos.link(): %s)rQ   r4   ÚlinkÚPermissionErrorÚskipTest)r   Úer   r   r   Útest_samefile_on_linkä   s    z!GenericTest.test_samefile_on_linkc             C   s’   t j}t jd }|  t j|¡ |  t j|¡ t|ƒ t |¡}|  | j 	|t |¡¡¡ t|ƒ t |¡}|  
| j 	||¡¡ |  t| jj	¡ d S )NrK   )r   r1   r2   r3   r   r4   Ústatr'   r   ÚsamestatrC   r   r   )r   rO   rP   Zstat1Zstat2r   r   r   Útest_samestatê   s    


zGenericTest.test_samestatc             C   s˜   t jd }t jd }|  t j|¡ |  t j|¡ t|ƒ |||ƒ |  | j t 	|¡t 	|¡¡¡ t 
|¡ t|ƒ |  | j t 	|¡t 	|¡¡¡ d S )NÚ1rK   )r   r1   r2   r3   r   r'   r   rZ   r4   rY   r5   rC   )r   rN   rO   rP   r   r   r   Ú_test_samestat_on_link_funcú   s    



z'GenericTest._test_samestat_on_link_funcc             C   s   |   tj¡ d S )N)r]   r4   rR   )r   r   r   r   Útest_samestat_on_symlink
  s    z$GenericTest.test_samestat_on_symlinkc          
   C   sF   y|   tj¡ W n0 tk
r@ } z|  d| ¡ W d d }~X Y nX d S )Nzos.link(): %s)r]   r4   rT   rU   rV   )r   rW   r   r   r   Útest_samestat_on_link  s    z!GenericTest.test_samestat_on_linkc          
   C   st   t j}|  t j|¡ t|ƒ t|ddƒB}| ¡ }t|ddƒ"}| ¡ }|  | j 	||¡¡ W d Q R X W d Q R X d S )Nr7   r   )
r   r1   r2   r3   r   r   Úfilenor'   r   Úsameopenfile)r   r   Úfp1Zfd1Úfp2Zfd2r   r   r   Útest_sameopenfile  s    zGenericTest.test_sameopenfileN)r   Ú
__module__Ú__qualname__r   r   r!   r.   r6   r:   r@   ÚunittestÚ
skipUnlessÚhasattrr4   rF   rI   rJ   rM   rQ   r   Zskip_unless_symlinkrS   rX   r[   r]   r^   r_   rd   r   r   r   r   r      s(   C
r   c               @   s   e Zd ZeZdd„ ZdS )ÚTestGenericTestc             C   sÎ   xÈt jD ]¾}|dkrqt| j|ƒ}| j|d’ y|dƒ W n ttfk
rT   Y nX y|dƒ W n ttfk
rz   Y nX |  t	d¡ |dƒ W d Q R X |  t	d¡ |dƒ W d Q R X W d Q R X qW d S )Nr   )r    u   /tmpí¿¿abcdss
   /tmpÿabcdszembedded nullz
/tmp abcdss
   /tmp abcds)
r   r   r   r   ZsubTestÚOSErrorÚUnicodeEncodeErrorÚUnicodeDecodeErrorÚassertRaisesRegexÚ
ValueError)r   r    rN   r   r   r   Útest_invalid_paths&  s"    z"TestGenericTest.test_invalid_pathsN)r   re   rf   r=   r   rp   r   r   r   r   rj      s   rj   c               @   s¶   e Zd Zejddddddddd	d
dddddddddddddddg Zdd„ Zdd„ Zdd„ Ze 	e
jd¡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d2S )3Ú
CommonTestÚcurdirÚpardirÚextsepÚsepÚpathsepÚdefpathÚaltsepÚdevnullÚnormcaseÚ
splitdriveÚ
expandvarsÚnormpathÚabspathÚjoinÚsplitÚsplitextÚisabsÚbasenameÚdirnamer>   ÚislinkÚismountÚ
expanduserÚrealpathc             C   s„   | j j}x$dD ]}||ƒ}|  |||ƒ¡ qW |  |dƒd¡ |  |dƒd¡ x0d dddg tdƒddhfD ]}|  t||¡ qjW d S )N)z	FoO/./BaRs	   FoO/./BaRr"   ó    Tr   g      @Úo)r   rz   r&   Ú	bytearrayr   r   )r   rz   r,   Úpathr   r   r   Útest_normcaseH  s    
 zCommonTest.test_normcasec             C   sl   | j j}|  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |d	ƒd
¡ |  |dƒd¡ d S )Nz/foo/bar)r"   z/foo/barzfoo:bar)r"   zfoo:barz:foo:bar)r"   z:foo:bars   /foo/bar)r‰   s   /foo/bars   foo:bar)r‰   s   foo:bars   :foo:bar)r‰   s   :foo:bar)r   r{   r&   )r   r{   r   r   r   Útest_splitdriveV  s    zCommonTest.test_splitdrivec          	   C   s¸  | j jdkr|  d¡ | j j}t ¡ †}| ¡  d|d< d|d< d|d< |  |dƒd¡ |  |d	ƒd
¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd¡ |  |dƒd ¡ |  |d!ƒd!¡ |  |d"ƒd#¡ |  |d$ƒd¡ |  |d%ƒd%¡ W d Q R X d S )&NÚmacpathzmacpath.expandvars is a stubZbarÚfooZbaz1z{fooZbaz2z{foo}z$foo barzbar barz	${foo}barZbarbarz	$[foo]barz$bar barz$?barz$foo}barzbar}barz${fooz${{foo}}zbaz1}z$foo$fooz$bar$bars   foos   $foo bars   bar bars	   ${foo}bars   barbars	   $[foo]bars   $bar bars   $?bars   $foo}bars   bar}bars   ${foos   ${{foo}}s   baz1}s   $foo$foos   $bar$bar)r   r   rV   r|   r   ÚEnvironmentVarGuardÚclearr&   )r   r|   Úenvr   r   r   Útest_expandvarsa  s<    
zCommonTest.test_expandvarszneed support.FS_NONASCIIc          	      sN  ˆj jdkrˆ d¡ ˆj j‰ ‡ ‡fdd„}t ¡ }| ¡  tj}||d< d| ||< |||ƒ |dd| ƒ |d	d
| ƒ |d| d| ƒ |d| d| ƒ |dd| ƒ |t 	|¡t 	|¡ƒ |dt 	d| ¡ƒ |dt 	d
| ¡ƒ |t 	d| ¡t 	d| ¡ƒ |t 	d| ¡t 	d| ¡ƒ |dt 	d| ¡ƒ W d Q R X d S )Nr   zmacpath.expandvars is a stubc                s   ˆ  ˆ | ƒ|¡ d S )N)r&   )ÚvalueZexpected)r|   r   r   r   Úcheck‡  s    z2CommonTest.test_expandvars_nonascii.<locals>.checkZspamZhamz	$spam barz%s barz
${spam}barz%sbarz${%s}barzham%sbarz
$bar%s barz	$spam}barz%s}bars	   $spam bars
   ${spam}bars	   $spam}bar)
r   r   rV   r|   r   r‘   r’   ÚFS_NONASCIIr4   r;   )r   r–   r“   Znonasciir   )r|   r   r   Útest_expandvars_nonascii‚  s.    

z#CommonTest.test_expandvars_nonasciic          	   C   sª   |   d| j d¡¡ t ¡ & t dt¡ |   d| j d¡¡ W d Q R X tjdkrVdnd}t ¡ > t dt¡ x*dd|ddfD ]}|  	| j |¡t
¡ q€W W d Q R X d S )	Nr   Úignores   fooÚwin32r‰   s   fòòs   /foos   C:\)ÚassertInr   r~   ÚwarningsÚcatch_warningsÚsimplefilterÚDeprecationWarningÚsysÚplatformÚassertIsInstanceÚbytes)r   Zundecodable_pathrŒ   r   r   r   Útest_abspathž  s    

zCommonTest.test_abspathc          	   C   sL   |   d| j d¡¡ t ¡ & t dt¡ |   d| j d¡¡ W d Q R X d S )Nr   r™   s   foo)r›   r   rˆ   rœ   r   rž   rŸ   )r   r   r   r   Útest_realpath­  s    
zCommonTest.test_realpathc             C   s&   x dD ]}|   | j |¡t¡ qW d S )N)r"   Ú.ú/ú\z///foo/.//bar//)r¢   r   r}   Ústr)r   rŒ   r   r   r   Útest_normpath_issue5827³  s    
z"CommonTest.test_normpath_issue5827c          	   C   sŠ   | j j}xdD ]}|  ||ƒt¡ qW d}yt |¡ W n ttfk
rP   Y n6X t 	|¡$ xdD ]}|  ||ƒt¡ qdW W d Q R X d S )N)r"   Zfuuu   fÃ¹Ã¹z/fuuzU:\u   Ã§wÃ°)
r   r~   r¢   r©   r4   r;   ÚAttributeErrorrl   r   Útemp_cwd)r   r~   rŒ   Zunicwdr   r   r   Útest_abspath_issue3426¸  s    

z!CommonTest.test_abspath_issue3426c          
   C   sr   t jrtjdkrt j}nt jr&t j}n
|  d¡ t ¡ 0 t dt	¡ t  
|¡ |  ¡  W d Q R X W d Q R X d S )N)rš   Údarwinzneed support.TESTFN_NONASCIIr™   )r   ZTESTFN_UNDECODABLEr    r¡   ZTESTFN_NONASCIIrV   rœ   r   rž   rŸ   r¬   r¤   )r   Únamer   r   r   Útest_nonascii_abspathÊ  s    


z CommonTest.test_nonascii_abspathc          
   C   s6  t jdtfdd d}|  t|¡ | j dd¡ W d Q R X |  t|¡ | j dd¡ W d Q R X |  td¡ | j dd¡ W d Q R X |  td¡ | j dd¡ W d Q R X |  td¡ | j d¡ W d Q R X |  td	¡ | j g ¡ W d Q R X |  td
¡ | j tdƒtdƒ¡ W d Q R X W d Q R X d S )Nr"   T)Úquietz.Can't mix strings and bytes in path componentss   bytesr©   Úinté*   Úlistr‹   s   foos   bar)r   Úcheck_warningsÚBytesWarningrn   r   r   r   r‹   )r   Úerrmsgr   r   r   Útest_join_errorsÜ  s     zCommonTest.test_join_errorsc          
   C   sð   t jdttffddÐ d}|  t|¡ | j dd¡ W d Q R X |  t|¡ | j dd¡ W d Q R X |  td¡ | j dd¡ W d Q R X |  td¡ | j dd¡ W d Q R X |  td	¡ | j td
ƒtdƒ¡ W d Q R X W d Q R X d S )Nr"   T)r±   z.Can't mix strings and bytes in path componentss   bytesr©   r²   r³   r‹   s   foos   bar)	r   rµ   r¶   rŸ   rn   r   r   Úrelpathr‹   )r   r·   r   r   r   Útest_relpath_errorsð  s    
zCommonTest.test_relpath_errorsc             C   s   t ddd| jj ƒ d S )Nz-Sz-czimport )r   r   r   )r   r   r   r   Útest_import   s    zCommonTest.test_importN)r   re   rf   r   r   r   rŽ   r”   rg   rh   r   r—   r˜   r¤   r¥   rª   r­   r°   r¸   rº   r»   r   r   r   r   rq   =  s$   
!rq   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S )ÚPathLikeTestsc             C   s8   t j ¡ | _tt jƒ| _|  t j| j¡ t| jdƒ d S )Ns   test_genericpath.PathLikeTests)	r   r1   ÚlowerÚ	file_namer   Ú	file_pathr2   r3   r   )r   r   r   r   ÚsetUp  s    zPathLikeTests.setUpc             C   s   |   || jƒ|| jƒ¡ d S )N)r&   r¿   r¾   )r   rN   r   r   r   ÚassertPathEqual  s    zPathLikeTests.assertPathEqualc             C   s   |   tjj¡ d S )N)rÁ   r4   rŒ   r   )r   r   r   r   Útest_path_exists  s    zPathLikeTests.test_path_existsc             C   s   |   tjj¡ d S )N)rÁ   r4   rŒ   r   )r   r   r   r   Útest_path_isfile  s    zPathLikeTests.test_path_isfilec             C   s   |   tjj¡ d S )N)rÁ   r4   rŒ   r   )r   r   r   r   Útest_path_isdir  s    zPathLikeTests.test_path_isdirc             C   s"   |   tj | j| jg¡| j¡ d S )N)r&   r4   rŒ   r   r¿   r¾   )r   r   r   r   Útest_path_commonprefix  s    z$PathLikeTests.test_path_commonprefixc             C   s   |   tjj¡ d S )N)rÁ   r4   rŒ   r   )r   r   r   r   Útest_path_getsize  s    zPathLikeTests.test_path_getsizec             C   s   |   tjj¡ d S )N)rÁ   r4   rŒ   r   )r   r   r   r   Útest_path_getmtime  s    z PathLikeTests.test_path_getmtimec             C   s   |   tjj¡ d S )N)rÁ   r4   rŒ   r   )r   r   r   r   Útest_path_getctime"  s    z PathLikeTests.test_path_getctimec             C   s   |   tj | j| j¡¡ d S )N)r'   r4   rŒ   rL   r¿   r¾   )r   r   r   r   Útest_path_samefile%  s    z PathLikeTests.test_path_samefileN)r   re   rf   rÀ   rÁ   rÂ   rÃ   rÄ   rÅ   rÆ   rÇ   rÈ   rÉ   r   r   r   r   r¼     s   r¼   Ú__main__)r   )Ú__doc__r=   r4   r    rg   rœ   Ztestr   Ztest.support.script_helperr   Ztest.supportr   r   r   ZTestCaserj   rq   r¼   r   Úmainr   r   r   r   Ú<module>   s$   
   H%