common.py :  » Development » PyChecker » pychecker-0.8.18 » test » 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 » PyChecker 
PyChecker » pychecker 0.8.18 » test » common.py
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

import os
import commands
import unittest

def canImport(module):
    '''
    Check if we can import the given module name.

    @type module: str

    @rtype: bool
    '''
    try:
        __import__(module)
        return True
    except ImportError:
        return False

# lifted from flumotion with permission
def _diff(old, new, desc):
    import difflib
    lines = difflib.unified_diff(old, new)
    lines = list(lines)
    if not lines:
        return
    output = ''
    for line in lines:
        output += '%s: %s\n' % (desc, line[:-1])

    raise AssertionError(
        ("\nError while comparing strings:\n"
         "%s") % (output,))

def diffStrings(orig, new, desc='input'):
    def tolines(s):
        return [line + '\n' for line in s.split('\n')]

    return _diff(tolines(orig), tolines(new), desc=desc)


# our base class for all tests
class TestCase(unittest.TestCase):
    def check(self, testname, args=''):
        """
        Run pychecker on the given test, located in input/
        Will compare to output of the same name in expected/
        """
        return self.checkMultiple(testname, [testname + '.py'], args)

    def checkMultiple(self, testname, checkables, args=''):
        """
        Run pychecker on the given test, located in input/
        Will compare to output of the same name in expected/

        @type checkables: list of str
        """
        testdir = os.path.dirname(__file__)
        # make this relative to where we are, so paths shown are relative too
        if testdir.startswith(os.getcwd()):
            testdir = testdir[len(os.getcwd()) + 1:]

        pycheckerpy = os.path.join(os.path.dirname(testdir),
            'pychecker', 'checker.py')
        testfiles = [os.path.join(testdir, 'input', c) for c in checkables]

        cmd = "python -tt %s " \
            "--limit 0 --no-argsused " \
            "%s %s" % (pycheckerpy, args, " ".join(testfiles))
        # getoutput output never ends on a newline the way
        # pychecker ... > expected/... would
        output = commands.getoutput(cmd) + '\n'
        
        # here we can select a different file based on os/python version/arch
        expectedfile = os.path.join(testdir, 'expected', testname)

        # FIXME: make generating an option
        # for now, do it every time we don't have the expected output
        # to help us
        if not os.path.exists(expectedfile):
            open(expectedfile, 'w').write(output)

        expected = open(expectedfile).read()

        diffStrings(output, expected, desc=expectedfile)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.