11
11
from pyflakes .messages import UnusedImport
12
12
from pyflakes .reporter import Reporter
13
13
from pyflakes .api import (
14
+ main ,
14
15
checkPath ,
15
16
checkRecursive ,
16
17
iterSourceCode ,
@@ -52,6 +53,33 @@ def __init__(self, lineno, col_offset=0):
52
53
self .col_offset = col_offset
53
54
54
55
56
+ class SysStreamCapturing (object ):
57
+
58
+ """Replaces sys.stdin, sys.stdout and sys.stderr with StringIO objects."""
59
+
60
+ def __init__ (self , stdin ):
61
+ self ._stdin = StringIO (stdin or '' )
62
+
63
+ def __enter__ (self ):
64
+ self ._orig_stdin = sys .stdin
65
+ self ._orig_stdout = sys .stdout
66
+ self ._orig_stderr = sys .stderr
67
+
68
+ sys .stdin = self ._stdin
69
+ sys .stdout = self ._stdout_stringio = StringIO ()
70
+ sys .stderr = self ._stderr_stringio = StringIO ()
71
+
72
+ return self
73
+
74
+ def __exit__ (self , * args ):
75
+ self .output = self ._stdout_stringio .getvalue ()
76
+ self .error = self ._stderr_stringio .getvalue ()
77
+
78
+ sys .stdin = self ._orig_stdin
79
+ sys .stdout = self ._orig_stdout
80
+ sys .stderr = self ._orig_stderr
81
+
82
+
55
83
class LoggingReporter (object ):
56
84
"""
57
85
Implementation of Reporter that just appends any error to a list.
@@ -588,8 +616,8 @@ def runPyflakes(self, paths, stdin=None):
588
616
"""
589
617
Launch a subprocess running C{pyflakes}.
590
618
591
- @param args : Command-line arguments to pass to pyflakes.
592
- @param kwargs: Options passed on to C{subprocess.Popen} .
619
+ @param paths : Command-line arguments to pass to pyflakes.
620
+ @param stdin: Text to use as stdin .
593
621
@return: C{(returncode, stdout, stderr)} of the completed pyflakes
594
622
process.
595
623
"""
@@ -600,7 +628,7 @@ def runPyflakes(self, paths, stdin=None):
600
628
if stdin :
601
629
p = subprocess .Popen (command , env = env , stdin = subprocess .PIPE ,
602
630
stdout = subprocess .PIPE , stderr = subprocess .PIPE )
603
- (stdout , stderr ) = p .communicate (stdin )
631
+ (stdout , stderr ) = p .communicate (stdin . encode ( 'ascii' ) )
604
632
else :
605
633
p = subprocess .Popen (command , env = env ,
606
634
stdout = subprocess .PIPE , stderr = subprocess .PIPE )
@@ -648,6 +676,21 @@ def test_readFromStdin(self):
648
676
"""
649
677
If no arguments are passed to C{pyflakes} then it reads from stdin.
650
678
"""
651
- d = self .runPyflakes ([], stdin = 'import contraband' . encode ( 'ascii' ) )
679
+ d = self .runPyflakes ([], stdin = 'import contraband' )
652
680
expected = UnusedImport ('<stdin>' , Node (1 ), 'contraband' )
653
681
self .assertEqual (d , ("%s%s" % (expected , os .linesep ), '' , 1 ))
682
+
683
+
684
+ class TestMain (IntegrationTests ):
685
+ """
686
+ Tests of the pyflakes main function.
687
+ """
688
+
689
+ def runPyflakes (self , paths , stdin = None ):
690
+ try :
691
+ with SysStreamCapturing (stdin ) as capture :
692
+ main (args = paths )
693
+ except SystemExit as e :
694
+ return (capture .output , capture .error , e .code )
695
+ else :
696
+ raise RuntimeError ('SystemExit not raised' )
0 commit comments