
 X                 @   s   d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z m Z m Z m	 Z	 m
 Z
 d d l Z d d l Z d d l m Z Gd d   d e j  Z Gd d   d e j  Z d S)	zPackage Index Tests
    N)urllib2httplib	HTTPErrorunicodepathname2url)IndexServerc               @   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)TestPackageIndexc             C   s   t  j j   } d } y | j |  } Wn; t k
 re t j   d } |  j | t |  k  Yn X|  j t	 | t
   d  S)Nz.http://127.0.0.1:0/nonesuch/test_package_index   )
setuptoolspackage_indexPackageIndexopen_url	Exceptionsysexc_info
assertTruestr
isinstancer   )selfindexurlv r   T/home/ubuntu/projects/ifolica/build/setuptools/setuptools/tests/test_packageindex.pytest_bad_url_bad_port   s    z&TestPackageIndex.test_bad_url_bad_portc             C   s   t  j j d d  } d } y | j |  } Wn; t k
 rk t j   d } |  j | t |  k  Yn X|  j t	 | t
   d  S)Nhostswww.example.comzMurl:%20https://svn.plone.org/svn/collective/inquant.contentmirror.plone/trunkr	   )r   )r
   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   test_bad_url_typo   s    	z"TestPackageIndex.test_bad_url_typoc             C   s   t  j j d d	  } d d   } | | _ d } y | j |  } Wn; t k
 r t j   d } |  j d t	 |  k  Yn Xt
 d   d  S)
Nr   www.example.comc              W   s   t  j d   d  S)Nline)r   BadStatusLine)argsr   r   r   _urlopen/   s    z?TestPackageIndex.test_bad_url_bad_status_line.<locals>._urlopenzhttp://example.comr	   r   zShould have raise here!)r   )r
   r   r   openerr   r   r   r   r   r   AssertionError)r   r   r"   r   r   r   r   r   test_bad_url_bad_status_line*   s    		z-TestPackageIndex.test_bad_url_bad_status_linec             C   s   t  j j d d
  } d } y | j |  Wnb t j j k
 r t j   d } t	 |  } d | k s d | k s d | k s t
  d SYn Xt d	   d S)zO
        A bad URL with a double scheme should raise a DistutilsError.
        r   www.example.comz2http://http://svn.pythonpaste.org/Paste/wphp/trunkr	   znonnumeric portzgetaddrinfo failedzName or service not knownNzDid not raise)r&   )r
   r   r   r   	distutilserrorsDistutilsErrorr   r   r   r$   RuntimeError)r   r   r   errormsgr   r   r   test_bad_url_double_scheme<   s    	*	z+TestPackageIndex.test_bad_url_double_schemec             C   s^   t  j j d d	  } t j d d k rZ t j d d k rZ d } d } | j | |  n  d  S)
Nr   www.example.comr      r	      zhttp://example.comz><a href="http://www.famfamfam.com](http://www.famfamfam.com/">)r.   )r
   r   r   r   version_infoZprocess_index)r   r   r   pager   r   r   test_bad_url_screwy_hrefO   s    	&z)TestPackageIndex.test_bad_url_screwy_hrefc             C   s8   t  j j d d  } d } |  j | j | d   d  S)Nr   www.example.comzfile:///tmp/test_package_indexT)r4   )r
   r   r   r   Zurl_ok)r   r   r   r   r   r   test_url_ok\   s    	zTestPackageIndex.test_url_okc             C   s   t  j j d  r d St   } | j   | j   d } t j j |  } t	 j
 j d  } | j |  | j   |  j d | k  |  j t | d  d k  |  j d | d d j k  d S)aq  
        Download links from the pypi simple index should be used before
        external download links.
        https://bitbucket.org/tarek/distribute/issue/163

        Usecase :
        - someone uploads a package on pypi, a md5 is generated
        - someone manually copies this link (with the md5 in the url) onto an
          external page accessible from the package page.
        - someone reuploads the package (with a different md5)
        - while easy_installing, an MD5 error occurs because the external link
          is used
        -> Setuptools should use the link from pypi, not the external one.
        javaNztest_links_priority/simple/Zfoobarr	   Zcorrect_md5r   )r   platform
