U
    uiz                     @   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mZ d dlZd dlm	Z	m
Z
mZmZmZmZmZmZmZ dd ZG dd dejZej	jZG dd	 d	ZG d
d dZG dd dZG dd dZG dd dZG dd dZdS )    N)SpecifierSet)	Distribution
EntryPointRequirementVersionConflict
WorkingSetparse_requirementsparse_version	safe_namesafe_versionc                 C   s"   t | \}}t|d t||S )z$s -> (s0,s1), (s1,s2), (s2, s3), ...N)	itertoolsteenextzip)iterableab r   F/tmp/pip-unpacked-wheel-9osl9suo/pkg_resources/tests/test_resources.pypairwise   s    
r   c                   @   s6   e Zd ZdZdd ZedddZdd Zd	d
 ZdS )MetadatazAMock object to return metadata as if from an on-disk distributionc                 G   s   t || _d S N)dictmetadata)selfpairsr   r   r   __init__#   s    zMetadata.__init__)returnc                 C   s
   || j kS r   r   r   namer   r   r   has_metadata&   s    zMetadata.has_metadatac                 C   s
   | j | S r   r   r   r   r   r   get_metadata)   s    zMetadata.get_metadatac                 C   s   t | |S r   )pkg_resourcesyield_linesr"   r   r   r   r   get_metadata_lines,   s    zMetadata.get_metadata_linesN)	__name__
__module____qualname____doc__r   boolr!   r"   r%   r   r   r   r   r       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d2ddZ	dd Z
ejjeed ddZdd Zedd Ze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jjejd+d, d-koejjd.kd/dd0d1 Z d+S )3
TestDistroc              	   C   s  t jg d d d}t|g ks t|d g ks0t|td |td |td |d sftt|dgksxtddd	g}d
d |d D |kst||d d  dd |d D dd	gkst|td dd |d D ddd	gksttg }td}td}td\}|	||j
dks4t|| |	||j
dksVttg }|| || tt |	|| W 5 Q R X tg }|| || || |	||j
dkstd S )N)platformpythonFooPkgFooPkg-1.3_1.eggzFooPkg-1.4-py2.4-win32.eggFooPkg-1.2-py2.4.eggfoopkgz1.41.3-11.2c                 S   s   g | ]
}|j qS r   version.0distr   r   r   
<listcomp>D   s     z-TestDistro.testCollection.<locals>.<listcomp>   c                 S   s   g | ]
}|j qS r   r4   r6   r   r   r   r9   H   s     zFooPkg-1.9.eggc                 S   s   g | ]
}|j qS r   r4   r6   r   r   r   r9   L   s     z1.9zFooPkg>=1.3)r#   EnvironmentlistAssertionErroradddist_from_fnremover   r   
best_matchr5   pytestraisesr   )r   adexpectedwsZfoo12Zfoo14reqr   r   r   testCollection4   s>    
 






