
    yIfg                     r   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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mZ ddlmZmZ d Z G d	 d
          Z G d d          Z G d d          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )z)
Tests for L{pyflakes.scripts.pyflakes}.
    N)PYPY)UnusedImport)Reporter)maincheck	checkPathcheckRecursiveiterSourceCode)TestCaseskipIfc                     t           j        | c}t           _        	  ||i ||t           _        S # |t           _        w xY w)z?
    Call C{f} with C{sys.stderr} redirected to C{stderr}.
    )sysstderr)r   fargskwargsouters        J/var/www/piapp/venv/lib/python3.11/site-packages/pyflakes/test/test_api.pywithStderrTor      sG     :vUCJq$!&!!

U
s   / =c                       e Zd ZdZddZdS )Nodez
    Mock an AST node.
    r   c                 "    || _         || _        d S N)lineno
col_offset)selfr   r   s      r   __init__zNode.__init__)   s    $    N)r   )__name__
__module____qualname____doc__r    r   r   r   r   %   s2         % % % % % %r   r   c                   $    e Zd ZdZd Zd Zd ZdS )SysStreamCapturingzContext manager capturing sys.stdin, sys.stdout and sys.stderr.

    The file handles are replaced with a StringIO object.
    c                 T    t          j        |pdt          j                  | _        d S )N newline)ioStringIOoslinesep_stdin)r   stdins     r   r   zSysStreamCapturing.__init__4   s"    k%+2rzBBBr   c                 N   t           j        | _        t           j        | _        t           j        | _        | j        t           _        t          j	        t          j                  xt           _        | _        t          j	        t          j                  xt           _        | _        | S )Nr(   )r   r/   _orig_stdinstdout_orig_stdoutr   _orig_stderrr.   r*   r+   r,   r-   _stdout_stringio_stderr_stringior   s    r   	__enter__zSysStreamCapturing.__enter__7   sj    9JJK	-/[-L-L-LL
T*-/[-L-L-LL
T*r   c                     | j                                         | _        | j                                        | _        | j        t          _        | j        t          _	        | j
        t          _        d S r   )r5   getvalueoutputr6   errorr1   r   r/   r3   r2   r4   r   )r   r   s     r   __exit__zSysStreamCapturing.__exit__B   sP    +4466*3355
$	&
&


