#!@PYTHON@ -t
# -*- Python -*-
# $Id: test-run.py.in,v 1.5 2008/12/10 13:31:26 rockyb Exp $
"Unit test for Extended Python debugger's runl and runv commands "
import difflib, os, sys, unittest, signal, time

top_builddir = "@top_builddir@"
if top_builddir[-1] != os.path.sep:
    top_builddir += os.path.sep
sys.path.insert(0, os.path.join(top_builddir, 'pydb'))
top_srcdir = "@top_srcdir@"
if top_srcdir[-1] != os.path.sep:
    top_srcdir += os.path.sep
sys.path.insert(0, os.path.join(top_srcdir, 'pydb'))

import pydb

builddir     = "@builddir@"
if builddir[-1] != os.path.sep:
    builddir += os.path.sep

srcdir = "@srcdir@"
if srcdir[-1] != os.path.sep:
    srcdir += os.path.sep

pydir        = os.path.join(top_builddir, "pydb")
pydb_short   = "pydb.py"
pydb_path    = os.path.join(pydir, pydb_short)

def diff_files(outfile, rightfile):
    fromfile  = rightfile
    fromdate  = time.ctime(os.stat(fromfile).st_mtime)
    fromlines = open(fromfile, 'U').readlines()
    tofile    = outfile
    todate    = time.ctime(os.stat(tofile).st_mtime)
    tolines   = open(tofile, 'U').readlines()
    
    diff = list(difflib.unified_diff(fromlines, tolines, fromfile,
                                     tofile, fromdate, todate))
    if len(diff) == 0:
        os.unlink(outfile)
    for line in diff:
        print line,
    return len(diff) == 0

class RunTests(unittest.TestCase):

    def test_runl(self):
        """Test pydb.runl()"""
        python_script = '%sgcd.py' % srcdir
        cmdfile       = '%sbrkpt2.cmd' % srcdir
        if sys.hexversion <= 0x020400f0:
            rightfile     = os.path.join(top_srcdir, 'test', 'data', 
                                         'brkpt2-2.3.right')
        else:
            rightfile     = os.path.join(top_srcdir, 'test', 'data', 
                                         'brkpt2.right')
        outfile       = 'runtest.out'
        if os.path.exists(outfile): os.unlink(outfile)
        pydb.runl('--basename', '--batch', '--nx', '--output', outfile,
                  '--command', cmdfile, python_script, '3', '5')
        result = diff_files(outfile, rightfile)
        self.assertEqual(True, result, "brkpt2 (via runl)")

    def test_runv(self):
        """Test pydb.runl()"""
        python_script = '%shanoi.py' % srcdir
        cmdfile       = '%scmdparse.cmd' % srcdir
        rightfile     = os.path.join(top_srcdir, 'test', 'data', 
                                     'cmdparse.right')
        outfile       = 'runtest.out'
        if os.path.exists(outfile): os.unlink(outfile)
        args = ('--basename', '--batch', '--nx', '--output', outfile,
                '--command', cmdfile, python_script)
        pydb.runv(args)
        result = diff_files(outfile, rightfile)
        self.assertEqual(True, result, "cmdparse (via runv)")

    def test_pydbrc(self):
        """Test pydb.runl()"""
        python_script = '%shanoi.py' % srcdir
        cmdfile       = '%spydbrc.cmd' % srcdir
        rightfile     = os.path.join(top_srcdir, 'test', 'data', 
                                     'pydbrc.right')
        outfile       = 'pydbrc.out'
        if os.path.exists(outfile): os.unlink(outfile)
        os.environ['HOME'] = srcdir
        args = ('--basename', '--batch', '--output', outfile,
                '--command', cmdfile, python_script)
        pydb.runv(args)
        result = diff_files(outfile, rightfile)
        self.assertEqual(True, result, "pydbrc test")
        if srcdir[-1] == os.path.sep:
            # Chop off pathsep to make sure that works
            os.environ['HOME'] = srcdir[0:-1]
        else:
            # Add of pathsep to make sure that works
            os.environ['HOME'] = srcdir + os.path.sep
        pydb.runv(args)
        result = diff_files(outfile, rightfile)

if __name__ == '__main__':
    unittest.main()
