<s> """<STR_LIT>""" <EOL> import warnings , os , stat , sys , signal <EOL> from twisted . python . runtime import platform <EOL> from twisted . trial import unittest <EOL> from twisted . internet import error , reactor , utils , interfaces <EOL> class ProcessUtilsTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> if interfaces . IReactorProcess ( reactor , None ) is None : <EOL> skip = "<STR_LIT>" <EOL> output = None <EOL> value = None <EOL> exe = sys . executable <EOL> def makeSourceFile ( self , sourceLines ) : <EOL> """<STR_LIT>""" <EOL> script = self . mktemp ( ) <EOL> scriptFile = file ( script , '<STR_LIT>' ) <EOL> scriptFile . write ( os . linesep . join ( sourceLines ) + os . linesep ) <EOL> scriptFile . close ( ) <EOL> return os . path . abspath ( script ) <EOL> def test_output ( self ) : <EOL> """<STR_LIT>""" <EOL> scriptFile = self . makeSourceFile ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> d = utils . getProcessOutput ( self . exe , [ '<STR_LIT>' , scriptFile ] ) <EOL> return d . addCallback ( self . assertEquals , "<STR_LIT>" ) <EOL> def test_outputWithErrorIgnored ( self ) : <EOL> """<STR_LIT>""" <EOL> scriptFile = self . makeSourceFile ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> d = utils . getProcessOutput ( self . exe , [ '<STR_LIT>' , scriptFile ] ) <EOL> d = self . assertFailure ( d , IOError ) <EOL> def cbFailed ( err ) : <EOL> return self . assertFailure ( err . processEnded , error . ProcessDone ) <EOL> d . addCallback ( cbFailed ) <EOL> return d <EOL> def test_outputWithErrorCollected ( self ) : <EOL> """<STR_LIT>""" <EOL> scriptFile = self . makeSourceFile ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> d = utils . getProcessOutput ( self . exe , [ '<STR_LIT>' , scriptFile ] , errortoo = True ) <EOL> return d . addCallback ( self . assertEqual , "<STR_LIT>" ) <EOL> def test_value ( self ) : <EOL> """<STR_LIT>""" <EOL> scriptFile = self . makeSourceFile ( [ "<STR_LIT>" ] ) <EOL> d = utils . getProcessValue ( self . exe , [ '<STR_LIT>' , scriptFile ] ) <EOL> return d . addCallback ( self . assertEqual , <NUM_LIT:1> ) <EOL> def test_outputAndValue ( self ) : <EOL> """<STR_LIT>""" <EOL> exe = sys . executable <EOL> scriptFile = self . makeSourceFile ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> def gotOutputAndValue ( ( out , err , code ) ) : <EOL> self . assertEquals ( out , "<STR_LIT>" ) <EOL> self . assertEquals ( err , "<STR_LIT>" + os . linesep ) <EOL> self . assertEquals ( code , <NUM_LIT:1> ) <EOL> d = utils . getProcessOutputAndValue ( self . exe , [ "<STR_LIT>" , scriptFile ] ) <EOL> return d . addCallback ( gotOutputAndValue ) <EOL> def test_outputSignal ( self ) : <EOL> """<STR_LIT>""" <EOL> scriptFile = self . makeSourceFile ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def gotOutputAndValue ( ( out , err , sig ) ) : <EOL> self . assertEquals ( out , "<STR_LIT>" ) <EOL> self . assertEquals ( err , "<STR_LIT>" ) <EOL> self . assertEquals ( sig , signal . SIGKILL ) <EOL> d = utils . getProcessOutputAndValue ( self . exe , [ '<STR_LIT>' , scriptFile ] ) <EOL> d = self . assertFailure ( d , tuple ) <EOL> return d . addCallback ( gotOutputAndValue ) <EOL> if platform . isWindows ( ) : <EOL> test_outputSignal . skip = "<STR_LIT>" <EOL> def _pathTest ( self , utilFunc , check ) : <EOL> dir = os . path . abspath ( self . mktemp ( ) ) <EOL> os . makedirs ( dir ) <EOL> scriptFile = self . makeSourceFile ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> d = utilFunc ( self . exe , [ '<STR_LIT>' , scriptFile ] , path = dir ) <EOL> d . addCallback ( check , dir ) <EOL> return d <EOL> def test_getProcessOutputPath ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _pathTest ( utils . getProcessOutput , self . assertEqual ) <EOL> def test_getProcessValuePath ( self ) : <EOL> """<STR_LIT>""" <EOL> def check ( result , ignored ) : <EOL> self . assertEqual ( result , <NUM_LIT:0> ) <EOL> return self . _pathTest ( utils . getProcessValue , check ) <EOL> def test_getProcessOutputAndValuePath ( self ) : <EOL> """<STR_LIT>""" <EOL> def check ( ( out , err , status ) , dir ) : <EOL> self . assertEqual ( out , dir ) <EOL> self . assertEqual ( status , <NUM_LIT:0> ) <EOL> return self . _pathTest ( utils . getProcessOutputAndValue , check ) <EOL> def _defaultPathTest ( self , utilFunc , check ) : <EOL> dir = os . path . abspath ( self . mktemp ( ) ) <EOL> os . makedirs ( dir ) <EOL> scriptFile = self . makeSourceFile ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % ( dir , ) , <EOL> "<STR_LIT>" ] ) <EOL> self . addCleanup ( os . chdir , os . getcwd ( ) ) <EOL> os . chdir ( dir ) <EOL> self . addCleanup ( <EOL> os . chmod , dir , stat . S_IMODE ( os . stat ( '<STR_LIT:.>' ) . st_mode ) ) <EOL> os . chmod ( dir , <NUM_LIT:0> ) <EOL> d = utilFunc ( self . exe , [ '<STR_LIT>' , scriptFile ] ) <EOL> d . addCallback ( check , dir ) <EOL> return d <EOL> def test_getProcessOutputDefaultPath ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _defaultPathTest ( utils . getProcessOutput , self . assertEqual ) <EOL> def test_getProcessValueDefaultPath ( self ) : <EOL> """<STR_LIT>""" <EOL> def check ( result , ignored ) : <EOL> self . assertEqual ( result , <NUM_LIT:0> ) <EOL> return self . _defaultPathTest ( utils . getProcessValue , check ) <EOL> def test_getProcessOutputAndValueDefaultPath ( self ) : <EOL> """<STR_LIT>""" <EOL> def check ( ( out , err , status ) , dir ) : <EOL> self . assertEqual ( out , dir ) <EOL> self . assertEqual ( status , <NUM_LIT:0> ) <EOL> return self . _defaultPathTest ( <EOL> utils . getProcessOutputAndValue , check ) <EOL> class WarningSuppression ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . warnings = [ ] <EOL> self . originalshow = warnings . showwarning <EOL> warnings . showwarning = self . showwarning <EOL> def tearDown ( self ) : <EOL> warnings . showwarning = self . originalshow <EOL> def showwarning ( self , * a , ** kw ) : <EOL> self . warnings . append ( ( a , kw ) ) <EOL> def testSuppressWarnings ( self ) : <EOL> def f ( msg ) : <EOL> warnings . warn ( msg ) <EOL> g = utils . suppressWarnings ( f , ( ( '<STR_LIT:ignore>' , ) , dict ( message = "<STR_LIT>" ) ) ) <EOL> f ( "<STR_LIT>" ) <EOL> self . assertEquals ( len ( self . warnings ) , <NUM_LIT:1> ) <EOL> g ( "<STR_LIT>" ) <EOL> self . assertEquals ( len ( self . warnings ) , <NUM_LIT:1> ) <EOL> g ( "<STR_LIT>" ) <EOL> self . assertEquals ( len ( self . warnings ) , <NUM_LIT:2> ) </s>