r   N)r   r    r!   r"   r   r8   r=   r#   r   r   r%   r%   .   sN         
C C C	 	 	' ' ' ' 'r   r%   c                   *    e Zd ZdZd Zd Zd Zd ZdS )LoggingReporterzK
    Implementation of Reporter that just appends any error to a list.
    c                     || _         dS )zh
        Construct a C{LoggingReporter}.

        @param log: A list to append log messages to.
        N)log)r   rA   s     r   r   zLoggingReporter.__init__P   s     r   c                 X    | j                             dt          |          f           d S )Nflake)rA   appendstr)r   messages     r   rC   zLoggingReporter.flakeX   s'    #g,,/00000r   c                 @    | j                             d||f           d S )NunexpectedErrorrA   rD   )r   filenamerF   s      r   rH   zLoggingReporter.unexpectedError[   s$    *Hg>?????r   c                 F    | j                             d|||||f           d S )NsyntaxErrorrI   )r   rJ   msgr   offsetlines         r   rL   zLoggingReporter.syntaxError^   s)    #vvtLMMMMMr   N)r   r    r!   r"   r   rC   rH   rL   r#   r   r   r?   r?   K   sb           1 1 1@ @ @N N N N Nr   r?   c                   N    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S )TestIterSourceCodez&
    Tests for L{iterSourceCode}.
    c                 6    t          j                    | _        d S r   )tempfilemkdtemptempdirr7   s    r   setUpzTestIterSourceCode.setUpg   s    '))r   c                 8    t          j        | j                   d S r   shutilrmtreerU   r7   s    r   tearDownzTestIterSourceCode.tearDownj       dl#####r   c                     |sJ t          j        j        | j        g|R  }t	          |d                                           |S )Na)r,   pathjoinrU   openclose)r   partsfpaths      r   makeEmptyFilez TestIterSourceCode.makeEmptyFilem   sG    T\2E222UC   r   c                 r    |                      t          t          | j        g                    g            dS )zB
        There are no Python files in an empty directory.
        N)assertEquallistr
   rU   r7   s    r   test_emptyDirectoryz&TestIterSourceCode.test_emptyDirectorys   s4     	ndl^<<==rBBBBBr   c                     |                      d          }|                     t          t          | j        g                    |g           dS )zd
        If the directory contains one Python file, C{iterSourceCode} will find
        it.
        foo.pyNre   rg   rh   r
   rU   )r   	childpaths     r   test_singleFilez"TestIterSourceCode.test_singleFiley   sI    
 &&x00	ndl^<<==	{KKKKKr   c                     |                      d           |                     t          t          | j        g                    g            dS )zJ
        Files that are not Python source files are not included.
        zfoo.pycNrl   r7   s    r   test_onlyPythonSourcez(TestIterSourceCode.test_onlyPythonSource   sH     	9%%%ndl^<<==rBBBBBr   c                    t          j        t           j                            | j        d                     |                     dd          }|                     dd           t          j        t           j                            | j        d                     |                     dd          }|                     d          }|                     t          t          | j        g                    t          |||g                     dS )zk
        If the Python files are hidden deep down in child directories, we will
        find them.
        fooa.pyza.py~barb.pyzc.pyN)	r,   mkdirr_   r`   rU   re   rg   sortedr
   )r   apathbpathcpaths       r   test_recursesz TestIterSourceCode.test_recurses   s    
 	dlE22333""5&115'***
dlE22333""5&11""6**>4<.1122E5%())	+ 	+ 	+ 	+ 	+r   c                    t           j                            | j        d          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   |                     d           t	          t           j                            | j        d          d          5 }|                    d           ddd           n# 1 swxY w Y   t           j                            | j        d          }t	          |d          5 }|                    d	           ddd           n# 1 swxY w Y   t           j                            | j        d
          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   t           j                            | j        d          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   t           j                            | j        d          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   t           j                            | j        d          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   t           j                            | j        d          }t	          |d          5 }|                    d           ddd           n# 1 swxY w Y   |                     t          t          | j        g                    t          ||||||g                     dS )zd
        Find Python files that don't end with `.py`, but contain a Python
        shebang.
        r^   wz#!/usr/bin/env python
Nbczhello
world
ez#!/usr/bin/env python3
r   z#!/usr/bin/env pythonw
gz#!/usr/bin/python3 -u
iz#!/usr/local/bin/python3d
jz#! /usr/bin/env python3.8m
lz#!/bin/sh
#!/usr/bin/python
)
r,   r_   r`   rU   ra   writere   rg   rw   r
   )	r   pythonfdpython3pythonwpython3argspython3d	python38mnotfirsts	            r   test_shebangzTestIterSourceCode.test_shebang   s   
 dlC00&# 	0"HH.///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	3"',,t|S11377 	'2HH%&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' ',,t|S11'3 	12HH/000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 ',,t|S11'3 	12HH/000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 gll4<55+s## 	0rHH.///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 7<<c22(C   	4BHH2333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 GLLs33	)S!! 	5RHH3444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 7<<c22(C   	7BHH5666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	>4<.1122+x  	 	 	 	 	s   AAA+CCCD//D36D3/FFFG33G7:G73IIIJ77J;>J;7LL Lc                    t           j                            | j        d          }t           j                            | j        d          }t          j        |           |                     dd          }t          j        |           |                     dd          }|                     t          t          ||g                    t          ||g                     dS )zr
        L{iterSourceCode} can be given multiple directories.  It will recurse
        into each of them.
        rr   rt   rs   ru   N)	r,   r_   r`   rU   rv   re   rg   rw   r
   )r   foopathbarpathrx   ry   s        r   test_multipleDirectoriesz+TestIterSourceCode.test_multipleDirectories   s    
 ',,t|U33',,t|U33
""5&11
""5&11>7G"45566E5>""	$ 	$ 	$ 	$ 	$r   c                     |                      d          }|                     t          t          |g                    |g           dS )z
        If one of the paths given to L{iterSourceCode} is not a directory but
        a file, it will include that in its output.
        ze.pyN)re   rg   rh   r
   )r   epaths     r   test_explicitFilesz%TestIterSourceCode.test_explicitFiles   sR    
 ""6**neW5566	" 	" 	" 	" 	"r   N)r   r    r!   r"   rV   r[   re   ri   rn   rp   r{   r   r   r   r#   r   r   rQ   rQ   b   s         * * *$ $ $  C C CL L LC C C+ + +, , ,\$ $ $" " " " "r   rQ   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	TestReporterz 
    Tests for L{Reporter}.
    c                     t          j                    }t          d|          }|                    ddddd           |                     d|                                           dS )a  
        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.
        Nrk   	a problem      bad line of sourcez2foo.py:3:8: a problem
bad line of source
       ^
r*   r+   r   rL   rg   r:   r   errreporters      r   test_syntaxErrorzTestReporter.test_syntaxError   so     kmmD#&&X{Aq:NOOO LLNN		 	 	 	 	r   c                     t          j                    }t          d|          }|                    ddddd           |                     d|                                           dS )zy
        C{syntaxError} doesn't include a caret pointing to the error if
        C{offset} is passed as C{None}.
        Nrk   r   r   r   z'foo.py:3: a problem
bad line of source
r   r   s      r   test_syntaxErrorNoOffsetz%TestReporter.test_syntaxErrorNoOffset   ss    
 kmmD#&&X{At1	3 	3 	3$LLNN	 	 	 	 	r   c                     t          j                    }t          d|          }|                    ddddd           |                     d|                                           dS )z
        C{syntaxError} doesn't include text or nonsensical offsets if C{text} is C{None}.

        This typically happens when reporting syntax errors from stdin.
        N<stdin>r   r   z<stdin>:1:1: a problem
r   r   s      r   test_syntaxErrorNoTextz#TestReporter.test_syntaxErrorNoText  s_     kmmD#&&YQ4@@@4s||~~FFFFFr   c           	      L   t          j                    }ddg}t          d|          }|                    dddt	          |d                   dz   d	                    |                     |                     d
|d         z   d	z   dz   dz   |                                           dS )z
        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.
        r   zmore bad lines of sourceNrk   r   r   r      
zfoo.py:3:25: a problem
z                        ^
)r*   r+   r   rL   lenr`   rg   r:   )r   r   linesr   s       r   test_multiLineSyntaxErrorz&TestReporter.test_multiLineSyntaxError  s     kmm &
 D#&&X{As58}}q7H!YYu--	/ 	/ 	/'2Y LLNN		 	 	 	 	r   c                     t          j                    }t          d|          }|                    dd           |                     d|                                           dS )zO
        C{unexpectedError} reports an error processing a source file.
        Nz	source.pyzerror messagezsource.py: error message
)r*   r+   r   rH   rg   r:   r   s      r   test_unexpectedErrorz!TestReporter.test_unexpectedError   sY     kmmD#&&  o>>>5s||~~FFFFFr   c                    t          j                    }t          |d          }t          dt	          d          d          }|                    |           |                     |                                | d           dS )z
        C{flake} reports a code warning from Pyflakes.  It is exactly the
        str() of a L{pyflakes.messages.Message}.
        Nrk   *   rt   r   )r*   r+   r   r   r   rC   rg   r:   )r   outr   rF   s       r   
test_flakezTestReporter.test_flake)  sq    
 kmmC&&xb599wG88888r   N)
r   r    r!   r"   r   r   r   r   r   r   r#   r   r   r   r      s~              	G 	G 	G  (G G G	9 	9 	9 	9 	9r   r   c                       e Zd 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d ZdS )
CheckTestszL
    Tests for L{check} and L{checkPath} which check a file for flakes.
    c              #   h  K   t          j                    \  }}	 t          j        |d          5 }t	          |d          s|                    d          }|                    |           ddd           n# 1 swxY w Y   |V  t          j        |           dS # t          j        |           w xY w)zV
        Make a temporary file containing C{content} and return a path to it.
        wbdecodeasciiN)rS   mkstempr,   fdopenhasattrencoder   remove)r   contentr   namer   s        r   makeTempFilezCheckTests.makeTempFile:  s      
 #%%D	2t$$ !w11 6%nnW55G   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! JJJIdOOOOOBIdOOOOs.   B ;A6*B 6A::B =A:>B B1c                     t          j                    }t          |t          |          }|                     ||                                ft          |          d                    |          f           dS )z
        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   r   rg   r:   r   r`   )r   r_   	errorListr   counts        r   assertHasErrorszCheckTests.assertHasErrorsI  sp     kmmS)T22CLLNN#c)nnbggi6H6H%I	K 	K 	K 	K 	Kr   c                 L    g }t          |          }t          ||          }||fS )a  
        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.
        )r?   r   )r   r_   rA   r   r   s        r   	getErrorszCheckTests.getErrorsU  s/     "3''$))czr   c                 R    ddl m} |                     |j        t                     d S )Nr   )pyflakes)pyflakes.scriptsr   assertIsr   )r   script_pyflakess     r   test_legacyScriptzCheckTests.test_legacyScriptc  s1    @@@@@@o/;;;;;r   c                     |                      d          5 }|                     |g            ddd           dS # 1 swxY w Y   dS )z
        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}.
        zdef foo():
	pass
	Nr   r   )r   fNames     r   test_missingTrailingNewlinez&CheckTests.test_missingTrailingNewlineg  s     788 	,E  +++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,   :>>c                     |                      d          \  }}|                     |d           |                     |dg           dS )z:
        L{checkPath} handles non-existing files.
        extremo   )rH   r   zNo such file or directoryN)r   rg   )r   r   errorss      r   test_checkPathNonExistingz$CheckTests.test_checkPathNonExistingp  s_     y11v"""HI	K 	K 	K 	K 	Kr   c                 *   d}d }	  ||           |                                   n_# t          $ rR}t          sAt          j        dk     r1|                     |j                            d          dk               Y d}~nd}~ww xY w|                     |          5 }t          rd}nt          j        dk    rd}nd	}t          st          j        dk    rd
}nd}| 	                    |d|||d|dz
  z  fz  g           ddd           dS # 1 swxY w Y   dS )z
        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.
        zCdef foo():
    '''

def bar():
    pass

def baz():
    '''quux'''
c                 $    t          |            d S r   )exec)sources    r   evaluatez6CheckTests.test_multilineSyntaxError.<locals>.evaluate  s    LLLLLr   r   
   r   r   Nz=end of file (EOF) while scanning triple-quoted string literalz>unterminated triple-quoted string literal (detected at line 8)zinvalid syntax   r   z%s:8:%d: %s
    '''quux'''
%s^
 )
failSyntaxErrorr   r   version_info
assertTruetextr   r   r   )r   r   r   r   
sourcePathrF   columns          r   test_multilineSyntaxErrorz$CheckTests.test_multilineSyntaxErrorz  s   		 	 		HV
 IIKKKK	  	8 	8 	8 8C,w66T 2 2Q 6777	8 v&& 	:* +Y!W,,Z* s'722   67C6A:$67	8 9: : :	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s(   ' 
BAA>>BA DDDc                 F   |                      d          5 }t          rd}nt          j        dk    rd}nd}t          st          j        dk    rd}nd}d|d	z
  z  }d
                    ||||          }|                     ||g           ddd           dS # 1 swxY w Y   dS )
        The error reported for source files which end prematurely causing a
        syntax error reflects the cause for the syntax error.
        zdef foo(zparenthesis is never closedr   z'(' was never closedzunexpected EOF while parsingr   	   r   r   z{}:1:{}: {}
def foo(
{}^
N)r   r   r   r   formatr   )r   r   rM   r   spacesexpecteds         r   test_eofSyntaxErrorzCheckTests.test_eofSyntaxError  s	   
 z** 	9j 53!W,,,4 s'722FQJ'F5<<FC H   hZ888%	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9s   A3BBBc                     |                      d          5 }|                     || dg           ddd           dS # 1 swxY w Y   dS )r   zif True:
	foo =z$:2:7: invalid syntax
	foo =
	     ^
Nr   r   r   s     r   test_eofSyntaxErrorWithTabz%CheckTests.test_eofSyntaxErrorWithTab  s    
 233 	z       	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   >AAc                    d}|                      |          5 }t          j        dk    rd}nd}t          rt          j        dk    rd}n2t          rd}n(t          j        dk    rd}nt          j        dk    rd	}nd
}d|dz
  z  dz   }|                     || d| d| d| g           ddd           dS # 1 swxY w Y   dS )z
        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.
        z def foo(bar=baz, bax):
    pass
r   r   z<parameter without a default follows parameter with a defaultz-non-default argument follows default argumentr   r      r   r      r   r   r   r   z:1:z: z
def foo(bar=baz, bax):
N)r   r   r   r   r   )r   r   r   rM   r   	last_lines         r   (test_nonDefaultFollowsDefaultSyntaxErrorz3CheckTests.test_nonDefaultFollowsDefaultSyntaxError  s]    v&& 	*7**TE 	(F22 !W,,!V++vz*U2I          #	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   BB44B8;B8c           
      "   d}|                      |          5 }t          j        dk    rd}nt          sd}nd}d|dz
  z  dz   }d	|z  }d
}|                     |d                    ||||          g           ddd           dS # 1 swxY w Y   dS )z
        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.
        zfoo(bar=baz, bax)
r            r   r   r   z%d:z,positional argument follows keyword argumentz{}:1:{} {}
foo(bar=baz, bax)
{}N)r   r   r   r   r   r   )r   r   r   r   r   	columnstrrF   s          r   &test_nonKeywordAfterKeywordSyntaxErrorz1CheckTests.test_nonKeywordAfterKeywordSyntaxError  s    v&& 	:*6)) vz*U2IIDG   fZGY779: : :	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s   ABBBc                 B   |                      d          5 }d}t          rt          j        dk    rd}n)t          rd}ndt          j        cxk    rdk     rn nd}nd}dd	|dz
  z  z  }d
||||fz  }|                     ||g           ddd           dS # 1 swxY w Y   dS )zI
        The invalid escape syntax raises ValueError in Python 2
        zfoo = '\xyz'r   r   r      r   r   z%s^
r   zy%s:1:%d: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-%d: truncated \xXX escape
foo = '\xyz'
%sN)r   r   r   r   r   )r   r   position_endr   r   decoding_errors         r   test_invalidEscapezCheckTests.test_invalidEscape  s)   
 // 	.:L (F22 3+5555g555553&1*#56I 
V\95	6N   ^,. . .'	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.   A1BBBwin32zunsupported on Windowsc                 t   t          j                    dk    r|                     d           |                     d          5 }t          j        |d           |                     |          \  }}|                     |d           |                     |d|dfg           ddd           dS # 1 swxY w Y   dS )za
        If the source file is not readable, this is reported on standard
        error.
        r   z8root user can access all files regardless of permissionsr'   r   rH   zPermission deniedN)r,   getuidskipTestr   chmodr   rg   r   r   r   r   s       r   test_permissionDeniedz CheckTests.test_permissionDenied,  s!    9;;!MM ( ) ) )r"" 	HjHZ### NN:66ME6UA&&&#Z1DEFH H H		H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	Hs   AB--B14B1c                 B   |                      d          5 }|                     |          \  }}|                     |d           |                     |dt          t	          |t          d          d                    fg           ddd           dS # 1 swxY w Y   dS )zc
        If the source file has a pyflakes warning, this is reported as a
        'flake'.
        z
import foor   rC   rr   N)r   r   rg   rE   r   r   r  s       r   test_pyflakesWarningzCheckTests.test_pyflakesWarning=  s    
 |,, 	T
 NN:66ME6UA&&&'3|JQ'O'O#P#PQRT T T	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	Tr  c                     t          d          }d|z                      d          }|                     |          5 }|                     |g            ddd           dS # 1 swxY w Y   dS )zU
        If source file declares the correct encoding, no error is reported.
        &  z# coding: utf-8
x = "%s"
utf-8Nchrr   r   r   r   SNOWMANr   r   s       r   test_encodedFileUTF8zCheckTests.test_encodedFileUTF8H  s     f++  vg 	 v&& 	1*  R000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   A!!A%(A%c                     |                      d          5 }|                     |g            ddd           dS # 1 swxY w Y   dS )zW
        Source files with Windows CR LF line endings are parsed successfully.
        zx = 42
Nr   r   s     r   test_CRLFLineEndingszCheckTests.test_CRLFLineEndingsT  s     |,, 	1
  R000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1r   c                     t          d          }d|z                      d          }|                     |          5 }|                     || dg           ddd           dS # 1 swxY w Y   dS )n
        If a source file contains bytes which cannot be decoded, this is
        reported on stderr.
        r  # coding: ascii
x = "%s"
r  zU:1:1: 'ascii' codec can't decode byte 0xe2 in position 21: ordinal not in range(128)
Nr  r  s       r   test_misencodedFileUTF8z"CheckTests.test_misencodedFileUTF8[  s    
 f++  vg 	 v&& 	y*  vvvwy y y	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	ys   A%%A),A)c                    t          d          }d|z                      d          }|                     |          5 }t          j        dk     r| d}n| d}|                     ||g           ddd           dS # 1 swxY w Y   dS )r  r  r  zutf-16)r         z: problem decoding source
z1:1: source code string cannot contain null bytes
N)r  r   r   r   r   r   )r   r  r   r   r   s        r   test_misencodedFileUTF16z#CheckTests.test_misencodedFileUTF16j  s    
 f++  vh 	 v&& 	9**,,(EEE(\\\  hZ888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9s   3A==BBc                    t          j                    }	 t          j        t          j                            |d                     t          j                            |dd          }t          |d          5 }|                    d           ddd           n# 1 swxY w Y   t          j                            |d          }t          |d          5 }|                    d           ddd           n# 1 swxY w Y   g }t          |          }t          |g|          }| 
                    |d           | 
                    t          |          t          d	t          t          |t          d
          d                    fd	t          t          |t          d
          d                    fg                     t          j        |           dS # t          j        |           w xY w)zv
        L{checkRecursive} descends into each directory, finding Python files
        and reporting problems.
        rr   zbar.pyr   s   import baz
Nzbaz.pys   import contraband   rC   r   baz
contraband)rS   rT   r,   rv   r_   r`   ra   r   r?   r	   rg   rw   rE   r   r   rY   rZ   )r   rU   file1r   file2rA   r   warningss           r   test_checkRecursivezCheckTests.test_checkRecursive|  s3   
 "$$	#HRW\\'511222GLL%::EeT"" *b)))* * * * * * * * * * * * * * *GLL(33EeT"" /b-.../ / / / / / / / / / / / / / /C&s++H%wi::HXq)))s#l5$q''5&I&I"J"JK \%a,GGHHJK L LM M M M'"""""FM'""""sU   A#G 8BG BG !B"3G C7+G 7C;;G >C;?CG G.c                 \   d}t          j                    }t          |t          |d          }|                     |d           |                                                    d          dd         }t          j        dk    rg d}nt          rg d	}nd