zTestDistro.testCollectionc                 C   s\   |j dkst|jdkst|jdks*t|jdks8t|jdksFt|jtdksXtd S )Nr.   r1   z	1.3.post12.4win32r2   )project_namer=   keyr5   
py_versionr,   parsed_versionr	   r   dr   r   r   checkFooPkgg   s    zTestDistro.checkFooPkgc                 C   sL   t dddddd}| | t d}|jdjtj ks:t|jd ksHtd S )N
/some/pathr.   r2   rI   rJ   )rK   r5   rM   r,   z{}.{})r   rQ   rM   formatsysversion_infor=   r,   rO   r   r   r   testDistroBasicso   s    
zTestDistro.testDistroBasicsc                 C   s(   t d}| | t d}| | d S )Nz FooPkg-1.3.post1-py2.4-win32.eggz%FooPkg-1.3.post1-py2.4-win32.egg-info)r?   rQ   rO   r   r   r   testDistroParse}   s    
zTestDistro.testDistroParsec                 C   s$   t ddddtdd}| | d S )NrR   r.   rI   rJ   )zPKG-INFOz%Metadata-Version: 1.0
Version: 1.3-1
)rK   rM   r,   r   )r   r   rQ   rO   r   r   r   testDistroMetadata   s    zTestDistro.testDistroMetadatac                 C   s   t dtd|fdS )Nz/foodepends.txtr   )r   r   )r   txtr   r   r   distRequires   s    zTestDistro.distRequiresr   c                 C   s"   t ||t t|kstd S r   )r<   requiresr   r=   )r   r8   rZ   extrasr   r   r   checkRequires   s    zTestDistro.checkRequiresc                 C   s    dD ]}|  | || qd S )N)Twisted>=1.5zTwisted>=1.5
ZConfig>=2.0)r^   r[   )r   vr   r   r   testDistroDependsSimple   s    z"TestDistro.testDistroDependsSimple__dir__z8object.__dir__ necessary for self.__dir__ implementationreasonc                 C   s   t  }t| d S r   )r#   r   dirrO   r   r   r   test_distribution_dir   s    z TestDistro.test_distribution_dirc                 C   sT   t  }| }d|kstd |j_| }t|t|d ksDtd|ksPtd S )N	test_attrr:   )r#   r   rb   r=   	_providerrg   lenr   rP   beforeafterr   r   r   +test_distribution_dir_includes_provider_dir   s    z6TestDistro.test_distribution_dir_includes_provider_dirc                 C   sP   t  }| }d|kstd |j_| }t|t|ks@td|ksLtd S )N
_test_attr)r#   r   rb   r=   rh   rn   ri   rj   r   r   r   =test_distribution_dir_ignores_provider_dir_leading_underscore   s    zHTestDistro.test_distribution_dir_ignores_provider_dir_leading_underscorec           	   	   C   s  t g }tg }t|g |g ks*ttt j |t	d| W 5 Q R X t
jdtdd}|| |t
d tdD ]6}t|t	d|}||gksttt|j| qtt |t	d| W 5 Q R X tg }tt j |t	d| W 5 Q R X t
jd	td
d}|| t|t	d|||gksRttt}|t	d| W 5 Q R X d}|j |kstd S )NFoo/foo_dir/Foo-1.2.egg)rY   z[bar]
Baz>=2.0r   zFoo-0.9.egg   zFoo==0.9zFoo[bar]z/foo_dir/Baz-2.1.egg)rY   rp   zFoo==1.2
Foo!=1.2z-Foo 0.9 is installed but Foo==1.2 is required)r#   r;   r   r<   resolver=   rB   rC   DistributionNotFoundr   r   from_filenamer   r>   rangemapr   valuereport)	r   rD   rF   rp   itargetsBazvcmsgr   r   r   testResolve   s<    

 
"zTestDistro.testResolvec                 C   s6   t g }tg }|td|}t|g ks2tdS )z5Environment markers are evaluated at resolution time.zFoo;python_version<'2'N)r#   r;   r   rs   r   r<   r=   )r   rD   rF   resr   r   r   +test_environment_marker_evaluation_negative   s    
z6TestDistro.test_environment_marker_evaluation_negativec                 C   sL   t g }tg }td}|| |td|}t||gksHt	d S )N/foo_dir/Foo-1.2.dist-infozFoo;python_version>='2')
r#   r;   r   r   ru   r>   rs   r   r<   r=   )r   rD   rF   rp   r   r   r   r   +test_environment_marker_evaluation_positive   s    


z6TestDistro.test_environment_marker_evaluation_positivec                 C   sh   t d\}t d\}t||ji}||s2tt d\}t d\}t||ji}||sdtdS )zy
        If one package foo requires bar without any extras,
        markers should pass for bar without extras.
        foozbar;python_version>='2'zfoo[]N)r   r#   
_ReqExtrasr]   markers_passr=   )r   Z
parent_reqrG   
req_extrasr   r   r   )test_environment_marker_evaluation_called   s    



z4TestDistro.test_environment_marker_evaluation_calledc                 C   s   t g }tg }tjdtdd}|| t|t	d||gksLt
td}|| t|t	d|}|||gkst
dS )8Extras are also evaluated as markers at resolution time.r   )METADATAz5Provides-Extra: baz
Requires-Dist: quux; extra=='baz'r   rp   /foo_dir/quux-1.0.dist-infozFoo[baz]Nr#   r;   r   r   ru   r   r>   r<   rs   r   r=   r   rD   rF   rp   quuxr   r   r   r   "test_marker_evaluation_with_extras   s    



z-TestDistro.test_marker_evaluation_with_extrasc                 C   s   t g }tg }tjdtdd}|| t|t	d||gksLt
td}|| t|t	d|}|||gkst
dS )r   r   )r   zIProvides-Extra: baz-lightyear
Requires-Dist: quux; extra=='baz-lightyear'r   rp   r   zFoo[baz-lightyear]Nr   r   r   r   r   ,test_marker_evaluation_with_extras_normlized  s    



z7TestDistro.test_marker_evaluation_with_extras_normlizedc                 C   s   t g }tg }tjdtdd}|| td}|| td}|| t|t	d|}t
||||gkstd S )Nr   )r   zlProvides-Extra: baz
Requires-Dist: quux; extra=='baz'
Provides-Extra: bar
Requires-Dist: fred; extra=='bar'
r   r   z/foo_dir/fred-0.1.dist-infozFoo[baz,bar])r#   r;   r   r   ru   r   r>   r<   rs   r   sortedr=   )r   rD   rF   rp   r   Zfredr   r   r   r   +test_marker_evaluation_with_multiple_extras!  s    






z6TestDistro.test_marker_evaluation_with_multiple_extrasc           	      C   s   t g }tg }tjdtdd}tjdtdd}tjdtdd}td}||||fD ]}|| q^t|t	d	|}|||||gkst
d S )
Nz/foo_dir/a-0.2.dist-info)r   zRequires-Dist: c[a]r   z/foo_dir/b-0.3.dist-info)r   zRequires-Dist: c[b]z/foo_dir/c-1.0.dist-info)r   z]Provides-Extra: a
Requires-Dist: b;extra=='a'
Provides-Extra: b
Requires-Dist: foo;extra=='b'z/foo_dir/foo-0.1.dist-infor   r   )	r   rD   rF   r   r   cr   r8   r   r   r   r   'test_marker_evaluation_with_extras_loop6  s&    


z2TestDistro.test_marker_evaluation_with_extras_loopN   )rr      finalz/https://github.com/python/cpython/issues/103632c              	   C   s   |  d}| |d | |d dg | |d dg | |d ddg | |d ddg ttj |d	g W 5 Q R X d S )
Nz
            Twisted>=1.5
            [docgen]
            ZConfig>=2.0
            docutils>=0.3
            [fastcgi]
            fcgiapp>=0.1r_   z'Twisted>=1.5 ZConfig>=2.0 docutils>=0.3ZdocgenzTwisted>=1.5 fcgiapp>=0.1Zfastcgiz4Twisted>=1.5 ZConfig>=2.0 docutils>=0.3 fcgiapp>=0.1z4Twisted>=1.5 fcgiapp>=0.1 ZConfig>=2.0 docutils>=0.3r   )r[   r^   splitrB   rC   r#   UnknownExtrar\   rO   r   r   r   testDistroDependsOptionsQ  s,    	  z#TestDistro.testDistroDependsOptions)r   )!r&   r'   r(   rH   rQ   rV   rW   rX   r[   r^   ra   rB   markZskipifhasattrobjectZneeds_object_dirrf   rm   ro   r   r   r   r   r   r   r   r   xfailrT   rU   releaselevelr   r   r   r   r   r+   3   s<   3



	
	*r+   c                   @   s   e Zd Zdd Zdd ZdS )TestWorkingSetc              	   C   sb   t g }td}|| ttd}tt}|	| W 5 Q R X d}|j
 |ks^td S )Nrq   zFoo<1.2z,Foo 1.2 is installed but Foo<1.2 is required)r   r   ru   r>   r   r   rB   rC   r   findrx   ry   r=   )r   rF   rp   rG   r}   r~   r   r   r   test_find_conflictings  s    

z$TestWorkingSet.test_find_conflictingc              	   C   s   t g }td}tjd|d}|| td}tjd|d}|| td}|| td}|| tt}|t	d W 5 Q R X d	}|t
td
g7 }|j |kstdS )z
        A ContextualVersionConflict should be raised when a requirement
        conflicts with a prior requirement for a different package.
        )rY   zBaz==1.0z/foo_dir/Foo-1.0.eggr   )rY   zBaz==2.0z/foo_dir/Bar-1.0.eggz/foo_dir/Baz-1.0.eggz/foo_dir/Baz-2.0.eggzFoo
Bar
z1Baz 1.0 is installed but Baz==2.0 is required by BarN)r   r   r   ru   r>   rB   rC   r   rs   r   reprsetrx   ry   r=   )r   rF   Zmdrp   r   r|   r}   r~   r   r   r   !test_resolve_conflicts_with_prior  s     





z0TestWorkingSet.test_resolve_conflicts_with_priorN)r&   r'   r(   r   r   r   r   r   r   r   r  s   r   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	Zej	
d
edd Zdd Zdd ZeedddgedddgddgedddgddZdZdd  Zd!d" Zd#d$ Zd%S )&TestEntryPointsc                 C   s`   |j dkst|jdkst|jdks*t|jdks8t| tksHtd}t||ks\td S )Nr   "pkg_resources.tests.test_resources)r   )x<foo = pkg_resources.tests.test_resources:TestEntryPoints [x])r    r=   module_nameattrsr]   loadr   str)r   epexpectr   r   r   assertfields  s    zTestEntryPoints.assertfieldsc                 C   s   t jdtdd| _d S )Nr0   )zrequires.txtz[x]r   )r   ru   r   r8   )r   methodr   r   r   setup_method  s     zTestEntryPoints.setup_methodc                 C   s$   t dddgdg| j}| | d S )Nr   r   r   r   )r   r8   r   r   r   r   r   r   
testBasics  s    zTestEntryPoints.testBasicsc                 C   s   d}t || j}| | t d}|jdks4t|jdksBt|jdksPt|jdks^tt d}|jdksvt|jd	kst|jd
kst|jdkstd}t |}|jdkstd S )Nr   zbar baz=  spammity[PING]zbar bazZspammityr   )Zpingz fizzly =  wocka:fooZfizzlyZwocka)r   z0html+mako = mako.ext.pygmentplugin:MakoHtmlLexerz	html+mako)	r   parser8   r   r    r=   r   r   r]   )r   sr   specr   r   r   	testParse  s     



zTestEntryPoints.testParse)r   zx=a:b:czq=x/nazfez=pish:tush-zzx=f[a]>2reject_specc              	   C   s$   t t t| W 5 Q R X d S r   )rB   rC   
ValueErrorr   r   )r   r   r   r   r   test_reject_spec  s    z TestEntryPoints.test_reject_specc                 C   s4   t j }djf t }t|}|j|ks0tdS )z<
        Allow any printable character in the name.
        z{name} = module:attrN)	string	printablestriprS   localsr   r   r    r=   )r   r    r   r   r   r   r   test_printable_name  s    

z#TestEntryPoints.test_printable_namec                 C   s   t |t | jkst| j D ]p\}}||j|jks>t||j|jksTtt||jt|jksrtt||j	t|j	ks tq d S r   )
ri   submap_expectr=   itemsgetr    r   r   r   r]   )r   mrL   r   r   r   r   checkSubMap  s    zTestEntryPoints.checkSubMapfeature1Z
somemoduleZsomefunctionfeature2zanother.moduleZ	SomeClassZextra1Zextra2feature3zthis.moduleZ	something)r]   )r   r   r   z
            # define features for blah blah
            feature1 = somemodule:somefunction
            feature2 = another.module:SomeClass [extra1,extra2]
            feature3 = this.module [something]
    c              	   C   s`   |  td| j tt tdd W 5 Q R X tt tdddg W 5 Q R X d S )Nxyzzx azfoo=barr   zfoo=baz)r   r   parse_group
submap_strrB   rC   r   r   r   r   r   testParseList  s
    zTestEntryPoints.testParseListc              	   C   s   t d| ji}| |d  t| dgks4tt d| j }| |d  t| dgkshttt	 t ddg W 5 Q R X tt	 t | j W 5 Q R X d S )Nr   z[xyz]
z[xyz])
r   	parse_mapr   r   r<   keysr=   rB   rC   r   )r   r   r   r   r   testParseMap  s    zTestEntryPoints.testParseMapc              	   C   s:   t dddgdg}ttj |jdd W 5 Q R X d S )Nr   r   r   r   F)require)r   rB   warnsr#   PkgResourcesDeprecationWarningr   r   r   r   r   testDeprecationWarnings  s       z'TestEntryPoints.testDeprecationWarningsN)r&   r'   r(   r   r   r   r   Zreject_specsrB   r   parametrizer   r   r   r   r   r   r   r   r   r   r   r   r   r   r     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S )TestRequirementsc                 C   s   t d}t|dkstt|dks*t|t dks:t|t dksJt|t dksZt|t dksjt|t dkszt|t dkstd S )NTwisted>=1.2z!Requirement.parse('Twisted>=1.2')ztwisTed>=1.2zTwisted>=2.0z	Zope>=1.2z	Zope>=3.0zTwisted[extras]>=1.2)r   r   r   r=   r   )r   rr   r   r   r     s    
zTestRequirements.testBasicsc                 C   sX   t d}t d}||kstt|t|ks0tt|dks@tt dt dksTtd S )NzTwisted==1.2c1,>=1.2zTwisted>=1.2,==1.2c1ZTwisted'Twisted @ https://localhost/twisted.zip)r   r=   r   r   r1r2r   r   r   testOrdering  s    zTestRequirements.testOrderingc                 C   s   t d}td}td}td}td|ks6ttd|ksFtd|ksRtd|ks^t||ksjt||ksvt||kstd S )Nr   r/   zTwisted-1.1.eggzTwisted-1.2.eggr3   1.1)r   r   ru   r	   r=   )r   r   Zfoo_distZtwist11Ztwist12r   r   r   testBasicContains#  s    


z"TestRequirements.testBasicContainsc                 C   s   t d}t d}||ks tt|jtdks6tt|jtdksLtt|t|ks`tt|tdd tdtddgd fksttt dtdd	t t d fkstd S )
NzTwisted[foo,bar]>=1.2zTwisted[bar,FOO]>=1.2)r   barZtwistedz>=1.2r   r   r   zhttps://localhost/twisted.zip)r   r   r=   r   r]   hashr   	frozensetr   r   r   r   testOptionsAndHashing0  s0    


z&TestRequirements.testOptionsAndHashingc                 C   s   t d}t d}tj}|d|ks*t|d|ks:t|d|ksJt|d|ksZt|d|ksjt|d|kszt|d|kstd S )Nz
foo==0.3a2z
foo!=0.3a4zfoo-0.3a4.eggzfoo-0.3a1.eggzfoo-0.3a2.eggzfoo-0.3a3.eggzfoo-0.3a5.egg)r   r   r   ru   r=   )r   r   r   rP   r   r   r   testVersionEqualityH  s    

z$TestRequirements.testVersionEqualityc                 C   sT   t djdkstt djdks(tt djdks<tt djdksPtdS )zQ
        The setuptools project should implement the setuptools package.
        
setuptoolszsetuptools == 0.7zsetuptools == 0.7a1zsetuptools >= 0.7N)r   r   rK   r=   r   r   r   r   testSetuptoolsProjectNameV  s    z*TestRequirements.testSetuptoolsProjectNameN)	r&   r'   r(   r   r   r   r   r   r   r   r   r   r   r     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ejddgddddddddddg
dd  Zd!Zejddgd"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1geeee  d2d3 Zd4d5 Zd6S )7TestParsingc                 C   s   t tdg kstd S )N )r<   r   r=   r   r   r   r   testEmptyParsec  s    zTestParsing.testEmptyParsec                 C   sV   g g fddgfg gg fdddgfddgddgffD ]\}}t t||ks2tq2d S )Nr   z x
 yyzx

)r<   r#   r$   r=   )r   inpoutr   r   r   testYieldingf  s    
zTestParsing.testYieldingc              	   C   sj   d}t t|d dgfdddgfddgfdg fd	d
gfgksBttt t td W 5 Q R X d S )Na   
                    x
                    [Y]
                    z

                    a
                    [b ]
                    # foo
                    c
                    [ d]
                    [q]
                    v
                    r   Yzr   r   r   rP   qr`   z[foo)r<   r#   split_sectionsr=   rB   rC   r   )r   sampler   r   r   testSplittingp  s    

zTestParsing.testSplittingc                 C   sT   t ddkstt ddks tt ddks0tt ddks@tt ddksPtd S )	Nzadns-pythonz
WSGI Utilsz
WSGI-UtilszWSGI  UtilsMoney$$$MakerMoney-Makerpeak.webzpeak-web)r
   r=   r   r   r   r   testSafeName  s
    zTestParsing.testSafeNamec                 C   sT   t ddkstt ddks tt ddks0tt ddks@tt d	d	ksPtd S )
Nz1.2-1z	1.2.post1z	1.2 alphaz	1.2.alphaz2.3.4 20050521z2.3.4.20050521r   r   r   )r   r=   r   r   r   r   testSafeVersion  s
    zTestParsing.testSafeVersionc              	   C   s   t tdtdgkstt tdtdgks4ttdtdksJttt td W 5 Q R X tt td W 5 Q R X tt td W 5 Q R X tt td W 5 Q R X tt td	 W 5 Q R X d S )
NzTwis-Ted>=1.2-1zTwisted >=1.2, \ # more
<2.0zTwisted>=1.2,<2.0zFooBar==1.99a3z>=2.3zx\zx==2 qz	X==1
Y==2#)r<   r   r   r=   r   rB   rC   r   r   r   r   r   testSimpleRequirements  s"    



z"TestParsing.testSimpleRequirementsc                 C   sd   t dt dkstt dt dks0tt dt dksHtt dt dks`td S )Nzfoobar;os_name=='a'zname==1.1;python_version=='2.7'zname==1.1;python_version=='3.6'zname==1.0;python_version=='2.7'zname==1.2;python_version=='2.7'z$name[foo]==1.0;python_version=='3.6'z(name[foo,bar]==1.0;python_version=='3.6')r   r   r=   r   r   r   r   test_requirements_with_markers  s$    
z*TestParsing.test_requirements_with_markersc                 C   s   t d\}d S )Nzfoo==1.0+org1r   r   rG   r   r   r   test_local_version  s    zTestParsing.test_local_versionc                 C   s   t d\}t d\}d S )Nzfoo>=1.0, <3zfoo >= 1.0, < 3r   r   r   r   r   %test_spaces_between_multiple_versions  s    
z1TestParsing.test_spaces_between_multiple_versionslowerupper)z1.2-rc1z1.2rc1)0.40.4.0)z0.4.0.0r  )z0.4.0-0z0.4-0)0post1z0.0post1)Z0pre10.0c1)z0.0.0preview1Z0c1)r  z0-rc1)z1.2a1z1.2.a.1)z1.2.az1.2ac                 C   s   t |t |kstd S r   r	   r=   r   r  r  r   r   r   r     s    zTestParsing.testVersionEqualityz
        0.80.1-3 0.80.1-2 0.80.1-1 0.79.9999+0.80.0pre4-1
        0.79.9999+0.80.0pre2-3 0.79.9999+0.80.0pre2-2
        0.77.2-1 0.77.1-1 0.77.0-1
        )2.12.1.1)2a1Z2b0)r  r	  )z2.3a1z2.3)2.1-1z2.1-2)r  r
  )r	  z2.1post4)z2.1a0-20040501r	  )r   z02.1)z3.2z	3.2.post0)z3.2post1z3.2post2)r  z4.0)z0.0.4r  )r  z0.4post1)z	2.1.0-rc1z2.1.0)z2.1devz2.1a0c                 C   s   t |t |k std S r   r  r  r   r   r   testVersionOrdering  s    zTestParsing.testVersionOrderingc                 C   s    t tdt tdkstdS )z
        Ensure that our versions stay hashable even though we've subclassed
        them and added some shim code to them.
        z1.0N)r   r	   r=   r   r   r   r   testVersionHashable  s    zTestParsing.testVersionHashableN)r&   r'   r(   r   r   r   r   r   r   r   r   r   rB   r   r   r   Ztorturer<   r   reversedr   r  r  r   r   r   r   r   b  sb   


