testutils.py :  » Development » Bicycle-Repair-Man » bicyclerepair-0.9 » bike » 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 » Bicycle Repair Man 
Bicycle Repair Man » bicyclerepair 0.9 » bike » testutils.py
from bike.globals import *
import unittest
import os
import os.path
from mock import Mock
from bike.parsing.fastparserast import getRoot,Root,resetRoot
from parsing.utils import fqn_rcar,fqn_rcdr
import re
from bike import log
filesToDelete = None
dirsToDelete = None

class BRMTestCase(unittest.TestCase):
    def setUp(self):
        log.warning = log.SilentLogger()
        try: os.makedirs(tmproot)
        except: pass
        os.chdir(tmproot)
        
        resetRoot(Root([tmproot]))
        getRoot().unittestmode = True
        global filesToDelete
        global dirsToDelete
        filesToDelete = []
        dirsToDelete = []
        from bike.parsing.load import Cache
        Cache.instance.reset()



    def tearDown(self):
        global filesToDelete
        global dirsToDelete

        for path in filesToDelete:
            try: os.remove(path)
            except: pass
        filesToDelete = []
            
        for path in dirsToDelete:
            try: os.removedirs(path)
            except: pass
        dirsToDelete = []

        os.chdir("..")
        try: os.removedirs(tmproot)
        except: pass



tmproot = os.path.abspath("tmproot")
tmpfile = os.path.join(tmproot, "bicyclerepairman_tmp_testfile.py")
tmpmodule = "bicyclerepairman_tmp_testfile"


def writeFile(filename, src):
    f = open(filename, "w+")
    f.write(src)
    f.close()
    filesToDelete.append(filename)

def readFile(filename):
    f = open(filename)
    src = f.read()
    f.close()
    return src

def writeTmpTestFile(src):
    try:
        os.makedirs(tmproot)
    except OSError:
        pass
    writeFile(tmpfile, src)

def readTmpTestFile():
    return readFile(tmpfile)

def deleteTmpTestFile():
    os.remove(tmpfile)
    os.removedirs(tmproot)


pkgstructureRootDir = tmproot
pkgstructureBasedir = os.path.join(pkgstructureRootDir, "a")
pkgstructureChilddir = os.path.join(pkgstructureBasedir, "b")
pkgstructureFile0 = os.path.join(pkgstructureRootDir, "top.py")
pkgstructureFile1 = os.path.join(pkgstructureBasedir, "foo.py")
pkgstructureFile2 = os.path.join(pkgstructureChilddir, "bah.py")


def createPackageStructure(src1, src2, src0="pass"):
    try: os.makedirs(pkgstructureChilddir)
    except: pass
    writeFile(os.path.join(pkgstructureBasedir, "__init__.py"), "#")
    writeFile(os.path.join(pkgstructureChilddir, "__init__.py"), "#")
    writeFile(pkgstructureFile0, src0)
    writeFile(pkgstructureFile1, src1)
    writeFile(pkgstructureFile2, src2)

def removePackageStructure():
    os.remove(os.path.join(pkgstructureBasedir, "__init__.py"))
    os.remove(os.path.join(pkgstructureChilddir, "__init__.py"))
    os.remove(pkgstructureFile0)
    os.remove(pkgstructureFile1)
    os.remove(pkgstructureFile2)
    os.removedirs(pkgstructureChilddir)


pkgstructureBasedir2 = os.path.join(pkgstructureRootDir, "c")
pkgstructureFile3 = os.path.join(pkgstructureBasedir2, "bing.py")

def createSecondPackageStructure(src3):
    try: os.makedirs(pkgstructureBasedir2)
    except: pass
    writeFile(os.path.join(pkgstructureBasedir2, "__init__.py"), "#")
    writeFile(pkgstructureFile3, src3)

def removeSecondPackageStructure():
    os.remove(os.path.join(pkgstructureBasedir2, "__init__.py"))
    os.remove(pkgstructureFile3)
    os.removedirs(pkgstructureBasedir2)



def createAST(src):
    from bike.parsing.load import getSourceNode
    writeFile(tmpfile,src)
    return getSourceNode(tmpfile)


def createSourceNodeAt(src, fqn):
    modname = fqn_rcar(fqn)
    packagefqn = fqn_rcdr(fqn)
    dirpath = os.path.join(*packagefqn.split("."))
    filepath = os.path.join(dirpath,modname+".py")
    try: os.makedirs(dirpath)
    except: pass
    dirsToDelete.append(dirpath)

    # add the __init__.py files
    path = "."
    for pathelem in packagefqn.split("."):
        path = os.path.join(path,pathelem)
        initfile = os.path.join(path,"__init__.py")
        writeFile(initfile,"#")
        filesToDelete.append(initfile)
    writeFile(filepath,src)
    filesToDelete.append(filepath)
    return getRoot()


# takes the leading whitespace out of a multi line comment.
# means you can imbed """
#                     text like
#                     this
#                     """
# in your code, and it will come out
#"""text like
#this"""
def trimLines(src):
    lines = src.splitlines(1)[1:]
    tabwidth = re.match("\s*",lines[0]).end(0)
    newlines = []
    for line in lines:
        if line == "\n" or line == "\r\n":
            newlines.append(line)
        else:
            newlines.append(line[tabwidth:])
    return "".join(newlines)

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