From: Kevin J. B. <kev...@us...> - 2003-04-18 05:47:30
|
Update of /cvsroot/jython/jython/Lib/test In directory sc8-pr-cvs1:/tmp/cvs-serv31692/test Added Files: test_javashell.py Log Message: Optimize startup by delaying javashell functions until needed --- NEW FILE: test_javashell.py --- import unittest import test_support from org.python.core import PyFile import re from LazyDict import LazyDict import os import javashell class JavaShellTest(unittest.TestCase): """This test validates the subshell functionality (javashell, os.environ, popen*). Does some white box as well as black box testing. """ def testGetOsType( self ): """Test expected values for various Java os names""" testVals = { "Windows NT": "nt", "Windows 95": "dos", "MacOS": "mac", "Solaris": "posix", "Linux": "posix", "None": "None" } msgFmt = "javashell._getOsType( '%s' ) should return '%s', not '%s'" # test basic mappings for key, val in testVals.items(): got = javashell._getOsType( key ) assert got == val, msgFmt % ( key, val, got ) def _testCmds( self, _shellEnv, testCmds, whichEnv ): """test commands (key) and compare output to expected output (value). this actually executes all the commands twice, testing the return code by calling system(), and testing some of the output by calling execute() """ for cmd, pattern in testCmds: print "\nExecuting '%s' with %s environment" % (cmd, whichEnv) p = javashell.shellexecute(cmd) line = PyFile( p.getInputStream() ).readlines()[0] assert re.match( pattern, line ), \ "expected match for %s, got %s" % ( pattern, line ) print "waiting for", cmd, "to complete" assert not p.waitFor(), \ "%s failed with %s environment" % (cmd, whichEnv) def testSystem( self ): """test system and environment functionality""" os.environ = LazyDict( populate=os._getEnvironment ) assert not os.environ._populated, \ "before population, os.environ._populated should be false" key, value = "testKey", "testValue" org = os.environ testCmds = [ # test commands and regexes to match first line of expected # output on first and second runs # Note that the validation is incomplete for several of these # - they should validate depending on platform and pre-post, but # they don't. # no quotes, should output both words ("echo hello there", "hello there"), # should print PATH (on NT) ("echo PATH=%PATH%", "(PATH=.*;.*)|(PATH=%PATH%)"), # should print 'testKey=%testKey%' on NT before initialization, # should print 'testKey=' on 95 before initialization, # and 'testKey=testValue' after ("echo %s=%%%s%%" % (key,key), "(%s=)" % (key,)), # should print PATH (on Unix) ( "echo PATH=$PATH", "PATH=.*" ), # should print 'testKey=testValue' on Unix after initialization ( "echo %s=$%s" % (key,key), "(%s=$%s)|(%s=)|(%s=%s)" % (key, key, key, key, value ) ), # should output quotes on NT but not on Unix ( 'echo "hello there"', '"?hello there"?' ), # should print 'why' to stdout. ( r'''jython -c "import sys;sys.stdout.write( 'why\n' )"''', "why" ), # should print 'why' to stderr, but it won't right now. Have # to add the print to give some output...empty string matches every # thing... ( r'''jython -c "import sys;sys.stderr.write('why\n');print " ''', "" ) ] self._testCmds( javashell._shellEnv, testCmds, "default" ) # trigger initialization of environment os.environ[ key ] = value assert os.environ._populated, \ "after population, os.environ._populated should be true" assert org.get( key, None ) == value, \ "expected stub to have %s set" % key assert os.environ.get( key, None ) == value, \ "expected real os.environment to have %s set" % key # if environment is initialized and jython gets ARGS=-i, it thinks # it is running in interactive mode, and fails to exit until # process.getOutputStream().close() try: del os.environ[ "ARGS" ] except KeyError: pass # test system using the non-default environment self._testCmds( javashell._shellEnv, testCmds, "initialized" ) assert os.environ.has_key( "PATH" ), \ "expected environment to have PATH attribute " \ "(this may not apply to all platforms!)" def testBadShell( self ): "attempt to get an environment with a shell that is not startable" se2 = javashell._ShellEnv( ["badshell", "-c"], "set" ) str(se2.environment) # trigger initialization assert not se2.environment.items(), "environment should be empty" def testBadGetEnv( self ): "attempt to get an environment with a command that does not print an environment" envCmd="echo This command does not print environment" se2 = javashell._ShellEnv( javashell._shellEnv.cmd, envCmd, None ) str(se2.environment) # trigger initialization assert not se2.environment.items(), "environment should be empty" def testPutEnv( self ): "Put an environment variable and ensure that spawned processes see the change" value = "Value we set" os.putenv( "NEWVARIABLE", value ) newValue = os.popen( "echo $NEWVARIABLE" ).read().strip() if newValue == "$NEWVARIABLE": newValue = os.popen( "echo %NEWVARIABLE%" ).read().strip() if newValue == "%NEWVARIABLE%": raise test_support.TestSkipped( "Unable to find a subshell to execute echo" ) assert newValue == value, ( "Expected (%s) to equal value we set (%s)" % ( newValue, value )) if __name__ == "__main__": unittest.main() |