dejagnu.py :  » Development » PyObjC » trunk » pyobjc » pyobjc-core » PyObjCTest » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyObjC 
PyObjC » trunk » pyobjc » pyobjc core » PyObjCTest » dejagnu.py
"""
A very crude emulator of dejagnu, just enough to integrate the libbfi 
unittests into the pyobjc ones.
"""
import os
import re
import sys
import signal
import codecs
from fnmatch import fnmatch
import unittest
from distutils.util import get_platform
from distutils.sysconfig import get_config_var

gDgCommands=re.compile(r'''
        (?:{\s*(dg-do)\s*run\s*({[^}]*})?\s*})
        |
        (?:{\s*(dg-output)\s*"([^"]*)"\s*})
        ''',
            re.VERBOSE|re.MULTILINE)

def signame(code):
    for nm in dir(signal):
        if nm.startswith('SIG') and nm[3] != '_' \
                and getattr(signal, nm) == code:
            return nm
    return code

def exitCode2Description(code):
    """
    Convert the exit code as returned by os.popen().close() to a string
    """
    if os.WIFEXITED(code):
        return 'exited with status %s'%(os.WEXITSTATUS(code),)
    
    elif os.WIFSIGNALED(code):
        sig = os.WTERMSIG(code)
        return 'crashed with signal %s [%s]'%(signame(sig), sig)

    else:
        return 'exit code %s'%(code,)

def platform_matches(matchstr):
    # This is a hack
    if sys.byteorder == 'little':
        platform = 'i386-apple-darwin'
    else:
        platform = 'powerpc-apple-darwin'

    return fnmatch(platform, matchstr)

def parseDG(fdata):
    result = []
    for  item in gDgCommands.findall(fdata):
        if item[0] == 'dg-do':
            result.append(('run', item[1]))
        elif item[2] == 'dg-output':
            if sys.version_info[0] == 3:
                value = codecs.decode(item[3], 'unicode_escape')
            else:
                value = item[3].decode('string_escape')
            result.append(('expect', value))

    return result


class DgTestCase (unittest.TestCase):
    def __init__(self, filename):
        unittest.TestCase.__init__(self)
        self.filename = filename

    def runTest(self):
        script = parseDG(open(self.filename).read())
        output = []

        for command, data in script:
            if command == 'run':
                action = 'run'
                action_data = data
            if command == 'expect':
                output.append(data)
        output = ''.join(output)
        output = output.replace('\\', '')

        d = action_data.split()
        if d and d[1] == 'target':
            for item in d[2:]:
                if platform_matches(item):
                    break

            else:
                # Test shouldn't be run on this platform
                return

        # NOTE: We're ignoring the xfail data for now, none of the
        # testcases are supposed to fail on darwin.
        
        self.compileTestCase()
        data = self.runTestCase()

        if output != '':
            self.assertEquals(data.rstrip(), output.rstrip())
        os.unlink('/tmp/test.bin')


    def shortDescription(self):
        fn = os.path.basename(self.filename)[:-2]
        dn = os.path.basename(os.path.dirname(self.filename))
        return "dejagnu.%s.%s"%(dn, fn)

    def compileTestCase(self):
        libdir = os.path.join('build', 'temp.%s-%d.%d'%(get_platform(), sys.version_info[0], sys.version_info[1]))
        if hasattr(sys, 'gettotalrefcount'):
            libdir += "-pydebug"
        libdir = os.path.join(libdir, 'libffi-src')

        libffiobjects = self.object_files(libdir)


        if self.filename.endswith('.m'):
            extra_link = '-framework Foundation'
        else:
            extra_link = ''

        commandline='MACOSX_DEPLPOYMENT_TARGET=%s %s %s -g -DMACOSX -Ilibffi-src/include -Ilibffi-src/powerpc -o /tmp/test.bin %s %s %s 2>&1'%(
                get_config_var('MACOSX_DEPLOYMENT_TARGET'),
                get_config_var('CC'),
                get_config_var('CFLAGS'), self.filename, ' '.join(libffiobjects),
    extra_link)

        fp = os.popen(commandline)
        data = fp.read()
        xit = fp.close()
        if xit != None:
            self.fail("Compile failed[%s]:\n%s"%(xit, data))


    def runTestCase(self):
        os.environ['DYLD_BIND_AT_LAUNCH'] = '1'
        fp = os.popen('/tmp/test.bin', 'r')
        del os.environ['DYLD_BIND_AT_LAUNCH']
        data = fp.read()
        xit = fp.close()
        if xit != None:
            self.fail("Running failed (%s)"%(exitCode2Description(xit),))
        return data


    def object_files(self, basedir):
        result = []
        for dirpath, dirnames, filenames in os.walk(basedir):
            for fn in filenames:
                if fn.endswith('.o'):
                    result.append(os.path.join(dirpath, fn))
        return result
            

def testSuiteForDirectory(dirname):
    tests = []
    for fn in os.listdir(dirname):
        if not fn.endswith('.c') and not fn.endswith('.m'): continue
        tst = DgTestCase(os.path.join(dirname, fn))
        if alltests and tst.shortDescription() not in alltests:
            continue
        tests.append(tst)

    return unittest.TestSuite(tests)


alltests = []
if __name__ == "__main__":
    alltests = sys.argv[1:]
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(testSuiteForDirectory('libffi-src/tests/testsuite/libffi.call'))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.