
4\c           @   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 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
 rCe Z! n Xe j d d
 f k p\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(    (    s5   lib/python2.7/site-packages/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   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   __init__;   s    	(   t   __name__t
   __module__t   __doc__R   (    (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR   7   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(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   _create_StringIOK   s    	c         C   s%   t  |  _ |  j | p d  |  _ d  S(   Nt    (   t   FalseR   R"   t   _stdin(   R   t   stdin(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR   W   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   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt	   __enter__[   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   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   __exit__f   s    N(   R   R   R   R    R"   R   R-   R2   (    (    (    s5   lib/python2.7/site-packages/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   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR   x   s    c         C   s    |  j  j d t |  f  d  S(   Nt   flake(   R4   t   appendt   str(   R   t   message(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR5      s    c         C   s   |  j  j d | | f  d  S(   Nt   unexpectedError(   R4   R6   (   R   t   filenameR8   (    (    s5   lib/python2.7/site-packages/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(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR;      s    (   R   R   R   R   R5   R9   R;   (    (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR3   s   s
   			t   TestIterSourceCodec           B   sh   e  Z d  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   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   setUp   s    c         C   s   t  j |  j  d  S(   N(   t   shutilt   rmtreeRB   (   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   tearDown   s    c         G   s;   | s t   t j j |  j |  } t | d  j   | S(   Nt   a(   t   AssertionErrorR   t   patht   joinRB   t   opent   close(   R   t   partst   fpath(    (    s5   lib/python2.7/site-packages/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   (    (    s5   lib/python2.7/site-packages/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(   RO   RP   RQ   R   RB   (   R   t	   childpath(    (    s5   lib/python2.7/site-packages/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(   RO   RP   RQ   R   RB   (   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_onlyPythonSource   s    c         C   s   t  j t  j j |  j d   |  j d 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.pys   a.py~t   bars   b.pys   c.pyN(	   R   t   mkdirRI   RJ   RB   RO   RP   t   sortedR   (   R   t   apatht   bpatht   cpath(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_recurses   s    c         C   sp  t  j j |  j d  } t | d   } | j d  Wd QX|  j d  t t  j j |  j d  d   } | j d  Wd QXt  j j |  j d  } t | d   } | j d	  Wd QXt  j j |  j d
  } t | d   } | j d  Wd QXt  j j |  j d  } t | d   } | j d  Wd QX|  j t t	 |  j g   t | | | | g   d S(   sd   
        Find Python files that don't end with `.py`, but contain a Python
        shebang.
        RG   t   ws   #!/usr/bin/env python
Nt   bt   cs   hello
world
t   ds   #!/usr/bin/env python2
t   es   #!/usr/bin/env python3
R   s   #!/usr/bin/env pythonw
(
   R   RI   RJ   RB   RK   t   writeRO   RP   RY   R   (   R   t   pythont   fdt   python2t   python3t   pythonw(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_shebang   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.
        RV   RW   s   a.pys   b.pyN(	   R   RI   RJ   RB   RX   RO   RP   RY   R   (   R   t   foopatht   barpathRZ   R[   (    (    s5   lib/python2.7/site-packages/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(   RO   RP   RQ   R   (   R   t   epath(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_explicitFiles   s    (   R   R   R   RC   RF   RO   RR   RT   RU   R]   Ri   Rl   Rn   (    (    (    s5   lib/python2.7/site-packages/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   s`   t    } t d |  } | j d d d t j d	 k r< d n 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   i   s   bad line of sources2   foo.py:3:8: a problem
bad line of source
       ^
N(   i   i   (   R	   R   R    R;   R
   t   version_infoRP   R.   (   R   t   errt   reporter(    (    s5   lib/python2.7/site-packages/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;   RP   R.   (   R   Rq   Rr   (    (    s5   lib/python2.7/site-packages/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 |   t j d k ri d
 n d } |  j d | | d 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   
i   i   s   foo.py:3:%d: a problem
it    i   s   ^
N(   i   i   (
   R	   R   R    R;   t   lenRJ   R
   Rp   RP   R.   (   R   Rq   t   linesRr   t   column(    (    s5   lib/python2.7/site-packages/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   RP   R.   (   R   Rq   Rr   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_unexpectedError0  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*   RW   s   %s
N(   R	   R   R    R   R   R5   RP   R.   (   R   t   outRr   R8   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt
   test_flake9  s
    	(   R   R   R   Rs   Rt   Ry   Rz   R|   (    (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyRo      s   					t
   CheckTestsc           B   s   e  Z d  Z e j 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   sy   t  j   \ } } zR t j | d  5 } t | d  sK | j d  } n  | j |  Wd QX| VWd t j |  Xd S(   sV   
        Make a temporary file containing C{content} and return a path to it.
        t   wbt   decodet   asciiN(   R@   t   mkstempR   t   fdopent   hasattrt   encodeRc   t   remove(   R   t   contentRe   t   nameR   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   makeTempFileJ  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   RP   R.   Rv   RJ   (   R   RI   t	   errorListRq   t   count(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   assertHasErrorsY  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   Rr   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt	   getErrorse  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(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_legacyScripts  s    c         C   s,   |  j  d   } |  j | g   Wd QXd 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(   R   R   (   R   t   fName(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_missingTrailingNewlinew  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(   R9   R   s   No such file or directory(   R   RP   (   R   R   t   errors(    (    s5   lib/python2.7/site-packages/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 |  b } t r d } n d } t j	 d k r d n d	 } |  j
 | d
 | | | d | d f g  Wd QXd 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(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   evaluate  s    i   s   
s/   EOF while scanning triple-quoted string literals   invalid syntaxi   i   i   s   %s:8:%d: %s
    '''quux'''
%s^
Ru   N(   i   i   (   t   SyntaxErrorR
   t   exc_infot   PYPYt
   assertTruet   textR   t   failR   Rp   R   (   R   R   R   Rb   t
   sourcePathR8   Rx   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_multilineSyntaxError  s"    	&
	c         C   sR   |  j  d  = } t r( d | f } n d | f } |  j | | g  Wd QXd 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(   R   R   R   (   R   R   t   result(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_eofSyntaxError  s    c      
   C   s`   |  j  d  K } t r d n d } t r0 d n d } |  j | d | | | f g  Wd QXd 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(   R   R   R   (   R   R   Rx   t	   last_line(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_eofSyntaxErrorWithTab  s    c      
   C   s   d } |  j  |  q } t rX t j d k r3 d n d } d | d d } d | } n
 d	 } } |  j | d
 | | | f g  Wd QXd 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
i   i   i	   Ru   i   s   ^
s   %d:R#   sO   %s:1:%s non-default argument follows default argument
def foo(bar=baz, bax):
%sN(   i   i   (   R   t   ERROR_HAS_LAST_LINER
   Rp   R   (   R   R   R   Rx   R   t	   columnstr(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt(   test_nonDefaultFollowsDefaultSyntaxError  s    	
c         C   s   d } |  j  |   } t rX t j d k r3 d n d } d | d d } d	 | } n
 d
 } } t j d k rz d } n d } |  j | d | | | | f g  Wd QXd 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)
i   i   i   i   Ru   i   s   ^
s   %d:R#   i   s,   positional argument follows keyword arguments!   non-keyword arg after keyword args   %s:1:%s %s
foo(bar=baz, bax)
%sN(   i   i   (   i   i   (   R   R   R
   Rp   R   (   R   R   R   Rx   R   R   R8   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt&   test_nonKeywordAfterKeywordSyntaxError  s    
	c      	   C   s   t  j } |  j d   } | d k  r7 d | f } nw d } t rL d } n+ d } | d k  sn | d  d k rw d } n  t r d
 d | d } n d } d | | | | f } |  j | | g  Wd QXd S(   sI   
        The invalid escape syntax raises ValueError in Python 2
        s   foo = '\xyz'i   s   %s: problem decoding source
i   i   i   i   i   i    s   %s^
Ru   R#   sy   %s:1:%d: (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    (   R
   Rp   R   R   R   R   (   R   t   verR   t   decoding_errort   position_endRx   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_invalidEscape  s     			t   win32s   unsupported on Windowsc      	   C   s   t  j   d k r" |  j d  n  |  j d  X } t  j | d  |  j |  \ } } |  j | d  |  j | d | d f g  Wd QXd S(   sa   
        If the source file is not readable, this is reported on standard
        error.
        i    s8   root user can access all files regardless of permissionsR#   i   R9   s   Permission deniedN(   R   t   getuidt   skipTestR   t   chmodR   RP   (   R   R   R   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_permissionDenied5  s    c         C   sr   |  j  d  ] } |  j |  \ } } |  j | d  |  j | d t t | t d  d   f g  Wd QXd S(   sc   
        If the source file has a pyflakes warning, this is reported as a
        'flake'.
        s
   import fooi   R5   RV   N(   R   R   RP   R7   R   R   (   R   R   R   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_pyflakesWarningF  s
    c         C   sK   t  d  } d | j d  } |  j |   } |  j | g   Wd QXd S(   sU   
        If source file declares the correct encoding, no error is reported.
        i&  s   # coding: utf-8
x = "%s"
s   utf-8N(   t   unichrR   R   R   (   R   t   SNOWMANR   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_encodedFileUTF8Q  s    c         C   s,   |  j  d   } |  j | g   Wd QXd S(   sW   
        Source files with Windows CR LF line endings are parsed successfully.
        s   x = 42
N(   R   R   (   R   R   (    (    s5   lib/python2.7/site-packages/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  Wd	 QXd	 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   R   R   R   R
   Rp   R   (   R   R   R   R   R8   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_misencodedFileUTF8d  s    c         C   sU   t  d  } d | j d  } |  j |  ! } |  j | d | f g  Wd QXd 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   R   R   R   (   R   R   R   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_misencodedFileUTF16~  s
    c         C   s\  t  j   } z;t j t j j | d   t j j | d d  } t | d   } | j d j d   Wd QXt j j | d  } t | d   } | j d j d   Wd QXg  } t	 |  } t
 | g |  } |  j | d	  |  j t |  t d
 t t | t d  d   f d
 t t | t d  d   f g   Wd t j |  Xd S(   sv   
        L{checkRecursive} descends into each directory, finding Python files
        and reporting problems.
        RV   s   bar.pyR~   s   import baz
R   Ns   baz.pys   import contrabandi   R5   i   t   bazt
   contraband(   R@   RA   R   RX   RI   RJ   RK   Rc   R   R3   R   RP   RY   R7   R   R   RD   RE   (   R   RB   t   file1Re   t   file2R4   Rr   t   warnings(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_checkRecursive  s&    	$,(   R   R   R   t
   contextlibt   contextmanagerR   R   R   R   R   R   R   R   R   R   R   R   R   R
   t   platformR   R   R   R   R   R   R   (    (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR}   E  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   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyRC     s    c         C   s   t  j |  j  d  S(   N(   RD   RE   RB   (   R   (    (    s5   lib/python2.7/site-packages/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(    (    s5   lib/python2.7/site-packages/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   R   i   s   utf-8s   
s   
(   i   (   t   dictR   t   environt   pathsepRJ   R
   RI   t
   executableR   t   extendt
   subprocesst   Popent   PIPEt   communicateR   t   waitRp   R   R   R    t   WINR1   (	   R   t   pathsR&   R   t   commandt   pR(   R   t   rv(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   runPyflakes  s$    	!c         C   s?   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   RP   (   R   Ra   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_goodFile  s    c         C   s   t  |  j d   } | j d j d   Wd QX|  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.
        R~   s   import contraband
R   Ni   R   s   %s%sR#   (
   RK   R   Rc   R   R   R   R   RP   R   R   (   R   Re   Ra   t   expected(    (    s5   lib/python2.7/site-packages/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   RP   (   R   Ra   t	   error_msg(    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_errors_io  s    	c         C   s   t  |  j d   } | j d j d   Wd QX|  j |  j g  } d j |  j t j t rd d n d t rs d n d	  } |  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.
        R~   t   importR   Ns0   {0}:1:{2}: invalid syntax{1}import{1}    {3}^{1}i   i   R#   s     i   (
   RK   R   Rc   R   R   t   formatR   R   R   RP   (   R   Re   Ra   R   (    (    s5   lib/python2.7/site-packages/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   RP   R   R   (   R   Ra   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   test_readFromStdin  s    N(   R   R   R   RC   RF   R   R    R   R   R   R   R   R   (    (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR     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  QXWnE t k
 rm } |  j | j t  t | j  } | j | j | f SXt	 d   d  S(   NR   s   SystemExit not raised(
   R   R   t
   SystemExitt   assertIsInstancet   codet   boolt   intR/   R0   t   RuntimeError(   R   R   R&   t   captureRb   R   (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR     s    N(   R   R   R   R    R   (    (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyR     s   (/   R   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   Rp   t	   cStringIOR	   t   iot   chrR   t   pypy_version_infoR   R   t   AttributeErrorR$   t   WindowsErrorR   t	   NameErrort   ERROR_HAS_COL_NUMR   R   t   objectR   R   R3   R?   Ro   R}   R   R   (    (    (    s5   lib/python2.7/site-packages/pyflakes/test/test_api.pyt   <module>   sH   "



		3lO cg