g}|                     ||           dS )z;
        L{check} reports syntax errors from stdin
        z-max(1 for i in range(10), key=lambda x: x+1)
r   r   r   Nr   r   )7<stdin>:1:5: Generator expression must be parenthesized,max(1 for i in range(10), key=lambda x: x+1)z    ^)zL<stdin>:1:4: Generator expression must be parenthesized if not sole argumentr,  z   ^r+  )
r*   r+   r   r   rg   r:   splitr   r   r   )r   r   r   r   errlinesexpected_errors         r   test_stdinReportsErrorsz"CheckTests.test_stdinReportsErrors  s     BkmmS%;;"""<<>>''--crc2v%%  NN
  		  NN JN 	>22222r   N)r   r    r!   r"   
contextlibcontextmanagerr   r   r   r   r   r   r   r   r   r   r   r  r   r   platformr  r  r  r  r  r!  r)  r0  r#   r   r   r   r   5  s           
K 
K 
K  < < <, , ,K K K/: /: /:b9 9 92  ! ! !F: : :8. . .6 VCLG#%=>>H H ?>H 	T 	T 	T
1 
1 
11 1 1y y y9 9 9$# # #43 3 3 3 3r   r   c                   J    e Zd ZdZd Zd Zd ZddZd Zd Z	d	 Z
d
 Zd ZdS )IntegrationTestszF
    Tests of the pyflakes script that actually spawn the script.
    c                     t          j                    | _        t          j                            | j        d          | _        d S )Ntemp)rS   rT   rU   r,   r_   r`   tempfilepathr7   s    r   rV   zIntegrationTests.setUp  s2    '))GLLv>>r   c                 8    t          j        | j                   d S r   rX   r7   s    r   r[   zIntegrationTests.tearDown  r\   r   c                     ddl }t          j                            |j                  }t          j                            |ddd          S )z9
        Return the path to the pyflakes binary.
        r   Nz..binr   )r   r,   r_   dirname__file__r`   )r   r   package_dirs      r   getPyflakesBinaryz"IntegrationTests.getPyflakesBinary  s=     	gooh&788w||KujAAAr   Nc                    t          t          j                  }t          j                            t
          j                  |d<   t
          j        |                                 g}|	                    |           |rct          j        ||t          j        t          j        t          j                  }|                    |                    d                    \  }}nCt          j        ||t          j        t          j                  }|                                \  }}|                                }|                    d          }|                    d          }|||fS )a	  
        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.
        
