From: <otm...@us...> - 2010-05-17 22:38:57
|
Revision: 7057 http://jython.svn.sourceforge.net/jython/?rev=7057&view=rev Author: otmarhumbel Date: 2010-05-17 22:38:50 +0000 (Mon, 17 May 2010) Log Message: ----------- increase test coverage Modified Paths: -------------- trunk/jython/Lib/test/test_bat_jy.py Modified: trunk/jython/Lib/test/test_bat_jy.py =================================================================== --- trunk/jython/Lib/test/test_bat_jy.py 2010-04-30 17:06:38 UTC (rev 7056) +++ trunk/jython/Lib/test/test_bat_jy.py 2010-05-17 22:38:50 UTC (rev 7057) @@ -6,7 +6,6 @@ import tempfile from test import test_support -from subprocess import Popen, PIPE from java.lang import IllegalThreadStateException from java.lang import Runtime @@ -47,34 +46,67 @@ def getStream(self): return self.process.getErrorStream() -class SimpleSubprocess: - def __init__(self, args): - self.args = args - self.exitValue = -999 +class StarterProcess: + def writeStarter(self, args, javaHome, jythonHome, jythonOpts): + (starter, starterPath) = tempfile.mkstemp(suffix='.bat', prefix='starter', text=True) + starter.close() + outfilePath = starterPath[:-4] + '.out' + starter = open(starterPath, 'w') # open starter as simple file + try: + if javaHome: + starter.write('set JAVA_HOME=%s\n' % javaHome) + if jythonHome: + starter.write('set JYTHON_HOME=%s\n' % jythonHome) + if jythonOpts: + starter.write('set JYTHON_OPTS=%s\n' % jythonOpts) + starter.write(self.buildCommand(args, outfilePath)) + return (starterPath, outfilePath) + finally: + starter.close() - def run(self): - self.process = Runtime.getRuntime().exec(self.args) - self.stdoutMonitor = StdoutMonitor(self.process) - self.stderrMonitor = StderrMonitor(self.process) - self.stdoutMonitor.start() - self.stderrMonitor.start() - while self.isAlive(): - Thread.sleep(1000) - return self.exitValue + def buildCommand(self, args, outfilePath): + line = '' + for arg in args: + line += arg + line += ' ' + line += '> ' + line += outfilePath + line += ' 2>&1' + return line - def getStdout(self): - return self.stdoutMonitor.getOutput() + def getOutput(self, outfilePath): + lines = '' + outfile = open(outfilePath, 'r') + try: + for line in outfile.readlines(): + lines += line + finally: + outfile.close() + return lines - def getStderr(self): - return self.stderrMonitor.getOutput() - - def isAlive(self): + def isAlive(self, process): try: - self.exitValue = self.process.exitValue() + process.exitValue() return False except IllegalThreadStateException: return True + def run(self, args, javaHome, jythonHome, jythonOpts): + ''' creates a start script, executes it and captures the output ''' + (starterPath, outfilePath) = self.writeStarter(args, javaHome, jythonHome, jythonOpts) + try: + process = Runtime.getRuntime().exec(starterPath) + stdoutMonitor = StdoutMonitor(process) + stderrMonitor = StderrMonitor(process) + stdoutMonitor.start() + stderrMonitor.start() + while self.isAlive(process): + Thread.sleep(500) + return self.getOutput(outfilePath) + finally: + os.remove(starterPath) + os.remove(outfilePath) + class BaseTest(unittest.TestCase): def quote(self, s): return '"' + s + '"' @@ -97,12 +129,6 @@ home = ex[:-11] # \jython.bat return home - def copyOsEnviron(self): - theCopy = {} - for key in os.environ.keys(): - theCopy[key] = os.environ[key] - return theCopy - def assertOutput(self, flags=None, javaHome=None, jythonHome=None, jythonOpts=None): args = [sys.executable, '--print'] memory = None @@ -132,27 +158,8 @@ else: jythonArgs = flag args.append(flag) - environ = None - if javaHome or jythonHome or jythonOpts: - environ = self.copyOsEnviron() # copy to prevent os.environ from being changed - if javaHome: - environ['JAVA_HOME'] = javaHome - if jythonHome: - environ['JYTHON_HOME'] = jythonHome - if jythonOpts: - environ['JYTHON_OPTS'] = jythonOpts - if environ: - # env changes can only be handled by Popen - process = Popen(args, stdout=PIPE, stderr=PIPE, env=environ) - out = process.stdout.read() - err = process.stderr.read() - else: - # since Popen escapes double quotes, we use a simple subprocess if no env changes are needed - simpleProcess = SimpleSubprocess(args) - simpleProcess.run() - out = simpleProcess.getStdout() - err = simpleProcess.getStderr() - self.assertEquals('', err) + process = StarterProcess() + out = process.run(args, javaHome, jythonHome, jythonOpts) self.assertNotEquals('', out) homeIdx = out.find('-Dpython.home=') java = 'java' @@ -275,21 +282,8 @@ def test_combined(self): self.assertOutput(['-W', 'action', 'line']) - # something adds double quotes, but in real life this works: - # jython.bat --print -c 'import sys;' def test_singlequoted(self): - args = [sys.executable] - args.append('--print') - args.append('-c') - args.append("'import sys;'") - simpleProcess = SimpleSubprocess(args) - simpleProcess.run() - out = simpleProcess.getStdout() - err = simpleProcess.getStderr() - self.assertEquals('', err) - self.assertNotEquals('', out) - tail = out[-19:] - self.assertEquals('-c "\'import sys;\'" ', tail) + self.assertOutput(['-c', "'import sys;'"]) def test_doublequoted(self): self.assertOutput(['-c', '"print \'something\'"']) @@ -323,6 +317,9 @@ def test_main(): if os._name == 'nt': test_support.run_unittest(VanillaTest, + JavaHomeTest, + JythonHomeTest, + JythonOptsTest, JavaOptsTest, ArgsTest, DoubleDashTest) @@ -333,4 +330,4 @@ if __name__ == '__main__': test_main() - \ No newline at end of file + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |