<s> from __future__ import with_statement <EOL> import os <EOL> import sys <EOL> import textwrap <EOL> import unittest <EOL> import subprocess <EOL> import tempfile <EOL> try : <EOL> from test . support import strip_python_stderr <EOL> except ImportError : <EOL> try : <EOL> from test . test_support import strip_python_stderr <EOL> except ImportError : <EOL> import re <EOL> def strip_python_stderr ( stderr ) : <EOL> return re . sub ( <EOL> r"<STR_LIT>" . encode ( ) , <EOL> "<STR_LIT>" . encode ( ) , <EOL> stderr ) . strip ( ) <EOL> class TestTool ( unittest . TestCase ) : <EOL> data = """<STR_LIT>""" <EOL> expect = textwrap . dedent ( """<STR_LIT>""" ) <EOL> def runTool ( self , args = None , data = None ) : <EOL> argv = [ sys . executable , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if args : <EOL> argv . extend ( args ) <EOL> proc = subprocess . Popen ( argv , <EOL> stdin = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> stdout = subprocess . PIPE ) <EOL> out , err = proc . communicate ( data ) <EOL> self . assertEqual ( strip_python_stderr ( err ) , '<STR_LIT>' . encode ( ) ) <EOL> self . assertEqual ( proc . returncode , <NUM_LIT:0> ) <EOL> return out <EOL> def test_stdin_stdout ( self ) : <EOL> self . assertEqual ( <EOL> self . runTool ( data = self . data . encode ( ) ) , <EOL> self . expect . encode ( ) ) <EOL> def test_infile_stdout ( self ) : <EOL> with tempfile . NamedTemporaryFile ( ) as infile : <EOL> infile . write ( self . data . encode ( ) ) <EOL> infile . flush ( ) <EOL> self . assertEqual ( <EOL> self . runTool ( args = [ infile . name ] ) , <EOL> self . expect . encode ( ) ) <EOL> def test_infile_outfile ( self ) : <EOL> with tempfile . NamedTemporaryFile ( ) as infile : <EOL> infile . write ( self . data . encode ( ) ) <EOL> infile . flush ( ) <EOL> outfile = tempfile . NamedTemporaryFile ( ) <EOL> try : <EOL> self . assertEqual ( <EOL> self . runTool ( args = [ infile . name , outfile . name ] ) , <EOL> '<STR_LIT>' . encode ( ) ) <EOL> with open ( outfile . name , '<STR_LIT:rb>' ) as f : <EOL> self . assertEqual ( f . read ( ) , self . expect . encode ( ) ) <EOL> finally : <EOL> outfile . close ( ) <EOL> if os . path . exists ( outfile . name ) : <EOL> os . unlink ( outfile . name ) </s>
