
^Yc           @   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 l m Z m Z m Z m Z d d l m Z m Z e j d f k  r d d	 l m Z n d d	 l m Z e Z y e j e Z Wn e k
 re Z n Xy e e Z  Wn e! k
 r7e Z  n Xe j d d
 f k pPe Z" Z# d   Z$ 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 f d     YZ+ d e f d     YZ, d e, f d     YZ- d S(   s)   
Tests for L{pyflakes.scripts.pyflakes}.
iN(   t   PY2(   t   UnusedImport(   t   Reporter(   t   maint	   checkPatht   checkRecursivet   iterSourceCode(   t   TestCaset   skipIfi   (   t   StringIOi   c         O   s5   t  j |  } t  _ z | | |   SWd | t  _ Xd S(   s?   
    Call C{f} with C{sys.stderr} redirected to C{stderr}.
    N(   t   syst   stderr(   R   t   ft   argst   kwargst   outer(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   withStderrTo+   s    t   Nodec           B   s   e  Z d  Z d d  Z RS(   s   
    Mock an AST node.
    i    c         C   s   | |  _  | |  _ d  S(   N(   t   linenot
   col_offset(   t   selfR   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   __init__:   s    	(   t   __name__t
   __module__t   __doc__R   (    (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR   6   s   t   SysStreamCapturingc           B   s5   e  Z d  Z d d  Z d   Z d   Z d   Z RS(   s/  
    Context manager capturing sys.stdin, sys.stdout and sys.stderr.

    The file handles are replaced with a StringIO object.
    On environments that support it, the StringIO object uses newlines
    set to os.linesep.  Otherwise newlines are converted from \n to
    os.linesep during __exit__.
    c         C   sU   y t  | d t j SWn7 t k
 rP t |  _ | d  k rC t    St  |  Sn Xd  S(   Nt   newline(   R	   t   ost   linesept	   TypeErrort   Truet   _newlinet   None(   R   t   buffer(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   _create_StringIOJ   s    	c         C   s%   t  |  _ |  j | p d  |  _ d  S(   Nt    (   t   FalseR   R"   t   _stdin(   R   t   stdin(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR   V   s    	c         C   s`   t  j |  _ t  j |  _ t  j |  _ |  j t  _ |  j   t  _ |  _	 |  j   t  _ |  _
 |  S(   N(   R
   R&   t   _orig_stdint   stdoutt   _orig_stdoutR   t   _orig_stderrR%   R"   t   _stdout_stringiot   _stderr_stringio(   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt	   __enter__Z   s    c         G   s   |  j  j   |  _ |  j j   |  _ |  j ru t j d k ru |  j j d t j  |  _ |  j j d t j  |  _ n  |  j	 t
 _ |  j t
 _ |  j t
 _ d  S(   Ns   
(   R+   t   getvaluet   outputR,   t   errorR   R   R   t   replaceR'   R
   R&   R)   R(   R*   R   (   R   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   __exit__e   s    N(   R   R   R   R    R"   R   R-   R2   (    (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR   ?   s
   			t   LoggingReporterc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   sK   
    Implementation of Reporter that just appends any error to a list.
    c         C   s   | |  _  d S(   sh   
        Construct a C{LoggingReporter}.

        @param log: A list to append log messages to.
        N(   t   log(   R   R4   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR   w   s    c         C   s    |  j  j d t |  f  d  S(   Nt   flake(   R4   t   appendt   str(   R   t   message(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR5      s    c         C   s   |  j  j d | | f  d  S(   Nt   unexpectedError(   R4   R6   (   R   t   filenameR8   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR9      s    c         C   s&   |  j  j d | | | | | f  d  S(   Nt   syntaxError(   R4   R6   (   R   R:   t   msgR   t   offsett   line(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR;      s    (   R   R   R   R   R5   R9   R;   (    (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR3   r   s
   			t   TestIterSourceCodec           B   s_   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z RS(
   s&   
    Tests for L{iterSourceCode}.
    c         C   s   t  j   |  _ d  S(   N(   t   tempfilet   mkdtempt   tempdir(   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   setUp   s    c         C   s   t  j |  j  d  S(   N(   t   shutilt   rmtreeRB   (   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   tearDown   s    c         G   sA   | s t   t j j |  j |  } t | d  } | j   | S(   Nt   a(   t   AssertionErrorR   t   patht   joinRB   t   opent   close(   R   t   partst   fpatht   fd(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   makeEmptyFile   s
    
c         C   s&   |  j  t t |  j g   g   d S(   sB   
        There are no Python files in an empty directory.
        N(   t   assertEqualt   listR   RB   (   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_emptyDirectory   s    c         C   s8   |  j  d  } |  j t t |  j g   | g  d S(   sd   
        If the directory contains one Python file, C{iterSourceCode} will find
        it.
        s   foo.pyN(   RP   RQ   RR   R   RB   (   R   t	   childpath(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_singleFile   s    c         C   s3   |  j  d  |  j t t |  j g   g   d S(   sJ   
        Files that are not Python source files are not included.
        s   foo.pycN(   RP   RQ   RR   R   RB   (   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_onlyPythonSource   s    c         C   s   t  j t  j j |  j d   |  j d d  } t  j t  j j |  j d   |  j d d  } |  j d  } |  j t t |  j g   t | | | g   d S(   sk   
        If the Python files are hidden deep down in child directories, we will
        find them.
        t   foos   a.pyt   bars   b.pys   c.pyN(	   R   t   mkdirRI   RJ   RB   RP   RQ   t   sortedR   (   R   t   apatht   bpatht   cpath(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_recurses   s    c         C   s   t  j j |  j d  } t  j j |  j d  } t  j |  |  j d d  } t  j |  |  j d d  } |  j t t | | g   t | | g   d S(   sr   
        L{iterSourceCode} can be given multiple directories.  It will recurse
        into each of them.
        RW   RX   s   a.pys   b.pyN(	   R   RI   RJ   RB   RY   RP   RQ   RZ   R   (   R   t   foopatht   barpathR[   R\   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_multipleDirectories   s    c         C   s5   |  j  d  } |  j t t | g   | g  d S(   s   
        If one of the paths given to L{iterSourceCode} is not a directory but
        a file, it will include that in its output.
        s   e.pyN(   RP   RQ   RR   R   (   R   t   epath(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_explicitFiles   s    (   R   R   R   RC   RF   RP   RS   RU   RV   R^   Ra   Rc   (    (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR?      s   								t   TestReporterc           B   s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s    
    Tests for L{Reporter}.
    c         C   sK   t    } t d |  } | j d d d d d  |  j d | j    d S(   s  
        C{syntaxError} reports that there was a syntax error in the source
        file.  It reports to the error stream and includes the filename, line
        number, error message, actual line of source and a caret pointing to
        where the error is.
        s   foo.pys	   a problemi   i   s   bad line of sources2   foo.py:3:8: a problem
bad line of source
       ^
N(   R	   R   R    R;   RQ   R.   (   R   t   errt   reporter(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_syntaxError   s    	c         C   sK   t    } t d |  } | j d d d d d  |  j d | j    d S(   sy   
        C{syntaxError} doesn't include a caret pointing to the error if
        C{offset} is passed as C{None}.
        s   foo.pys	   a problemi   s   bad line of sources'   foo.py:3: a problem
bad line of source
N(   R	   R   R    R;   RQ   R.   (   R   Re   Rf   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_syntaxErrorNoOffset   s    	c         C   s~   t    } d d g } t d |  } | j d d d t | d  d d j |   |  j d	 | d
 d d | j    d S(   s   
        If there's a multi-line syntax error, then we only report the last
        line.  The offset is adjusted so that it is relative to the start of
        the last line.
        s   bad line of sources   more bad lines of sources   foo.pys	   a problemi   i    i   s   
s   foo.py:3:7: a problem
is         ^
N(   R	   R   R    R;   t   lenRJ   RQ   R.   (   R   Re   t   linesRf   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_multiLineSyntaxError   s    		 c         C   sB   t    } t d |  } | j d d  |  j d | j    d S(   sO   
        C{unexpectedError} reports an error processing a source file.
        s	   source.pys   error messages   source.py: error message
N(   R	   R   R    R9   RQ   R.   (   R   Re   Rf   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_unexpectedError  s    	c         C   s^   t    } t | d  } t d t d  d  } | j |  |  j | j   d | f  d S(   s   
        C{flake} reports a code warning from Pyflakes.  It is exactly the
        str() of a L{pyflakes.messages.Message}.
        s   foo.pyi*   RX   s   %s
N(   R	   R   R    R   R   R5   RQ   R.   (   R   t   outRf   R8   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt
   test_flake  s
    	(   R   R   R   Rg   Rh   Rk   Rl   Rn   (    (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyRd      s   					t
   CheckTestsc           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z e e j d k d  d    Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   sL   
    Tests for L{check} and L{checkPath} which check a file for flakes.
    c         C   s]   t  j   \ } } t | d  s3 | j d  } n  t | d  } | j |  | j   | S(   sV   
        Make a temporary file containing C{content} and return a path to it.
        t   decodet   asciit   wb(   R@   t   mkstempt   hasattrt   encodeRK   t   writeRL   (   R   t   contentt   _RN   RO   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   makeTempFile(  s    
c         C   sP   t    } t | t |  } |  j | | j   f t |  d j |  f  d S(   s   
        Assert that C{path} causes errors.

        @param path: A path to a file to check.
        @param errorList: A list of errors expected to be printed to stderr.
        R#   N(   R	   R   R   RQ   R.   Ri   RJ   (   R   RI   t	   errorListRe   t   count(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   assertHasErrors4  s    	c         C   s+   g  } t  |  } t | |  } | | f S(   s  
        Get any warnings or errors reported by pyflakes for the file at C{path}.

        @param path: The path to a Python file on disk that pyflakes will check.
        @return: C{(count, log)}, where C{count} is the number of warnings or
            errors generated, and log is a list of those warnings, presented
            as structured data.  See L{LoggingReporter} for more details.
        (   R3   R   (   R   RI   R4   Rf   R{   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt	   getErrors@  s    	c         C   s'   d d l  m } |  j | j t  d  S(   Ni(   t   pyflakes(   t   pyflakes.scriptsR~   t   assertIsR   (   R   t   script_pyflakes(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_legacyScriptN  s    c         C   s#   |  j  d  } |  j | g   d S(   s   
        Source which doesn't end with a newline shouldn't cause any
        exception to be raised nor an error indicator to be returned by
        L{check}.
        s   def foo():
	pass
	N(   Ry   R|   (   R   t   fName(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_missingTrailingNewlineR  s    c         C   s<   |  j  d  \ } } |  j | d  |  j | d g  d S(   s:   
        L{checkPath} handles non-existing files.
        t   extremoi   R9   s   No such file or directoryN(   s   unexpectedErrorR   s   No such file or directory(   R}   RQ   (   R   R{   t   errors(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_checkPathNonExisting[  s
    c         C   s   d } d   } y | |  WnI t  k
 rh t j   d } t ss |  j | j j d  d k  qs n X|  j   |  j |  } t r d } n d } |  j	 | d | | f g  d S(	   s   
        Source which includes a syntax error which results in the raised
        L{SyntaxError.text} containing multiple lines of source are reported
        with only the last line of that source.
        sC   def foo():
    '''

def bar():
    pass

def baz():
    '''quux'''
c         R   s   |  d  Ud  S(   N(    (   t   source(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   evaluatex  s    i   s   
s/   EOF while scanning triple-quoted string literals   invalid syntaxs'   %s:8:11: %s
    '''quux'''
          ^
N(
   t   SyntaxErrorR
   t   exc_infot   PYPYt
   assertTruet   textR{   t   failRy   R|   (   R   R   R   t   et
   sourcePathR8   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_multilineSyntaxErrore  s     	&
	c         C   sI   |  j  d  } t r% d | f } n d | f } |  j | | g  d S(   s   
        The error reported for source files which end prematurely causing a
        syntax error reflects the cause for the syntax error.
        s   def foo(s5   %s:1:7: parenthesis is never closed
def foo(
      ^
s8   %s:1:9: unexpected EOF while parsing
def foo(
        ^
N(   Ry   R   R|   (   R   R   t   result(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_eofSyntaxError  s    c         C   sW   |  j  d  } t r d n d } t r- d n d } |  j | d | | | f g  d S(   s   
        The error reported for source files which end prematurely causing a
        syntax error reflects the cause for the syntax error.
        s   if True:
	foo =i   i   s   	   ^s   	     ^s"   %s:2:%s: invalid syntax
	foo =
%s
N(   Ry   R   R|   (   R   R   t   columnt	   last_line(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_eofSyntaxErrorWithTab  s    c         C   s]   d } |  j  |  } t r! d n d } t r3 d n d } |  j | d | | | f g  d S(   s   
        Source which has a non-default argument following a default argument
        should include the line number of the syntax error.  However these
        exceptions do not include an offset.
        s    def foo(bar=baz, bax):
    pass
s	          ^
R#   s   8:sO   %s:1:%s non-default argument follows default argument
def foo(bar=baz, bax):
%sN(   Ry   t   ERROR_HAS_LAST_LINEt   ERROR_HAS_COL_NUMR|   (   R   R   R   R   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt(   test_nonDefaultFollowsDefaultSyntaxError  s    	c         C   s   d } |  j  |  } t r! d n d } t s3 t r9 d n d } t j d k rW d } n d } |  j | d	 | | | | f g  d
 S(   s   
        Source which has a non-keyword argument after a keyword argument should
        include the line number of the syntax error.  However these exceptions
        do not include an offset.
        s   foo(bar=baz, bax)
s               ^
R#   s   13:i   i   s,   positional argument follows keyword arguments!   non-keyword arg after keyword args   %s:1:%s %s
foo(bar=baz, bax)
%sN(   i   i   (   Ry   R   R   R   R
   t   version_infoR|   (   R   R   R   R   R   R8   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt&   test_nonKeywordAfterKeywordSyntaxError  s    	c         C   s   t  j } |  j d  } | d k  r4 d | f } nr t rM d | d f } nY t rY d n d } | d k s d | k o d k  n r d n d	 } d | | | f } |  j | | g  d S(   sI   
        The invalid escape syntax raises ValueError in Python 2
        s   foo = '\xyz'i   s   %s: problem decoding source
s\   %s:1:6: %s: ('unicodeescape', b'\\xyz', 0, 2, 'truncated \\xXX escape')
foo = '\xyz'
     ^
t   UnicodeDecodeErrors         ^
R#   i   i   i   sx   %s:1:7: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-%d: truncated \xXX escape
foo = '\xyz'
%sN(   i   (   i   i   i   (   i   i   i   (   i   i   (   R
   R   Ry   R   R   R|   (   R   t   verR   t   decoding_errorR   t   col(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_invalidEscape  s    	4t   win32s   unsupported on Windowsc         C   sd   |  j  d  } t j | d  |  j |  \ } } |  j | d  |  j | d | d f g  d S(   sa   
        If the source file is not readable, this is reported on standard
        error.
        R#   i    i   R9   s   Permission deniedN(   Ry   R   t   chmodR}   RQ   (   R   R   R{   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_permissionDenied  s    c         C   si   |  j  d  } |  j |  \ } } |  j | d  |  j | d t t | t d  d   f g  d S(   sc   
        If the source file has a pyflakes warning, this is reported as a
        'flake'.
        s
   import fooi   R5   RW   N(   Ry   R}   RQ   R7   R   R   (   R   R   R{   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_pyflakesWarning  s
    c         C   sB   t  d  } d | j d  } |  j |  } |  j | g   d S(   sU   
        If source file declares the correct encoding, no error is reported.
        i&  s   # coding: utf-8
x = "%s"
s   utf-8N(   t   unichrRu   Ry   R|   (   R   t   SNOWMANR   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_encodedFileUTF8  s    c         C   s#   |  j  d  } |  j | g   d S(   sW   
        Source files with Windows CR LF line endings are parsed successfully.
        s   x = 42
N(   Ry   R|   (   R   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_CRLFLineEndings(  s    c         C   s   t  d  } d | j d  } |  j |  } t r\ t j d
 k  r\ d } d | | f } n d } d | f } |  j | | g  d	 S(   sn   
        If a source file contains bytes which cannot be decoded, this is
        reported on stderr.
        i&  s   # coding: ascii
x = "%s"
s   utf-8i   sN   'ascii' codec can't decode byte 0xe2 in position 21: ordinal not in range(128)s   %s:0:0: %s
x = "☃"
        ^
s   problem decoding sources   %s: problem decoding source
N(   i   (   R   Ru   Ry   R   R
   R   R|   (   R   R   R   R   R8   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_misencodedFileUTF8/  s    c         C   sL   t  d  } d | j d  } |  j |  } |  j | d | f g  d S(   sn   
        If a source file contains bytes which cannot be decoded, this is
        reported on stderr.
        i&  s   # coding: ascii
x = "%s"
s   utf-16s   %s: problem decoding source
N(   R   Ru   Ry   R|   (   R   R   R   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_misencodedFileUTF16J  s
    c      
   C   sI  t  j   } t j t j j | d   t j j | d d  } t | d  } | j d j d   | j	   t j j | d  } t | d  } | j d j d   | j	   g  } t
 |  } t | g |  } |  j | d  |  j t |  t d	 t t | t d
  d   f d	 t t | t d
  d   f g   d S(   sv   
        L{checkRecursive} descends into each directory, finding Python files
        and reporting problems.
        RW   s   bar.pyRr   s   import baz
Rq   s   baz.pys   import contrabandi   R5   i   t   bazt
   contrabandN(   R@   RA   R   RY   RI   RJ   RK   Rv   Ru   RL   R3   R   RQ   RZ   R7   R   R   (   R   RB   t   file1RO   t   file2R4   Rf   t   warnings(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_checkRecursiveX  s&    

	$(   R   R   R   Ry   R|   R}   R   R   R   R   R   R   R   R   R   R   R
   t   platformR   R   R   R   R   R   R   (    (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyRo   #  s(   							
	-					!					t   IntegrationTestsc           B   sb   e  Z d  Z d   Z d   Z d   Z d
 d  Z d   Z d   Z	 d   Z
 d   Z d	   Z RS(   sF   
    Tests of the pyflakes script that actually spawn the script.
    c         C   s.   t  j   |  _ t j j |  j d  |  _ d  S(   Nt   temp(   R@   RA   RB   R   RI   RJ   t   tempfilepath(   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyRC   w  s    c         C   s   t  j |  j  d  S(   N(   RD   RE   RB   (   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyRF   {  s    c         C   s:   d d l  } t j j | j  } t j j | d d d  S(   s9   
        Return the path to the pyflakes binary.
        iNs   ..t   binR~   (   R~   R   RI   t   dirnamet   __file__RJ   (   R   R~   t   package_dir(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   getPyflakesBinary~  s    c   	   
   C   sJ  t  t j  } t j j t j  | d <t j |  j   g } | j	 |  | r t
 j | d | d t
 j d t
 j d t
 j } | j | j d   \ } } n9 t
 j | d | d t
 j d t
 j } | j   \ } } | j   } t j d k r| j d  } | j d  } n  t r=t r=t r=| j d	 d
  } n  | | | f S(   s	  
        Launch a subprocess running C{pyflakes}.

        @param paths: Command-line arguments to pass to pyflakes.
        @param stdin: Text to use as stdin.
        @return: C{(returncode, stdout, stderr)} of the completed pyflakes
            process.
        t
   PYTHONPATHt   envR&   R(   R   Rq   i   s   utf-8s   
s   
(   i   (   t   dictR   t   environt   pathsepRJ   R
   RI   t
   executableR   t   extendt
   subprocesst   Popent   PIPEt   communicateRu   t   waitR   Rp   R   R    t   WINR1   (	   R   t   pathsR&   R   t   commandt   pR(   R   t   rv(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   runPyflakes  s$    	!c         C   sE   t  |  j d  } | j   |  j |  j g  } |  j | d  d S(   s   
        When a Python source file is all good, the return code is zero and no
        messages are printed to either stdout or stderr.
        RG   R#   i    N(   R#   R#   i    (   RK   R   RL   R   RQ   (   R   RO   t   d(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_goodFile  s    
c         C   s   t  |  j d  } | j d j d   | j   |  j |  j g  } t |  j t d  d  } |  j | d | t	 j
 f d d f  d S(	   s   
        When a Python source file has warnings, the return code is non-zero
        and the warnings are printed to stdout.
        Rr   s   import contraband
Rq   i   R   s   %s%sR#   N(   RK   R   Rv   Ru   RL   R   R   R   RQ   R   R   (   R   RO   R   t   expected(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_fileWithFlakes  s    
c         C   sH   |  j  |  j g  } d |  j t j f } |  j | d | d f  d S(   s   
        When pyflakes finds errors with the files it's given, (if they don't
        exist, say), then the return code is non-zero and the errors are
        printed to stderr.
        s   %s: No such file or directory%sR#   i   N(   R   R   R   R   RQ   (   R   R   t	   error_msg(    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_errors_io  s    	c         C   s   t  |  j d  } | j d j d   | j   |  j |  j g  } d j |  j t j t	 re d n d t	 rt d n d  } |  j
 | d | t f  d	 S(
   s   
        When pyflakes finds errors with the files it's given, (if they don't
        exist, say), then the return code is non-zero and the errors are
        printed to stderr.
        Rr   t   importRq   s0   {0}:1:{2}: invalid syntax{1}import{1}    {3}^{1}i   i   R#   s     N(   RK   R   Rv   Ru   RL   R   t   formatR   R   R   RQ   R   (   R   RO   R   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_errors_syntax  s    
0c         C   sW   |  j  g  d d } t d t d  d  } |  j | d | t j f d d f  d S(	   sU   
        If no arguments are passed to C{pyflakes} then it reads from stdin.
        R&   s   import contrabands   <stdin>i   R   s   %s%sR#   N(   R   R   R   RQ   R   R   (   R   R   R   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   test_readFromStdin  s    N(   R   R   R   RC   RF   R   R    R   R   R   R   R   R   (    (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR   r  s   				
			t   TestMainc           B   s   e  Z d  Z d d  Z RS(   s.   
    Tests of the pyflakes main function.
    c         C   s_   y& t  |   } t d |  Wd  QXWn& t k
 rN } | j | j | j f SXt d   d  S(   NR   s   SystemExit not raised(   R   R   t
   SystemExitR/   R0   t   codet   RuntimeError(   R   R   R&   t   captureR   (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR     s    N(   R   R   R   R    R   (    (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyR     s   (.   R   R   R
   RD   R   R@   t   pyflakes.checkerR    t   pyflakes.messagesR   t   pyflakes.reporterR   t   pyflakes.apiR   R   R   R   t   pyflakes.test.harnessR   R   R   t	   cStringIOR	   t   iot   chrR   t   pypy_version_infoR   R   t   AttributeErrorR$   t   WindowsErrorR   t	   NameErrorR   R   R   t   objectR   R   R3   R?   Rd   Ro   R   R   (    (    (    s8   /tmp/pip-build-EndXZ2/pyflakes/pyflakes/test/test_api.pyt   <module>   sF   "



		3NL Pj