r   c                   @   sb   e Zd ZdZejdd Zejdddd Zejj	e
 dkd	d
Zedd Zedd ZdS )TestNamespacesz8__import__('pkg_resources').declare_namespace(__name__)
c              	   c   sX   t tdst|V  dS t|d }tt|| zt||V  W 5 t| X dS )z
        Where available, return the tempdir as a symlink,
        which as revealed in #231 is more fragile than
        a natural tempdir.
        symlinkNz-linked)r   osr   r  unlinktype)r   tmpdirZ	link_namer   r   r   symlinked_tmpdir  s    

zTestNamespaces.symlinked_tmpdirT)Zautousec                 c   sP   t j }tjdd }|d}tjt| z
dV  W 5 |t _|t_X dS )z
        Patch sys.path to include the 'site-pkgs' dir. Also
        restore pkg_resources._namespace_packages to its
        former state.
        N	site-pkgs)r#   _namespace_packagescopyrT   pathmkdirappendr   )r   r  Zsaved_ns_pkgsZsaved_sys_pathZ	site_pkgsr   r   r   patched_path  s    


zTestNamespaces.patched_pathWindowsz#591rc   c           	   	   C   s,  |  }|}tjt|d  |d |d f}|D ]D}|d }|d }|  |d j| jdd |d j| jdd q4tj	t
dd	 d
dl}W 5 Q R X dtjksttj	t
dd	 d
dl}W 5 Q R X dtjksttjd dgkstt|d d d t|d d d g}|jj|ks(tdS )a  
        Test nested namespace packages
        Create namespace packages in the following tree :
            site-packages-1/pkg1/pkg2
            site-packages-2/pkg1/pkg2
        Check both are in the _namespace_packages dict and that their __path__
        is correct
        
site-pkgs2r  pkg1pkg2__init__.pyutf-8encodingpkg_resources.declare_namespacematchr   Nz	pkg1.pkg2)realpathrT   r  r  r   
ensure_dir
write_textns_strrB   r   DeprecationWarningr   r#   r  r=   Z	pkg1.pkg2r!  __path__)	r   r  Zreal_tmpdirr  	site_dirssiter   r!  rE   r   r   r   test_two_levels_deep  s*    
z#TestNamespaces.test_two_levels_deepc           
   	   C   s   |}|d |d |d f}d}t |dD ]b\}}|dkrHtjt| |d }|d }|  |d j| jd	d
 |d j|| d	d
 q(tj	t
dd ddl}ddl}W 5 Q R X dd |D }	|j|	kst|jjdkstdS )aV  
        Test that if multiple versions of the same namespace package subpackage
        are on different sys.path entries, that only the one earliest on
        sys.path is imported, and that the namespace package's __path__ is in
        the correct order.

        Regression test for https://github.com/pypa/setuptools/issues/207
        r  r  z
site-pkgs3z__version__ = %rr:   nspkgsubpkgr"  r#  r$  r&  r'  r   Nc                 S   s   g | ]}t | d  qS )r2  )r   r)  )r7   r0  r   r   r   r9   c  s     z2TestNamespaces.test_path_order.<locals>.<listcomp>)	enumeraterT   r  r  r   r*  r+  r,  rB   r   r-  r2  Znspkg.subpkgr.  r=   r3  __version__)
r   r  r  r/  Zvers_strnumberr0  r2  r3  rE   r   r   r   test_path_orderC  s(    zTestNamespaces.test_path_orderN)r&   r'   r(   r,  rB   Zfixturer  r  r   r   r,   systemZissue591r1  r7  r   r   r   r   r    s   



#r  )r   r  r,   r   rT   rB   Zpackaging.specifiersr   r#   r   r   r   r   r   r   r	   r
   r   r   EmptyProviderr   ru   r?   r+   r   r   r   r   r  r   r   r   r   <module>   s(   ,  A*qU 