Please note:The SCons wiki is in read-only mode due to ongoing spam/DoS issues. Also, new account creation is currently disabled. We are looking into alternative wiki hosts.

Attachment 'mstest.py'

Download

   1 """
   2 This builder is for running mstest with VS 2005/2008, 
   3 If env['MSTEST_REMOVE_TMP_DIR'] is True, it will also delets all previous temporary mstest directories 
   4 which matche reg. expression: 'username_hostname\s\d{4}-\d{2}-\d{2}\s\d{2}_\d{2}_\d{2}' where username and hostname are real values;
   5 directory like that will be matched and deleted "username_hostname 2000-01-01 10_10_25" 
   6 
   7 """
   8 
   9 import os.path
  10 import SCons.Defaults
  11 import SCons.Platform.win32
  12 import SCons.Util
  13 import re
  14 import shutil
  15 import getpass 
  16 import socket
  17 import string
  18 import libxml2
  19 import libxslt
  20 
  21 def _detect(env):
  22     mstest=None
  23     HLM = SCons.Util.HKEY_LOCAL_MACHINE
  24     Key = r'Software\Microsoft\VisualStudio\%s\Setup\VS'%env['MSVS_VERSION']
  25     k=SCons.Util.RegOpenKeyEx(HLM, Key)
  26     path = SCons.Util.RegQueryValueEx(k, 'EnvironmentDirectory' )
  27     if path and path[0]:
  28        path=str(path[0])
  29        fullpath=os.path.join(path,'mstest.exe')
  30        if os.path.exists(fullpath):
  31           mstest=fullpath
  32     return mstest
  33 
  34 def _generateCoverageReport(target, source, env):
  35    #find coverage report file
  36    testDir=None
  37    status=0
  38    username=getpass.getuser()
  39    hostname=socket.gethostname().upper()
  40    reTempDir=re.compile('%s_%s\s\d{4}-\d{2}-\d{2}\s\d{2}_\d{2}_\d{2}'%(username,hostname))
  41    pathdir=os.path.dirname(target[0].abspath)
  42    if os.path.exists(pathdir):
  43       for dir in os.listdir(pathdir):
  44          outputDir=os.path.join(pathdir,dir)
  45          if os.path.isdir(outputDir):
  46             match=reTempDir.match(dir)
  47             if match:
  48                testDir=outputDir
  49                break
  50    if testDir:
  51       covReport=os.path.join(testDir,"In",socket.gethostname().upper(),"data.coverage")
  52       if os.path.exists(covReport):
  53          out=os.path.join(outputDir,"Out")
  54          xml=os.path.join(out,"coverage.xml")
  55          status=env.Execute("\"mstest2xml.exe\" \"%s\" \"%s\" \"%s\""%(out,covReport,xml))
  56          if status == 0:
  57             xslfile="coverage.xsl"
  58             htmlfile="coverage.html"
  59             styledoc = libxml2.parseFile(xslfile)
  60             style = libxslt.parseStylesheetDoc(styledoc)
  61             doc = libxml2.parseFile(xml)
  62             result = style.applyStylesheet(doc, None)
  63             style.saveResultToFilename(htmlfile, result, 0)
  64             style.freeStylesheet()
  65             doc.freeDoc()
  66             result.freeDoc()
  67    return status
  68 
  69 def DevMsTestMessage(target, source, env):
  70     return "Running Mstest %s"%source[0]
  71 def DevMsTest(target, source, env):
  72    if env['MSTEST_REMOVE_TMP_DIR']:
  73       username=getpass.getuser()
  74       hostname=socket.gethostname().upper()
  75       reTempDir=re.compile('%s_%s\s\d{4}-\d{2}-\d{2}\s\d{2}_\d{2}_\d{2}'%(username,hostname))
  76       pathdir=os.path.dirname(target[0].abspath)
  77       if os.path.exists(pathdir):
  78          for dir in os.listdir(pathdir):
  79             removeDir=os.path.join(pathdir,dir)
  80             if os.path.isdir(removeDir):
  81                match=reTempDir.match(dir)
  82                if match:
  83                   print "Removing temporary directory:", removeDir
  84                   shutil.rmtree(removeDir)
  85       #Run tests:
  86       status=env.Execute("\"$MSTEST\" /nologo /testmetadata:%s /resultsfile:%s"%(source[0],target[0]))
  87       #Jenerate coverage report
  88       _generateCoverageReport(target, source, env)
  89       
  90    return status
  91 def generate(env):
  92    MsTestAction=SCons.Action.Action(DevMsTest,strfunction=DevMsTestMessage)
  93    MsTestBuilder=SCons.Builder.Builder(action=MsTestAction, suffix="$MSTEST_RES_SUFFIX")
  94    env['MSTEST']=_detect(env)
  95    env['MSTEST_RES_SUFFIX']=".trx"
  96    env['MSTEST_REMOVE_TMP_DIR']=True
  97    env['BUILDERS']['MsTest'] = MsTestBuilder
  98 
  99 def exists(env):
 100    return _detect(env)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.