PYTHONPATH)envr/   r2   r   r   )rB  r2   r   r  )dictr,   environpathsepr`   r   r_   
executabler?  extend
subprocessPopenPIPEcommunicater   waitr   )	r   pathsr/   rB  commandpr2   r   rvs	            r   runPyflakeszIntegrationTests.runPyflakes  s#    2:JOOCH55L>4#9#9#;#;<u 	/ c(2
Q Q QA }}U\\'-B-BCCVVV c(2
Q Q QA }}VVVVXXw''w''##r   c                     t          | j        d                                           |                     | j        g          }|                     |d           dS )z
        When a Python source file is all good, the return code is zero and no
        messages are printed to either stdout or stderr.
        r^   )r'   r'   r   N)ra   r8  rb   rQ  rg   )r   ds     r   test_goodFilezIntegrationTests.test_goodFile  sX    
 	T$$**,,,d/011K(((((r   c                 T   t          | j        d          5 }|                    d           ddd           n# 1 swxY w Y   |                     | j        g          }t	          | j        t          d          d          }|                     || t          j         ddf           dS )z
        When a Python source file has warnings, the return code is non-zero
        and the warnings are printed to stdout.
        r   s   import contraband
Nr   r%  r'   )	ra   r8  r   rQ  r   r   rg   r,   r-   )r   r   rS  r   s       r   test_fileWithFlakesz$IntegrationTests.test_fileWithFlakes  s    
 $#T** 	-bHH+,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-d/011 1477LII6"*66A>?????   8<<c                     |                      | j        g          }d                    | j        t          j                  }|                     |d|df           d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.
        z{}: No such file or directory{}r'   r   N)rQ  r8  r   r,   r-   rg   )r   rS  	error_msgs      r   test_errors_iozIntegrationTests.test_errors_io  sa     d/0115<<T=N=?ZI I	RA./////r   c                 <   t          | j        d          5 }|                    d           ddd           n# 1 swxY w Y   |                     | j        g          }d                    | j        t
          j                  }|                     |d|df           dS )rY  r   s   importNz-{0}:1:7: invalid syntax{1}import{1}      ^{1}r'   r   )ra   r8  r   rQ  r   r,   r-   rg   )r   r   rS  rZ  s       r   test_errors_syntaxz#IntegrationTests.test_errors_syntax   s     $#T** 	 bHHY	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 d/011CJJrz+ +	RA./////rW  c                     |                      g d          }t          dt          d          d          }|                     || t          j         ddf           dS )zU
        If no arguments are passed to C{pyflakes} then it reads from stdin.
        zimport contraband)r/   r   r   r%  r'   N)rQ  r   r   rg   r,   r-   )r   rS  r   s      r   test_readFromStdinz#IntegrationTests.test_readFromStdin  sd     R':;;	477LAA6"*66A>?????r   r   )r   r    r!   r"   rV   r[   r?  rQ  rT  rV  r[  r]  r_  r#   r   r   r5  r5    s         ? ? ?$ $ $B B B$ $ $ $4) ) )	@ 	@ 	@	0 	0 	00 0 0@ @ @ @ @r   r5  c                       e Zd ZdZddZdS )TestMainz.
    Tests of the pyflakes main function.
    Nc                 D   	 t          |          5 }t          |           d d d            n# 1 swxY w Y   t          d          # t          $ rM}|                     |j        t                     t          |j                  }|j        |j	        |fcY d }~S d }~ww xY w)N)r   zSystemExit not raised)
r%   r   RuntimeError
SystemExitassertIsInstancecodeboolintr;   r<   )r   rM  r/   capturer   rP  s         r   rQ  zTestMain.runPyflakes  s    	8#E** !g%    ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! 6777  	7 	7 	7!!!&$///QVBNGM26666666	7s9   A .A 2A 2A 
BABBBr   )r   r    r!   r"   rQ  r#   r   r   ra  ra    s2         	8 	8 	8 	8 	8 	8r   ra  ) r"   r1  r*   r,   r   rY   rH  rS   pyflakes.checkerr   pyflakes.messagesr   pyflakes.reporterr   pyflakes.apir   r   r   r	   r
   pyflakes.test.harnessr   r   r   r   r%   r?   rQ   r   r   r5  ra  r#   r   r   <module>ro     s        				 				 



       ! ! ! ! ! ! * * * * * * & & & & & &              3 2 2 2 2 2 2 2  % % % % % % % %' ' ' ' ' ' ' ':N N N N N N N N.y" y" y" y" y" y" y" y"xT9 T9 T9 T9 T98 T9 T9 T9n|3 |3 |3 |3 |3 |3 |3 |3~_@ _@ _@ _@ _@x _@ _@ _@D8 8 8 8 8 8 8 8 8 8r   