Advertisement
Guest User

Untitled

a guest
Mar 1st, 2010
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import libxml2
  5. import libxslt
  6.  
  7. jsunit_files = []
  8. xunit_files = []
  9. files_to_create = []
  10. docText = """<?xml version="1.0" encoding="UTF-8"?>
  11. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  12.                xmlns:lxslt="http://xml.apache.org/xslt"
  13.                xmlns:redirect="http://xml.apache.org/xalan/redirect"
  14.                xmlns:stringutils="xalan://org.apache.tools.ant.util.StringUtils"
  15.                extension-element-prefixes="redirect">
  16.    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
  17.    <xsl:template match="browserResult">
  18.        <xsl:variable name="browser" select="browser/displayName" />
  19.        <testsuites>
  20.            <xsl:variable name="numberOfTests" select="count(descendant::testCaseResult)" />
  21.            <xsl:variable name="numberOfErrors" select="count(descendant::error)" />
  22.            <xsl:variable name="numberOfFailures" select="count(descendant::failure)" />
  23.            <xsl:variable name="hostName" select="properties/property[@name = 'remoteAddress']/@value" />
  24.            <xsl:variable name="testSuiteName" select="properties/property[@name = 'testPage']/@value" />
  25.            <xsl:variable name="totalTime" select='format-number(sum(descendant::testCaseResult/@time), "#.###")' />
  26.            <testsuite errors="{$numberOfErrors}" failures="{$numberOfFailures}" hostname="{$hostName}" name="{$browser}.{$testSuiteName}" tests="{$numberOfTests}" time="{$totalTime}">
  27.                <xsl:copy-of select="properties" /> <!-- copy all properties -->
  28.                <xsl:for-each select="testCaseResults/testCaseResult">
  29.                    <xsl:variable name="classname">
  30.                        <xsl:call-template name="get-classname">
  31.                            <xsl:with-param name="testname" select="substring-before(@name,'.html:')"/>
  32.                        </xsl:call-template>
  33.                    </xsl:variable>
  34.                    <testcase classname="JsUnit.{$browser}.{$classname}" name="{substring-after(@name,'.html:')}" time="{@time}">
  35.                        <xsl:copy-of select="*" /> <!-- copy all failures and errors -->
  36.                    </testcase>
  37.                </xsl:for-each>
  38.            </testsuite>
  39.        </testsuites>
  40.    </xsl:template>
  41.  
  42.    <!-- recursively strips slashes from the beginning until none are left -->
  43.    <xsl:template name="get-classname">
  44.        <xsl:param name="testname" />
  45.        <xsl:choose>
  46.            <xsl:when test="contains($testname, '/')">
  47.                <xsl:call-template name="get-classname">
  48.                    <xsl:with-param name="testname" select="substring-after($testname, '/')" />
  49.                </xsl:call-template>
  50.            </xsl:when>
  51.            <xsl:otherwise>
  52.                <xsl:value-of select="$testname" />
  53.            </xsl:otherwise>
  54.        </xsl:choose>
  55.    </xsl:template>
  56. </xsl:stylesheet>
  57. """
  58.  
  59. # Load up the transformation
  60. styleDoc = libxml2.parseDoc(docText)
  61. style = libxslt.parseStylesheetDoc(styleDoc)
  62.  
  63. # Get the files that have been generated by jsunit
  64. for test_file in os.listdir(os.path.dirname(__file__)):
  65.     file_name = os.path.basename(test_file)
  66.     if file_name.startswith('JSTEST-'):
  67.         jsunit_files.append(file_name)
  68.     elif file_name.startswith('XUNIT-'):
  69.         xunit_files.append(file_name)
  70.        
  71. # Strip out the files that have already been converted from jsunit to xunit
  72. for xunit_file in xunit_files:
  73.     if 'JSTEST-' + xunit_file[6:] in jsunit_files:
  74.         jsunit_files.remove('JSTEST-' + xunit_file[6:])
  75.        
  76. # Create the new xunit files
  77. for jsunit_file in jsunit_files:
  78.     new_file = 'XUNIT-' + jsunit_file[7:]
  79.     doc = libxml2.parseDoc(open(os.path.join(os.path.dirname(__file__), jsunit_file)).read())
  80.     save_to = open(os.path.join(os.path.dirname(__file__), new_file), 'w')
  81.     xml = style.saveResultToString(style.applyStylesheet(doc,{}))
  82.     # Write out the transformed xml, stripping out any cruft
  83.     save_to.write(xml[:xml.index("?>") + 2] + xml[xml.index("<testsuites"):])
  84.     save_to.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement