ó
ç'ÊUc           @   s™  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	 d  d l
 m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d	 l m Z d  d
 l m Z d  d l m Z d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e e f d „  ƒ  YZ  d e f d „  ƒ  YZ! d S(   iÿÿÿÿN(   t   sleep(   t   existst   joint   abspath(   t   rmtree(   t   mkdtemp(   t   unittest(   t   defer(   t   retry_on_eintr(   t   get_testenv(   t   SiteTest(   t   ProcessTestt   ProjectTestc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   t   testprojectc         C   sX   t  ƒ  |  _ |  j |  _ t |  j |  j ƒ |  _ t |  j |  j ƒ |  _ t ƒ  |  _ d  S(   N(	   R   t	   temp_patht   cwdR   t   project_namet	   proj_patht   proj_mod_pathR	   t   env(   t   self(    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   setUp   s
    c         C   s   t  |  j ƒ d  S(   N(   R   R   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   tearDown   s    c         O   s]   t  j ƒ  K } t j d d f | } t j | d | d | d |  j d |  j | SWd  QXd  S(   Ns   -ms   scrapy.cmdlinet   stdoutt   stderrR   R   (   t   tempfilet   TemporaryFilet   syst
   executablet
   subprocesst   callR   R   (   R   t   new_argst   kwargst   outt   args(    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyR       s    !c         O   s´   t  j d d f | } t j | d |  j d |  j d t j d t j | } d } d } xU | j ƒ  d  k r¯ t	 | ƒ | | 7} | d	 k r[ | j
 ƒ  t s¬ t d
 ƒ ‚ q[ q[ W| S(   Ns   -ms   scrapy.cmdlineR   R   R   R   i    gš™™™™™É?i   s&   Command took too much time to complete(   R   R   R   t   PopenR   R   t   PIPEt   pollt   NoneR    t   killt   Falset   AssertionError(   R   R   R    R"   t   pt   waitedt   interval(    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   proc&   s    	


(   t   __name__t
   __module__R   R   R   R   R-   (    (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyR      s
   			t   StartprojectTestc           B   s   e  Z d  „  Z RS(   c         C   sO  |  j  d |  j d |  j ƒ ƒ t t |  j d ƒ ƒ s= t ‚ t t |  j d ƒ ƒ s[ t ‚ t t |  j d ƒ ƒ sy t ‚ t t |  j d ƒ ƒ s— t ‚ t t |  j d ƒ ƒ sµ t ‚ t t |  j d ƒ ƒ sÓ t ‚ t t |  j d	 d ƒ ƒ sô t ‚ |  j  d
 |  j d |  j ƒ ƒ |  j  d
 |  j d d ƒ ƒ |  j  d
 |  j d d ƒ ƒ d  S(   Ni    t   startprojects
   scrapy.cfgR   s   __init__.pys   items.pys   pipelines.pys   settings.pyt   spidersi   s   wrong---project---nameR   (   t   assertEqualR   R   R   R   R   R)   R   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_startproject:   s    !(   R.   R/   R4   (    (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyR0   8   s   t   CommandTestc           B   s   e  Z d  „  Z RS(   c         C   sV   t  t |  ƒ j ƒ  |  j d |  j ƒ t |  j |  j ƒ |  _ d |  j |  j d <d  S(   NR1   s   %s.settingst   SCRAPY_SETTINGS_MODULE(	   t   superR5   R   R   R   R   R   R   R   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyR   L   s    (   R.   R/   R   (    (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyR5   J   s   t   GenspiderCommandTestc           B   sS   e  Z d  „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(	   c         C   s‚   |  j  d |  j d d ƒ ƒ t t |  j d d ƒ ƒ s> t ‚ |  j  d |  j d d d ƒ ƒ t t |  j d d ƒ ƒ s~ t ‚ d  S(   Ni   t	   genspidert	   test_nameR2   s   test_name.pyi    s   test.com(   R3   R   R   R   R   R)   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_argumentsU   s    "t   crawlc         C   sÇ   | r d | g n g  } d } |  j  d | d | Œ } t | j j ƒ } |  j d | | f | ƒ |  j t t |  j d d ƒ ƒ ƒ |  j  d | d | Œ } t | j j ƒ } |  j d | | ƒ d  S(	   Ns   --template=%st   test_spiderR9   s   test.coms-   Created spider %r using template %r in moduleR2   s   test_spider.pys"   Spider %r already exists in module(	   R-   R   R   t   readt   assertInt
   assertTrueR   R   R   (   R   t   tplnameR"   t   spnameR*   R!   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_template]   s    "c         C   s   |  j  d ƒ d  S(   Nt   basic(   RC   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_template_basich   s    c         C   s   |  j  d ƒ d  S(   Nt   csvfeed(   RC   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_template_csvfeedk   s    c         C   s   |  j  d ƒ d  S(   Nt   xmlfeed(   RC   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_template_xmlfeedn   s    c         C   s    |  j  d |  j d d ƒ ƒ d  S(   Ni    R9   s   --list(   R3   R   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt	   test_listq   s    c         C   s?   |  j  d |  j d d ƒ ƒ |  j  d |  j d d d ƒ ƒ d  S(   Ni    R9   s   --dump=basics   -dRD   (   R3   R   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt	   test_dumpt   s    c         C   sL   |  j  d |  j d |  j ƒ ƒ t t |  j d d |  j ƒ ƒ sH t ‚ d  S(   Ni   R9   R2   s   %s.py(   R3   R   R   R   R   R   R)   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_same_name_as_projectx   s    (
   R.   R/   R;   RC   RE   RG   RI   RJ   RK   RL   (    (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyR8   S   s   						t   MiscCommandsTestc           B   s   e  Z d  „  Z RS(   c         C   s   |  j  d |  j d ƒ ƒ d  S(   Ni    t   list(   R3   R   (   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyRJ      s    (   R.   R/   RJ   (    (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyRM   }   s   t   RunSpiderCommandTestc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s¸   |  j  ƒ  } t j | ƒ t t | d ƒ ƒ } t | d ƒ  } | j d ƒ Wd  QX|  j d | ƒ } | j j	 ƒ  } |  j
 d | ƒ |  j
 d | ƒ |  j
 d | ƒ |  j
 d | ƒ d  S(	   Ns   myspider.pyt   ws   
import scrapy

class MySpider(scrapy.Spider):
    name = 'myspider'

    def start_requests(self):
        self.logger.debug("It Works!")
        return []
t	   runspiders   DEBUG: It Works!s   INFO: Spider openeds   INFO: Closing spider (finished)s   INFO: Spider closed (finished)(   t   mktempt   ost   mkdirR   R   t   opent   writeR-   R   R>   R?   (   R   t   tmpdirt   fnamet   fR*   t   log(    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_runspider…   s    	c         C   sˆ   |  j  ƒ  } t j | ƒ t t | d ƒ ƒ } t | d ƒ  } | j d ƒ Wd  QX|  j d | ƒ } | j j	 ƒ  } |  j
 d | ƒ d  S(   Ns   myspider.pyRP   s#   
from scrapy.spiders import Spider
RQ   s   No spider found in file(   RR   RS   RT   R   R   RU   RV   R-   R   R>   R?   (   R   RW   RX   RY   R*   RZ   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_runspider_no_spider_found›   s    c         C   s5   |  j  d d ƒ } | j j ƒ  } |  j d | ƒ d  S(   NRQ   t   some_non_existent_files&   File not found: some_non_existent_file(   R-   R   R>   R?   (   R   R*   RZ   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_runspider_file_not_found§   s    c         C   sˆ   |  j  ƒ  } t j | ƒ t t | d ƒ ƒ } t | d ƒ  } | j d ƒ Wd  QX|  j d | ƒ } | j j	 ƒ  } |  j
 d | ƒ d  S(   Ns   myspider.txtRP   t    RQ   s   Unable to load(   RR   RS   RT   R   R   RU   RV   R-   R   R>   R?   (   R   RW   RX   RY   R*   RZ   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_runspider_unable_to_load¬   s    (   R.   R/   R[   R\   R^   R`   (    (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyRO   ƒ   s   			t   ParseCommandTestc           B   sM   e  Z d  Z d „  Z e j d „  ƒ Z e j d „  ƒ Z e j d „  ƒ Z RS(   t   parsec      	   C   sí   t  t |  ƒ j ƒ  d |  _ t t |  j d d ƒ ƒ } t | d ƒ   } | j d j	 |  j ƒ ƒ Wd  QXt t |  j d ƒ ƒ } t | d ƒ  } | j d ƒ Wd  QXt t |  j d ƒ ƒ } t | d	 ƒ  } | j d
 |  j
 ƒ Wd  QXd  S(   Nt   parse_spiderR2   s   myspider.pyRP   sç   
import scrapy

class MySpider(scrapy.Spider):
    name = '{0}'

    def parse(self, response):
        if getattr(self, 'test_arg', None):
            self.logger.debug('It Works!')
        return [scrapy.Item(), dict(foo='bar')]
s   pipelines.pys¯   
import logging

class MyPipeline(object):
    component_name = 'my_pipeline'

    def process_item(self, item, spider):
        logging.info('It Works!')
        return item
s   settings.pyt   as1   
ITEM_PIPELINES = {'%s.pipelines.MyPipeline': 1}
(   R7   Ra   R   t   spider_nameR   R   R   RU   RV   t   formatR   (   R   RX   RY   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyR   »   s    	
	c      	   c   sN   |  j  d |  j d d d d |  j d ƒ g ƒ V\ } } } |  j d | ƒ d  S(   Ns   --spiders   -as
   test_arg=1s   -cRb   s   /htmls   DEBUG: It Works!(   t   executeRe   t   urlR?   (   R   t   _R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_spider_argumentsß   s
    c         c   sK   |  j  d |  j d d d |  j d ƒ g ƒ V\ } } } |  j d | ƒ d  S(   Ns   --spiders   --pipeliness   -cRb   s   /htmls   INFO: It Works!(   Rg   Re   Rh   R?   (   R   Ri   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_pipelinesç   s
    c         c   sH   |  j  d |  j d d |  j d ƒ g ƒ V\ } } } |  j d | ƒ d  S(   Ns   --spiders   -cRb   s   /htmls   [{}, {'foo': 'bar'}](   Rg   Re   Rh   R?   (   R   t   statusR!   R   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_parse_itemsï   s    .(	   R.   R/   t   commandR   R   t   inlineCallbacksRj   Rk   Rm   (    (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyRa   ·   s
   	$t   BenchCommandTestc           B   s   e  Z d  „  Z RS(   c         C   s>   |  j  d d d d d ƒ } | j j ƒ  } |  j d | ƒ d  S(   Nt   benchs   -ss   LOGSTATS_INTERVAL=0.001s   CLOSESPIDER_TIMEOUT=0.01s   INFO: Crawled(   R-   R   R>   R?   (   R   R*   RZ   (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   test_runú   s    (   R.   R/   Rr   (    (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyRp   ø   s   ("   RS   R   R   R   t   timeR    t   os.pathR   R   R   t   shutilR   R   t   twisted.trialR   t   twisted.internetR   t   scrapy.utils.pythonR   t   scrapy.utils.testR	   t   scrapy.utils.testsiteR
   t   scrapy.utils.testprocR   t   TestCaseR   R0   R5   R8   RM   RO   Ra   Rp   (    (    (    s7   /home/travis/build/scrapy/scrapy/tests/test_commands.pyt   <module>   s*   %	*4A