startswithr   startbase_urlr
   r   r   pkg_resourcesRequirementparseZfind_packagesstopr   lenlocation)r   server	index_urlpirequirementr   r   r   test_links_priorityc   s    	

z$TestPackageIndex.test_links_priorityc             C   st   |  j  t j j d  d
  |  j  t j j d  d  |  j  t j j d  d  |  j  t j j d	  d  d  S)Nzreportlab-2.5.win32-py2.4.exereportlab-2.52.4win32zreportlab-2.5.win32.exez!reportlab-2.5.win-amd64-py2.7.exe2.7	win-amd64zreportlab-2.5.win-amd64.exe)rF   rG   win32)rF   NrK   )rF   rI   rJ   )rF   NrJ   )assertEqualr
   r   Zparse_bdist_wininst)r   r   r   r   test_parse_bdist_wininst   s    z)TestPackageIndex.test_parse_bdist_wininstc             C   sE   t  j j j } | d  \ } } |  j | d  |  j | d  d S)zA
        Test the basic usage of _vcs_split_rev_from_url
        zhttps://example.com/bar@2995zhttps://example.com/barZ2995N)r
   r   r   Z_vcs_split_rev_from_urlrL   )r   Zvsrfur   revr   r   r   test__vcs_split_rev_from_url   s    z-TestPackageIndex.test__vcs_split_rev_from_urlc             C   s   t  d d  } | j d  | j   z0 d t t j    d } t j j |  } Wd t j	 d  Xd | j
   k s t  d S)zR
        local_open should be able to read an index from the file system.
        z
index.htmlwz<div>content</div>zfile:/Ncontent)openwritecloser   osgetcwdr
   r   Z
local_openremovereadr$   )r   fr   resr   r   r   test_local_index   s    
z!TestPackageIndex.test_local_indexN)__name__
__module____qualname__r   r   r%   r-   r3   r5   rE   rM   rO   r\   r   r   r   r   r      s   %
	r   c               @   sL   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d S)TestContentCheckersc             C   s[   t  j j j d  } | j d j d   |  j | j j   d  |  j	 | j
    d  S)Nz3http://foo/bar#md5=f12895fdffbd45007040d2e44df98478z$You should probably not be using MD5asciiZ f12895fdffbd45007040d2e44df98478)r
   r   HashCheckerfrom_urlfeedencoderL   hash	hexdigestr   is_valid)r   checkerr   r   r   test_md5   s    	zTestContentCheckers.test_md5c             C   sB   t  j j j d  } | j d j d   |  j | j    d S)z<Content checks should succeed silently if no hash is presentz1http://foo/bar#something%20completely%20differentanythingra   N)r
   r   rb   rc   rd   re   r   rh   )r   ri   r   r   r   test_other_fragment   s    	z'TestContentCheckers.test_other_fragmentc             C   sB   t  j j j d  } | j d j d   |  j | j    d S)z0Content checks should succeed if a hash is emptyzhttp://foo/bar#md5=rk   ra   N)r
   r   rb   rc   rd   re   r   rh   )r   ri   r   r   r   test_blank_md5   s    	z"TestContentCheckers.test_blank_md5c             C   s,   t  j j j d  } |  j | j d  d  S)Nz3http://foo/bar#md5=f12895fdffbd45007040d2e44df98478md5)r
   r   rb   rc   rL   	hash_name)r   ri   r   r   r   test_get_hash_name_md5   s    	z*TestContentCheckers.test_get_hash_name_md5c             C   sA   t  j j j d  } | j d d   d  } |  j | d  d  S)Nz3http://foo/bar#md5=f12895fdffbd45007040d2e44df98478c             S   s   |  S)Nr   )xr   r   r   <lambda>   s    z1TestContentCheckers.test_report.<locals>.<lambda>zMy message about %szMy message about md5)r
   r   rb   rc   reportrL   )r   ri   Zrepr   r   r   test_report   s    	zTestContentCheckers.test_reportN)r]   r^   r_   rj   rl   rm   rp   rt   r   r   r   r   r`      s
   r`   )__doc__r   rV   Zunittestr;   Zsetuptools.compatr   r   r   r   r   distutils.errorsr'   Zsetuptools.package_indexr
   Zsetuptools.tests.serverr   ZTestCaser   r`   r   r   r   r   <module>   s   (